babel-plugin-closure-elimination
Advanced tools
Comparing version 1.0.0 to 1.0.1
@@ -1,2 +0,2 @@ | ||
"use strict"; | ||
'use strict'; | ||
@@ -15,2 +15,7 @@ var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }(); | ||
*/ | ||
// Do not use Symbol here. class constructor/body missed Symbol-props | ||
var $classConstructor = '__classConstructor'; | ||
var $classMethod = '__classMethod'; | ||
function build(babel) { | ||
@@ -58,3 +63,3 @@ var t = babel.types; | ||
_createClass(PathHoister, [{ | ||
key: "isCompatibleScope", | ||
key: 'isCompatibleScope', | ||
value: function isCompatibleScope(scope) { | ||
@@ -70,3 +75,3 @@ for (var key in this.bindings) { | ||
}, { | ||
key: "getCompatibleScopes", | ||
key: 'getCompatibleScopes', | ||
value: function getCompatibleScopes() { | ||
@@ -83,3 +88,3 @@ var checkScope = this.path.scope; | ||
}, { | ||
key: "getAttachmentPath", | ||
key: 'getAttachmentPath', | ||
value: function getAttachmentPath() { | ||
@@ -103,3 +108,3 @@ var scopes = this.scopes; | ||
}, { | ||
key: "getNextScopeStatementParent", | ||
key: 'getNextScopeStatementParent', | ||
value: function getNextScopeStatementParent() { | ||
@@ -112,3 +117,3 @@ var scope = this.scopes.pop(); | ||
}, { | ||
key: "hasNonParamBindings", | ||
key: 'hasNonParamBindings', | ||
value: function hasNonParamBindings() { | ||
@@ -124,3 +129,3 @@ for (var name in this.bindings) { | ||
}, { | ||
key: "run", | ||
key: 'run', | ||
value: function run() { | ||
@@ -163,2 +168,15 @@ var node = this.path.node; | ||
visitor: { | ||
Class: { | ||
enter: function enter(path) { | ||
var node = path.node; | ||
node[$classConstructor] = node.body[$classConstructor] = true; | ||
//path.traverse({// @todo maybe need skip methods? | ||
// ClassMethod: { | ||
// enter({node}) { | ||
// node[$classMethod] = node.body[$classMethod] = true; | ||
// } | ||
// } | ||
//}); | ||
} | ||
}, | ||
Function: { | ||
@@ -170,2 +188,12 @@ enter: function enter(path) { | ||
parentScope = scope.parent.getFunctionParent(); | ||
if (node[$classConstructor] || node.body[$classConstructor]) { | ||
return; | ||
} | ||
if (path.findParent(function (_ref) { | ||
var node = _ref.node; | ||
return node._generated || node._compact; | ||
})) { | ||
path.skip(); | ||
return; | ||
} | ||
if (parent.type === 'Program' || parentScope.block.type === 'Program' || parent.type === 'CallExpression' && parent.callee === node) { | ||
@@ -172,0 +200,0 @@ return; |
{ | ||
"name": "babel-plugin-closure-elimination", | ||
"version": "1.0.0", | ||
"version": "1.0.1", | ||
"description": "Removes closures from your JavaScript in the name of performance.", | ||
@@ -5,0 +5,0 @@ "main": "lib/index.js", |
/** | ||
* # Closure Eliminator | ||
*/ | ||
// Do not use Symbol here. class constructor/body missed Symbol-props | ||
const $classConstructor = '__classConstructor'; | ||
const $classMethod = '__classMethod'; | ||
export default function build (babel: Object): Object { | ||
@@ -146,2 +151,15 @@ const {types: t, traverse} = babel; | ||
visitor: { | ||
Class: { | ||
enter(path) { | ||
const node = path.node; | ||
node[$classConstructor] = node.body[$classConstructor] = true; | ||
//path.traverse({// @todo maybe need skip methods? | ||
// ClassMethod: { | ||
// enter({node}) { | ||
// node[$classMethod] = node.body[$classMethod] = true; | ||
// } | ||
// } | ||
//}); | ||
} | ||
}, | ||
Function: { | ||
@@ -153,2 +171,9 @@ enter (path) { | ||
parentScope = scope.parent.getFunctionParent(); | ||
if (node[$classConstructor] || node.body[$classConstructor]) { | ||
return; | ||
} | ||
if (path.findParent(({node}) => node._generated || node._compact)) { | ||
path.skip(); | ||
return; | ||
} | ||
if ( | ||
@@ -155,0 +180,0 @@ parent.type === 'Program' || |
@@ -19,4 +19,2 @@ import Plugin from '../src'; | ||
collected[JSON.stringify(node.loc)] = extractPath(path.scope); | ||
} else { | ||
throw new Error('Wrong transformed function. maybe wrong sourcemap?'); | ||
} | ||
@@ -46,22 +44,31 @@ } | ||
function runTest (basename, numberToRemove) { | ||
function runTest (basename, numberToRemove, expectedResult) { | ||
const source = load(basename); | ||
const transformedNaked = transform(source, {plugins: ["transform-flow-strip-types"]}); | ||
const transformedNaked = transform(source, {"presets": ["es2015"],plugins: ["transform-flow-strip-types"]}); | ||
//console.log(transformedNaked.code); | ||
const transformedWithPlugin = transform(source, {plugins: [Plugin, "transform-flow-strip-types"]}); | ||
const transformedWithPlugin = transform(source, {"presets": ["es2015"],plugins: [Plugin, "transform-flow-strip-types"]}); | ||
//console.log(transformedWithPlugin.code); | ||
const diff = countHoisted(transformedNaked.ast, transformedWithPlugin.ast); | ||
diff.should.equal(numberToRemove); | ||
if (expectedResult) { | ||
const context = { | ||
exports: {} | ||
}; | ||
const loaded = new Function('module', 'exports', transformedWithPlugin.code); | ||
loaded(context, context.exports); | ||
const result = typeof context.exports.default === 'function' ? context.exports.default() : context.exports.default; | ||
result.should.eql(expectedResult); | ||
} | ||
} | ||
function eliminate (basename, numberToRemove) { | ||
function eliminate (basename, numberToRemove, result) { | ||
it(`should eliminate ${numberToRemove} closure(s) from "${basename}"`, function () { | ||
runTest(basename, numberToRemove); | ||
runTest(basename, numberToRemove, result); | ||
}); | ||
} | ||
eliminate.only = function (basename: string, numberToRemove: number) { | ||
eliminate.only = function (basename: string, numberToRemove: number, result) { | ||
it.only(`should eliminate ${numberToRemove} closure(s) from "${basename}"`, function () { | ||
try { | ||
runTest(basename, numberToRemove); | ||
runTest(basename, numberToRemove, result); | ||
} | ||
@@ -101,4 +108,6 @@ catch (e) { | ||
eliminate("class-compiled", 3); | ||
eliminate("class-complex", 2); | ||
eliminate("class-complex", 3); | ||
eliminate("extended-class-from-outer-parent", 2, [["foo", String.prototype.indexOf], ["bar", String.prototype.indexOf]]); | ||
eliminate("extended-class-from-known-class", 2, [["base", "foo"], ["base", "bar"]]); | ||
}); | ||
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
Uses eval
Supply chain riskPackage uses dynamic code execution (e.g., eval()), which is a dangerous practice. This can prevent the code from running in certain environments and increases the risk that the code may contain exploits or malicious behavior.
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
25877
28
728
1