Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Port to acorn-node for modern syntax support #62

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
language: node_js
node_js:
- 0.10
- 0.11
- '0.10'
- '0.12'
- '4'
- '6'
- '8'
- '10'
- '11'
- '12'
59 changes: 37 additions & 22 deletions custom.js
Original file line number Diff line number Diff line change
@@ -1,22 +1,34 @@
var esprima = require('esprima')
, through = require('through')
var acorn = require('acorn-node')
, dash = require('dash-ast')
, msplice = require('multisplice')
, through = require('through2')

var processEnvPattern = /\bprocess\.env\b/

function equalNodes(a, b) {
if (a.type !== b.type) return false
switch (a.type) {
case 'Literal': return a.value === b.value
case 'Identifier': return a.name === b.name
case 'MemberExpression': return a.computed === b.computed && equalNodes(a.object, b.object) && equalNodes(a.property, b.property)
}
return false
}

module.exports = function(rootEnv) {
rootEnv = rootEnv || process.env || {}

return function envify(file, argv) {
if (/\.json$/.test(file)) return through()

var Syntax = esprima.Syntax
var buffer = []
argv = argv || {}

return through(write, flush)

function write(data) {
function write(data, enc, cb) {
buffer.push(data)
cb()
}

function transform(source, envs) {
Expand All @@ -26,54 +38,56 @@ module.exports = function(rootEnv) {

function match(node) {
return (
node.type === Syntax.MemberExpression
&& node.object.type === Syntax.MemberExpression
node.type === 'MemberExpression'
&& node.object.type === 'MemberExpression'
&& node.object.computed === false
&& node.object.object.type === Syntax.Identifier
&& node.object.object.type === 'Identifier'
&& node.object.object.name === 'process'
&& node.object.property.type === Syntax.Identifier
&& node.object.property.type === 'Identifier'
&& node.object.property.name === 'env'
&& (node.computed ? node.property.type === Syntax.Literal : node.property.type === Syntax.Identifier)
&& (node.computed ? node.property.type === 'Literal' : node.property.type === 'Identifier')
)
}

esprima.parse(source, { tolerant: true }, function(node, meta) {
var ast = acorn.parse(source)

dash(ast, { leave: function(node) {
if (match(node)) {
var key = node.property.name || node.property.value
for (var i = 0; i < envs.length; i++) {
var value = envs[i][key]
if (value !== undefined) {
replacements.push({ node: node, meta: meta, value: JSON.stringify(value) })
replacements.push({ node: node, value: JSON.stringify(value) })
return
}
}
if (purge) {
replacements.push({ node: node, meta: meta, value: undefined })
replacements.push({ node: node, value: 'undefined' })
}
} else if (node.type === Syntax.AssignmentExpression) {
} else if (node.type === 'AssignmentExpression') {
for (var i = 0; i < replacements.length; ++i) {
if (replacements[i].node === node.left) {
if (equalNodes(replacements[i].node, node.left)) {
replacements.splice(i, 1)
}
}
}
})
} })

var result = source
var splicer = msplice(source)
if (replacements.length > 0) {
replacements.sort(function (a, b) {
return b.meta.start.offset - a.meta.start.offset
return b.start - a.start
})
for (var i = 0; i < replacements.length; i++) {
var r = replacements[i]
result = result.slice(0, r.meta.start.offset) + r.value + result.slice(r.meta.end.offset)
splicer.splice(r.node.start, r.node.end, r.value)
}
}

return result
return splicer.toString()
}

function flush() {
function flush(cb) {
var source = buffer.join('')

if (processEnvPattern.test(source)) {
Expand All @@ -84,8 +98,9 @@ module.exports = function(rootEnv) {
}
}

this.queue(source)
this.queue(null)
this.push(source)
this.push(null)
cb()
}
}
}
10 changes: 6 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,14 @@
"author": "Hugh Kennedy <hughskennedy@gmail.com> (http://hughskennedy.com/)",
"license": "MIT",
"devDependencies": {
"tap-spec": "^4.1.1",
"tape": "^4.6.0"
"tap-spec": "^4.1.2",
"tape": "^4.13.3"
},
"dependencies": {
"esprima": "^4.0.0",
"through": "~2.3.4"
"acorn-node": "^2.0.1",
"dash-ast": "^2.0.1",
"multisplice": "^1.0.0",
"through2": "^2.0.5"
},
"keywords": [
"environment",
Expand Down
17 changes: 17 additions & 0 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -183,3 +183,20 @@ test('-t [ envify purge --argument ]', function(t) {
, 'var y = process.env.argument'
].join('\n'))
})

test('Supports modern JS syntax', function(t) {
var env = { ES6: 'es2015', ES2015: 'es6' }
var buffer = ''
var stream = envify(env)

stream().on('data', function(d) { buffer += d })
.on('end', function() {
t.notEqual(-1, buffer.indexOf('foo = "es2015"'))
t.notEqual(-1, buffer.indexOf('b: "es6"'))
t.end()
})
.end([
'async function lol (foo = process.env.ES6) { console.log(foo) }'
, ';({ b, ...xyz }) => ({ ...xyz, b: process.env.ES2015, c: b })'
].join('\n'))
})