babel-plugin-transform-es2015-block-scoping
Advanced tools
Comparing version
@@ -12,3 +12,2 @@ "use strict"; | ||
scope = path.scope; | ||
if (!isBlockScoped(node)) return; | ||
@@ -22,2 +21,3 @@ convertBlockScopedToVar(path, null, parent, scope, true); | ||
var decl = node.declarations[i]; | ||
if (decl.init) { | ||
@@ -28,2 +28,3 @@ var assign = t.assignmentExpression("=", decl.id, decl.init); | ||
} | ||
decl.init = file.addHelper("temporalUndefined"); | ||
@@ -45,3 +46,2 @@ } | ||
scope = path.scope; | ||
t.ensureBlock(node); | ||
@@ -55,3 +55,2 @@ var blockScoping = new BlockScoping(path, path.get("body"), parent, scope, file); | ||
scope = path.scope; | ||
var blockScoping = new BlockScoping(null, path.get("body"), parent, scope, file); | ||
@@ -96,3 +95,3 @@ blockScoping.run(); | ||
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } | ||
var DONE = new WeakSet(); | ||
@@ -132,2 +131,3 @@ function ignoreBlock(path) { | ||
var ids = path.getBindingIdentifiers(); | ||
for (var name in ids) { | ||
@@ -142,3 +142,5 @@ var binding = scope.getOwnBinding(name); | ||
function isVar(node) { | ||
return t.isVariableDeclaration(node, { kind: "var" }) && !isBlockScoped(node); | ||
return t.isVariableDeclaration(node, { | ||
kind: "var" | ||
}) && !isBlockScoped(node); | ||
} | ||
@@ -159,2 +161,3 @@ | ||
} | ||
return path.skip(); | ||
@@ -167,8 +170,5 @@ } | ||
var ref = state.letReferences[path.node.name]; | ||
if (!ref) return; | ||
var localBinding = path.scope.getBindingIdentifier(path.node.name); | ||
if (localBinding && localBinding !== ref) return; | ||
state.closurify = true; | ||
@@ -183,6 +183,6 @@ } | ||
if (path.isForStatement()) { | ||
if (isVar(node.init, node)) { | ||
var nodes = self.pushDeclar(node.init); | ||
if (nodes.length === 1) { | ||
@@ -208,11 +208,8 @@ node.init = nodes[0]; | ||
}; | ||
var loopLabelVisitor = { | ||
LabeledStatement: function LabeledStatement(_ref, state) { | ||
var node = _ref.node; | ||
state.innerLabels.push(node.label.name); | ||
} | ||
}; | ||
var continuationVisitor = { | ||
@@ -222,2 +219,3 @@ enter: function enter(path, state) { | ||
var bindings = path.getBindingIdentifiers(); | ||
for (var name in bindings) { | ||
@@ -227,2 +225,3 @@ if (state.outsideReferences[name] !== path.scope.getBindingIdentifier(name)) { | ||
} | ||
state.reassignments[name] = true; | ||
@@ -264,5 +263,3 @@ } | ||
scope = path.scope; | ||
if (node[this.LOOP_IGNORE]) return; | ||
var replace = void 0; | ||
@@ -280,3 +277,2 @@ var loopText = loopNodeTo(node); | ||
if (state.ignoreLabeless) return; | ||
if (t.isBreakStatement(node) && t.isSwitchCase(parent)) return; | ||
@@ -306,11 +302,7 @@ } | ||
function BlockScoping(loopPath, blockPath, parent, scope, file) { | ||
_classCallCheck(this, BlockScoping); | ||
this.parent = parent; | ||
this.scope = scope; | ||
this.file = file; | ||
this.blockPath = blockPath; | ||
this.block = blockPath.node; | ||
this.outsideLetReferences = Object.create(null); | ||
@@ -331,5 +323,4 @@ this.hasLetReferences = false; | ||
var block = this.block; | ||
if (block._letDone) return; | ||
block._letDone = true; | ||
if (DONE.has(block)) return; | ||
DONE.add(block); | ||
var needsClosure = this.getLetReferences(); | ||
@@ -366,2 +357,3 @@ | ||
if (!binding) continue; | ||
if (binding.kind === "let" || binding.kind === "const") { | ||
@@ -402,4 +394,4 @@ binding.kind = "var"; | ||
} | ||
var block = this.block; | ||
var outsideRefs = this.outsideLetReferences; | ||
@@ -414,5 +406,3 @@ | ||
delete this.letReferences[id.name]; | ||
this.scope.rename(id.name); | ||
this.letReferences[id.name] = id; | ||
@@ -425,14 +415,8 @@ outsideRefs[id.name] = id; | ||
this.has = this.checkLoop(); | ||
this.hoistVarDeclarations(); | ||
var params = (0, _values2.default)(outsideRefs); | ||
var args = (0, _values2.default)(outsideRefs); | ||
var isSwitch = this.blockPath.isSwitchStatement(); | ||
var fn = t.functionExpression(null, params, t.blockStatement(isSwitch ? [block] : block.body)); | ||
this.addContinuations(fn); | ||
var call = t.callExpression(t.nullLiteral(), args); | ||
@@ -442,2 +426,3 @@ var basePath = ".callee"; | ||
var hasYield = _babelTraverse2.default.hasType(fn.body, this.scope, "YieldExpression", t.FUNCTION_TYPES); | ||
if (hasYield) { | ||
@@ -450,2 +435,3 @@ fn.generator = true; | ||
var hasAsync = _babelTraverse2.default.hasType(fn.body, this.scope, "AwaitExpression", t.FUNCTION_TYPES); | ||
if (hasAsync) { | ||
@@ -459,9 +445,8 @@ fn.async = true; | ||
var index = void 0; | ||
if (this.has.hasReturn || this.has.hasBreakContinue) { | ||
var ret = this.scope.generateUidIdentifier("ret"); | ||
this.body.push(t.variableDeclaration("var", [t.variableDeclarator(ret, call)])); | ||
placeholderPath = "declarations.0.init" + basePath; | ||
index = this.body.length - 1; | ||
this.buildHas(ret); | ||
@@ -481,4 +466,2 @@ } else { | ||
key = _blockPath.key; | ||
this.blockPath.replaceWithMultiple(this.body); | ||
@@ -492,8 +475,7 @@ callPath = parentPath.get(listKey)[key + index]; | ||
var placeholder = callPath.get(placeholderPath); | ||
var fnPath = void 0; | ||
var fnPath = void 0; | ||
if (this.loop) { | ||
var ref = this.scope.generateUidIdentifier("loop"); | ||
var p = this.loopPath.insertBefore(t.variableDeclaration("var", [t.variableDeclarator(ref, fn)])); | ||
placeholder.replaceWith(ref); | ||
@@ -514,3 +496,2 @@ fnPath = p[0].get("declarations.0.init"); | ||
}; | ||
this.scope.traverse(fn, continuationVisitor, state); | ||
@@ -521,8 +502,5 @@ | ||
if (!state.reassignments[param.name]) continue; | ||
var newParam = this.scope.generateUidIdentifier(param.name); | ||
fn.params[i] = newParam; | ||
this.scope.rename(param.name, newParam.name, fn); | ||
fn.body.body.push(t.expressionStatement(t.assignmentExpression("=", param, newParam))); | ||
@@ -536,3 +514,2 @@ } | ||
var block = this.block; | ||
var declarators = []; | ||
@@ -542,2 +519,3 @@ | ||
var init = this.loop.left || this.loop.init; | ||
if (isBlockScoped(init)) { | ||
@@ -551,2 +529,3 @@ declarators.push(init); | ||
node = node || path.node; | ||
if (t.isClassDeclaration(node) || t.isFunctionDeclaration(node) || isBlockScoped(node)) { | ||
@@ -556,4 +535,6 @@ if (isBlockScoped(node)) { | ||
} | ||
declarators = declarators.concat(node.declarations || node); | ||
} | ||
if (t.isLabeledStatement(node)) { | ||
@@ -577,2 +558,3 @@ addDeclarationsFromChild(path.get("body"), node.body); | ||
var _declarPath = this.blockPath.get("cases")[_i]; | ||
var declar = consequents[j]; | ||
@@ -586,3 +568,2 @@ addDeclarationsFromChild(_declarPath, declar); | ||
var _declar = declarators[_i2]; | ||
var keys = t.getBindingIdentifiers(_declar, false, true); | ||
@@ -594,3 +575,2 @@ (0, _extend2.default)(this.letReferences, keys); | ||
if (!this.hasLetReferences) return; | ||
var state = { | ||
@@ -602,6 +582,6 @@ letReferences: this.letReferences, | ||
}; | ||
var loopOrFunctionParent = this.blockPath.find(function (path) { | ||
return path.isLoop() || path.isFunction(); | ||
}); | ||
if (loopOrFunctionParent && loopOrFunctionParent.isLoop()) { | ||
@@ -612,3 +592,2 @@ state.loopDepth++; | ||
this.blockPath.traverse(letReferenceBlockVisitor, state); | ||
return state.closurify; | ||
@@ -628,6 +607,4 @@ }; | ||
}; | ||
this.blockPath.traverse(loopLabelVisitor, state); | ||
this.blockPath.traverse(loopVisitor, state); | ||
return state; | ||
@@ -643,2 +620,3 @@ }; | ||
var names = t.getBindingIdentifiers(node); | ||
for (var name in names) { | ||
@@ -649,3 +627,2 @@ declars.push(t.variableDeclarator(names[name])); | ||
this.body.push(t.variableDeclaration(node.kind, declars)); | ||
var replace = []; | ||
@@ -656,3 +633,2 @@ | ||
if (!declar.init) continue; | ||
var expr = t.assignmentExpression("=", declar.id, declar.init); | ||
@@ -667,3 +643,2 @@ replace.push(t.inherits(expr, declar)); | ||
var body = this.body; | ||
var retCheck = void 0; | ||
@@ -695,2 +670,3 @@ var has = this.has; | ||
var caseConsequent = cases[i].consequent[0]; | ||
if (t.isBreakStatement(caseConsequent) && !caseConsequent.label) { | ||
@@ -697,0 +673,0 @@ caseConsequent.label = this.loopLabel = this.loopLabel || this.scope.generateUidIdentifier("loop"); |
@@ -31,3 +31,2 @@ "use strict"; | ||
if (!declared) return false; | ||
return scope.getBindingIdentifier(node.name) === declared; | ||
@@ -39,13 +38,10 @@ } | ||
if (!this.file.opts.tdz) return; | ||
var node = path.node, | ||
parent = path.parent, | ||
scope = path.scope; | ||
if (path.parentPath.isFor({ left: node })) return; | ||
if (path.parentPath.isFor({ | ||
left: node | ||
})) return; | ||
if (!isReference(node, scope, state)) return; | ||
var bindingPath = scope.getBinding(node.name).path; | ||
var status = getTDZStatus(path, bindingPath); | ||
@@ -56,5 +52,3 @@ if (status === "inside") return; | ||
var assert = buildTDZAssert(node, state.file); | ||
bindingPath.parent._tdzThis = true; | ||
path.skip(); | ||
@@ -72,12 +66,7 @@ | ||
}, | ||
AssignmentExpression: { | ||
exit: function exit(path, state) { | ||
if (!this.file.opts.tdz) return; | ||
var node = path.node; | ||
if (node._ignoreBlockScopingTDZ) return; | ||
var nodes = []; | ||
@@ -84,0 +73,0 @@ var ids = path.getBindingIdentifiers(); |
{ | ||
"name": "babel-plugin-transform-es2015-block-scoping", | ||
"version": "7.0.0-alpha.15", | ||
"version": "7.0.0-alpha.16", | ||
"description": "Compile ES2015 block scoping (const and let) to ES5", | ||
@@ -9,5 +9,5 @@ "repository": "https://github.com/babel/babel/tree/master/packages/babel-plugin-transform-es2015-block-scoping", | ||
"dependencies": { | ||
"babel-template": "7.0.0-alpha.15", | ||
"babel-traverse": "7.0.0-alpha.15", | ||
"babel-types": "7.0.0-alpha.15", | ||
"babel-template": "7.0.0-alpha.16", | ||
"babel-traverse": "7.0.0-alpha.16", | ||
"babel-types": "7.0.0-alpha.16", | ||
"lodash": "^4.2.0" | ||
@@ -19,4 +19,4 @@ }, | ||
"devDependencies": { | ||
"babel-helper-plugin-test-runner": "7.0.0-alpha.15" | ||
"babel-helper-plugin-test-runner": "7.0.0-alpha.16" | ||
} | ||
} |
601
0.5%23548
-0.83%+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
Updated