Skip to content

Commit 604e862

Browse files
lattsinolanlawson
authored andcommitted
use estree-walker to properly parse es6 (#31)
remove comment
1 parent fc35e88 commit 604e862

File tree

4 files changed

+61
-50
lines changed

4 files changed

+61
-50
lines changed

lib/index.js

Lines changed: 46 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -1,69 +1,66 @@
11
'use strict'
22

33
var acorn = require('acorn')
4-
var walk = require('walk-ast')
54
var MagicString = require('magic-string')
5+
var walk = require('estree-walker').walk
66

77
function optimizeJs (jsString, opts) {
88
opts = opts || {}
99
var ast = acorn.parse(jsString)
1010
var magicString = new MagicString(jsString)
1111

12-
function walkIt (node) {
13-
if (node.type === 'FunctionExpression') {
14-
handleFunctionExpression(node)
15-
}
16-
}
17-
18-
function handleFunctionExpression (node) {
19-
var prePreChar = jsString.charAt(node.start - 2)
20-
var preChar = jsString.charAt(node.start - 1)
21-
var postChar = jsString.charAt(node.end)
22-
var postPostChar = jsString.charAt(node.end + 1)
23-
24-
// assuming this node is an argument to a function, return true if it itself
25-
// is already padded with parentheses
26-
function isPaddedArgument (node) {
27-
var idx = node.parentNode.arguments.indexOf(node)
28-
if (idx === 0) { // first arg
29-
if (prePreChar === '(' && preChar === '(' && postChar === ')') { // already padded
30-
return true
31-
}
32-
} else if (idx === node.parentNode.arguments.length - 1) { // last arg
33-
if (preChar === '(' && postChar === ')' && postPostChar === ')') { // already padded
34-
return true
35-
}
36-
} else { // middle arg
37-
if (preChar === '(' && postChar === ')') { // already padded
38-
return true
12+
walk(ast, {
13+
enter: function (node, parent) {
14+
// assuming this node is an argument to a function, return true if it itself
15+
// is already padded with parentheses
16+
function isPaddedArgument (node) {
17+
var idx = parent.arguments.indexOf(node)
18+
if (idx === 0) { // first arg
19+
if (prePreChar === '(' && preChar === '(' && postChar === ')') { // already padded
20+
return true
21+
}
22+
} else if (idx === parent.arguments.length - 1) { // last arg
23+
if (preChar === '(' && postChar === ')' && postPostChar === ')') { // already padded
24+
return true
25+
}
26+
} else { // middle arg
27+
if (preChar === '(' && postChar === ')') { // already padded
28+
return true
29+
}
3930
}
31+
return false
4032
}
41-
return false
42-
}
4333

44-
if (node.parentNode && node.parentNode.type === 'CallExpression') {
45-
// this function is getting called itself or
46-
// it is getting passed in to another call expression
47-
// the else statement is strictly never hit, but I think the code is easier to read this way
48-
/* istanbul ignore else */
49-
if (node.parentNode.arguments.length && node.parentNode.arguments.indexOf(node) !== -1) {
50-
// function passed in to another function. these are almost _always_ executed, e.g.
51-
// UMD bundles, Browserify bundles, Node-style errbacks, Promise then()s and catch()s, etc.
52-
if (!isPaddedArgument(node)) { // don't double-pad
53-
magicString = magicString.insertLeft(node.start, '(')
54-
.insertRight(node.end, ')')
55-
}
56-
} else if (node.parentNode.callee === node) {
57-
// this function is getting immediately invoked, e.g. function(){}()
58-
if (preChar !== '(') {
59-
magicString.insertLeft(node.start, '(')
60-
.insertRight(node.end, ')')
34+
if (node.type === 'FunctionExpression') {
35+
var prePreChar = jsString.charAt(node.start - 2)
36+
var preChar = jsString.charAt(node.start - 1)
37+
var postChar = jsString.charAt(node.end)
38+
var postPostChar = jsString.charAt(node.end + 1)
39+
40+
if (parent && parent.type === 'CallExpression') {
41+
// this function is getting called itself or
42+
// it is getting passed in to another call expression
43+
// the else statement is strictly never hit, but I think the code is easier to read this way
44+
/* istanbul ignore else */
45+
if (parent.arguments && parent.arguments.indexOf(node) !== -1) {
46+
// function passed in to another function. these are almost _always_ executed, e.g.
47+
// UMD bundles, Browserify bundles, Node-style errbacks, Promise then()s and catch()s, etc.
48+
if (!isPaddedArgument(node)) { // don't double-pad
49+
magicString = magicString.insertLeft(node.start, '(')
50+
.insertRight(node.end, ')')
51+
}
52+
} else if (parent.callee === node) {
53+
// this function is getting immediately invoked, e.g. function(){}()
54+
if (preChar !== '(') {
55+
magicString.insertLeft(node.start, '(')
56+
.insertRight(node.end, ')')
57+
}
58+
}
6159
}
6260
}
6361
}
64-
}
62+
})
6563

66-
walk(ast, walkIt)
6764
var out = magicString.toString()
6865
if (opts.sourceMap) {
6966
out += '\n//# sourceMappingURL=' + magicString.generateMap().toUrl()

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,8 @@
2323
"dependencies": {
2424
"acorn": "^3.3.0",
2525
"concat-stream": "^1.5.1",
26+
"estree-walker": "^0.3.0",
2627
"magic-string": "^0.16.0",
27-
"walk-ast": "^0.0.2",
2828
"yargs": "^4.8.1"
2929
},
3030
"devDependencies": {
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
// examples taken from https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/Arrow_functions
2+
var func = x => x * x;
3+
var func = (x, y) => { return x + y; };
4+
5+
setInterval(() => {
6+
this.age++;
7+
}, 1000);
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
// examples taken from https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/Arrow_functions
2+
var func = x => x * x;
3+
var func = (x, y) => { return x + y; };
4+
5+
setInterval(() => {
6+
this.age++;
7+
}, 1000);

0 commit comments

Comments
 (0)