Changing front

This commit is contained in:
ewandor committed 2023-01-16 17:49:38 +01:00
1 parent 0b8a93b256
commit 4fe4be7730
48586 files changed
+4725790 -17464

No files matched your search

@@ -0,0 +1,668 @@
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = void 0;
var _helperPluginUtils = require("@babel/helper-plugin-utils");
var _tdz = require("./tdz");
var _core = require("@babel/core");
const DONE = new WeakSet();
var _default = (0, _helperPluginUtils.declare)((api, opts) => {
api.assertVersion(7);
const {
throwIfClosureRequired = false,
tdz: tdzEnabled = false
} = opts;
if (typeof throwIfClosureRequired !== "boolean") {
throw new Error(`.throwIfClosureRequired must be a boolean, or undefined`);
}
if (typeof tdzEnabled !== "boolean") {
throw new Error(`.tdz must be a boolean, or undefined`);
}
return {
name: "transform-block-scoping",
visitor: {
VariableDeclaration(path) {
const {
node,
parent,
scope
} = path;
if (!isBlockScoped(node)) return;
convertBlockScopedToVar(path, null, parent, scope, true);
if (node._tdzThis) {
const nodes = [node];
for (let i = 0; i < node.declarations.length; i++) {
const decl = node.declarations[i];
const assign = _core.types.assignmentExpression("=", _core.types.cloneNode(decl.id), decl.init || scope.buildUndefinedNode());
_tdz.skipTDZChecks.add(assign);
nodes.push(_core.types.expressionStatement(assign));
decl.init = this.addHelper("temporalUndefined");
}
node._blockHoist = 2;
if (path.isCompletionRecord()) {
nodes.push(_core.types.expressionStatement(scope.buildUndefinedNode()));
}
path.replaceWithMultiple(nodes);
}
},
Loop(path, state) {
const {
parent,
scope
} = path;
path.ensureBlock();
const blockScoping = new BlockScoping(path, path.get("body"), parent, scope, throwIfClosureRequired, tdzEnabled, state);
const replace = blockScoping.run();
if (replace) path.replaceWith(replace);
},
CatchClause(path, state) {
const {
parent,
scope
} = path;
const blockScoping = new BlockScoping(null, path.get("body"), parent, scope, throwIfClosureRequired, tdzEnabled, state);
blockScoping.run();
},
"BlockStatement|SwitchStatement|Program"(path, state) {
if (!ignoreBlock(path)) {
const blockScoping = new BlockScoping(null, path, path.parent, path.scope, throwIfClosureRequired, tdzEnabled, state);
blockScoping.run();
}
}
}
};
});
exports.default = _default;
function ignoreBlock(path) {
return _core.types.isLoop(path.parent) || _core.types.isCatchClause(path.parent);
}
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.kind !== "let" && node.kind !== "const" && node.kind !== "using") {
return false;
}
return true;
}
function isInLoop(path) {
const loopOrFunctionParent = path.find(path => path.isLoop() || path.isFunction());
return loopOrFunctionParent == null ? void 0 : loopOrFunctionParent.isLoop();
}
function convertBlockScopedToVar(path, node, parent, scope, moveBindingsToParent = false) {
if (!node) {
node = path.node;
}
if (isInLoop(path) && !_core.types.isFor(parent)) {
for (let i = 0; i < node.declarations.length; i++) {
const declar = node.declarations[i];
declar.init = declar.init || scope.buildUndefinedNode();
}
}
node[_core.types.BLOCK_SCOPED_SYMBOL] = true;
node.kind = "var";
if (moveBindingsToParent) {
const parentScope = scope.getFunctionParent() || scope.getProgramParent();
for (const name of Object.keys(path.getBindingIdentifiers())) {
const binding = scope.getOwnBinding(name);
if (binding) binding.kind = "var";
scope.moveBindingTo(name, parentScope);
}
}
}
function isVar(node) {
return _core.types.isVariableDeclaration(node, {
kind: "var"
}) && !isBlockScoped(node);
}
const letReferenceFunctionVisitor = _core.traverse.visitors.merge([{
ReferencedIdentifier(path, state) {
const ref = state.letReferences.get(path.node.name);
if (!ref) return;
const localBinding = path.scope.getBindingIdentifier(path.node.name);
if (localBinding && localBinding !== ref) return;
state.closurify = true;
}
}, _tdz.visitor]);
const letReferenceBlockVisitor = _core.traverse.visitors.merge([{
Loop: {
enter(path, state) {
state.loopDepth++;
},
exit(path, state) {
state.loopDepth--;
}
},
FunctionParent(path, state) {
if (state.loopDepth > 0) {
path.traverse(letReferenceFunctionVisitor, state);
} else {
path.traverse(_tdz.visitor, state);
}
return path.skip();
}
}, _tdz.visitor]);
const hoistVarDeclarationsVisitor = {
enter(path, self) {
if (path.isForStatement()) {
const {
node
} = path;
if (isVar(node.init)) {
const nodes = self.pushDeclar(node.init);
if (nodes.length === 1) {
node.init = nodes[0];
} else {
node.init = _core.types.sequenceExpression(nodes);
}
}
} 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(path.node)) {
path.replaceWithMultiple(self.pushDeclar(path.node).map(expr => _core.types.expressionStatement(expr)));
} else if (path.isFunction()) {
return path.skip();
}
}
};
const loopLabelVisitor = {
LabeledStatement({
node
}, state) {
state.innerLabels.push(node.label.name);
}
};
const continuationVisitor = {
enter(path, state) {
if (path.isAssignmentExpression() || path.isUpdateExpression()) {
for (const name of Object.keys(path.getBindingIdentifiers())) {
if (state.outsideReferences.get(name) !== path.scope.getBindingIdentifier(name)) {
continue;
}
state.reassignments[name] = true;
}
} else if (path.isReturnStatement()) {
state.returnStatements.push(path);
}
}
};
function loopNodeTo(node) {
if (_core.types.isBreakStatement(node)) {
return "break";
} else if (_core.types.isContinueStatement(node)) {
return "continue";
}
}
const loopVisitor = {
Loop(path, state) {
const oldIgnoreLabeless = state.ignoreLabeless;
state.ignoreLabeless = true;
path.traverse(loopVisitor, state);
state.ignoreLabeless = oldIgnoreLabeless;
path.skip();
},
Function(path) {
path.skip();
},
SwitchCase(path, state) {
const oldInSwitchCase = state.inSwitchCase;
state.inSwitchCase = true;
path.traverse(loopVisitor, state);
state.inSwitchCase = oldInSwitchCase;
path.skip();
},
"BreakStatement|ContinueStatement|ReturnStatement"(path, state) {
const {
node,
scope
} = path;
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;
}
loopText = `${loopText}|${node.label.name}`;
} else {
if (state.ignoreLabeless) return;
if (_core.types.isBreakStatement(node) && state.inSwitchCase) return;
}
state.hasBreakContinue = true;
state.map.set(loopText, node);
replace = _core.types.stringLiteral(loopText);
}
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);
state.loopIgnored.add(replace);
path.skip();
path.replaceWith(_core.types.inherits(replace, node));
}
}
};
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;
this.throwIfClosureRequired = throwIfClosureRequired;
this.tdzEnabled = tdzEnabled;
this.blockPath = blockPath;
this.block = blockPath.node;
this.outsideLetReferences = new Map();
this.hasLetReferences = false;
this.letReferences = new Map();
this.body = [];
if (loopPath) {
this.loopParent = loopPath.parent;
this.loopLabel = _core.types.isLabeledStatement(this.loopParent) && this.loopParent.label;
this.loopPath = loopPath;
this.loop = loopPath.node;
}
}
run() {
const block = this.block;
if (DONE.has(block)) return;
DONE.add(block);
const needsClosure = this.getLetReferences();
this.checkConstants();
if (_core.types.isFunction(this.parent) || _core.types.isProgram(this.block)) {
this.updateScopeInfo();
return;
}
if (!this.hasLetReferences) return;
if (needsClosure) {
this.wrapClosure();
} else {
this.remap();
}
this.updateScopeInfo(needsClosure);
if (this.loopLabel && !_core.types.isLabeledStatement(this.loopParent)) {
return _core.types.labeledStatement(this.loopLabel, this.loop);
}
}
checkConstants() {
const constBindings = new Map();
for (const scope of new Set([this.scope, this.blockPath.scope])) {
for (const name of Object.keys(scope.bindings)) {
const binding = scope.bindings[name];
if (binding.kind === "const") constBindings.set(name, binding);
}
}
const {
state
} = this;
for (const [name, binding] of constBindings) {
for (const violation of binding.constantViolations) {
const readOnlyError = state.addHelper("readOnlyError");
const throwNode = _core.types.callExpression(readOnlyError, [_core.types.stringLiteral(name)]);
if (violation.isAssignmentExpression()) {
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([_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));
}
}
}
}
updateScopeInfo(wrappedInClosure) {
const blockScope = this.blockPath.scope;
const parentScope = blockScope.getFunctionParent() || blockScope.getProgramParent();
const letRefs = this.letReferences;
for (const key of letRefs.keys()) {
const ref = letRefs.get(key);
const binding = blockScope.getBinding(ref.name);
if (!binding) continue;
if (binding.kind === "let" || binding.kind === "const") {
binding.kind = "var";
if (wrappedInClosure) {
if (blockScope.hasOwnBinding(ref.name)) {
blockScope.removeBinding(ref.name);
}
} else {
blockScope.moveBindingTo(ref.name, parentScope);
}
}
}
}
remap() {
const letRefs = this.letReferences;
const outsideLetRefs = this.outsideLetReferences;
const scope = this.scope;
const blockPathScope = this.blockPath.scope;
for (const key of letRefs.keys()) {
const ref = letRefs.get(key);
if (scope.parentHasBinding(key) || scope.hasGlobal(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);
}
if (blockPathScope.hasOwnBinding(key)) {
blockPathScope.rename(ref.name);
}
}
}
for (const key of outsideLetRefs.keys()) {
const ref = letRefs.get(key);
if (isInLoop(this.blockPath) && blockPathScope.hasOwnBinding(key)) {
blockPathScope.rename(ref.name);
}
}
}
wrapClosure() {
if (this.throwIfClosureRequired) {
throw this.blockPath.buildCodeFrameError("Compiling let/const in this block would add a closure " + "(throwIfClosureRequired).");
}
const block = this.block;
const outsideRefs = this.outsideLetReferences;
if (this.loop) {
for (const name of Array.from(outsideRefs.keys())) {
const id = outsideRefs.get(name);
if (this.scope.hasGlobal(id.name) || this.scope.parentHasBinding(id.name)) {
outsideRefs.delete(id.name);
this.letReferences.delete(id.name);
this.scope.rename(id.name);
this.letReferences.set(id.name, id);
outsideRefs.set(id.name, id);
}
}
}
this.has = this.checkLoop();
this.hoistVarDeclarations();
const args = Array.from(outsideRefs.values(), node => _core.types.cloneNode(node));
const params = args.map(id => _core.types.cloneNode(id));
const isSwitch = block.type === "SwitchStatement";
const fn = _core.types.functionExpression(null, params, _core.types.blockStatement(isSwitch ? [block] : block.body));
this.addContinuations(fn);
let call = _core.types.callExpression(_core.types.nullLiteral(), args);
let basePath = ".callee";
const hasYield = _core.traverse.hasType(fn.body, "YieldExpression", _core.types.FUNCTION_TYPES);
if (hasYield) {
fn.generator = true;
call = _core.types.yieldExpression(call, true);
basePath = ".argument" + basePath;
}
const hasAsync = _core.traverse.hasType(fn.body, "AwaitExpression", _core.types.FUNCTION_TYPES);
if (hasAsync) {
fn.async = true;
call = _core.types.awaitExpression(call);
basePath = ".argument" + basePath;
}
let placeholderPath;
let index;
if (this.has.hasReturn || this.has.hasBreakContinue) {
const ret = this.scope.generateUid("ret");
this.body.push(_core.types.variableDeclaration("var", [_core.types.variableDeclarator(_core.types.identifier(ret), call)]));
placeholderPath = "declarations.0.init" + basePath;
index = this.body.length - 1;
this.buildHas(ret);
} else {
this.body.push(_core.types.expressionStatement(call));
placeholderPath = "expression" + basePath;
index = this.body.length - 1;
}
let callPath;
if (isSwitch) {
const {
parentPath,
listKey,
key
} = this.blockPath;
this.blockPath.replaceWithMultiple(this.body);
callPath = parentPath.get(listKey)[key + index];
} else {
block.body = this.body;
callPath = this.blockPath.get("body")[index];
}
const placeholder = callPath.get(placeholderPath);
let fnPath;
if (this.loop) {
const loopId = this.scope.generateUid("loop");
const p = this.loopPath.insertBefore(_core.types.variableDeclaration("var", [_core.types.variableDeclarator(_core.types.identifier(loopId), fn)]));
placeholder.replaceWith(_core.types.identifier(loopId));
fnPath = p[0].get("declarations.0.init");
} else {
placeholder.replaceWith(fn);
fnPath = placeholder;
}
fnPath.unwrapFunctionEnvironment();
}
addContinuations(fn) {
const state = {
reassignments: {},
returnStatements: [],
outsideReferences: this.outsideLetReferences
};
this.scope.traverse(fn, continuationVisitor, state);
for (let i = 0; i < fn.params.length; i++) {
const param = fn.params[i];
if (!state.reassignments[param.name]) continue;
const paramName = param.name;
const newParamName = this.scope.generateUid(param.name);
fn.params[i] = _core.types.identifier(newParamName);
this.scope.rename(paramName, newParamName, fn);
state.returnStatements.forEach(returnStatement => {
returnStatement.insertBefore(_core.types.expressionStatement(_core.types.assignmentExpression("=", _core.types.identifier(paramName), _core.types.identifier(newParamName))));
});
fn.body.body.push(_core.types.expressionStatement(_core.types.assignmentExpression("=", _core.types.identifier(paramName), _core.types.identifier(newParamName))));
}
}
getLetReferences() {
const block = this.block;
const declarators = [];
if (this.loop) {
const init = this.loop.left || this.loop.init;
if (isBlockScoped(init)) {
declarators.push(init);
const names = _core.types.getBindingIdentifiers(init);
for (const name of Object.keys(names)) {
this.outsideLetReferences.set(name, names[name]);
}
}
}
const addDeclarationsFromChild = (path, node) => {
if (_core.types.isClassDeclaration(node) || _core.types.isFunctionDeclaration(node) || isBlockScoped(node)) {
if (isBlockScoped(node)) {
convertBlockScopedToVar(path, node, block, this.scope);
}
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)) {
addDeclarationsFromChild(path.get("body"), node.body);
}
};
if (block.type === "SwitchStatement") {
const declarPaths = this.blockPath.get("cases");
for (let i = 0; i < block.cases.length; i++) {
const consequents = block.cases[i].consequent;
for (let j = 0; j < consequents.length; j++) {
const declar = consequents[j];
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++) {
const declar = declarators[i];
const keys = _core.types.getBindingIdentifiers(declar, false, true);
for (const key of Object.keys(keys)) {
this.letReferences.set(key, keys[key]);
}
this.hasLetReferences = true;
}
if (!this.hasLetReferences) return;
const state = {
letReferences: this.letReferences,
closurify: false,
loopDepth: 0,
tdzEnabled: this.tdzEnabled,
addHelper: name => this.state.addHelper(name)
};
if (isInLoop(this.blockPath)) {
state.loopDepth++;
}
this.blockPath.traverse(letReferenceBlockVisitor, state);
return state.closurify;
}
checkLoop() {
const state = {
hasBreakContinue: false,
ignoreLabeless: false,
inSwitchCase: false,
innerLabels: [],
hasReturn: false,
isLoop: !!this.loop,
map: new Map(),
loopIgnored: new WeakSet()
};
this.blockPath.traverse(loopLabelVisitor, state);
this.blockPath.traverse(loopVisitor, state);
return state;
}
hoistVarDeclarations() {
this.blockPath.traverse(hoistVarDeclarationsVisitor, this);
}
pushDeclar(node) {
const declars = [];
const names = _core.types.getBindingIdentifiers(node);
for (const name of Object.keys(names)) {
declars.push(_core.types.variableDeclarator(names[name]));
}
this.body.push(_core.types.variableDeclaration(node.kind, declars));
const replace = [];
for (let i = 0; i < node.declarations.length; i++) {
const declar = node.declarations[i];
if (!declar.init) continue;
const expr = _core.types.assignmentExpression("=", _core.types.cloneNode(declar.id), _core.types.cloneNode(declar.init));
replace.push(_core.types.inherits(expr, declar));
}
return replace;
}
buildHas(ret) {
const body = this.body;
const has = this.has;
if (has.hasBreakContinue) {
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) {
body.push(buildRetCheck({
RETURN: _core.types.identifier(ret)
}));
}
}
}
//# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
{"version":3,"names":["DONE","WeakSet","declare","api","opts","assertVersion","throwIfClosureRequired","tdz","tdzEnabled","Error","name","visitor","VariableDeclaration","path","node","parent","scope","isBlockScoped","convertBlockScopedToVar","_tdzThis","nodes","i","declarations","length","decl","assign","t","assignmentExpression","cloneNode","id","init","buildUndefinedNode","skipTDZChecks","add","push","expressionStatement","addHelper","_blockHoist","isCompletionRecord","replaceWithMultiple","Loop","state","ensureBlock","blockScoping","BlockScoping","get","replace","run","replaceWith","CatchClause","ignoreBlock","isLoop","isCatchClause","buildRetCheck","template","statement","isVariableDeclaration","BLOCK_SCOPED_SYMBOL","kind","isInLoop","loopOrFunctionParent","find","isFunction","moveBindingsToParent","isFor","declar","parentScope","getFunctionParent","getProgramParent","Object","keys","getBindingIdentifiers","binding","getOwnBinding","moveBindingTo","isVar","letReferenceFunctionVisitor","traverse","visitors","merge","ReferencedIdentifier","ref","letReferences","localBinding","getBindingIdentifier","closurify","tdzVisitor","letReferenceBlockVisitor","enter","loopDepth","exit","FunctionParent","skip","hoistVarDeclarationsVisitor","self","isForStatement","pushDeclar","sequenceExpression","isForInStatement","isForOfStatement","left","map","expr","loopLabelVisitor","LabeledStatement","innerLabels","label","continuationVisitor","isAssignmentExpression","isUpdateExpression","outsideReferences","reassignments","isReturnStatement","returnStatements","loopNodeTo","isBreakStatement","isContinueStatement","loopVisitor","oldIgnoreLabeless","ignoreLabeless","Function","SwitchCase","oldInSwitchCase","inSwitchCase","loopIgnored","has","loopText","indexOf","hasBreakContinue","set","stringLiteral","hasReturn","objectExpression","objectProperty","identifier","argument","returnStatement","inherits","isStrict","isProgram","sourceType","isBlockStatement","directives","some","directive","value","constructor","loopPath","blockPath","block","outsideLetReferences","hasLetReferences","body","loopParent","loopLabel","loop","Map","isLabeledStatement","needsClosure","getLetReferences","checkConstants","updateScopeInfo","wrapClosure","remap","labeledStatement","constBindings","Set","bindings","violation","constantViolations","readOnlyError","throwNode","callExpression","operator","includes","logicalExpression","slice","binaryExpression","unaryExpression","isForXStatement","variableDeclaration","variableDeclarator","generateUidIdentifier","unshift","wrappedInClosure","blockScope","letRefs","key","getBinding","hasOwnBinding","removeBinding","outsideLetRefs","blockPathScope","parentHasBinding","hasGlobal","parentBinding","async","generator","parentPath","rename","buildCodeFrameError","outsideRefs","Array","from","delete","checkLoop","hoistVarDeclarations","args","values","params","isSwitch","type","fn","functionExpression","blockStatement","addContinuations","call","nullLiteral","basePath","hasYield","hasType","FUNCTION_TYPES","yieldExpression","hasAsync","awaitExpression","placeholderPath","index","ret","generateUid","buildHas","callPath","listKey","placeholder","fnPath","loopId","p","insertBefore","unwrapFunctionEnvironment","param","paramName","newParamName","forEach","declarators","names","addDeclarationsFromChild","isClassDeclaration","isFunctionDeclaration","declarPaths","cases","consequents","consequent","j","declars","ifStatement","RETURN"],"sources":["../src/index.ts"],"sourcesContent":["import { declare } from \"@babel/helper-plugin-utils\";\nimport type { NodePath, Visitor, Scope, Binding } from \"@babel/traverse\";\nimport { skipTDZChecks, visitor as tdzVisitor } from \"./tdz\";\nimport type { TDZVisitorState } from \"./tdz\";\nimport { traverse, template, types as t } from \"@babel/core\";\nimport type { PluginPass } from \"@babel/core\";\n\nconst DONE = new WeakSet();\n\nexport interface Options {\n tdz?: boolean;\n throwIfClosureRequired?: boolean;\n}\n\nexport default declare((api, opts: Options) => {\n api.assertVersion(7);\n\n const { throwIfClosureRequired = false, tdz: tdzEnabled = false } = opts;\n if (typeof throwIfClosureRequired !== \"boolean\") {\n throw new Error(`.throwIfClosureRequired must be a boolean, or undefined`);\n }\n if (typeof tdzEnabled !== \"boolean\") {\n throw new Error(`.tdz must be a boolean, or undefined`);\n }\n\n return {\n name: \"transform-block-scoping\",\n\n visitor: {\n VariableDeclaration(path) {\n const { node, parent, scope } = path;\n if (!isBlockScoped(node)) return;\n convertBlockScopedToVar(path, null, parent, scope, true);\n\n // @ts-expect-error todo(flow->ts): avoid mutations\n if (node._tdzThis) {\n const nodes: t.Node[] = [node];\n\n for (let i = 0; i < node.declarations.length; i++) {\n const decl = node.declarations[i];\n const assign = t.assignmentExpression(\n Line truncated
+101
View File
@@ -0,0 +1,101 @@
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.visitor = exports.skipTDZChecks = void 0;
var _core = require("@babel/core");
function getTDZStatus(refPath, bindingPath) {
const executionStatus = bindingPath._guessExecutionStatusRelativeTo(refPath);
if (executionStatus === "before") {
return "outside";
} else if (executionStatus === "after") {
return "inside";
} else {
return "maybe";
}
}
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)]);
}
}
function isReference(node, scope, state) {
const declared = state.letReferences.get(node.name);
if (!declared) return false;
return scope.getBindingIdentifier(node.name) === declared;
}
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
} = path;
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(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 > 0) path.insertBefore(nodes);
}
};
exports.visitor = visitor;
//# sourceMappingURL=tdz.js.map
@@ -0,0 +1 @@
{"version":3,"names":["getTDZStatus","refPath","bindingPath","executionStatus","_guessExecutionStatusRelativeTo","skipTDZChecks","WeakSet","buildTDZAssert","status","node","state","clone","t","cloneNode","add","callExpression","addHelper","stringLiteral","name","isReference","scope","declared","letReferences","get","getBindingIdentifier","getTDZReplacement","path","id","has","getBinding","isFunctionDeclaration","parent","_tdzThis","visitor","ReferencedIdentifier","tdzEnabled","parentPath","isUpdateExpression","isFor","left","replacement","replaceWith","UpdateExpression","arg","isIdentifier","insertBefore","AssignmentExpression","nodes","ids","getBindingIdentifiers","Object","keys","push","expressionStatement","length"],"sources":["../src/tdz.ts"],"sourcesContent":["import { types as t, type PluginPass } from \"@babel/core\";\nimport type { NodePath, Scope, Visitor } from \"@babel/traverse\";\n\nfunction getTDZStatus(refPath: NodePath, bindingPath: NodePath) {\n const executionStatus = bindingPath._guessExecutionStatusRelativeTo(refPath);\n\n if (executionStatus === \"before\") {\n return \"outside\";\n } else if (executionStatus === \"after\") {\n return \"inside\";\n } else {\n return \"maybe\";\n }\n}\n\nexport const skipTDZChecks = new WeakSet();\n\nfunction buildTDZAssert(\n status: \"maybe\" | \"inside\",\n node: t.Identifier | t.JSXIdentifier,\n state: TDZVisitorState,\n) {\n if (status === \"maybe\") {\n const clone = t.cloneNode(node);\n skipTDZChecks.add(clone);\n return t.callExpression(state.addHelper(\"temporalRef\"), [\n // @ts-expect-error Fixme: we may need to handle JSXIdentifier\n clone,\n t.stringLiteral(node.name),\n ]);\n } else {\n return t.callExpression(state.addHelper(\"tdz\"), [\n t.stringLiteral(node.name),\n ]);\n }\n}\n\nfunction isReference(\n node: t.Identifier | t.JSXIdentifier,\n scope: Scope,\n state: TDZVisitorState,\n) {\n const declared = state.letReferences.get(node.name);\n if (!declared) return false;\n\n // declared node is different in this scope\n return scope.getBindingIdentifier(node.name) === declared;\n}\n\ntype TDZReplacement = { status: \"maybe\" | \"inside\"; node: t.Expression };\nfunction getTDZReplacement(\n path: NodePath<t.Identifier | t.JSXIdentifier>,\n state: TDZVisitorState,\n): TDZReplacement | undefined;\nfunction getTDZReplacement(\n path: NodePath,\n state: TDZVisitorState,\n id: t.Identifier | t.JSXIdentifier,\n): TDZReplacement | undefined;\nfunction getTDZReplacement(\n path: NodePath,\n state: TDZVisitorState,\n id: t.Identifier | t.JSXIdentifier = path.node as any,\n): TDZReplacement | undefined {\n if (!isReference(id, path.scope, state)) return;\n\n if (skipTDZChecks.has(id)) return;\n skipTDZChecks.add(id);\n\n const bindingPath = path.scope.getBinding(id.name).path;\n\n if (bindingPath.isFunctionDeclaration()) return;\n\n const status = getTDZStatus(path, bindingPath);\n if (status === \"outside\") return;\n\n if (status === \"maybe\") {\n // add tdzThis to parent variable declarator so it's exploded\n // @ts-expect-error todo(flow->ts): avoid mutations\n bindingPath.parent._tdzThis = true;\n }\n\n return { status, node: buildTDZAssert(status, id, state) };\n}\n\nexport interface TDZVisitorState {\n tdzEnabled: boolean;\n addHelper: PluginPass[\"addHelper\"];\n letReferences: Map<string, t.Identifier>;\n}\n\nexport const visitor: Visitor<TDZVisitorState> = {\n ReferencedIdentifier(path, state) {\n if (!state.tdzEnabled) return;\n if (path.parentPath.isUpdateExpression()) return;\n // It will be handled after transforming the loop\n if (path.parentPath.isFor({ left: path.node })) return;\n\n const replacement = getTDZReplacement(path, state);\n if (!replacement) return;\n\n path.replaceWith(replacement.node);\n },\n\n UpdateExpression(path, state) {\n if (!state.tdzEnabled) return;\n\n const { node } = path;\n if (skipTDZChecks.has(node)) return;\n skipTDZChecks.add(node);\n\n const arg = path.get(\"argument\");\n if (!arg.isIdentifier()) return;\n\n const replacement = getTDZReplacement(path, state, arg.node);\n if (!replacement) return;\n\n if (replacement.status === \"maybe\") {\n path.insertBefore(replacement.node);\n } else {\n path.replaceWith(replacement.node);\n }\n },\n\n AssignmentExpression(path, state) {\n if (!state.tdzEnabled) return;\n\n const { node } = path;\n if (skipTDZChecks.has(node)) return;\n skipTDZChecks.add(node);\n\n const nodes = [];\n const ids = path.getBindingIdentifiers();\n\n for (const name of Object.keys(ids)) {\n const replacement = getTDZReplacement(path, state, ids[name]);\n if (replacement) {\n nodes.push(t.expressionStatement(replacement.node));\n if (replacement.status === \"inside\") break;\n }\n }\n\n if (nodes.length > 0) path.insertBefore(nodes);\n },\n};\n"],"mappings":";;;;;Line truncated