babel-plugin-closure-elimination
Advanced tools
Comparing version 1.2.0 to 1.2.1
211
lib/index.js
@@ -17,113 +17,6 @@ 'use strict'; | ||
function _ref2(_ref) { | ||
var node = _ref.node; | ||
return node._generated || node._compact; | ||
} | ||
function _ref3(path) { | ||
return path.isFunction(); | ||
} | ||
function _ref4(path) { | ||
return path.type !== 'ArrowFunctionExpression'; | ||
} | ||
function _ref5(parentArrow) { | ||
parentArrow.node[$boundArrowFunction] = true; | ||
} | ||
function _enter(path) { | ||
var parentFunctions = path.getAncestry().filter(_ref3), | ||
nearestNoArrowFunction = parentFunctions.findIndex(_ref4); | ||
parentFunctions.slice(0, nearestNoArrowFunction).forEach(_ref5); | ||
} | ||
function _ref6(path) { | ||
return path.isFunction(); | ||
} | ||
function _ref7(parentArrow) { | ||
parentArrow.node[$usedEval] = true; | ||
} | ||
function _enter2(path) { | ||
if (path.node.name === 'eval' && path.parentPath.type === 'CallExpression') { | ||
path.getAncestry().filter(_ref6).forEach(_ref7); | ||
} | ||
} | ||
function getNearestScopeWithLocalUsedVars(path) { | ||
var disableRoot = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : false; | ||
var scope = path.scope, | ||
rootScope = path.scope.getProgramParent(); | ||
function _ref8(refPath) { | ||
return refPath.isDescendant(path); | ||
} | ||
do { | ||
scope = scope.parent; | ||
var bindings = scope.getAllBindings(); | ||
for (var id in bindings) { | ||
if (!scope.hasOwnBinding(id)) { | ||
break; | ||
} | ||
var references = [].concat(bindings[id].referencePaths).concat(bindings[id].constantViolations), | ||
usedReferences = references.filter(_ref8); | ||
if (usedReferences.length) { | ||
return scope; | ||
} | ||
} | ||
if (disableRoot && scope.parent === rootScope) { | ||
return scope; | ||
} | ||
} while (scope.parent); | ||
return scope; | ||
} | ||
function getAttachmentPosition(bestParent, prevPath) { | ||
var prevParents = prevPath.getAncestry(), | ||
bestParentIdx = prevParents.indexOf(bestParent); | ||
if (-1 === bestParentIdx) { | ||
throw new Error('Possible parent not really in ancestry'); | ||
} | ||
return prevParents.slice(1, bestParentIdx).reverse().find(function (path) { | ||
return (path.parentPath.isBlockStatement() || path.parentPath.isProgram()) && path.parentPath.scope !== prevPath.parentPath.scope; | ||
}); | ||
} | ||
function _logAllProgram(path, label) { | ||
var rootNode = path.getAncestry().pop().node; | ||
console.error(label); | ||
console.error(require('babel-generator').default(rootNode).code); | ||
console.error('\n=======================================================\n'); | ||
} | ||
function build(babel) { | ||
var t = babel.types; | ||
function _exit(path) { | ||
var node = path.node; | ||
if (path.node._hoisted) { | ||
return; | ||
} | ||
if (path.isClassMethod() || path.isObjectMethod() || node[$boundArrowFunction] || node[$usedEval]) { | ||
return; | ||
} | ||
if (path.findParent(_ref2)) { | ||
path.skip(); | ||
return; | ||
} | ||
var bestParentScope = getHighestCompatibleHoistedScope(path); | ||
if (bestParentScope) { | ||
var attachPath = getAttachmentPosition(bestParentScope.path, path); | ||
if (attachPath) { | ||
// _logAllProgram(path, 'before');// debug | ||
moveToNewPosition(path, attachPath); | ||
// _logAllProgram(path, 'after');// debug | ||
} | ||
} | ||
} | ||
return { | ||
@@ -133,13 +26,60 @@ visitor: { | ||
exit: function exit(path) { | ||
babel.traverse.clearCache(); | ||
if (babel.traverse.cache) { | ||
babel.traverse.cache.clear(); | ||
} else { | ||
babel.traverse.clearCache(); | ||
} | ||
path.scope.crawl(); | ||
path.traverse({ | ||
Function: { | ||
exit: _exit | ||
exit: function exit(path) { | ||
var node = path.node; | ||
if (path.node._hoisted) { | ||
return; | ||
} | ||
if (path.isClassMethod() || path.isObjectMethod() || node[$boundArrowFunction] || node[$usedEval]) { | ||
return; | ||
} | ||
if (path.findParent(function (_ref) { | ||
var node = _ref.node; | ||
return node._generated || node._compact; | ||
})) { | ||
path.skip(); | ||
return; | ||
} | ||
var bestParentScope = getHighestCompatibleHoistedScope(path); | ||
if (bestParentScope) { | ||
var attachPath = getAttachmentPosition(bestParentScope.path, path); | ||
if (attachPath) { | ||
// _logAllProgram(path, 'before');// debug | ||
moveToNewPosition(path, attachPath); | ||
// _logAllProgram(path, 'after');// debug | ||
} | ||
} | ||
} | ||
}, | ||
ThisExpression: { | ||
enter: _enter | ||
enter: function enter(path) { | ||
var parentFunctions = path.getAncestry().filter(function (path) { | ||
return path.isFunction(); | ||
}), | ||
nearestNoArrowFunction = parentFunctions.findIndex(function (path) { | ||
return path.type !== 'ArrowFunctionExpression'; | ||
}); | ||
parentFunctions.slice(0, nearestNoArrowFunction).forEach(function (parentArrow) { | ||
parentArrow.node[$boundArrowFunction] = true; | ||
}); | ||
} | ||
}, | ||
Identifier: { | ||
enter: _enter2 | ||
enter: function enter(path) { | ||
if (path.node.name === 'eval' && path.parentPath.type === 'CallExpression') { | ||
path.getAncestry().filter(function (path) { | ||
return path.isFunction(); | ||
}).forEach(function (parentArrow) { | ||
parentArrow.node[$usedEval] = true; | ||
}); | ||
} | ||
} | ||
} | ||
@@ -157,3 +97,43 @@ }); | ||
} | ||
}function moveToNewPosition(path, attachPath) { | ||
} | ||
function getNearestScopeWithLocalUsedVars(path) { | ||
var disableRoot = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : false; | ||
var scope = path.scope, | ||
rootScope = path.scope.getProgramParent(); | ||
do { | ||
scope = scope.parent; | ||
var bindings = scope.getAllBindings(); | ||
for (var id in bindings) { | ||
if (!scope.hasOwnBinding(id)) { | ||
break; | ||
} | ||
var references = [].concat(bindings[id].referencePaths).concat(bindings[id].constantViolations), | ||
usedReferences = references.filter(function (refPath) { | ||
return refPath.isDescendant(path); | ||
}); | ||
if (usedReferences.length) { | ||
return scope; | ||
} | ||
} | ||
if (disableRoot && scope.parent === rootScope) { | ||
return scope; | ||
} | ||
} while (scope.parent); | ||
return scope; | ||
} | ||
function getAttachmentPosition(bestParent, prevPath) { | ||
var prevParents = prevPath.getAncestry(), | ||
bestParentIdx = prevParents.indexOf(bestParent); | ||
if (-1 === bestParentIdx) { | ||
throw new Error('Possible parent not really in ancestry'); | ||
} | ||
return prevParents.slice(1, bestParentIdx).reverse().find(function (path) { | ||
return (path.parentPath.isBlockStatement() || path.parentPath.isProgram()) && path.parentPath.scope !== prevPath.parentPath.scope; | ||
}); | ||
} | ||
function moveToNewPosition(path, attachPath) { | ||
var node = path.node, | ||
@@ -206,2 +186,9 @@ scope = path.scope, | ||
} | ||
function _logAllProgram(path, label) { | ||
var rootNode = path.getAncestry().pop().node; | ||
console.error(label); | ||
console.error(require('babel-generator').default(rootNode).code); | ||
console.error('\n=======================================================\n'); | ||
} | ||
} |
{ | ||
"name": "babel-plugin-closure-elimination", | ||
"version": "1.2.0", | ||
"version": "1.2.1", | ||
"description": "Removes closures from your JavaScript in the name of performance.", | ||
@@ -8,3 +8,3 @@ "main": "lib/index.js", | ||
"build": "babel -d ./lib ./src", | ||
"build-eliminated": "babel --plugins ../lib -d ./lib ./src", | ||
"build-eliminated": "cd src && babel --plugins ../lib -d ./lib ./", | ||
"prepublish": "npm run test", | ||
@@ -36,16 +36,16 @@ "pretest": "npm run build && npm run build-eliminated", | ||
"devDependencies": { | ||
"babel-cli": "^6.4.0", | ||
"babel-core": "^6.4.0", | ||
"babel-generator": "^6.18.0", | ||
"babel-plugin-syntax-async-functions": "^6.3.13", | ||
"babel-cli": "^6.26.0", | ||
"babel-core": "^6.26.0", | ||
"babel-generator": "^6.26.0", | ||
"babel-plugin-syntax-async-functions": "^6.13.0", | ||
"babel-plugin-transform-async-to-generator": "^6.24.1", | ||
"babel-plugin-transform-es2015-destructuring": "^6.18.0", | ||
"babel-plugin-transform-es2015-modules-commonjs": "^6.18.0", | ||
"babel-plugin-transform-flow-strip-types": "^6.4.0", | ||
"babel-plugin-transform-regenerator": "^6.4.4", | ||
"babel-preset-es2015": "^6.3.13", | ||
"babel-plugin-transform-es2015-destructuring": "^6.23.0", | ||
"babel-plugin-transform-es2015-modules-commonjs": "^6.26.0", | ||
"babel-plugin-transform-flow-strip-types": "^6.22.0", | ||
"babel-plugin-transform-regenerator": "^6.26.0", | ||
"babel-preset-env": "^1.6.0", | ||
"babel-preset-es2015": "^6.24.1", | ||
"babel-preset-es2015-node5": "^1.2.0", | ||
"babel-preset-latest": "^6.16.0", | ||
"babel-regenerator-runtime": "^6.3.13", | ||
"babel-register": "^6.4.3", | ||
"babel-register": "^6.26.0", | ||
"lodash": "^4.16.5", | ||
@@ -52,0 +52,0 @@ "mocha": "^2.2.4", |
@@ -16,3 +16,7 @@ import 'babel-polyfill'; | ||
exit(path) { | ||
babel.traverse.clearCache() | ||
if (babel.traverse.cache) { | ||
babel.traverse.cache.clear() | ||
} else { | ||
babel.traverse.clearCache() | ||
} | ||
path.scope.crawl(); | ||
@@ -19,0 +23,0 @@ path.traverse({ |
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
34113
8
889
1