paradiego
This commit is contained in:
commit
3f0e204289
12510 files changed
+1486101
No files matched your search
+83
@@ -0,0 +1,83 @@
|
||||
"use strict";
|
||||
|
||||
Object.defineProperty(exports, "__esModule", {
|
||||
value: true
|
||||
});
|
||||
exports.default = void 0;
|
||||
class Binding {
|
||||
constructor({
|
||||
identifier,
|
||||
scope,
|
||||
path,
|
||||
kind
|
||||
}) {
|
||||
this.identifier = void 0;
|
||||
this.scope = void 0;
|
||||
this.path = void 0;
|
||||
this.kind = void 0;
|
||||
this.constantViolations = [];
|
||||
this.constant = true;
|
||||
this.referencePaths = [];
|
||||
this.referenced = false;
|
||||
this.references = 0;
|
||||
this.identifier = identifier;
|
||||
this.scope = scope;
|
||||
this.path = path;
|
||||
this.kind = kind;
|
||||
if ((kind === "var" || kind === "hoisted") && isDeclaredInLoop(path)) {
|
||||
this.reassign(path);
|
||||
}
|
||||
this.clearValue();
|
||||
}
|
||||
deoptValue() {
|
||||
this.clearValue();
|
||||
this.hasDeoptedValue = true;
|
||||
}
|
||||
setValue(value) {
|
||||
if (this.hasDeoptedValue) return;
|
||||
this.hasValue = true;
|
||||
this.value = value;
|
||||
}
|
||||
clearValue() {
|
||||
this.hasDeoptedValue = false;
|
||||
this.hasValue = false;
|
||||
this.value = null;
|
||||
}
|
||||
reassign(path) {
|
||||
this.constant = false;
|
||||
if (this.constantViolations.includes(path)) {
|
||||
return;
|
||||
}
|
||||
this.constantViolations.push(path);
|
||||
}
|
||||
reference(path) {
|
||||
if (this.referencePaths.includes(path)) {
|
||||
return;
|
||||
}
|
||||
this.referenced = true;
|
||||
this.references++;
|
||||
this.referencePaths.push(path);
|
||||
}
|
||||
dereference() {
|
||||
this.references--;
|
||||
this.referenced = !!this.references;
|
||||
}
|
||||
}
|
||||
exports.default = Binding;
|
||||
function isDeclaredInLoop(path) {
|
||||
for (let {
|
||||
parentPath,
|
||||
key
|
||||
} = path; parentPath; ({
|
||||
parentPath,
|
||||
key
|
||||
} = parentPath)) {
|
||||
if (parentPath.isFunctionParent()) return false;
|
||||
if (parentPath.isWhile() || parentPath.isForXStatement() || parentPath.isForStatement() && key === "body") {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
//# sourceMappingURL=binding.js.map
|
||||
+1
@@ -0,0 +1 @@
|
||||
{"version":3,"names":["Binding","constructor","identifier","scope","path","kind","constantViolations","constant","referencePaths","referenced","references","isDeclaredInLoop","reassign","clearValue","deoptValue","hasDeoptedValue","setValue","value","hasValue","includes","push","reference","dereference","exports","default","parentPath","key","isFunctionParent","isWhile","isForXStatement","isForStatement"],"sources":["../../src/scope/binding.ts"],"sourcesContent":["import type NodePath from \"../path/index.ts\";\nimport type * as t from \"@babel/types\";\nimport type Scope from \"./index.ts\";\n\nexport type BindingKind =\n | \"var\" /* var declarator */\n | \"let\" /* let declarator, class declaration id, catch clause parameters */\n | \"const\" /* const/using declarator */\n | \"module\" /* import specifiers */\n | \"hoisted\" /* function declaration id */\n | \"param\" /* function declaration parameters */\n | \"local\" /* function expression id, class expression id */\n | \"unknown\"; /* export specifiers */\n/**\n * This class is responsible for a binding inside of a scope.\n *\n * It tracks the following:\n *\n * * Node path.\n * * Amount of times referenced by other nodes.\n * * Paths to nodes that reassign or modify this binding.\n * * The kind of binding. (Is it a parameter, declaration etc)\n */\n\nexport default class Binding {\n identifier: t.Identifier;\n scope: Scope;\n path: NodePath;\n kind: BindingKind;\n\n constructor({\n identifier,\n scope,\n path,\n kind,\n }: {\n identifier: t.Identifier;\n scope: Scope;\n path: NodePath;\n kind: BindingKind;\n }) {\n this.identifier = identifier;\n this.scope = scope;\n this.path = path;\n this.kind = kind;\n\n if ((kind === \"var\" || kind === \"hoisted\") && isDeclaredInLoop(path)) {\n this.reassign(path);\n }\n\n this.clearValue();\n }\n\n constantViolations: Array<NodePath> = [];\n constant: boolean = true;\n\n referencePaths: Array<NodePath> = [];\n referenced: boolean = false;\n references: number = 0;\n\n declare hasDeoptedValue: boolean;\n declare hasValue: boolean;\n declare value: any;\n\n deoptValue() {\n this.clearValue();\n this.hasDeoptedValue = true;\n }\n\n setValue(value: any) {\n if (this.hasDeoptedValue) return;\n this.hasValue = true;\n this.value = value;\n }\n\n clearValue() {\n this.hasDeoptedValue = false;\n this.hasValue = false;\n this.value = null;\n }\n\n /**\n * Register a constant violation with the provided `path`.\n */\n\n reassign(path: NodePath) {\n this.constant = false;\n if (this.constantViolations.includes(path)) {\n return;\n }\n this.constantViolations.push(path);\n }\n\n /**\n * Increment the amount of references to this binding.\n */\n\n reference(path: NodePath) {\n if (this.referencePaths.includes(path)) {\n return;\n }\n this.referenced = true;\n this.references++;\n this.referencePaths.push(path);\n }\n\n /**\n * Decrement the amount of references to this binding.\n */\n\n dereference() {\n this.references--;\n this.referenced = !!this.references;\n }\n}\n\nfunction isDeclaredInLoop(path: NodePath) {\n for (\n let { parentPath, key } = path;\n parentPath;\n { parentPath, key } = parentPath\n ) {\n if (parentPath.isFunctionParent()) return false;\n if (\n parentPath.isWhile() ||\n parentPath.isForXStatement() ||\n (parentPath.isForStatement() && key === \"body\")\n ) {\n return true;\n }\n }\n return false;\n}\n"],"mappings":";;;;;;AAwBe,MAAMA,OAAO,CAAC;EAM3BC,WAAWA,CAAC;IACVC,UAAU;IACVC,KAAK;IACLC,IAAI;IACJC;EAMF,CAAC,EAAE;IAAA,KAfHH,UAAU;IAAA,KACVC,KAAK;IAAA,KACLC,IAAI;IAAA,KACJC,IAAI;IAAA,KAyBJC,kBAAkB,GAAoB,EAAE;IAAA,KACxCC,QAAQ,GAAY,IAAI;IAAA,KAExBC,cAAc,GAAoB,EAAE;IAAA,KACpCC,UAAU,GAAY,KAAK;IAAA,KAC3BC,UAAU,GAAW,CAAC;IAjBpB,IAAI,CAACR,UAAU,GAAGA,UAAU;IAC5B,IAAI,CAACC,KAAK,GAAGA,KAAK;IAClB,IAAI,CAACC,IAAI,GAAGA,IAAI;IAChB,IAAI,CAACC,IAAI,GAAGA,IAAI;IAEhB,IAAI,CAACA,IAAI,KAAK,KAAK,IAAIA,IAAI,KAAK,SAAS,KAAKM,gBAAgB,CAACP,IAAI,CAAC,EAAE;MACpE,IAAI,CAACQ,QAAQ,CAACR,IAAI,CAAC;IACrB;IAEA,IAAI,CAACS,UAAU,CAAC,CAAC;EACnB;EAaAC,UAAUA,CAAA,EAAG;IACX,IAAI,CAACD,UAAU,CAAC,CAAC;IACjB,IAAI,CAACE,eAAe,GAAG,IAAI;EAC7B;EAEAC,QAAQA,CAACC,KAAU,EAAE;IACnB,IAAI,IAAI,CAACF,eAAe,EAAE;IAC1B,IAAI,CAACG,QAAQ,GAAG,IAAI;IACpB,IAAI,CAACD,KAAK,GAAGA,KAAK;EACpB;EAEAJ,UAAUA,CAAA,EAAG;IACX,IAAI,CAACE,eAAe,GAAG,KAAK;IAC5B,IAAI,CAACG,QAAQ,GAAG,KAAK;IACrB,IAAI,CAACD,KAAK,GAAG,IAAI;EACnB;EAMAL,QAAQA,CAACR,IAAc,EAAE;IACvB,IAAI,CAACG,QAAQ,GAAG,KAAK;IACrB,IAAI,IAAI,CAACD,kBAAkB,CAACa,QAAQ,CAACf,IAAI,CAAC,EAAE;MAC1C;IACF;IACA,IAAI,CAACE,kBAAkB,CAACc,IAAI,CAAChB,IAAI,CAAC;EACpC;EAMAiB,SAASA,CAACjB,IAAc,EAAE;IACxB,IAAI,IAAI,CAACI,cAAc,CAACW,QAAQ,CAACf,IAAI,CAAC,EAAE;MACtC;IACF;IACA,IAAI,CAACK,UAAU,GAAG,IAAI;IACtB,IAAI,CAACC,UAAU,EAAE;IACjB,IAAI,CAACF,cAAc,CAACY,IAAI,CAAChB,IAAI,CAAC;EAChC;EAMAkB,WAAWA,CAAA,EAAG;IACZ,IAAI,CAACZ,UAAU,Line truncated
|
||||
+962
@@ -0,0 +1,962 @@
|
||||
"use strict";
|
||||
|
||||
Object.defineProperty(exports, "__esModule", {
|
||||
value: true
|
||||
});
|
||||
exports.default = void 0;
|
||||
var _renamer = require("./lib/renamer.js");
|
||||
var _index = require("../index.js");
|
||||
var _binding = require("./binding.js");
|
||||
var _globals = require("globals");
|
||||
var _t = require("@babel/types");
|
||||
var t = _t;
|
||||
var _cache = require("../cache.js");
|
||||
var _visitors = require("../visitors.js");
|
||||
const {
|
||||
NOT_LOCAL_BINDING,
|
||||
assignmentExpression,
|
||||
callExpression,
|
||||
cloneNode,
|
||||
getBindingIdentifiers,
|
||||
identifier,
|
||||
isArrayExpression,
|
||||
isBinary,
|
||||
isCallExpression,
|
||||
isClass,
|
||||
isClassBody,
|
||||
isClassDeclaration,
|
||||
isExportAllDeclaration,
|
||||
isExportDefaultDeclaration,
|
||||
isExportNamedDeclaration,
|
||||
isFunctionDeclaration,
|
||||
isIdentifier,
|
||||
isImportDeclaration,
|
||||
isLiteral,
|
||||
isMemberExpression,
|
||||
isMethod,
|
||||
isModuleSpecifier,
|
||||
isNullLiteral,
|
||||
isObjectExpression,
|
||||
isProperty,
|
||||
isPureish,
|
||||
isRegExpLiteral,
|
||||
isSuper,
|
||||
isTaggedTemplateExpression,
|
||||
isTemplateLiteral,
|
||||
isThisExpression,
|
||||
isUnaryExpression,
|
||||
isVariableDeclaration,
|
||||
expressionStatement,
|
||||
matchesPattern,
|
||||
memberExpression,
|
||||
numericLiteral,
|
||||
toIdentifier,
|
||||
variableDeclaration,
|
||||
variableDeclarator,
|
||||
isRecordExpression,
|
||||
isTupleExpression,
|
||||
isObjectProperty,
|
||||
isTopicReference,
|
||||
isMetaProperty,
|
||||
isPrivateName,
|
||||
isExportDeclaration,
|
||||
buildUndefinedNode,
|
||||
sequenceExpression
|
||||
} = _t;
|
||||
function gatherNodeParts(node, parts) {
|
||||
switch (node == null ? void 0 : node.type) {
|
||||
default:
|
||||
if (isImportDeclaration(node) || isExportDeclaration(node)) {
|
||||
var _node$specifiers;
|
||||
if ((isExportAllDeclaration(node) || isExportNamedDeclaration(node) || isImportDeclaration(node)) && node.source) {
|
||||
gatherNodeParts(node.source, parts);
|
||||
} else if ((isExportNamedDeclaration(node) || isImportDeclaration(node)) && (_node$specifiers = node.specifiers) != null && _node$specifiers.length) {
|
||||
for (const e of node.specifiers) gatherNodeParts(e, parts);
|
||||
} else if ((isExportDefaultDeclaration(node) || isExportNamedDeclaration(node)) && node.declaration) {
|
||||
gatherNodeParts(node.declaration, parts);
|
||||
}
|
||||
} else if (isModuleSpecifier(node)) {
|
||||
gatherNodeParts(node.local, parts);
|
||||
} else if (isLiteral(node) && !isNullLiteral(node) && !isRegExpLiteral(node) && !isTemplateLiteral(node)) {
|
||||
parts.push(node.value);
|
||||
}
|
||||
break;
|
||||
case "MemberExpression":
|
||||
case "OptionalMemberExpression":
|
||||
case "JSXMemberExpression":
|
||||
gatherNodeParts(node.object, parts);
|
||||
gatherNodeParts(node.property, parts);
|
||||
break;
|
||||
case "Identifier":
|
||||
case "JSXIdentifier":
|
||||
parts.push(node.name);
|
||||
break;
|
||||
case "CallExpression":
|
||||
case "OptionalCallExpression":
|
||||
case "NewExpression":
|
||||
gatherNodeParts(node.callee, parts);
|
||||
break;
|
||||
case "ObjectExpression":
|
||||
case "ObjectPattern":
|
||||
for (const e of node.properties) {
|
||||
gatherNodeParts(e, parts);
|
||||
}
|
||||
break;
|
||||
case "SpreadElement":
|
||||
case "RestElement":
|
||||
gatherNodeParts(node.argument, parts);
|
||||
break;
|
||||
case "ObjectProperty":
|
||||
case "ObjectMethod":
|
||||
case "ClassProperty":
|
||||
case "ClassMethod":
|
||||
case "ClassPrivateProperty":
|
||||
case "ClassPrivateMethod":
|
||||
gatherNodeParts(node.key, parts);
|
||||
break;
|
||||
case "ThisExpression":
|
||||
parts.push("this");
|
||||
break;
|
||||
case "Super":
|
||||
parts.push("super");
|
||||
break;
|
||||
case "Import":
|
||||
parts.push("import");
|
||||
break;
|
||||
case "DoExpression":
|
||||
parts.push("do");
|
||||
break;
|
||||
case "YieldExpression":
|
||||
parts.push("yield");
|
||||
gatherNodeParts(node.argument, parts);
|
||||
break;
|
||||
case "AwaitExpression":
|
||||
parts.push("await");
|
||||
gatherNodeParts(node.argument, parts);
|
||||
break;
|
||||
case "AssignmentExpression":
|
||||
gatherNodeParts(node.left, parts);
|
||||
break;
|
||||
case "VariableDeclarator":
|
||||
gatherNodeParts(node.id, parts);
|
||||
break;
|
||||
case "FunctionExpression":
|
||||
case "FunctionDeclaration":
|
||||
case "ClassExpression":
|
||||
case "ClassDeclaration":
|
||||
gatherNodeParts(node.id, parts);
|
||||
break;
|
||||
case "PrivateName":
|
||||
gatherNodeParts(node.id, parts);
|
||||
break;
|
||||
case "ParenthesizedExpression":
|
||||
gatherNodeParts(node.expression, parts);
|
||||
break;
|
||||
case "UnaryExpression":
|
||||
case "UpdateExpression":
|
||||
gatherNodeParts(node.argument, parts);
|
||||
break;
|
||||
case "MetaProperty":
|
||||
gatherNodeParts(node.meta, parts);
|
||||
gatherNodeParts(node.property, parts);
|
||||
break;
|
||||
case "JSXElement":
|
||||
gatherNodeParts(node.openingElement, parts);
|
||||
break;
|
||||
case "JSXOpeningElement":
|
||||
gatherNodeParts(node.name, parts);
|
||||
break;
|
||||
case "JSXFragment":
|
||||
gatherNodeParts(node.openingFragment, parts);
|
||||
break;
|
||||
case "JSXOpeningFragment":
|
||||
parts.push("Fragment");
|
||||
break;
|
||||
case "JSXNamespacedName":
|
||||
gatherNodeParts(node.namespace, parts);
|
||||
gatherNodeParts(node.name, parts);
|
||||
break;
|
||||
}
|
||||
}
|
||||
const collectorVisitor = {
|
||||
ForStatement(path) {
|
||||
const declar = path.get("init");
|
||||
if (declar.isVar()) {
|
||||
const {
|
||||
scope
|
||||
} = path;
|
||||
const parentScope = scope.getFunctionParent() || scope.getProgramParent();
|
||||
parentScope.registerBinding("var", declar);
|
||||
}
|
||||
},
|
||||
Declaration(path) {
|
||||
if (path.isBlockScoped()) return;
|
||||
if (path.isImportDeclaration()) return;
|
||||
if (path.isExportDeclaration()) return;
|
||||
const parent = path.scope.getFunctionParent() || path.scope.getProgramParent();
|
||||
parent.registerDeclaration(path);
|
||||
},
|
||||
ImportDeclaration(path) {
|
||||
const parent = path.scope.getBlockParent();
|
||||
parent.registerDeclaration(path);
|
||||
},
|
||||
ReferencedIdentifier(path, state) {
|
||||
state.references.push(path);
|
||||
},
|
||||
ForXStatement(path, state) {
|
||||
const left = path.get("left");
|
||||
if (left.isPattern() || left.isIdentifier()) {
|
||||
state.constantViolations.push(path);
|
||||
} else if (left.isVar()) {
|
||||
const {
|
||||
scope
|
||||
} = path;
|
||||
const parentScope = scope.getFunctionParent() || scope.getProgramParent();
|
||||
parentScope.registerBinding("var", left);
|
||||
}
|
||||
},
|
||||
ExportDeclaration: {
|
||||
exit(path) {
|
||||
const {
|
||||
node,
|
||||
scope
|
||||
} = path;
|
||||
if (isExportAllDeclaration(node)) return;
|
||||
const declar = node.declaration;
|
||||
if (isClassDeclaration(declar) || isFunctionDeclaration(declar)) {
|
||||
const id = declar.id;
|
||||
if (!id) return;
|
||||
const binding = scope.getBinding(id.name);
|
||||
binding == null || binding.reference(path);
|
||||
} else if (isVariableDeclaration(declar)) {
|
||||
for (const decl of declar.declarations) {
|
||||
for (const name of Object.keys(getBindingIdentifiers(decl))) {
|
||||
const binding = scope.getBinding(name);
|
||||
binding == null || binding.reference(path);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
LabeledStatement(path) {
|
||||
path.scope.getBlockParent().registerDeclaration(path);
|
||||
},
|
||||
AssignmentExpression(path, state) {
|
||||
state.assignments.push(path);
|
||||
},
|
||||
UpdateExpression(path, state) {
|
||||
state.constantViolations.push(path);
|
||||
},
|
||||
UnaryExpression(path, state) {
|
||||
if (path.node.operator === "delete") {
|
||||
state.constantViolations.push(path);
|
||||
}
|
||||
},
|
||||
BlockScoped(path) {
|
||||
let scope = path.scope;
|
||||
if (scope.path === path) scope = scope.parent;
|
||||
const parent = scope.getBlockParent();
|
||||
parent.registerDeclaration(path);
|
||||
if (path.isClassDeclaration() && path.node.id) {
|
||||
const id = path.node.id;
|
||||
const name = id.name;
|
||||
path.scope.bindings[name] = path.scope.parent.getBinding(name);
|
||||
}
|
||||
},
|
||||
CatchClause(path) {
|
||||
path.scope.registerBinding("let", path);
|
||||
},
|
||||
Function(path) {
|
||||
const params = path.get("params");
|
||||
for (const param of params) {
|
||||
path.scope.registerBinding("param", param);
|
||||
}
|
||||
if (path.isFunctionExpression() && path.node.id && !path.node.id[NOT_LOCAL_BINDING]) {
|
||||
path.scope.registerBinding("local", path.get("id"), path);
|
||||
}
|
||||
},
|
||||
ClassExpression(path) {
|
||||
if (path.node.id && !path.node.id[NOT_LOCAL_BINDING]) {
|
||||
path.scope.registerBinding("local", path.get("id"), path);
|
||||
}
|
||||
},
|
||||
TSTypeAnnotation(path) {
|
||||
path.skip();
|
||||
}
|
||||
};
|
||||
let uid = 0;
|
||||
class Scope {
|
||||
constructor(path) {
|
||||
this.uid = void 0;
|
||||
this.path = void 0;
|
||||
this.block = void 0;
|
||||
this.labels = void 0;
|
||||
this.inited = void 0;
|
||||
this.bindings = void 0;
|
||||
this.references = void 0;
|
||||
this.globals = void 0;
|
||||
this.uids = void 0;
|
||||
this.data = void 0;
|
||||
this.crawling = void 0;
|
||||
const {
|
||||
node
|
||||
} = path;
|
||||
const cached = _cache.scope.get(node);
|
||||
if ((cached == null ? void 0 : cached.path) === path) {
|
||||
return cached;
|
||||
}
|
||||
_cache.scope.set(node, this);
|
||||
this.uid = uid++;
|
||||
this.block = node;
|
||||
this.path = path;
|
||||
this.labels = new Map();
|
||||
this.inited = false;
|
||||
}
|
||||
get parent() {
|
||||
var _parent;
|
||||
let parent,
|
||||
path = this.path;
|
||||
do {
|
||||
var _path;
|
||||
const shouldSkip = path.key === "key" || path.listKey === "decorators";
|
||||
path = path.parentPath;
|
||||
if (shouldSkip && path.isMethod()) path = path.parentPath;
|
||||
if ((_path = path) != null && _path.isScope()) parent = path;
|
||||
} while (path && !parent);
|
||||
return (_parent = parent) == null ? void 0 : _parent.scope;
|
||||
}
|
||||
get parentBlock() {
|
||||
return this.path.parent;
|
||||
}
|
||||
get hub() {
|
||||
return this.path.hub;
|
||||
}
|
||||
traverse(node, opts, state) {
|
||||
(0, _index.default)(node, opts, this, state, this.path);
|
||||
}
|
||||
generateDeclaredUidIdentifier(name) {
|
||||
const id = this.generateUidIdentifier(name);
|
||||
this.push({
|
||||
id
|
||||
});
|
||||
return cloneNode(id);
|
||||
}
|
||||
generateUidIdentifier(name) {
|
||||
return identifier(this.generateUid(name));
|
||||
}
|
||||
generateUid(name = "temp") {
|
||||
name = toIdentifier(name).replace(/^_+/, "").replace(/\d+$/g, "");
|
||||
let uid;
|
||||
let i = 1;
|
||||
do {
|
||||
uid = this._generateUid(name, i);
|
||||
i++;
|
||||
} while (this.hasLabel(uid) || this.hasBinding(uid) || this.hasGlobal(uid) || this.hasReference(uid));
|
||||
const program = this.getProgramParent();
|
||||
program.references[uid] = true;
|
||||
program.uids[uid] = true;
|
||||
return uid;
|
||||
}
|
||||
_generateUid(name, i) {
|
||||
let id = name;
|
||||
if (i > 1) id += i;
|
||||
return `_${id}`;
|
||||
}
|
||||
generateUidBasedOnNode(node, defaultName) {
|
||||
const parts = [];
|
||||
gatherNodeParts(node, parts);
|
||||
let id = parts.join("$");
|
||||
id = id.replace(/^_/, "") || defaultName || "ref";
|
||||
return this.generateUid(id.slice(0, 20));
|
||||
}
|
||||
generateUidIdentifierBasedOnNode(node, defaultName) {
|
||||
return identifier(this.generateUidBasedOnNode(node, defaultName));
|
||||
}
|
||||
isStatic(node) {
|
||||
if (isThisExpression(node) || isSuper(node) || isTopicReference(node)) {
|
||||
return true;
|
||||
}
|
||||
if (isIdentifier(node)) {
|
||||
const binding = this.getBinding(node.name);
|
||||
if (binding) {
|
||||
return binding.constant;
|
||||
} else {
|
||||
return this.hasBinding(node.name);
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
maybeGenerateMemoised(node, dontPush) {
|
||||
if (this.isStatic(node)) {
|
||||
return null;
|
||||
} else {
|
||||
const id = this.generateUidIdentifierBasedOnNode(node);
|
||||
if (!dontPush) {
|
||||
this.push({
|
||||
id
|
||||
});
|
||||
return cloneNode(id);
|
||||
}
|
||||
return id;
|
||||
}
|
||||
}
|
||||
checkBlockScopedCollisions(local, kind, name, id) {
|
||||
if (kind === "param") return;
|
||||
if (local.kind === "local") return;
|
||||
const duplicate = kind === "let" || local.kind === "let" || local.kind === "const" || local.kind === "module" || local.kind === "param" && kind === "const";
|
||||
if (duplicate) {
|
||||
throw this.hub.buildError(id, `Duplicate declaration "${name}"`, TypeError);
|
||||
}
|
||||
}
|
||||
rename(oldName, newName) {
|
||||
const binding = this.getBinding(oldName);
|
||||
if (binding) {
|
||||
newName || (newName = this.generateUidIdentifier(oldName).name);
|
||||
const renamer = new _renamer.default(binding, oldName, newName);
|
||||
{
|
||||
renamer.rename(arguments[2]);
|
||||
}
|
||||
}
|
||||
}
|
||||
_renameFromMap(map, oldName, newName, value) {
|
||||
if (map[oldName]) {
|
||||
map[newName] = value;
|
||||
map[oldName] = null;
|
||||
}
|
||||
}
|
||||
dump() {
|
||||
const sep = "-".repeat(60);
|
||||
console.log(sep);
|
||||
let scope = this;
|
||||
do {
|
||||
console.log("#", scope.block.type);
|
||||
for (const name of Object.keys(scope.bindings)) {
|
||||
const binding = scope.bindings[name];
|
||||
console.log(" -", name, {
|
||||
constant: binding.constant,
|
||||
references: binding.references,
|
||||
violations: binding.constantViolations.length,
|
||||
kind: binding.kind
|
||||
});
|
||||
}
|
||||
} while (scope = scope.parent);
|
||||
console.log(sep);
|
||||
}
|
||||
toArray(node, i, arrayLikeIsIterable) {
|
||||
if (isIdentifier(node)) {
|
||||
const binding = this.getBinding(node.name);
|
||||
if (binding != null && binding.constant && binding.path.isGenericType("Array")) {
|
||||
return node;
|
||||
}
|
||||
}
|
||||
if (isArrayExpression(node)) {
|
||||
return node;
|
||||
}
|
||||
if (isIdentifier(node, {
|
||||
name: "arguments"
|
||||
})) {
|
||||
return callExpression(memberExpression(memberExpression(memberExpression(identifier("Array"), identifier("prototype")), identifier("slice")), identifier("call")), [node]);
|
||||
}
|
||||
let helperName;
|
||||
const args = [node];
|
||||
if (i === true) {
|
||||
helperName = "toConsumableArray";
|
||||
} else if (typeof i === "number") {
|
||||
args.push(numericLiteral(i));
|
||||
helperName = "slicedToArray";
|
||||
} else {
|
||||
helperName = "toArray";
|
||||
}
|
||||
if (arrayLikeIsIterable) {
|
||||
args.unshift(this.hub.addHelper(helperName));
|
||||
helperName = "maybeArrayLike";
|
||||
}
|
||||
return callExpression(this.hub.addHelper(helperName), args);
|
||||
}
|
||||
hasLabel(name) {
|
||||
return !!this.getLabel(name);
|
||||
}
|
||||
getLabel(name) {
|
||||
return this.labels.get(name);
|
||||
}
|
||||
registerLabel(path) {
|
||||
this.labels.set(path.node.label.name, path);
|
||||
}
|
||||
registerDeclaration(path) {
|
||||
if (path.isLabeledStatement()) {
|
||||
this.registerLabel(path);
|
||||
} else if (path.isFunctionDeclaration()) {
|
||||
this.registerBinding("hoisted", path.get("id"), path);
|
||||
} else if (path.isVariableDeclaration()) {
|
||||
const declarations = path.get("declarations");
|
||||
const {
|
||||
kind
|
||||
} = path.node;
|
||||
for (const declar of declarations) {
|
||||
this.registerBinding(kind === "using" || kind === "await using" ? "const" : kind, declar);
|
||||
}
|
||||
} else if (path.isClassDeclaration()) {
|
||||
if (path.node.declare) return;
|
||||
this.registerBinding("let", path);
|
||||
} else if (path.isImportDeclaration()) {
|
||||
const isTypeDeclaration = path.node.importKind === "type" || path.node.importKind === "typeof";
|
||||
const specifiers = path.get("specifiers");
|
||||
for (const specifier of specifiers) {
|
||||
const isTypeSpecifier = isTypeDeclaration || specifier.isImportSpecifier() && (specifier.node.importKind === "type" || specifier.node.importKind === "typeof");
|
||||
this.registerBinding(isTypeSpecifier ? "unknown" : "module", specifier);
|
||||
}
|
||||
} else if (path.isExportDeclaration()) {
|
||||
const declar = path.get("declaration");
|
||||
if (declar.isClassDeclaration() || declar.isFunctionDeclaration() || declar.isVariableDeclaration()) {
|
||||
this.registerDeclaration(declar);
|
||||
}
|
||||
} else {
|
||||
this.registerBinding("unknown", path);
|
||||
}
|
||||
}
|
||||
buildUndefinedNode() {
|
||||
return buildUndefinedNode();
|
||||
}
|
||||
registerConstantViolation(path) {
|
||||
const ids = path.getAssignmentIdentifiers();
|
||||
for (const name of Object.keys(ids)) {
|
||||
var _this$getBinding;
|
||||
(_this$getBinding = this.getBinding(name)) == null || _this$getBinding.reassign(path);
|
||||
}
|
||||
}
|
||||
registerBinding(kind, path, bindingPath = path) {
|
||||
if (!kind) throw new ReferenceError("no `kind`");
|
||||
if (path.isVariableDeclaration()) {
|
||||
const declarators = path.get("declarations");
|
||||
for (const declar of declarators) {
|
||||
this.registerBinding(kind, declar);
|
||||
}
|
||||
return;
|
||||
}
|
||||
const parent = this.getProgramParent();
|
||||
const ids = path.getOuterBindingIdentifiers(true);
|
||||
for (const name of Object.keys(ids)) {
|
||||
parent.references[name] = true;
|
||||
for (const id of ids[name]) {
|
||||
const local = this.getOwnBinding(name);
|
||||
if (local) {
|
||||
if (local.identifier === id) continue;
|
||||
this.checkBlockScopedCollisions(local, kind, name, id);
|
||||
}
|
||||
if (local) {
|
||||
local.reassign(bindingPath);
|
||||
} else {
|
||||
this.bindings[name] = new _binding.default({
|
||||
identifier: id,
|
||||
scope: this,
|
||||
path: bindingPath,
|
||||
kind: kind
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
addGlobal(node) {
|
||||
this.globals[node.name] = node;
|
||||
}
|
||||
hasUid(name) {
|
||||
let scope = this;
|
||||
do {
|
||||
if (scope.uids[name]) return true;
|
||||
} while (scope = scope.parent);
|
||||
return false;
|
||||
}
|
||||
hasGlobal(name) {
|
||||
let scope = this;
|
||||
do {
|
||||
if (scope.globals[name]) return true;
|
||||
} while (scope = scope.parent);
|
||||
return false;
|
||||
}
|
||||
hasReference(name) {
|
||||
return !!this.getProgramParent().references[name];
|
||||
}
|
||||
isPure(node, constantsOnly) {
|
||||
if (isIdentifier(node)) {
|
||||
const binding = this.getBinding(node.name);
|
||||
if (!binding) return false;
|
||||
if (constantsOnly) return binding.constant;
|
||||
return true;
|
||||
} else if (isThisExpression(node) || isMetaProperty(node) || isTopicReference(node) || isPrivateName(node)) {
|
||||
return true;
|
||||
} else if (isClass(node)) {
|
||||
var _node$decorators;
|
||||
if (node.superClass && !this.isPure(node.superClass, constantsOnly)) {
|
||||
return false;
|
||||
}
|
||||
if (((_node$decorators = node.decorators) == null ? void 0 : _node$decorators.length) > 0) {
|
||||
return false;
|
||||
}
|
||||
return this.isPure(node.body, constantsOnly);
|
||||
} else if (isClassBody(node)) {
|
||||
for (const method of node.body) {
|
||||
if (!this.isPure(method, constantsOnly)) return false;
|
||||
}
|
||||
return true;
|
||||
} else if (isBinary(node)) {
|
||||
return this.isPure(node.left, constantsOnly) && this.isPure(node.right, constantsOnly);
|
||||
} else if (isArrayExpression(node) || isTupleExpression(node)) {
|
||||
for (const elem of node.elements) {
|
||||
if (elem !== null && !this.isPure(elem, constantsOnly)) return false;
|
||||
}
|
||||
return true;
|
||||
} else if (isObjectExpression(node) || isRecordExpression(node)) {
|
||||
for (const prop of node.properties) {
|
||||
if (!this.isPure(prop, constantsOnly)) return false;
|
||||
}
|
||||
return true;
|
||||
} else if (isMethod(node)) {
|
||||
var _node$decorators2;
|
||||
if (node.computed && !this.isPure(node.key, constantsOnly)) return false;
|
||||
if (((_node$decorators2 = node.decorators) == null ? void 0 : _node$decorators2.length) > 0) {
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
} else if (isProperty(node)) {
|
||||
var _node$decorators3;
|
||||
if (node.computed && !this.isPure(node.key, constantsOnly)) return false;
|
||||
if (((_node$decorators3 = node.decorators) == null ? void 0 : _node$decorators3.length) > 0) {
|
||||
return false;
|
||||
}
|
||||
if (isObjectProperty(node) || node.static) {
|
||||
if (node.value !== null && !this.isPure(node.value, constantsOnly)) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
} else if (isUnaryExpression(node)) {
|
||||
return this.isPure(node.argument, constantsOnly);
|
||||
} else if (isTemplateLiteral(node)) {
|
||||
for (const expression of node.expressions) {
|
||||
if (!this.isPure(expression, constantsOnly)) return false;
|
||||
}
|
||||
return true;
|
||||
} else if (isTaggedTemplateExpression(node)) {
|
||||
return matchesPattern(node.tag, "String.raw") && !this.hasBinding("String", {
|
||||
noGlobals: true
|
||||
}) && this.isPure(node.quasi, constantsOnly);
|
||||
} else if (isMemberExpression(node)) {
|
||||
return !node.computed && isIdentifier(node.object) && node.object.name === "Symbol" && isIdentifier(node.property) && node.property.name !== "for" && !this.hasBinding("Symbol", {
|
||||
noGlobals: true
|
||||
});
|
||||
} else if (isCallExpression(node)) {
|
||||
return matchesPattern(node.callee, "Symbol.for") && !this.hasBinding("Symbol", {
|
||||
noGlobals: true
|
||||
}) && node.arguments.length === 1 && t.isStringLiteral(node.arguments[0]);
|
||||
} else {
|
||||
return isPureish(node);
|
||||
}
|
||||
}
|
||||
setData(key, val) {
|
||||
return this.data[key] = val;
|
||||
}
|
||||
getData(key) {
|
||||
let scope = this;
|
||||
do {
|
||||
const data = scope.data[key];
|
||||
if (data != null) return data;
|
||||
} while (scope = scope.parent);
|
||||
}
|
||||
removeData(key) {
|
||||
let scope = this;
|
||||
do {
|
||||
const data = scope.data[key];
|
||||
if (data != null) scope.data[key] = null;
|
||||
} while (scope = scope.parent);
|
||||
}
|
||||
init() {
|
||||
if (!this.inited) {
|
||||
this.inited = true;
|
||||
this.crawl();
|
||||
}
|
||||
}
|
||||
crawl() {
|
||||
const path = this.path;
|
||||
this.references = Object.create(null);
|
||||
this.bindings = Object.create(null);
|
||||
this.globals = Object.create(null);
|
||||
this.uids = Object.create(null);
|
||||
this.data = Object.create(null);
|
||||
const programParent = this.getProgramParent();
|
||||
if (programParent.crawling) return;
|
||||
const state = {
|
||||
references: [],
|
||||
constantViolations: [],
|
||||
assignments: []
|
||||
};
|
||||
this.crawling = true;
|
||||
if (path.type !== "Program" && (0, _visitors.isExplodedVisitor)(collectorVisitor)) {
|
||||
for (const visit of collectorVisitor.enter) {
|
||||
visit.call(state, path, state);
|
||||
}
|
||||
const typeVisitors = collectorVisitor[path.type];
|
||||
if (typeVisitors) {
|
||||
for (const visit of typeVisitors.enter) {
|
||||
visit.call(state, path, state);
|
||||
}
|
||||
}
|
||||
}
|
||||
path.traverse(collectorVisitor, state);
|
||||
this.crawling = false;
|
||||
for (const path of state.assignments) {
|
||||
const ids = path.getAssignmentIdentifiers();
|
||||
for (const name of Object.keys(ids)) {
|
||||
if (path.scope.getBinding(name)) continue;
|
||||
programParent.addGlobal(ids[name]);
|
||||
}
|
||||
path.scope.registerConstantViolation(path);
|
||||
}
|
||||
for (const ref of state.references) {
|
||||
const binding = ref.scope.getBinding(ref.node.name);
|
||||
if (binding) {
|
||||
binding.reference(ref);
|
||||
} else {
|
||||
programParent.addGlobal(ref.node);
|
||||
}
|
||||
}
|
||||
for (const path of state.constantViolations) {
|
||||
path.scope.registerConstantViolation(path);
|
||||
}
|
||||
}
|
||||
push(opts) {
|
||||
let path = this.path;
|
||||
if (path.isPattern()) {
|
||||
path = this.getPatternParent().path;
|
||||
} else if (!path.isBlockStatement() && !path.isProgram()) {
|
||||
path = this.getBlockParent().path;
|
||||
}
|
||||
if (path.isSwitchStatement()) {
|
||||
path = (this.getFunctionParent() || this.getProgramParent()).path;
|
||||
}
|
||||
const {
|
||||
init,
|
||||
unique,
|
||||
kind = "var",
|
||||
id
|
||||
} = opts;
|
||||
if (!init && !unique && (kind === "var" || kind === "let") && path.isFunction() && !path.node.name && isCallExpression(path.parent, {
|
||||
callee: path.node
|
||||
}) && path.parent.arguments.length <= path.node.params.length && isIdentifier(id)) {
|
||||
path.pushContainer("params", id);
|
||||
path.scope.registerBinding("param", path.get("params")[path.node.params.length - 1]);
|
||||
return;
|
||||
}
|
||||
if (path.isLoop() || path.isCatchClause() || path.isFunction()) {
|
||||
path.ensureBlock();
|
||||
path = path.get("body");
|
||||
}
|
||||
const blockHoist = opts._blockHoist == null ? 2 : opts._blockHoist;
|
||||
const dataKey = `declaration:${kind}:${blockHoist}`;
|
||||
let declarPath = !unique && path.getData(dataKey);
|
||||
if (!declarPath) {
|
||||
const declar = variableDeclaration(kind, []);
|
||||
declar._blockHoist = blockHoist;
|
||||
[declarPath] = path.unshiftContainer("body", [declar]);
|
||||
if (!unique) path.setData(dataKey, declarPath);
|
||||
}
|
||||
const declarator = variableDeclarator(id, init);
|
||||
const len = declarPath.node.declarations.push(declarator);
|
||||
path.scope.registerBinding(kind, declarPath.get("declarations")[len - 1]);
|
||||
}
|
||||
getProgramParent() {
|
||||
let scope = this;
|
||||
do {
|
||||
if (scope.path.isProgram()) {
|
||||
return scope;
|
||||
}
|
||||
} while (scope = scope.parent);
|
||||
throw new Error("Couldn't find a Program");
|
||||
}
|
||||
getFunctionParent() {
|
||||
let scope = this;
|
||||
do {
|
||||
if (scope.path.isFunctionParent()) {
|
||||
return scope;
|
||||
}
|
||||
} while (scope = scope.parent);
|
||||
return null;
|
||||
}
|
||||
getBlockParent() {
|
||||
let scope = this;
|
||||
do {
|
||||
if (scope.path.isBlockParent()) {
|
||||
return scope;
|
||||
}
|
||||
} while (scope = scope.parent);
|
||||
throw new Error("We couldn't find a BlockStatement, For, Switch, Function, Loop or Program...");
|
||||
}
|
||||
getPatternParent() {
|
||||
let scope = this;
|
||||
do {
|
||||
if (!scope.path.isPattern()) {
|
||||
return scope.getBlockParent();
|
||||
}
|
||||
} while (scope = scope.parent.parent);
|
||||
throw new Error("We couldn't find a BlockStatement, For, Switch, Function, Loop or Program...");
|
||||
}
|
||||
getAllBindings() {
|
||||
const ids = Object.create(null);
|
||||
let scope = this;
|
||||
do {
|
||||
for (const key of Object.keys(scope.bindings)) {
|
||||
if (key in ids === false) {
|
||||
ids[key] = scope.bindings[key];
|
||||
}
|
||||
}
|
||||
scope = scope.parent;
|
||||
} while (scope);
|
||||
return ids;
|
||||
}
|
||||
getAllBindingsOfKind(...kinds) {
|
||||
const ids = Object.create(null);
|
||||
for (const kind of kinds) {
|
||||
let scope = this;
|
||||
do {
|
||||
for (const name of Object.keys(scope.bindings)) {
|
||||
const binding = scope.bindings[name];
|
||||
if (binding.kind === kind) ids[name] = binding;
|
||||
}
|
||||
scope = scope.parent;
|
||||
} while (scope);
|
||||
}
|
||||
return ids;
|
||||
}
|
||||
bindingIdentifierEquals(name, node) {
|
||||
return this.getBindingIdentifier(name) === node;
|
||||
}
|
||||
getBinding(name) {
|
||||
let scope = this;
|
||||
let previousPath;
|
||||
do {
|
||||
const binding = scope.getOwnBinding(name);
|
||||
if (binding) {
|
||||
var _previousPath;
|
||||
if ((_previousPath = previousPath) != null && _previousPath.isPattern() && binding.kind !== "param" && binding.kind !== "local") {} else {
|
||||
return binding;
|
||||
}
|
||||
} else if (!binding && name === "arguments" && scope.path.isFunction() && !scope.path.isArrowFunctionExpression()) {
|
||||
break;
|
||||
}
|
||||
previousPath = scope.path;
|
||||
} while (scope = scope.parent);
|
||||
}
|
||||
getOwnBinding(name) {
|
||||
return this.bindings[name];
|
||||
}
|
||||
getBindingIdentifier(name) {
|
||||
var _this$getBinding2;
|
||||
return (_this$getBinding2 = this.getBinding(name)) == null ? void 0 : _this$getBinding2.identifier;
|
||||
}
|
||||
getOwnBindingIdentifier(name) {
|
||||
const binding = this.bindings[name];
|
||||
return binding == null ? void 0 : binding.identifier;
|
||||
}
|
||||
hasOwnBinding(name) {
|
||||
return !!this.getOwnBinding(name);
|
||||
}
|
||||
hasBinding(name, opts) {
|
||||
if (!name) return false;
|
||||
let scope = this;
|
||||
do {
|
||||
if (scope.hasOwnBinding(name)) {
|
||||
return true;
|
||||
}
|
||||
} while (scope = scope.parent);
|
||||
let noGlobals;
|
||||
let noUids;
|
||||
if (typeof opts === "object") {
|
||||
noGlobals = opts.noGlobals;
|
||||
noUids = opts.noUids;
|
||||
} else if (typeof opts === "boolean") {
|
||||
noGlobals = opts;
|
||||
}
|
||||
if (!noUids && this.hasUid(name)) return true;
|
||||
if (!noGlobals && Scope.globals.includes(name)) return true;
|
||||
if (!noGlobals && Scope.contextVariables.includes(name)) return true;
|
||||
return false;
|
||||
}
|
||||
parentHasBinding(name, opts) {
|
||||
var _this$parent;
|
||||
return (_this$parent = this.parent) == null ? void 0 : _this$parent.hasBinding(name, opts);
|
||||
}
|
||||
moveBindingTo(name, scope) {
|
||||
const info = this.getBinding(name);
|
||||
if (info) {
|
||||
info.scope.removeOwnBinding(name);
|
||||
info.scope = scope;
|
||||
scope.bindings[name] = info;
|
||||
}
|
||||
}
|
||||
removeOwnBinding(name) {
|
||||
delete this.bindings[name];
|
||||
}
|
||||
removeBinding(name) {
|
||||
var _this$getBinding3;
|
||||
(_this$getBinding3 = this.getBinding(name)) == null || _this$getBinding3.scope.removeOwnBinding(name);
|
||||
let scope = this;
|
||||
do {
|
||||
if (scope.uids[name]) {
|
||||
scope.uids[name] = false;
|
||||
}
|
||||
} while (scope = scope.parent);
|
||||
}
|
||||
hoistVariables(emit = id => this.push({
|
||||
id
|
||||
})) {
|
||||
this.crawl();
|
||||
const seen = new Set();
|
||||
for (const name of Object.keys(this.bindings)) {
|
||||
const binding = this.bindings[name];
|
||||
if (!binding) continue;
|
||||
const {
|
||||
path
|
||||
} = binding;
|
||||
if (!path.isVariableDeclarator()) continue;
|
||||
const {
|
||||
parent,
|
||||
parentPath
|
||||
} = path;
|
||||
if (parent.kind !== "var" || seen.has(parent)) continue;
|
||||
seen.add(path.parent);
|
||||
let firstId;
|
||||
const init = [];
|
||||
for (const decl of parent.declarations) {
|
||||
var _firstId;
|
||||
(_firstId = firstId) != null ? _firstId : firstId = decl.id;
|
||||
if (decl.init) {
|
||||
init.push(assignmentExpression("=", decl.id, decl.init));
|
||||
}
|
||||
const ids = Object.keys(getBindingIdentifiers(decl, false, true, true));
|
||||
for (const name of ids) {
|
||||
emit(identifier(name), decl.init != null);
|
||||
}
|
||||
}
|
||||
if (parentPath.parentPath.isFor({
|
||||
left: parent
|
||||
})) {
|
||||
parentPath.replaceWith(firstId);
|
||||
} else if (init.length === 0) {
|
||||
parentPath.remove();
|
||||
} else {
|
||||
const expr = init.length === 1 ? init[0] : sequenceExpression(init);
|
||||
if (parentPath.parentPath.isForStatement({
|
||||
init: parent
|
||||
})) {
|
||||
parentPath.replaceWith(expr);
|
||||
} else {
|
||||
parentPath.replaceWith(expressionStatement(expr));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
exports.default = Scope;
|
||||
Scope.globals = Object.keys(_globals.builtin);
|
||||
Scope.contextVariables = ["arguments", "undefined", "Infinity", "NaN"];
|
||||
|
||||
//# sourceMappingURL=index.js.map
|
||||
+1
@@ -0,0 +1 @@
|
||||
{"version":3,"names":["_renamer","require","_index","_binding","_globals","_t","t","_cache","_visitors","NOT_LOCAL_BINDING","assignmentExpression","callExpression","cloneNode","getBindingIdentifiers","identifier","isArrayExpression","isBinary","isCallExpression","isClass","isClassBody","isClassDeclaration","isExportAllDeclaration","isExportDefaultDeclaration","isExportNamedDeclaration","isFunctionDeclaration","isIdentifier","isImportDeclaration","isLiteral","isMemberExpression","isMethod","isModuleSpecifier","isNullLiteral","isObjectExpression","isProperty","isPureish","isRegExpLiteral","isSuper","isTaggedTemplateExpression","isTemplateLiteral","isThisExpression","isUnaryExpression","isVariableDeclaration","expressionStatement","matchesPattern","memberExpression","numericLiteral","toIdentifier","variableDeclaration","variableDeclarator","isRecordExpression","isTupleExpression","isObjectProperty","isTopicReference","isMetaProperty","isPrivateName","isExportDeclaration","buildUndefinedNode","sequenceExpression","gatherNodeParts","node","parts","type","_node$specifiers","source","specifiers","length","e","declaration","local","push","value","object","property","name","callee","properties","argument","key","left","id","expression","meta","openingElement","openingFragment","namespace","collectorVisitor","ForStatement","path","declar","get","isVar","scope","parentScope","getFunctionParent","getProgramParent","registerBinding","Declaration","isBlockScoped","parent","registerDeclaration","ImportDeclaration","getBlockParent","ReferencedIdentifier","state","references","ForXStatement","isPattern","constantViolations","ExportDeclaration","exit","binding","getBinding","reference","decl","declarations","Object","keys","LabeledStatement","AssignmentExpression","assignments","UpdateExpression","UnaryExpression","operator","BlockScoped","bindings","CatchClause","Function","params","param","isFunctionExpression","ClassExpression","TSTypeAnnotation","skip","uid","Scope","constructor","block","labels","inited","globals","uids","data","crawling","cached","scopeCache","set","Map","_parent","_path","shouldSkip","listKey","parentPath","isScope","parentBlock","hub","traverse","opts","generateDeclaredUidIdentifier","generateUidIdentifier","generateUid","replace","i","_generateUid","hasLabel","hasBinding","hasGlobal","hasReference","program","generateUidBasedOnNode","defaultName","join","slice","generateUidIdentifierBasedOnNode","isStatic","constant","maybeGenerateMemoised","dontPush","checkBlockScopedCollisions","kind","duplicate","buildError","TypeError","rename","oldName","newName","renamer","Renamer","arguments","_renameFromMap","map","dump","sep","repeat","console","log","violations","toArray","arrayLikeIsIterable","isGenericType","helperName","args","unshift","addHelper","getLabel","registerLabel","label","isLabeledStatement","declare","isTypeDeclaration","importKind","specifier","isTypeSpecifier","isImportSpecifier","registerConstantViolation","ids","getAssignmentIdentifiers","_this$getBinding","reassign","bindingPath","ReferenceError","declarators","getOuterBindingIdentifiers","getOwnBinding","Binding","addGlobal","hasUid","isPure","constantsOnly","_node$decorators","superClass","decorators","body","method","right","elem","elements","prop","_node$decorators2","computed","_node$decorators3","static","expressions","tag","noGlobals","quasi","isStringLiteral","setData","val","getData","removeData","init","crawl","create","programParent","isExplodedVisitor","visit","enter","call","typeVisitors","ref","getPatternParent","isBlockStatement","isProgram","isSwitchStatement","unique","isFunction","pushContainer","isLoop","isCatchClause","ensureBlock","blockHoist","_blockHoist","dataKey","declarPath","unshiftContainer","declarator","len","Error","isFunctionParent","isBlockParent","getAllBindings","getAllBindingsOfKind","kinds","bindingIdentifierEquals","getBindingIdentifier","previousPath","_previousPath","isArrowFunctionExpression","_this$getBinding2","getOwnBindingIdentifier","hasOwnBinding","noUids","includes","contextVariables","parentHasBinding","_this$parent","moveBindingTo","info","removeOwnBinding","removeBinding","_this$getBinding3","hoistVariables","emit","seen","Set","isVariableDeclarator","has","add","firstId","_firstId","isFor","replaceWith","remove","expr","isForStatement","exports","default","builtin"],"sources":["../../src/scope/index.ts"],"sourcesContent":["import Renamer from \"./lib/renamer.ts\";\nimport type NodePath from \"../path/index.ts\";\nimport traverse from \"../index.ts\";\nimport type { TraverseOptions } from \"../index.ts\";\nimport Binding from \"./binding.ts\";\nimport type { BindingKind } from \"./binding.ts\";\nimport globals from \"globals\";\nimport {\n NOT_LOCAL_BINDING,\n assignmentExpression,\n callExpression,\n cloneNode,\n getBindingIdentifiers,\n identifier,\n isArrayExpression,\n isBinary,\n isCallExpression,\n isClass,\n isClassBody,\n isClassDeclaration,\n isExportAllDeclaratioLine truncated
|
||||
+131
@@ -0,0 +1,131 @@
|
||||
"use strict";
|
||||
|
||||
Object.defineProperty(exports, "__esModule", {
|
||||
value: true
|
||||
});
|
||||
exports.default = void 0;
|
||||
var t = require("@babel/types");
|
||||
var _t = t;
|
||||
var _traverseNode = require("../../traverse-node.js");
|
||||
var _visitors = require("../../visitors.js");
|
||||
var _context = require("../../path/context.js");
|
||||
const {
|
||||
getAssignmentIdentifiers
|
||||
} = _t;
|
||||
const renameVisitor = {
|
||||
ReferencedIdentifier({
|
||||
node
|
||||
}, state) {
|
||||
if (node.name === state.oldName) {
|
||||
node.name = state.newName;
|
||||
}
|
||||
},
|
||||
Scope(path, state) {
|
||||
if (!path.scope.bindingIdentifierEquals(state.oldName, state.binding.identifier)) {
|
||||
path.skip();
|
||||
if (path.isMethod()) {
|
||||
if (!path.requeueComputedKeyAndDecorators) {
|
||||
_context.requeueComputedKeyAndDecorators.call(path);
|
||||
} else {
|
||||
path.requeueComputedKeyAndDecorators();
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
ObjectProperty({
|
||||
node,
|
||||
scope
|
||||
}, state) {
|
||||
const {
|
||||
name
|
||||
} = node.key;
|
||||
if (node.shorthand && (name === state.oldName || name === state.newName) && scope.getBindingIdentifier(name) === state.binding.identifier) {
|
||||
node.shorthand = false;
|
||||
{
|
||||
var _node$extra;
|
||||
if ((_node$extra = node.extra) != null && _node$extra.shorthand) node.extra.shorthand = false;
|
||||
}
|
||||
}
|
||||
},
|
||||
"AssignmentExpression|Declaration|VariableDeclarator"(path, state) {
|
||||
if (path.isVariableDeclaration()) return;
|
||||
const ids = path.isAssignmentExpression() ? getAssignmentIdentifiers(path.node) : path.getOuterBindingIdentifiers();
|
||||
for (const name in ids) {
|
||||
if (name === state.oldName) ids[name].name = state.newName;
|
||||
}
|
||||
}
|
||||
};
|
||||
class Renamer {
|
||||
constructor(binding, oldName, newName) {
|
||||
this.newName = newName;
|
||||
this.oldName = oldName;
|
||||
this.binding = binding;
|
||||
}
|
||||
maybeConvertFromExportDeclaration(parentDeclar) {
|
||||
const maybeExportDeclar = parentDeclar.parentPath;
|
||||
if (!maybeExportDeclar.isExportDeclaration()) {
|
||||
return;
|
||||
}
|
||||
if (maybeExportDeclar.isExportDefaultDeclaration()) {
|
||||
const {
|
||||
declaration
|
||||
} = maybeExportDeclar.node;
|
||||
if (t.isDeclaration(declaration) && !declaration.id) {
|
||||
return;
|
||||
}
|
||||
}
|
||||
if (maybeExportDeclar.isExportAllDeclaration()) {
|
||||
return;
|
||||
}
|
||||
maybeExportDeclar.splitExportDeclaration();
|
||||
}
|
||||
maybeConvertFromClassFunctionDeclaration(path) {
|
||||
return path;
|
||||
}
|
||||
maybeConvertFromClassFunctionExpression(path) {
|
||||
return path;
|
||||
}
|
||||
rename() {
|
||||
const {
|
||||
binding,
|
||||
oldName,
|
||||
newName
|
||||
} = this;
|
||||
const {
|
||||
scope,
|
||||
path
|
||||
} = binding;
|
||||
const parentDeclar = path.find(path => path.isDeclaration() || path.isFunctionExpression() || path.isClassExpression());
|
||||
if (parentDeclar) {
|
||||
const bindingIds = parentDeclar.getOuterBindingIdentifiers();
|
||||
if (bindingIds[oldName] === binding.identifier) {
|
||||
this.maybeConvertFromExportDeclaration(parentDeclar);
|
||||
}
|
||||
}
|
||||
const blockToTraverse = arguments[0] || scope.block;
|
||||
const skipKeys = {
|
||||
discriminant: true
|
||||
};
|
||||
if (t.isMethod(blockToTraverse)) {
|
||||
if (blockToTraverse.computed) {
|
||||
skipKeys.key = true;
|
||||
}
|
||||
if (!t.isObjectMethod(blockToTraverse)) {
|
||||
skipKeys.decorators = true;
|
||||
}
|
||||
}
|
||||
(0, _traverseNode.traverseNode)(blockToTraverse, (0, _visitors.explode)(renameVisitor), scope, this, scope.path, skipKeys);
|
||||
if (!arguments[0]) {
|
||||
scope.removeOwnBinding(oldName);
|
||||
scope.bindings[newName] = binding;
|
||||
this.binding.identifier.name = newName;
|
||||
}
|
||||
if (parentDeclar) {
|
||||
this.maybeConvertFromClassFunctionDeclaration(path);
|
||||
this.maybeConvertFromClassFunctionExpression(path);
|
||||
}
|
||||
}
|
||||
}
|
||||
exports.default = Renamer;
|
||||
|
||||
//# sourceMappingURL=renamer.js.map
|
||||
+1
@@ -0,0 +1 @@
|
||||
{"version":3,"names":["t","require","_t","_traverseNode","_visitors","_context","getAssignmentIdentifiers","renameVisitor","ReferencedIdentifier","node","state","name","oldName","newName","Scope","path","scope","bindingIdentifierEquals","binding","identifier","skip","isMethod","requeueComputedKeyAndDecorators","call","ObjectProperty","key","shorthand","getBindingIdentifier","_node$extra","extra","AssignmentExpression|Declaration|VariableDeclarator","isVariableDeclaration","ids","isAssignmentExpression","getOuterBindingIdentifiers","Renamer","constructor","maybeConvertFromExportDeclaration","parentDeclar","maybeExportDeclar","parentPath","isExportDeclaration","isExportDefaultDeclaration","declaration","isDeclaration","id","isExportAllDeclaration","splitExportDeclaration","maybeConvertFromClassFunctionDeclaration","maybeConvertFromClassFunctionExpression","rename","find","isFunctionExpression","isClassExpression","bindingIds","blockToTraverse","arguments","block","skipKeys","discriminant","computed","isObjectMethod","decorators","traverseNode","explode","removeOwnBinding","bindings","exports","default"],"sources":["../../../src/scope/lib/renamer.ts"],"sourcesContent":["import type Binding from \"../binding.ts\";\nimport * as t from \"@babel/types\";\nimport type { NodePath, Visitor } from \"../../index.ts\";\nimport { traverseNode } from \"../../traverse-node.ts\";\nimport { explode } from \"../../visitors.ts\";\nimport { getAssignmentIdentifiers, type Identifier } from \"@babel/types\";\nimport { requeueComputedKeyAndDecorators } from \"../../path/context.ts\";\n\nconst renameVisitor: Visitor<Renamer> = {\n ReferencedIdentifier({ node }, state) {\n if (node.name === state.oldName) {\n node.name = state.newName;\n }\n },\n\n Scope(path, state) {\n if (\n !path.scope.bindingIdentifierEquals(\n state.oldName,\n state.binding.identifier,\n )\n ) {\n path.skip();\n if (path.isMethod()) {\n if (\n !process.env.BABEL_8_BREAKING &&\n !path.requeueComputedKeyAndDecorators\n ) {\n // See https://github.com/babel/babel/issues/16694\n requeueComputedKeyAndDecorators.call(path);\n } else {\n path.requeueComputedKeyAndDecorators();\n }\n }\n }\n },\n\n ObjectProperty({ node, scope }, state) {\n const { name } = node.key as Identifier;\n if (\n node.shorthand &&\n // In destructuring the identifier is already renamed by the\n // AssignmentExpression|Declaration|VariableDeclarator visitor,\n // while in object literals it's renamed later by the\n // ReferencedIdentifier visitor.\n (name === state.oldName || name === state.newName) &&\n // Ignore shadowed bindings\n scope.getBindingIdentifier(name) === state.binding.identifier\n ) {\n node.shorthand = false;\n if (!process.env.BABEL_8_BREAKING) {\n if (node.extra?.shorthand) node.extra.shorthand = false;\n }\n }\n },\n\n \"AssignmentExpression|Declaration|VariableDeclarator\"(\n path: NodePath<\n t.AssignmentExpression | t.Declaration | t.VariableDeclarator\n >,\n state,\n ) {\n if (path.isVariableDeclaration()) return;\n const ids = path.isAssignmentExpression()\n ? // See https://github.com/babel/babel/issues/16694\n getAssignmentIdentifiers(path.node)\n : path.getOuterBindingIdentifiers();\n\n for (const name in ids) {\n if (name === state.oldName) ids[name].name = state.newName;\n }\n },\n};\n\nexport default class Renamer {\n constructor(binding: Binding, oldName: string, newName: string) {\n this.newName = newName;\n this.oldName = oldName;\n this.binding = binding;\n }\n\n declare oldName: string;\n declare newName: string;\n declare binding: Binding;\n\n maybeConvertFromExportDeclaration(parentDeclar: NodePath) {\n const maybeExportDeclar = parentDeclar.parentPath;\n\n if (!maybeExportDeclar.isExportDeclaration()) {\n return;\n }\n\n if (maybeExportDeclar.isExportDefaultDeclaration()) {\n const { declaration } = maybeExportDeclar.node;\n if (t.isDeclaration(declaration) && !declaration.id) {\n return;\n }\n }\n\n if (maybeExportDeclar.isExportAllDeclaration()) {\n return;\n }\n\n maybeExportDeclar.splitExportDeclaration();\n }\n\n maybeConvertFromClassFunctionDeclaration(path: NodePath) {\n return path; // TODO\n\n // // retain the `name` of a class/function declaration\n\n // if (!path.isFunctionDeclaration() && !path.isClassDeclaration()) return;\n // if (this.binding.kind !== \"hoisted\") return;\n\n // path.node.id = identifier(this.oldName);\n // path.node._blockHoist = 3;\n\n // path.replaceWith(\n // variableDeclaration(\"let\", [\n // variableDeclarator(identifier(this.newName), toExpression(path.node)),\n // ]),\n // );\n }\n\n maybeConvertFromClassFunctionExpression(path: NodePath) {\Line truncated
|
||||
Reference in new issue
Block a user