@babel/plugin-transform-block-scoping
Advanced tools
Comparing version 7.0.0-beta.31 to 7.0.0-beta.32
163
lib/index.js
@@ -6,8 +6,4 @@ "use strict"; | ||
var _traverse = _interopRequireDefault(require("@babel/traverse")); | ||
var _tdz = require("./tdz"); | ||
var t = _interopRequireWildcard(require("@babel/types")); | ||
var _values = _interopRequireDefault(require("lodash/values")); | ||
@@ -17,6 +13,4 @@ | ||
var _template = _interopRequireDefault(require("@babel/template")); | ||
var _core = require("@babel/core"); | ||
function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } else { var newObj = {}; if (obj != null) { for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) newObj[key] = obj[key]; } } newObj.default = obj; return newObj; } } | ||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } | ||
@@ -26,6 +20,19 @@ | ||
function _default() { | ||
function _default(api, opts) { | ||
var _opts$throwIfClosureR = opts.throwIfClosureRequired, | ||
throwIfClosureRequired = _opts$throwIfClosureR === void 0 ? false : _opts$throwIfClosureR, | ||
_opts$tdz = opts.tdz, | ||
tdzEnabled = _opts$tdz === void 0 ? false : _opts$tdz; | ||
if (typeof throwIfClosureRequired !== "boolean") { | ||
throw new Error(".throwIfClosureRequired must be a boolean, or undefined"); | ||
} | ||
if (typeof tdzEnabled !== "boolean") { | ||
throw new Error(".throwIfClosureRequired must be a boolean, or undefined"); | ||
} | ||
return { | ||
visitor: { | ||
VariableDeclaration: function VariableDeclaration(path, file) { | ||
VariableDeclaration: function VariableDeclaration(path) { | ||
var node = path.node, | ||
@@ -44,8 +51,9 @@ parent = path.parent, | ||
if (decl.init) { | ||
var assign = t.assignmentExpression("=", decl.id, decl.init); | ||
var assign = _core.types.assignmentExpression("=", decl.id, decl.init); | ||
assign._ignoreBlockScopingTDZ = true; | ||
nodes.push(t.expressionStatement(assign)); | ||
nodes.push(_core.types.expressionStatement(assign)); | ||
} | ||
decl.init = file.addHelper("temporalUndefined"); | ||
decl.init = this.addHelper("temporalUndefined"); | ||
} | ||
@@ -56,3 +64,3 @@ | ||
if (path.isCompletionRecord()) { | ||
nodes.push(t.expressionStatement(scope.buildUndefinedNode())); | ||
nodes.push(_core.types.expressionStatement(scope.buildUndefinedNode())); | ||
} | ||
@@ -63,19 +71,19 @@ | ||
}, | ||
Loop: function Loop(path, file) { | ||
Loop: function Loop(path) { | ||
var parent = path.parent, | ||
scope = path.scope; | ||
path.ensureBlock(); | ||
var blockScoping = new BlockScoping(path, path.get("body"), parent, scope, file); | ||
var blockScoping = new BlockScoping(path, path.get("body"), parent, scope, throwIfClosureRequired, tdzEnabled); | ||
var replace = blockScoping.run(); | ||
if (replace) path.replaceWith(replace); | ||
}, | ||
CatchClause: function CatchClause(path, file) { | ||
CatchClause: function CatchClause(path) { | ||
var parent = path.parent, | ||
scope = path.scope; | ||
var blockScoping = new BlockScoping(null, path.get("body"), parent, scope, file); | ||
var blockScoping = new BlockScoping(null, path.get("body"), parent, scope, throwIfClosureRequired, tdzEnabled); | ||
blockScoping.run(); | ||
}, | ||
"BlockStatement|SwitchStatement|Program": function BlockStatementSwitchStatementProgram(path, file) { | ||
"BlockStatement|SwitchStatement|Program": function BlockStatementSwitchStatementProgram(path) { | ||
if (!ignoreBlock(path)) { | ||
var blockScoping = new BlockScoping(null, path, path.parent, path.scope, file); | ||
var blockScoping = new BlockScoping(null, path, path.parent, path.scope, throwIfClosureRequired, tdzEnabled); | ||
blockScoping.run(); | ||
@@ -89,10 +97,10 @@ } | ||
function ignoreBlock(path) { | ||
return t.isLoop(path.parent) || t.isCatchClause(path.parent); | ||
return _core.types.isLoop(path.parent) || _core.types.isCatchClause(path.parent); | ||
} | ||
var buildRetCheck = (0, _template.default)("\n if (typeof RETURN === \"object\") return RETURN.v;\n"); | ||
var buildRetCheck = (0, _core.template)("\n if (typeof RETURN === \"object\") return RETURN.v;\n"); | ||
function isBlockScoped(node) { | ||
if (!t.isVariableDeclaration(node)) return false; | ||
if (node[t.BLOCK_SCOPED_SYMBOL]) return true; | ||
if (!_core.types.isVariableDeclaration(node)) return false; | ||
if (node[_core.types.BLOCK_SCOPED_SYMBOL]) return true; | ||
if (node.kind !== "let" && node.kind !== "const") return false; | ||
@@ -118,3 +126,3 @@ return true; | ||
if (isInLoop(path) && !t.isFor(parent)) { | ||
if (isInLoop(path) && !_core.types.isFor(parent)) { | ||
for (var i = 0; i < node.declarations.length; i++) { | ||
@@ -126,3 +134,3 @@ var declar = node.declarations[i]; | ||
node[t.BLOCK_SCOPED_SYMBOL] = true; | ||
node[_core.types.BLOCK_SCOPED_SYMBOL] = true; | ||
node.kind = "var"; | ||
@@ -143,3 +151,3 @@ | ||
function isVar(node) { | ||
return t.isVariableDeclaration(node, { | ||
return _core.types.isVariableDeclaration(node, { | ||
kind: "var" | ||
@@ -149,3 +157,3 @@ }) && !isBlockScoped(node); | ||
var letReferenceBlockVisitor = _traverse.default.visitors.merge([{ | ||
var letReferenceBlockVisitor = _core.traverse.visitors.merge([{ | ||
Loop: { | ||
@@ -168,3 +176,3 @@ enter: function enter(path, state) { | ||
var letReferenceFunctionVisitor = _traverse.default.visitors.merge([{ | ||
var letReferenceFunctionVisitor = _core.traverse.visitors.merge([{ | ||
ReferencedIdentifier: function ReferencedIdentifier(path, state) { | ||
@@ -191,3 +199,3 @@ var ref = state.letReferences[path.node.name]; | ||
} else { | ||
node.init = t.sequenceExpression(nodes); | ||
node.init = _core.types.sequenceExpression(nodes); | ||
} | ||
@@ -202,3 +210,3 @@ } | ||
path.replaceWithMultiple(self.pushDeclar(node).map(function (expr) { | ||
return t.expressionStatement(expr); | ||
return _core.types.expressionStatement(expr); | ||
})); | ||
@@ -233,5 +241,5 @@ } else if (path.isFunction()) { | ||
function loopNodeTo(node) { | ||
if (t.isBreakStatement(node)) { | ||
if (_core.types.isBreakStatement(node)) { | ||
return "break"; | ||
} else if (t.isContinueStatement(node)) { | ||
} else if (_core.types.isContinueStatement(node)) { | ||
return "continue"; | ||
@@ -276,3 +284,3 @@ } | ||
if (state.ignoreLabeless) return; | ||
if (t.isBreakStatement(node) && t.isSwitchCase(parent)) return; | ||
if (_core.types.isBreakStatement(node) && _core.types.isSwitchCase(parent)) return; | ||
} | ||
@@ -282,3 +290,3 @@ | ||
state.map[loopText] = node; | ||
replace = t.stringLiteral(loopText); | ||
replace = _core.types.stringLiteral(loopText); | ||
} | ||
@@ -288,10 +296,10 @@ | ||
state.hasReturn = true; | ||
replace = t.objectExpression([t.objectProperty(t.identifier("v"), node.argument || scope.buildUndefinedNode())]); | ||
replace = _core.types.objectExpression([_core.types.objectProperty(_core.types.identifier("v"), node.argument || scope.buildUndefinedNode())]); | ||
} | ||
if (replace) { | ||
replace = t.returnStatement(replace); | ||
replace = _core.types.returnStatement(replace); | ||
replace[this.LOOP_IGNORE] = true; | ||
path.skip(); | ||
path.replaceWith(t.inherits(replace, node)); | ||
path.replaceWith(_core.types.inherits(replace, node)); | ||
} | ||
@@ -302,6 +310,7 @@ } | ||
var BlockScoping = function () { | ||
function BlockScoping(loopPath, blockPath, parent, scope, file) { | ||
function BlockScoping(loopPath, blockPath, parent, scope, throwIfClosureRequired, tdzEnabled) { | ||
this.parent = parent; | ||
this.scope = scope; | ||
this.file = file; | ||
this.throwIfClosureRequired = throwIfClosureRequired; | ||
this.tdzEnabled = tdzEnabled; | ||
this.blockPath = blockPath; | ||
@@ -316,3 +325,3 @@ this.block = blockPath.node; | ||
this.loopParent = loopPath.parent; | ||
this.loopLabel = t.isLabeledStatement(this.loopParent) && this.loopParent.label; | ||
this.loopLabel = _core.types.isLabeledStatement(this.loopParent) && this.loopParent.label; | ||
this.loopPath = loopPath; | ||
@@ -331,3 +340,3 @@ this.loop = loopPath.node; | ||
if (t.isFunction(this.parent) || t.isProgram(this.block)) { | ||
if (_core.types.isFunction(this.parent) || _core.types.isProgram(this.block)) { | ||
this.updateScopeInfo(); | ||
@@ -347,4 +356,4 @@ return; | ||
if (this.loopLabel && !t.isLabeledStatement(this.loopParent)) { | ||
return t.labeledStatement(this.loopLabel, this.loop); | ||
if (this.loopLabel && !_core.types.isLabeledStatement(this.loopParent)) { | ||
return _core.types.labeledStatement(this.loopLabel, this.loop); | ||
} | ||
@@ -395,3 +404,3 @@ }; | ||
_proto.wrapClosure = function wrapClosure() { | ||
if (this.file.opts.throwIfClosureRequired) { | ||
if (this.throwIfClosureRequired) { | ||
throw this.blockPath.buildCodeFrameError("Compiling let/const in this block would add a closure " + "(throwIfClosureRequired)."); | ||
@@ -421,23 +430,27 @@ } | ||
var params = args.map(function (id) { | ||
return t.clone(id); | ||
return _core.types.clone(id); | ||
}); | ||
var isSwitch = this.blockPath.isSwitchStatement(); | ||
var fn = t.functionExpression(null, params, t.blockStatement(isSwitch ? [block] : block.body)); | ||
var fn = _core.types.functionExpression(null, params, _core.types.blockStatement(isSwitch ? [block] : block.body)); | ||
this.addContinuations(fn); | ||
var call = t.callExpression(t.nullLiteral(), args); | ||
var call = _core.types.callExpression(_core.types.nullLiteral(), args); | ||
var basePath = ".callee"; | ||
var hasYield = _traverse.default.hasType(fn.body, "YieldExpression", t.FUNCTION_TYPES); | ||
var hasYield = _core.traverse.hasType(fn.body, "YieldExpression", _core.types.FUNCTION_TYPES); | ||
if (hasYield) { | ||
fn.generator = true; | ||
call = t.yieldExpression(call, true); | ||
call = _core.types.yieldExpression(call, true); | ||
basePath = ".argument" + basePath; | ||
} | ||
var hasAsync = _traverse.default.hasType(fn.body, "AwaitExpression", t.FUNCTION_TYPES); | ||
var hasAsync = _core.traverse.hasType(fn.body, "AwaitExpression", _core.types.FUNCTION_TYPES); | ||
if (hasAsync) { | ||
fn.async = true; | ||
call = t.awaitExpression(call); | ||
call = _core.types.awaitExpression(call); | ||
basePath = ".argument" + basePath; | ||
@@ -451,3 +464,3 @@ } | ||
var ret = this.scope.generateUidIdentifier("ret"); | ||
this.body.push(t.variableDeclaration("var", [t.variableDeclarator(ret, call)])); | ||
this.body.push(_core.types.variableDeclaration("var", [_core.types.variableDeclarator(ret, call)])); | ||
placeholderPath = "declarations.0.init" + basePath; | ||
@@ -457,3 +470,3 @@ index = this.body.length - 1; | ||
} else { | ||
this.body.push(t.expressionStatement(call)); | ||
this.body.push(_core.types.expressionStatement(call)); | ||
placeholderPath = "expression" + basePath; | ||
@@ -482,3 +495,3 @@ index = this.body.length - 1; | ||
var ref = this.scope.generateUidIdentifier("loop"); | ||
var p = this.loopPath.insertBefore(t.variableDeclaration("var", [t.variableDeclarator(ref, fn)])); | ||
var p = this.loopPath.insertBefore(_core.types.variableDeclaration("var", [_core.types.variableDeclarator(ref, fn)])); | ||
placeholder.replaceWith(ref); | ||
@@ -507,3 +520,3 @@ fnPath = p[0].get("declarations.0.init"); | ||
this.scope.rename(param.name, newParam.name, fn); | ||
fn.body.body.push(t.expressionStatement(t.assignmentExpression("=", param, newParam))); | ||
fn.body.body.push(_core.types.expressionStatement(_core.types.assignmentExpression("=", param, newParam))); | ||
} | ||
@@ -523,3 +536,3 @@ }; | ||
declarators.push(init); | ||
(0, _extend.default)(this.outsideLetReferences, t.getBindingIdentifiers(init)); | ||
(0, _extend.default)(this.outsideLetReferences, _core.types.getBindingIdentifiers(init)); | ||
} | ||
@@ -531,3 +544,3 @@ } | ||
if (t.isClassDeclaration(node) || t.isFunctionDeclaration(node) || isBlockScoped(node)) { | ||
if (_core.types.isClassDeclaration(node) || _core.types.isFunctionDeclaration(node) || isBlockScoped(node)) { | ||
if (isBlockScoped(node)) { | ||
@@ -540,3 +553,3 @@ convertBlockScopedToVar(path, node, block, _this.scope); | ||
if (t.isLabeledStatement(node)) { | ||
if (_core.types.isLabeledStatement(node)) { | ||
addDeclarationsFromChild(path.get("body"), node.body); | ||
@@ -568,3 +581,5 @@ } | ||
var _declar = declarators[_i2]; | ||
var keys = t.getBindingIdentifiers(_declar, false, true); | ||
var keys = _core.types.getBindingIdentifiers(_declar, false, true); | ||
(0, _extend.default)(this.letReferences, keys); | ||
@@ -578,4 +593,7 @@ this.hasLetReferences = true; | ||
closurify: false, | ||
file: this.file, | ||
loopDepth: 0 | ||
loopDepth: 0, | ||
tdzEnabled: this.tdzEnabled, | ||
addHelper: function addHelper(name) { | ||
return _this.addHelper(name); | ||
} | ||
}; | ||
@@ -613,9 +631,10 @@ | ||
var declars = []; | ||
var names = t.getBindingIdentifiers(node); | ||
var names = _core.types.getBindingIdentifiers(node); | ||
for (var name in names) { | ||
declars.push(t.variableDeclarator(names[name])); | ||
declars.push(_core.types.variableDeclarator(names[name])); | ||
} | ||
this.body.push(t.variableDeclaration(node.kind, declars)); | ||
this.body.push(_core.types.variableDeclaration(node.kind, declars)); | ||
var replace = []; | ||
@@ -626,4 +645,6 @@ | ||
if (!declar.init) continue; | ||
var expr = t.assignmentExpression("=", declar.id, declar.init); | ||
replace.push(t.inherits(expr, declar)); | ||
var expr = _core.types.assignmentExpression("=", declar.id, declar.init); | ||
replace.push(_core.types.inherits(expr, declar)); | ||
} | ||
@@ -648,7 +669,7 @@ | ||
for (var key in has.map) { | ||
cases.push(t.switchCase(t.stringLiteral(key), [has.map[key]])); | ||
cases.push(_core.types.switchCase(_core.types.stringLiteral(key), [has.map[key]])); | ||
} | ||
if (has.hasReturn) { | ||
cases.push(t.switchCase(null, [retCheck])); | ||
cases.push(_core.types.switchCase(null, [retCheck])); | ||
} | ||
@@ -658,3 +679,3 @@ | ||
var single = cases[0]; | ||
body.push(t.ifStatement(t.binaryExpression("===", ret, single.test), single.consequent[0])); | ||
body.push(_core.types.ifStatement(_core.types.binaryExpression("===", ret, single.test), single.consequent[0])); | ||
} else { | ||
@@ -665,3 +686,3 @@ if (this.loop) { | ||
if (t.isBreakStatement(caseConsequent) && !caseConsequent.label) { | ||
if (_core.types.isBreakStatement(caseConsequent) && !caseConsequent.label) { | ||
caseConsequent.label = this.loopLabel = this.loopLabel || this.scope.generateUidIdentifier("loop"); | ||
@@ -672,3 +693,3 @@ } | ||
body.push(t.switchStatement(ret, cases)); | ||
body.push(_core.types.switchStatement(ret, cases)); | ||
} | ||
@@ -675,0 +696,0 @@ } else { |
@@ -6,6 +6,4 @@ "use strict"; | ||
var t = _interopRequireWildcard(require("@babel/types")); | ||
var _core = require("@babel/core"); | ||
function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } else { var newObj = {}; if (obj != null) { for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) newObj[key] = obj[key]; } } newObj.default = obj; return newObj; } } | ||
function getTDZStatus(refPath, bindingPath) { | ||
@@ -23,4 +21,4 @@ var executionStatus = bindingPath._guessExecutionStatusRelativeTo(refPath); | ||
function buildTDZAssert(node, file) { | ||
return t.callExpression(file.addHelper("temporalRef"), [node, t.stringLiteral(node.name)]); | ||
function buildTDZAssert(node, state) { | ||
return _core.types.callExpression(state.addHelper("temporalRef"), [node, _core.types.stringLiteral(node.name)]); | ||
} | ||
@@ -36,3 +34,3 @@ | ||
ReferencedIdentifier: function ReferencedIdentifier(path, state) { | ||
if (!this.file.opts.tdz) return; | ||
if (!state.tdzEnabled) return; | ||
var node = path.node, | ||
@@ -50,3 +48,3 @@ parent = path.parent, | ||
if (status === "maybe") { | ||
var assert = buildTDZAssert(node, state.file); | ||
var assert = buildTDZAssert(node, state); | ||
bindingPath.parent._tdzThis = true; | ||
@@ -57,3 +55,3 @@ path.skip(); | ||
if (parent._ignoreBlockScopingTDZ) return; | ||
path.parentPath.replaceWith(t.sequenceExpression([assert, parent])); | ||
path.parentPath.replaceWith(_core.types.sequenceExpression([assert, parent])); | ||
} else { | ||
@@ -63,3 +61,3 @@ path.replaceWith(assert); | ||
} else if (status === "outside") { | ||
path.replaceWith(t.throwStatement(t.inherits(t.newExpression(t.identifier("ReferenceError"), [t.stringLiteral(node.name + " is not defined - temporal dead zone")]), node))); | ||
path.replaceWith(_core.types.throwStatement(_core.types.inherits(_core.types.newExpression(_core.types.identifier("ReferenceError"), [_core.types.stringLiteral(node.name + " is not defined - temporal dead zone")]), node))); | ||
} | ||
@@ -69,3 +67,3 @@ }, | ||
exit: function exit(path, state) { | ||
if (!this.file.opts.tdz) return; | ||
if (!state.tdzEnabled) return; | ||
var node = path.node; | ||
@@ -80,3 +78,3 @@ if (node._ignoreBlockScopingTDZ) return; | ||
if (isReference(id, path.scope, state)) { | ||
nodes.push(buildTDZAssert(id, state.file)); | ||
nodes.push(buildTDZAssert(id, state)); | ||
} | ||
@@ -88,3 +86,3 @@ } | ||
nodes.push(node); | ||
path.replaceWithMultiple(nodes.map(t.expressionStatement)); | ||
path.replaceWithMultiple(nodes.map(_core.types.expressionStatement)); | ||
} | ||
@@ -91,0 +89,0 @@ } |
{ | ||
"name": "@babel/plugin-transform-block-scoping", | ||
"version": "7.0.0-beta.31", | ||
"version": "7.0.0-beta.32", | ||
"description": "Compile ES2015 block scoping (const and let) to ES5", | ||
@@ -9,5 +9,2 @@ "repository": "https://github.com/babel/babel/tree/master/packages/babel-plugin-transform-block-scoping", | ||
"dependencies": { | ||
"@babel/template": "7.0.0-beta.31", | ||
"@babel/traverse": "7.0.0-beta.31", | ||
"@babel/types": "7.0.0-beta.31", | ||
"lodash": "^4.2.0" | ||
@@ -19,8 +16,8 @@ }, | ||
"peerDependencies": { | ||
"@babel/core": "7.0.0-beta.31" | ||
"@babel/core": "7.0.0-beta.32" | ||
}, | ||
"devDependencies": { | ||
"@babel/core": "7.0.0-beta.31", | ||
"@babel/helper-plugin-test-runner": "7.0.0-beta.31" | ||
"@babel/core": "7.0.0-beta.32", | ||
"@babel/helper-plugin-test-runner": "7.0.0-beta.32" | ||
} | ||
} |
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
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
23886
2
614
+ Added@babel/code-frame@7.0.0-beta.32(transitive)
+ Added@babel/core@7.0.0-beta.32(transitive)
+ Added@babel/generator@7.0.0-beta.32(transitive)
+ Added@babel/helper-function-name@7.0.0-beta.32(transitive)
+ Added@babel/helper-get-function-arity@7.0.0-beta.32(transitive)
+ Added@babel/helpers@7.0.0-beta.32(transitive)
+ Added@babel/template@7.0.0-beta.32(transitive)
+ Added@babel/traverse@7.0.0-beta.32(transitive)
+ Added@babel/types@7.0.0-beta.32(transitive)
+ Addedbabylon@7.0.0-beta.32(transitive)
- Removed@babel/template@7.0.0-beta.31
- Removed@babel/traverse@7.0.0-beta.31
- Removed@babel/types@7.0.0-beta.31
- Removed@babel/code-frame@7.0.0-beta.31(transitive)
- Removed@babel/core@7.0.0-beta.31(transitive)
- Removed@babel/generator@7.0.0-beta.31(transitive)
- Removed@babel/helper-function-name@7.0.0-beta.31(transitive)
- Removed@babel/helper-get-function-arity@7.0.0-beta.31(transitive)
- Removed@babel/helpers@7.0.0-beta.31(transitive)
- Removed@babel/template@7.0.0-beta.31(transitive)
- Removed@babel/traverse@7.0.0-beta.31(transitive)
- Removed@babel/types@7.0.0-beta.31(transitive)
- Removedbabylon@7.0.0-beta.31(transitive)