babel-plugin-extract-string
Advanced tools
Comparing version 1.0.1 to 1.0.2
{ | ||
"name": "babel-plugin-extract-string", | ||
"version": "1.0.1", | ||
"version": "1.0.2", | ||
"description": "Babel plugin to extract string from js source file then save into array", | ||
"main": "dist/index.js", | ||
"main": "src/index.js", | ||
"scripts": { | ||
"release": "babel src --out-dir dist", | ||
"test": "cross-env NODE_ENV=test mocha --compilers js:babel-register" | ||
@@ -9,0 +8,0 @@ }, |
@@ -1,16 +0,16 @@ | ||
import fs from 'fs' | ||
var fs = require('fs') | ||
export default function ({types: t}) { | ||
var arr = [] | ||
module.exports = function ({types: t}) { | ||
return { | ||
visitor: { | ||
StringLiteral: { | ||
enter (path, state) { | ||
enter: function (path, state) { | ||
if(/ObjectProperty/i.test(path.parent.type)) return | ||
// console.log(Object.keys(path), path.scope.path.node.type) | ||
var programNode = path.scope.path.node | ||
var arr = programNode._extractStringArr | ||
if(!Array.isArray(arr)) throw Error('cannot get program node store') | ||
var arr = state.opts.store | ||
if(!Array.isArray(arr)) throw Error('cannot get store') | ||
var name = state.opts.name | ||
if(!name) return | ||
arr.push(path.node.value) | ||
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)) | ||
@@ -20,9 +20,9 @@ } | ||
Program: { | ||
enter (path, state) { | ||
path.node._extractStringArr = [] | ||
enter: function (path, state) { | ||
state.opts.store = state.opts.store || [] | ||
}, | ||
exit (path, state) { | ||
exit: function (path, state) { | ||
var file = state.opts.file | ||
if(!file) return | ||
fs.writeFileSync(file, JSON.stringify(path.node._extractStringArr), 'utf8') | ||
fs.writeFileSync(file, JSON.stringify(state.opts.store), 'utf8') | ||
} | ||
@@ -29,0 +29,0 @@ } |
import { transform } from 'babel-core' | ||
import assert from 'assert' | ||
import fs from 'fs' | ||
import lib from '../' | ||
import lib from '../src/index.js' | ||
@@ -15,5 +15,11 @@ const babelTranslationOptions = function(options) { | ||
const code = `var d = 'aaa'; d+='bbb';` | ||
const a = transform(code, babelTranslationOptions({name: 'abc'})) | ||
const arr = [] | ||
var a = transform(code, babelTranslationOptions({name: 'abc', store: arr})) | ||
assert.equal(a.code, 'var d = abc[0];d += abc[1];') | ||
assert.deepEqual(a.ast.program._extractStringArr, [ 'aaa', 'bbb' ]) | ||
// don't touch ObjectProperty | ||
var a = transform(`var d = {'aaa': 123};`, babelTranslationOptions({name: 'abc', store: arr})) | ||
assert.equal(a.code, `var d = { 'aaa': 123 };`) | ||
// cannot rely on options.store, since it's mutated by babel | ||
// assert.deepEqual(arr, [ 'aaa', 'bbb' ]) | ||
}) | ||
@@ -20,0 +26,0 @@ it('test string saved to file', function () { |
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
10
4403
72