babel-plugin-closure-elimination
Advanced tools
Comparing version 1.2.1 to 1.3.0
208
lib/index.js
@@ -7,5 +7,2 @@ 'use strict'; | ||
exports.default = build; | ||
require('babel-polyfill'); | ||
/** | ||
@@ -18,6 +15,113 @@ * # Closure Eliminator | ||
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 { | ||
@@ -35,52 +139,9 @@ visitor: { | ||
Function: { | ||
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 | ||
} | ||
} | ||
} | ||
exit: _exit | ||
}, | ||
ThisExpression: { | ||
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; | ||
}); | ||
} | ||
enter: _enter | ||
}, | ||
Identifier: { | ||
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; | ||
}); | ||
} | ||
} | ||
enter: _enter2 | ||
} | ||
@@ -98,43 +159,3 @@ }); | ||
} | ||
} | ||
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) { | ||
}function moveToNewPosition(path, attachPath) { | ||
var node = path.node, | ||
@@ -187,9 +208,2 @@ 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.1", | ||
"version": "1.3.0", | ||
"description": "Removes closures from your JavaScript in the name of performance.", | ||
@@ -8,4 +8,4 @@ "main": "lib/index.js", | ||
"build": "babel -d ./lib ./src", | ||
"build-eliminated": "cd src && babel --plugins ../lib -d ./lib ./", | ||
"prepublish": "npm run test", | ||
"build-eliminated": "cd src && babel --plugins ../lib -d ../lib ./", | ||
"prepublishOnly": "npm run test", | ||
"pretest": "npm run build && npm run build-eliminated", | ||
@@ -32,5 +32,3 @@ "test": "mocha test/index.js", | ||
"homepage": "https://github.com/codemix/babel-plugin-closure-elimination", | ||
"dependencies": { | ||
"babel-polyfill": "^6.3.14" | ||
}, | ||
"dependencies": {}, | ||
"devDependencies": { | ||
@@ -46,11 +44,12 @@ "babel-cli": "^6.26.0", | ||
"babel-plugin-transform-regenerator": "^6.26.0", | ||
"babel-polyfill": "^6.26.0", | ||
"babel-preset-env": "^1.6.0", | ||
"babel-preset-es2015": "^6.24.1", | ||
"babel-preset-es2015-node5": "^1.2.0", | ||
"babel-regenerator-runtime": "^6.3.13", | ||
"babel-regenerator-runtime": "^6.5.0", | ||
"babel-register": "^6.26.0", | ||
"lodash": "^4.16.5", | ||
"mocha": "^2.2.4", | ||
"should": "^6.0.1" | ||
"lodash": "^4.17.4", | ||
"mocha": "^4.0.1", | ||
"should": "^13.1.2" | ||
} | ||
} |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
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
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
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
0
9954
18
4
180
- Removedbabel-polyfill@^6.3.14
- Removedbabel-polyfill@6.26.0(transitive)
- Removedbabel-runtime@6.26.0(transitive)
- Removedcore-js@2.6.12(transitive)
- Removedregenerator-runtime@0.10.50.11.1(transitive)