@babel/plugin-transform-block-scoping
Advanced tools
Comparing version 7.20.2 to 7.20.5
@@ -40,3 +40,3 @@ "use strict"; | ||
const assign = _core.types.assignmentExpression("=", _core.types.cloneNode(decl.id), decl.init || scope.buildUndefinedNode()); | ||
assign._ignoreBlockScopingTDZ = true; | ||
_tdz.skipTDZChecks.add(assign); | ||
nodes.push(_core.types.expressionStatement(assign)); | ||
@@ -43,0 +43,0 @@ decl.init = this.addHelper("temporalUndefined"); |
116
lib/tdz.js
@@ -6,3 +6,3 @@ "use strict"; | ||
}); | ||
exports.visitor = void 0; | ||
exports.visitor = exports.skipTDZChecks = void 0; | ||
var _core = require("@babel/core"); | ||
@@ -19,5 +19,13 @@ function getTDZStatus(refPath, bindingPath) { | ||
} | ||
function buildTDZAssert(node, state) { | ||
return _core.types.callExpression(state.addHelper("temporalRef"), [ | ||
node, _core.types.stringLiteral(node.name)]); | ||
const skipTDZChecks = new WeakSet(); | ||
exports.skipTDZChecks = skipTDZChecks; | ||
function buildTDZAssert(status, node, state) { | ||
if (status === "maybe") { | ||
const clone = _core.types.cloneNode(node); | ||
skipTDZChecks.add(clone); | ||
return _core.types.callExpression(state.addHelper("temporalRef"), [ | ||
clone, _core.types.stringLiteral(node.name)]); | ||
} else { | ||
return _core.types.callExpression(state.addHelper("tdz"), [_core.types.stringLiteral(node.name)]); | ||
} | ||
} | ||
@@ -30,59 +38,63 @@ function isReference(node, scope, state) { | ||
} | ||
const visitedMaybeTDZNodes = new WeakSet(); | ||
function getTDZReplacement(path, state, id = path.node) { | ||
if (!isReference(id, path.scope, state)) return; | ||
if (skipTDZChecks.has(id)) return; | ||
skipTDZChecks.add(id); | ||
const bindingPath = path.scope.getBinding(id.name).path; | ||
if (bindingPath.isFunctionDeclaration()) return; | ||
const status = getTDZStatus(path, bindingPath); | ||
if (status === "outside") return; | ||
if (status === "maybe") { | ||
bindingPath.parent._tdzThis = true; | ||
} | ||
return { | ||
status, | ||
node: buildTDZAssert(status, id, state) | ||
}; | ||
} | ||
const visitor = { | ||
ReferencedIdentifier(path, state) { | ||
if (!state.tdzEnabled) return; | ||
if (path.parentPath.isUpdateExpression()) return; | ||
if (path.parentPath.isFor({ | ||
left: path.node | ||
})) return; | ||
const replacement = getTDZReplacement(path, state); | ||
if (!replacement) return; | ||
path.replaceWith(replacement.node); | ||
}, | ||
UpdateExpression(path, state) { | ||
if (!state.tdzEnabled) return; | ||
const { | ||
node, | ||
parent, | ||
scope | ||
node | ||
} = path; | ||
if (path.parentPath.isFor({ | ||
left: node | ||
})) return; | ||
if (!isReference(node, scope, state)) return; | ||
const bindingPath = scope.getBinding(node.name).path; | ||
if (bindingPath.isFunctionDeclaration()) return; | ||
const status = getTDZStatus(path, bindingPath); | ||
if (status === "outside") return; | ||
if (status === "maybe") { | ||
if (visitedMaybeTDZNodes.has(node)) { | ||
return; | ||
} | ||
visitedMaybeTDZNodes.add(node); | ||
const assert = buildTDZAssert(node, state); | ||
bindingPath.parent._tdzThis = true; | ||
if (path.parentPath.isUpdateExpression()) { | ||
if (parent._ignoreBlockScopingTDZ) return; | ||
path.parentPath.replaceWith(_core.types.sequenceExpression([assert, parent])); | ||
} else { | ||
path.replaceWith(assert); | ||
} | ||
} else if (status === "inside") { | ||
path.replaceWith(_core.template.ast`${state.addHelper("tdz")}("${node.name}")`); | ||
if (skipTDZChecks.has(node)) return; | ||
skipTDZChecks.add(node); | ||
const arg = path.get("argument"); | ||
if (!arg.isIdentifier()) return; | ||
const replacement = getTDZReplacement(path, state, arg.node); | ||
if (!replacement) return; | ||
if (replacement.status === "maybe") { | ||
path.insertBefore(replacement.node); | ||
} else { | ||
path.replaceWith(replacement.node); | ||
} | ||
}, | ||
AssignmentExpression: { | ||
exit(path, state) { | ||
if (!state.tdzEnabled) return; | ||
const { | ||
node | ||
} = path; | ||
if (node._ignoreBlockScopingTDZ) return; | ||
const nodes = []; | ||
const ids = path.getBindingIdentifiers(); | ||
for (const name of Object.keys(ids)) { | ||
const id = ids[name]; | ||
if (isReference(id, path.scope, state)) { | ||
nodes.push(id); | ||
} | ||
AssignmentExpression(path, state) { | ||
if (!state.tdzEnabled) return; | ||
const { | ||
node | ||
} = path; | ||
if (skipTDZChecks.has(node)) return; | ||
skipTDZChecks.add(node); | ||
const nodes = []; | ||
const ids = path.getBindingIdentifiers(); | ||
for (const name of Object.keys(ids)) { | ||
const replacement = getTDZReplacement(path, state, ids[name]); | ||
if (replacement) { | ||
nodes.push(_core.types.expressionStatement(replacement.node)); | ||
if (replacement.status === "inside") break; | ||
} | ||
if (nodes.length) { | ||
node._ignoreBlockScopingTDZ = true; | ||
nodes.push(node); | ||
path.replaceWithMultiple(nodes.map(n => _core.types.expressionStatement(n))); | ||
} | ||
} | ||
if (nodes.length > 0) path.insertBefore(nodes); | ||
} | ||
@@ -89,0 +101,0 @@ }; |
{ | ||
"name": "@babel/plugin-transform-block-scoping", | ||
"version": "7.20.2", | ||
"version": "7.20.5", | ||
"description": "Compile ES2015 block scoping (const and let) to ES5", | ||
@@ -26,5 +26,5 @@ "repository": { | ||
"devDependencies": { | ||
"@babel/core": "^7.20.2", | ||
"@babel/core": "^7.20.5", | ||
"@babel/helper-plugin-test-runner": "^7.18.6", | ||
"@babel/traverse": "^7.20.1" | ||
"@babel/traverse": "^7.20.5" | ||
}, | ||
@@ -31,0 +31,0 @@ "engines": { |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
96636
730