Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

babel-plugin-extract-string

Package Overview
Dependencies
Maintainers
1
Versions
7
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

babel-plugin-extract-string - npm Package Compare versions

Comparing version 1.0.1 to 1.0.2

change.js

5

package.json
{
"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 @@ },

26

src/index.js

@@ -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

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc