babel-plugin-extract-string
Advanced tools
Comparing version 1.0.2 to 1.1.0
{ | ||
"name": "babel-plugin-extract-string", | ||
"version": "1.0.2", | ||
"version": "1.1.0", | ||
"description": "Babel plugin to extract string from js source file then save into array", | ||
@@ -5,0 +5,0 @@ "main": "src/index.js", |
var fs = require('fs') | ||
function addToSet(arr, val) { | ||
var index = arr.indexOf(val) | ||
if(index<0) return arr.push(val)-1 | ||
return index | ||
} | ||
module.exports = function ({types: t}) { | ||
@@ -8,11 +14,12 @@ return { | ||
enter: function (path, state) { | ||
// filename : state.file.opts.filename | ||
if(/ObjectProperty/i.test(path.parent.type)) return | ||
// console.log(Object.keys(path), path.scope.path.node.type) | ||
var arr = state.opts.store | ||
var arr = state._store | ||
if(!Array.isArray(arr)) throw Error('cannot get store') | ||
var name = state.opts.name | ||
var str = path.node.value | ||
if(!name || str=='use strict') return | ||
arr.push(str) | ||
path.replaceWith(t.memberExpression(t.identifier(name), t.numericLiteral(arr.length-1), true)) | ||
if(!name || str.trim()=='use strict') return | ||
var index = addToSet(arr, str) | ||
path.replaceWith(t.memberExpression(t.identifier(name), t.numericLiteral(index), true)) | ||
} | ||
@@ -22,8 +29,9 @@ }, | ||
enter: function (path, state) { | ||
state.opts.store = state.opts.store || [] | ||
state._store = state._store || [] | ||
}, | ||
exit: function (path, state) { | ||
state.file.metadata._store = state._store | ||
var file = state.opts.file | ||
if(!file) return | ||
fs.writeFileSync(file, JSON.stringify(state.opts.store), 'utf8') | ||
fs.writeFileSync(file, JSON.stringify(state._store), 'utf8') | ||
} | ||
@@ -30,0 +38,0 @@ } |
@@ -16,10 +16,15 @@ import { transform } from 'babel-core' | ||
const arr = [] | ||
var a = transform(code, babelTranslationOptions({name: 'abc', store: arr})) | ||
var a = transform(code, babelTranslationOptions({name: 'abc'})) | ||
assert.equal(a.code, 'var d = abc[0];d += abc[1];') | ||
assert.deepEqual(a.metadata._store, [ 'aaa', 'bbb' ]) | ||
// dedupe strings | ||
var a = transform(`var d ='dd'; b='aa'; c='dd';`, babelTranslationOptions({name: 'abc'})) | ||
assert.equal(a.code, `var d = abc[0];b = abc[1];c = abc[0];`) | ||
assert.deepEqual(a.metadata._store, [ 'dd', 'aa' ]) | ||
// don't touch ObjectProperty | ||
var a = transform(`var d = {'aaa': 123};`, babelTranslationOptions({name: 'abc', store: arr})) | ||
var a = transform(`var d = {'aaa': 123};`, babelTranslationOptions({name: 'abc'})) | ||
assert.equal(a.code, `var d = { 'aaa': 123 };`) | ||
// cannot rely on options.store, since it's mutated by babel | ||
// assert.deepEqual(arr, [ 'aaa', 'bbb' ]) | ||
assert.deepEqual(a.metadata._store, []) | ||
}) | ||
@@ -26,0 +31,0 @@ it('test string saved to file', function () { |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
4445
3
9
71