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

Only handle top-level expressions #6

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
25 changes: 14 additions & 11 deletions lib/index.js
Original file line number Diff line number Diff line change
@@ -1,21 +1,24 @@
'use strict'

var acorn = require('acorn')
var walk = require('walk-ast')
var walk = require('estree-walker').walk
var MagicString = require('magic-string')

function optimizeJs (jsString, opts) {
opts = opts || {}
var ast = acorn.parse(jsString)
var magicString = new MagicString(jsString)

function walkIt (node) {
if (node.type === 'FunctionExpression') {
handleFunctionExpression(node)
function walkIt (node, parentNode) {
if (/Function/.test(node.type)) {
if (node.type === 'FunctionExpression') {
handleFunctionExpression(node, parentNode)
}
this.skip() // don't descend any further
}
}

function handleFunctionExpression (node) {
function handleFunctionExpression (node, parentNode) {
var prePreChar = jsString.charAt(node.start - 2)
var preChar = jsString.charAt(node.start - 1)
var postChar = jsString.charAt(node.end)
Expand All @@ -24,12 +27,12 @@ function optimizeJs (jsString, opts) {
// assuming this node is an argument to a function, return true if it itself
// is already padded with parentheses
function isPaddedArgument (node) {
var idx = node.parentNode.arguments.indexOf(node)
var idx = parentNode.arguments.indexOf(node)
if (idx === 0) { // first arg
if (prePreChar === '(' && preChar === '(' && postChar === ')') { // already padded
return true
}
} else if (idx === node.parentNode.arguments.length - 1) { // last arg
} else if (idx === parentNode.arguments.length - 1) { // last arg
if (preChar === '(' && postChar === ')' && postPostChar === ')') { // already padded
return true
}
Expand All @@ -41,19 +44,19 @@ function optimizeJs (jsString, opts) {
return false
}

if (node.parentNode && node.parentNode.type === 'CallExpression') {
if (parentNode && parentNode.type === 'CallExpression') {
// this function is getting called itself or
// it is getting passed in to another call expression
// the else statement is strictly never hit, but I think the code is easier to read this way
/* istanbul ignore else */
if (node.parentNode.arguments.length && node.parentNode.arguments.indexOf(node) !== -1) {
if (parentNode.arguments.length && parentNode.arguments.indexOf(node) !== -1) {
// function passed in to another function. these are almost _always_ executed, e.g.
// UMD bundles, Browserify bundles, Node-style errbacks, Promise then()s and catch()s, etc.
if (!isPaddedArgument(node)) { // don't double-pad
magicString = magicString.insertLeft(node.start, '(')
.insertRight(node.end, ')')
}
} else if (node.parentNode.callee === node) {
} else if (parentNode.callee === node) {
// this function is getting immediately invoked, e.g. function(){}()
if (preChar !== '(') {
magicString.insertLeft(node.start, '(')
Expand All @@ -63,7 +66,7 @@ function optimizeJs (jsString, opts) {
}
}

walk(ast, walkIt)
walk(ast, { enter: walkIt })
var out = magicString.toString()
if (opts.sourceMap) {
out += '\n//# sourceMappingURL=' + magicString.generateMap().toUrl()
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@
"dependencies": {
"acorn": "^3.3.0",
"concat-stream": "^1.5.1",
"estree-walker": "^0.2.1",
"magic-string": "^0.16.0",
"walk-ast": "^0.0.2",
"yargs": "^4.8.1"
},
"devDependencies": {
Expand Down