This commit is contained in:
2022-07-18 02:50:52 +00:00
parent befd344ab0
commit 06181b34d6
8569 changed files with 818704 additions and 352705 deletions
+1 -1
View File
@@ -2,7 +2,7 @@
> Compile ES2015 block scoping (const and let) to ES5
See our website [@babel/plugin-transform-block-scoping](https://babeljs.io/docs/en/next/babel-plugin-transform-block-scoping.html) for more information.
See our website [@babel/plugin-transform-block-scoping](https://babeljs.io/docs/en/babel-plugin-transform-block-scoping) for more information.
## Install
+131 -100
View File
@@ -9,14 +9,8 @@ var _helperPluginUtils = require("@babel/helper-plugin-utils");
var _tdz = require("./tdz");
var _values = _interopRequireDefault(require("lodash/values"));
var _extend = _interopRequireDefault(require("lodash/extend"));
var _core = require("@babel/core");
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
const DONE = new WeakSet();
var _default = (0, _helperPluginUtils.declare)((api, opts) => {
@@ -52,7 +46,7 @@ var _default = (0, _helperPluginUtils.declare)((api, opts) => {
for (let i = 0; i < node.declarations.length; i++) {
const decl = node.declarations[i];
const assign = _core.types.assignmentExpression("=", decl.id, decl.init || scope.buildUndefinedNode());
const assign = _core.types.assignmentExpression("=", _core.types.cloneNode(decl.id), decl.init || scope.buildUndefinedNode());
assign._ignoreBlockScopingTDZ = true;
nodes.push(_core.types.expressionStatement(assign));
@@ -106,13 +100,17 @@ function ignoreBlock(path) {
return _core.types.isLoop(path.parent) || _core.types.isCatchClause(path.parent);
}
const buildRetCheck = (0, _core.template)(`
const buildRetCheck = _core.template.statement(`
if (typeof RETURN === "object") return RETURN.v;
`);
function isBlockScoped(node) {
if (!_core.types.isVariableDeclaration(node)) return false;
if (node[_core.types.BLOCK_SCOPED_SYMBOL]) return true;
if (node[_core.types.BLOCK_SCOPED_SYMBOL]) {
return true;
}
if (node.kind !== "let" && node.kind !== "const") return false;
return true;
}
@@ -166,7 +164,7 @@ const letReferenceBlockVisitor = _core.traverse.visitors.merge([{
},
Function(path, state) {
FunctionParent(path, state) {
if (state.loopDepth > 0) {
path.traverse(letReferenceFunctionVisitor, state);
} else {
@@ -180,7 +178,7 @@ const letReferenceBlockVisitor = _core.traverse.visitors.merge([{
const letReferenceFunctionVisitor = _core.traverse.visitors.merge([{
ReferencedIdentifier(path, state) {
const ref = state.letReferences[path.node.name];
const ref = state.letReferences.get(path.node.name);
if (!ref) return;
const localBinding = path.scope.getBindingIdentifier(path.node.name);
if (localBinding && localBinding !== ref) return;
@@ -191,13 +189,12 @@ const letReferenceFunctionVisitor = _core.traverse.visitors.merge([{
const hoistVarDeclarationsVisitor = {
enter(path, self) {
const {
node,
parent
} = path;
if (path.isForStatement()) {
if (isVar(node.init, node)) {
const {
node
} = path;
if (isVar(node.init)) {
const nodes = self.pushDeclar(node.init);
if (nodes.length === 1) {
@@ -206,13 +203,17 @@ const hoistVarDeclarationsVisitor = {
node.init = _core.types.sequenceExpression(nodes);
}
}
} else if (path.isFor()) {
if (isVar(node.left, node)) {
} else if (path.isForInStatement() || path.isForOfStatement()) {
const {
node
} = path;
if (isVar(node.left)) {
self.pushDeclar(node.left);
node.left = node.left.declarations[0].id;
}
} else if (isVar(node, parent)) {
path.replaceWithMultiple(self.pushDeclar(node).map(expr => _core.types.expressionStatement(expr)));
} else if (isVar(path.node)) {
path.replaceWithMultiple(self.pushDeclar(path.node).map(expr => _core.types.expressionStatement(expr)));
} else if (path.isFunction()) {
return path.skip();
}
@@ -231,7 +232,7 @@ const continuationVisitor = {
enter(path, state) {
if (path.isAssignmentExpression() || path.isUpdateExpression()) {
for (const name of Object.keys(path.getBindingIdentifiers())) {
if (state.outsideReferences[name] !== path.scope.getBindingIdentifier(name)) {
if (state.outsideReferences.get(name) !== path.scope.getBindingIdentifier(name)) {
continue;
}
@@ -278,11 +279,15 @@ const loopVisitor = {
node,
scope
} = path;
if (node[this.LOOP_IGNORE]) return;
if (state.loopIgnored.has(node)) return;
let replace;
let loopText = loopNodeTo(node);
if (loopText) {
if (_core.types.isReturnStatement(node)) {
throw new Error("Internal error: unexpected return statement with `loopText`");
}
if (node.label) {
if (state.innerLabels.indexOf(node.label.name) >= 0) {
return;
@@ -295,18 +300,18 @@ const loopVisitor = {
}
state.hasBreakContinue = true;
state.map[loopText] = node;
state.map.set(loopText, node);
replace = _core.types.stringLiteral(loopText);
}
if (path.isReturnStatement()) {
if (_core.types.isReturnStatement(node)) {
state.hasReturn = true;
replace = _core.types.objectExpression([_core.types.objectProperty(_core.types.identifier("v"), node.argument || scope.buildUndefinedNode())]);
}
if (replace) {
replace = _core.types.returnStatement(replace);
replace[this.LOOP_IGNORE] = true;
state.loopIgnored.add(replace);
path.skip();
path.replaceWith(_core.types.inherits(replace, node));
}
@@ -314,8 +319,36 @@ const loopVisitor = {
};
function isStrict(path) {
return !!path.find(({
node
}) => {
if (_core.types.isProgram(node)) {
if (node.sourceType === "module") return true;
} else if (!_core.types.isBlockStatement(node)) return false;
return node.directives.some(directive => directive.value.value === "use strict");
});
}
class BlockScoping {
constructor(loopPath, blockPath, parent, scope, throwIfClosureRequired, tdzEnabled, state) {
this.parent = void 0;
this.state = void 0;
this.scope = void 0;
this.throwIfClosureRequired = void 0;
this.tdzEnabled = void 0;
this.blockPath = void 0;
this.block = void 0;
this.outsideLetReferences = void 0;
this.hasLetReferences = void 0;
this.letReferences = void 0;
this.body = void 0;
this.loopParent = void 0;
this.loopLabel = void 0;
this.loopPath = void 0;
this.loop = void 0;
this.has = void 0;
this.parent = parent;
this.scope = scope;
this.state = state;
@@ -323,9 +356,9 @@ class BlockScoping {
this.tdzEnabled = tdzEnabled;
this.blockPath = blockPath;
this.block = blockPath.node;
this.outsideLetReferences = Object.create(null);
this.outsideLetReferences = new Map();
this.hasLetReferences = false;
this.letReferences = Object.create(null);
this.letReferences = new Map();
this.body = [];
if (loopPath) {
@@ -377,11 +410,22 @@ class BlockScoping {
const throwNode = _core.types.callExpression(readOnlyError, [_core.types.stringLiteral(name)]);
if (violation.isAssignmentExpression()) {
violation.get("right").replaceWith(_core.types.sequenceExpression([throwNode, violation.get("right").node]));
const {
operator
} = violation.node;
if (operator === "=") {
violation.replaceWith(_core.types.sequenceExpression([violation.get("right").node, throwNode]));
} else if (["&&=", "||=", "??="].includes(operator)) {
violation.replaceWith(_core.types.logicalExpression(operator.slice(0, -1), violation.get("left").node, _core.types.sequenceExpression([violation.get("right").node, throwNode])));
} else {
violation.replaceWith(_core.types.sequenceExpression([_core.types.binaryExpression(operator.slice(0, -1), violation.get("left").node, violation.get("right").node), throwNode]));
}
} else if (violation.isUpdateExpression()) {
violation.replaceWith(_core.types.sequenceExpression([throwNode, violation.node]));
violation.replaceWith(_core.types.sequenceExpression([_core.types.unaryExpression("+", violation.get("argument").node), throwNode]));
} else if (violation.isForXStatement()) {
violation.ensureBlock();
violation.get("left").replaceWith(_core.types.variableDeclaration("var", [_core.types.variableDeclarator(violation.scope.generateUidIdentifier(name))]));
violation.node.body.body.unshift(_core.types.expressionStatement(throwNode));
}
}
@@ -393,8 +437,8 @@ class BlockScoping {
const parentScope = blockScope.getFunctionParent() || blockScope.getProgramParent();
const letRefs = this.letReferences;
for (const key of Object.keys(letRefs)) {
const ref = letRefs[key];
for (const key of letRefs.keys()) {
const ref = letRefs.get(key);
const binding = blockScope.getBinding(ref.name);
if (!binding) continue;
@@ -418,11 +462,19 @@ class BlockScoping {
const scope = this.scope;
const blockPathScope = this.blockPath.scope;
for (const key of Object.keys(letRefs)) {
const ref = letRefs[key];
for (const key of letRefs.keys()) {
const ref = letRefs.get(key);
if (scope.parentHasBinding(key) || scope.hasGlobal(key)) {
if (scope.hasOwnBinding(key)) {
const binding = scope.getOwnBinding(key);
if (binding) {
const parentBinding = scope.parent.getOwnBinding(key);
if (binding.kind === "hoisted" && !binding.path.node.async && !binding.path.node.generator && (!parentBinding || isVar(parentBinding.path.parent)) && !isStrict(binding.path.parentPath)) {
continue;
}
scope.rename(ref.name);
}
@@ -432,8 +484,8 @@ class BlockScoping {
}
}
for (const key of Object.keys(outsideLetRefs)) {
const ref = letRefs[key];
for (const key of outsideLetRefs.keys()) {
const ref = letRefs.get(key);
if (isInLoop(this.blockPath) && blockPathScope.hasOwnBinding(key)) {
blockPathScope.rename(ref.name);
@@ -450,24 +502,24 @@ class BlockScoping {
const outsideRefs = this.outsideLetReferences;
if (this.loop) {
for (const name of Object.keys(outsideRefs)) {
const id = outsideRefs[name];
for (const name of Array.from(outsideRefs.keys())) {
const id = outsideRefs.get(name);
if (this.scope.hasGlobal(id.name) || this.scope.parentHasBinding(id.name)) {
delete outsideRefs[id.name];
delete this.letReferences[id.name];
outsideRefs.delete(id.name);
this.letReferences.delete(id.name);
this.scope.rename(id.name);
this.letReferences[id.name] = id;
outsideRefs[id.name] = id;
this.letReferences.set(id.name, id);
outsideRefs.set(id.name, id);
}
}
}
this.has = this.checkLoop();
this.hoistVarDeclarations();
const args = (0, _values.default)(outsideRefs).map(id => _core.types.cloneNode(id));
const args = Array.from(outsideRefs.values(), node => _core.types.cloneNode(node));
const params = args.map(id => _core.types.cloneNode(id));
const isSwitch = this.blockPath.isSwitchStatement();
const isSwitch = block.type === "SwitchStatement";
const fn = _core.types.functionExpression(null, params, _core.types.blockStatement(isSwitch ? [block] : block.body));
@@ -563,26 +615,35 @@ class BlockScoping {
getLetReferences() {
const block = this.block;
let declarators = [];
const declarators = [];
if (this.loop) {
const init = this.loop.left || this.loop.init;
if (isBlockScoped(init)) {
declarators.push(init);
(0, _extend.default)(this.outsideLetReferences, _core.types.getBindingIdentifiers(init));
const names = _core.types.getBindingIdentifiers(init);
for (const name of Object.keys(names)) {
this.outsideLetReferences.set(name, names[name]);
}
}
}
const addDeclarationsFromChild = (path, node) => {
node = node || path.node;
if (_core.types.isClassDeclaration(node) || _core.types.isFunctionDeclaration(node) || isBlockScoped(node)) {
if (isBlockScoped(node)) {
convertBlockScopedToVar(path, node, block, this.scope);
}
declarators = declarators.concat(node.declarations || node);
if (node.type === "VariableDeclaration") {
for (let i = 0; i < node.declarations.length; i++) {
declarators.push(node.declarations[i]);
}
} else {
declarators.push(node);
}
}
if (_core.types.isLabeledStatement(node)) {
@@ -590,15 +651,7 @@ class BlockScoping {
}
};
if (block.body) {
const declarPaths = this.blockPath.get("body");
for (let i = 0; i < block.body.length; i++) {
addDeclarationsFromChild(declarPaths[i]);
}
}
if (block.cases) {
if (block.type === "SwitchStatement") {
const declarPaths = this.blockPath.get("cases");
for (let i = 0; i < block.cases.length; i++) {
@@ -609,6 +662,12 @@ class BlockScoping {
addDeclarationsFromChild(declarPaths[i], declar);
}
}
} else {
const declarPaths = this.blockPath.get("body");
for (let i = 0; i < block.body.length; i++) {
addDeclarationsFromChild(declarPaths[i], declarPaths[i].node);
}
}
for (let i = 0; i < declarators.length; i++) {
@@ -616,7 +675,10 @@ class BlockScoping {
const keys = _core.types.getBindingIdentifiers(declar, false, true);
(0, _extend.default)(this.letReferences, keys);
for (const key of Object.keys(keys)) {
this.letReferences.set(key, keys[key]);
}
this.hasLetReferences = true;
}
@@ -645,8 +707,8 @@ class BlockScoping {
innerLabels: [],
hasReturn: false,
isLoop: !!this.loop,
map: {},
LOOP_IGNORE: Symbol()
map: new Map(),
loopIgnored: new WeakSet()
};
this.blockPath.traverse(loopLabelVisitor, state);
this.blockPath.traverse(loopVisitor, state);
@@ -683,49 +745,18 @@ class BlockScoping {
buildHas(ret) {
const body = this.body;
let retCheck;
const has = this.has;
const cases = [];
if (has.hasReturn) {
retCheck = buildRetCheck({
RETURN: _core.types.identifier(ret)
});
}
if (has.hasBreakContinue) {
for (const key of Object.keys(has.map)) {
cases.push(_core.types.switchCase(_core.types.stringLiteral(key), [has.map[key]]));
for (const key of has.map.keys()) {
body.push(_core.types.ifStatement(_core.types.binaryExpression("===", _core.types.identifier(ret), _core.types.stringLiteral(key)), has.map.get(key)));
}
}
if (has.hasReturn) {
cases.push(_core.types.switchCase(null, [retCheck]));
}
if (cases.length === 1) {
const single = cases[0];
body.push(_core.types.ifStatement(_core.types.binaryExpression("===", _core.types.identifier(ret), single.test), single.consequent[0]));
} else {
if (this.loop) {
for (let i = 0; i < cases.length; i++) {
const caseConsequent = cases[i].consequent[0];
if (_core.types.isBreakStatement(caseConsequent) && !caseConsequent.label) {
if (!this.loopLabel) {
this.loopLabel = this.scope.generateUidIdentifier("loop");
}
caseConsequent.label = _core.types.cloneNode(this.loopLabel);
}
}
}
body.push(_core.types.switchStatement(_core.types.identifier(ret), cases));
}
} else {
if (has.hasReturn) {
body.push(retCheck);
}
if (has.hasReturn) {
body.push(buildRetCheck({
RETURN: _core.types.identifier(ret)
}));
}
}
+7 -2
View File
@@ -24,11 +24,12 @@ function buildTDZAssert(node, state) {
}
function isReference(node, scope, state) {
const declared = state.letReferences[node.name];
const declared = state.letReferences.get(node.name);
if (!declared) return false;
return scope.getBindingIdentifier(node.name) === declared;
}
const visitedMaybeTDZNodes = new WeakSet();
const visitor = {
ReferencedIdentifier(path, state) {
if (!state.tdzEnabled) return;
@@ -47,9 +48,13 @@ const visitor = {
if (status === "outside") return;
if (status === "maybe") {
if (visitedMaybeTDZNodes.has(node)) {
return;
}
visitedMaybeTDZNodes.add(node);
const assert = buildTDZAssert(node, state);
bindingPath.parent._tdzThis = true;
path.skip();
if (path.parentPath.isUpdateExpression()) {
if (parent._ignoreBlockScopingTDZ) return;
+26 -19
View File
@@ -1,52 +1,58 @@
{
"_args": [
[
"@babel/plugin-transform-block-scoping@7.10.4",
"/mnt/Foxconn/Digitalent/Deverloper/liff-push_2series"
"@babel/plugin-transform-block-scoping@7.18.6",
"/home/node/nuxt"
]
],
"_from": "@babel/plugin-transform-block-scoping@7.10.4",
"_id": "@babel/plugin-transform-block-scoping@7.10.4",
"_from": "@babel/plugin-transform-block-scoping@7.18.6",
"_id": "@babel/plugin-transform-block-scoping@7.18.6",
"_inBundle": false,
"_integrity": "sha512-J3b5CluMg3hPUii2onJDRiaVbPtKFPLEaV5dOPY5OeAbDi1iU/UbbFFTgwb7WnanaDy7bjU35kc26W3eM5Qa0A==",
"_integrity": "sha512-pRqwb91C42vs1ahSAWJkxOxU1RHWDn16XAa6ggQ72wjLlWyYeAcLvTtE0aM8ph3KNydy9CQF2nLYcjq1WysgxQ==",
"_location": "/@babel/plugin-transform-block-scoping",
"_phantomChildren": {},
"_requested": {
"type": "version",
"registry": true,
"raw": "@babel/plugin-transform-block-scoping@7.10.4",
"raw": "@babel/plugin-transform-block-scoping@7.18.6",
"name": "@babel/plugin-transform-block-scoping",
"escapedName": "@babel%2fplugin-transform-block-scoping",
"scope": "@babel",
"rawSpec": "7.10.4",
"rawSpec": "7.18.6",
"saveSpec": null,
"fetchSpec": "7.10.4"
"fetchSpec": "7.18.6"
},
"_requiredBy": [
"/@babel/preset-env"
],
"_resolved": "https://registry.npmjs.org/@babel/plugin-transform-block-scoping/-/plugin-transform-block-scoping-7.10.4.tgz",
"_spec": "7.10.4",
"_where": "/mnt/Foxconn/Digitalent/Deverloper/liff-push_2series",
"_resolved": "https://registry.npmjs.org/@babel/plugin-transform-block-scoping/-/plugin-transform-block-scoping-7.18.6.tgz",
"_spec": "7.18.6",
"_where": "/home/node/nuxt",
"author": {
"name": "The Babel Team",
"url": "https://babel.dev/team"
},
"bugs": {
"url": "https://github.com/babel/babel/issues"
},
"dependencies": {
"@babel/helper-plugin-utils": "^7.10.4",
"lodash": "^4.17.13"
"@babel/helper-plugin-utils": "^7.18.6"
},
"description": "Compile ES2015 block scoping (const and let) to ES5",
"devDependencies": {
"@babel/core": "^7.10.4",
"@babel/helper-plugin-test-runner": "^7.10.4"
"@babel/core": "^7.18.6",
"@babel/helper-plugin-test-runner": "^7.18.6",
"@babel/traverse": "^7.18.6"
},
"gitHead": "7fd40d86a0d03ff0e9c3ea16b29689945433d4df",
"homepage": "https://github.com/babel/babel#readme",
"engines": {
"node": ">=6.9.0"
},
"homepage": "https://babel.dev/docs/en/next/babel-plugin-transform-block-scoping",
"keywords": [
"babel-plugin"
],
"license": "MIT",
"main": "lib/index.js",
"main": "./lib/index.js",
"name": "@babel/plugin-transform-block-scoping",
"peerDependencies": {
"@babel/core": "^7.0.0-0"
@@ -59,5 +65,6 @@
"url": "git+https://github.com/babel/babel.git",
"directory": "packages/babel-plugin-transform-block-scoping"
},
"version": "7.10.4"
"type": "commonjs",
"version": "7.18.6"
}