forked from daren.hsu/line_push
update
This commit is contained in:
+8
-4
@@ -12,15 +12,19 @@ class Binding {
|
||||
path,
|
||||
kind
|
||||
}) {
|
||||
this.identifier = identifier;
|
||||
this.scope = scope;
|
||||
this.path = path;
|
||||
this.kind = 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;
|
||||
this.clearValue();
|
||||
}
|
||||
|
||||
|
||||
+228
-119
@@ -5,44 +5,79 @@ Object.defineProperty(exports, "__esModule", {
|
||||
});
|
||||
exports.default = void 0;
|
||||
|
||||
var _includes = _interopRequireDefault(require("lodash/includes"));
|
||||
var _renamer = require("./lib/renamer");
|
||||
|
||||
var _repeat = _interopRequireDefault(require("lodash/repeat"));
|
||||
var _index = require("../index");
|
||||
|
||||
var _renamer = _interopRequireDefault(require("./lib/renamer"));
|
||||
var _binding = require("./binding");
|
||||
|
||||
var _index = _interopRequireDefault(require("../index"));
|
||||
var _globals = require("globals");
|
||||
|
||||
var _defaults = _interopRequireDefault(require("lodash/defaults"));
|
||||
|
||||
var _binding = _interopRequireDefault(require("./binding"));
|
||||
|
||||
var _globals = _interopRequireDefault(require("globals"));
|
||||
|
||||
var t = _interopRequireWildcard(require("@babel/types"));
|
||||
var _t = require("@babel/types");
|
||||
|
||||
var _cache = require("../cache");
|
||||
|
||||
function _getRequireWildcardCache() { if (typeof WeakMap !== "function") return null; var cache = new WeakMap(); _getRequireWildcardCache = function () { return cache; }; return cache; }
|
||||
|
||||
function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
|
||||
|
||||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
||||
const {
|
||||
NOT_LOCAL_BINDING,
|
||||
callExpression,
|
||||
cloneNode,
|
||||
getBindingIdentifiers,
|
||||
identifier,
|
||||
isArrayExpression,
|
||||
isBinary,
|
||||
isClass,
|
||||
isClassBody,
|
||||
isClassDeclaration,
|
||||
isExportAllDeclaration,
|
||||
isExportDefaultDeclaration,
|
||||
isExportNamedDeclaration,
|
||||
isFunctionDeclaration,
|
||||
isIdentifier,
|
||||
isImportDeclaration,
|
||||
isLiteral,
|
||||
isMethod,
|
||||
isModuleDeclaration,
|
||||
isModuleSpecifier,
|
||||
isNullLiteral,
|
||||
isObjectExpression,
|
||||
isProperty,
|
||||
isPureish,
|
||||
isRegExpLiteral,
|
||||
isSuper,
|
||||
isTaggedTemplateExpression,
|
||||
isTemplateLiteral,
|
||||
isThisExpression,
|
||||
isUnaryExpression,
|
||||
isVariableDeclaration,
|
||||
matchesPattern,
|
||||
memberExpression,
|
||||
numericLiteral,
|
||||
toIdentifier,
|
||||
unaryExpression,
|
||||
variableDeclaration,
|
||||
variableDeclarator,
|
||||
isRecordExpression,
|
||||
isTupleExpression,
|
||||
isObjectProperty,
|
||||
isTopicReference,
|
||||
isMetaProperty,
|
||||
isPrivateName
|
||||
} = _t;
|
||||
|
||||
function gatherNodeParts(node, parts) {
|
||||
switch (node == null ? void 0 : node.type) {
|
||||
default:
|
||||
if (t.isModuleDeclaration(node)) {
|
||||
if (node.source) {
|
||||
if (isModuleDeclaration(node)) {
|
||||
if ((isExportAllDeclaration(node) || isExportNamedDeclaration(node) || isImportDeclaration(node)) && node.source) {
|
||||
gatherNodeParts(node.source, parts);
|
||||
} else if (node.specifiers && node.specifiers.length) {
|
||||
} else if ((isExportNamedDeclaration(node) || isImportDeclaration(node)) && node.specifiers && node.specifiers.length) {
|
||||
for (const e of node.specifiers) gatherNodeParts(e, parts);
|
||||
} else if (node.declaration) {
|
||||
} else if ((isExportDefaultDeclaration(node) || isExportNamedDeclaration(node)) && node.declaration) {
|
||||
gatherNodeParts(node.declaration, parts);
|
||||
}
|
||||
} else if (t.isModuleSpecifier(node)) {
|
||||
} else if (isModuleSpecifier(node)) {
|
||||
gatherNodeParts(node.local, parts);
|
||||
} else if (t.isLiteral(node)) {
|
||||
} else if (isLiteral(node) && !isNullLiteral(node) && !isRegExpLiteral(node) && !isTemplateLiteral(node)) {
|
||||
parts.push(node.value);
|
||||
}
|
||||
|
||||
@@ -152,7 +187,7 @@ function gatherNodeParts(node, parts) {
|
||||
break;
|
||||
|
||||
case "JSXOpeningElement":
|
||||
parts.push(node.name);
|
||||
gatherNodeParts(node.name, parts);
|
||||
break;
|
||||
|
||||
case "JSXFragment":
|
||||
@@ -171,28 +206,31 @@ function gatherNodeParts(node, parts) {
|
||||
}
|
||||
|
||||
const collectorVisitor = {
|
||||
For(path) {
|
||||
for (const key of t.FOR_INIT_KEYS) {
|
||||
const declar = path.get(key);
|
||||
ForStatement(path) {
|
||||
const declar = path.get("init");
|
||||
|
||||
if (declar.isVar()) {
|
||||
const parentScope = path.scope.getFunctionParent() || path.scope.getProgramParent();
|
||||
parentScope.registerBinding("var", declar);
|
||||
}
|
||||
if (declar.isVar()) {
|
||||
const {
|
||||
scope
|
||||
} = path;
|
||||
const parentScope = scope.getFunctionParent() || scope.getProgramParent();
|
||||
parentScope.registerBinding("var", declar);
|
||||
}
|
||||
},
|
||||
|
||||
Declaration(path) {
|
||||
if (path.isBlockScoped()) return;
|
||||
|
||||
if (path.isExportDeclaration() && path.get("declaration").isDeclaration()) {
|
||||
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);
|
||||
},
|
||||
@@ -202,6 +240,12 @@ const collectorVisitor = {
|
||||
|
||||
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);
|
||||
}
|
||||
},
|
||||
|
||||
@@ -211,18 +255,19 @@ const collectorVisitor = {
|
||||
node,
|
||||
scope
|
||||
} = path;
|
||||
if (isExportAllDeclaration(node)) return;
|
||||
const declar = node.declaration;
|
||||
|
||||
if (t.isClassDeclaration(declar) || t.isFunctionDeclaration(declar)) {
|
||||
if (isClassDeclaration(declar) || isFunctionDeclaration(declar)) {
|
||||
const id = declar.id;
|
||||
if (!id) return;
|
||||
const binding = scope.getBinding(id.name);
|
||||
if (binding) binding.reference(path);
|
||||
} else if (t.isVariableDeclaration(declar)) {
|
||||
binding == null ? void 0 : binding.reference(path);
|
||||
} else if (isVariableDeclaration(declar)) {
|
||||
for (const decl of declar.declarations) {
|
||||
for (const name of Object.keys(t.getBindingIdentifiers(decl))) {
|
||||
for (const name of Object.keys(getBindingIdentifiers(decl))) {
|
||||
const binding = scope.getBinding(name);
|
||||
if (binding) binding.reference(path);
|
||||
binding == null ? void 0 : binding.reference(path);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -231,7 +276,6 @@ const collectorVisitor = {
|
||||
},
|
||||
|
||||
LabeledStatement(path) {
|
||||
path.scope.getProgramParent().addGlobal(path.node);
|
||||
path.scope.getBlockParent().registerDeclaration(path);
|
||||
},
|
||||
|
||||
@@ -262,34 +306,24 @@ const collectorVisitor = {
|
||||
}
|
||||
},
|
||||
|
||||
Block(path) {
|
||||
const paths = path.get("body");
|
||||
|
||||
for (const bodyPath of paths) {
|
||||
if (bodyPath.isFunctionDeclaration()) {
|
||||
path.scope.getBlockParent().registerDeclaration(bodyPath);
|
||||
}
|
||||
}
|
||||
},
|
||||
|
||||
CatchClause(path) {
|
||||
path.scope.registerBinding("let", path);
|
||||
},
|
||||
|
||||
Function(path) {
|
||||
if (path.isFunctionExpression() && path.has("id") && !path.get("id").node[t.NOT_LOCAL_BINDING]) {
|
||||
path.scope.registerBinding("local", path.get("id"), path);
|
||||
}
|
||||
|
||||
const params = path.get("params");
|
||||
|
||||
for (const param of params) {
|
||||
path.scope.registerBinding("param", param);
|
||||
}
|
||||
|
||||
if (path.isFunctionExpression() && path.has("id") && !path.get("id").node[NOT_LOCAL_BINDING]) {
|
||||
path.scope.registerBinding("local", path.get("id"), path);
|
||||
}
|
||||
},
|
||||
|
||||
ClassExpression(path) {
|
||||
if (path.has("id") && !path.get("id").node[t.NOT_LOCAL_BINDING]) {
|
||||
if (path.has("id") && !path.get("id").node[NOT_LOCAL_BINDING]) {
|
||||
path.scope.registerBinding("local", path);
|
||||
}
|
||||
}
|
||||
@@ -299,6 +333,17 @@ 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;
|
||||
@@ -319,8 +364,19 @@ class Scope {
|
||||
}
|
||||
|
||||
get parent() {
|
||||
const parent = this.path.findParent(p => p.isScope());
|
||||
return parent == null ? void 0 : parent.scope;
|
||||
var _parent;
|
||||
|
||||
let parent,
|
||||
path = this.path;
|
||||
|
||||
do {
|
||||
const shouldSkip = path.key === "key" || path.listKey === "decorators";
|
||||
path = path.parentPath;
|
||||
if (shouldSkip && path.isMethod()) path = path.parentPath;
|
||||
if (path && path.isScope()) parent = path;
|
||||
} while (path && !parent);
|
||||
|
||||
return (_parent = parent) == null ? void 0 : _parent.scope;
|
||||
}
|
||||
|
||||
get parentBlock() {
|
||||
@@ -340,17 +396,17 @@ class Scope {
|
||||
this.push({
|
||||
id
|
||||
});
|
||||
return t.cloneNode(id);
|
||||
return cloneNode(id);
|
||||
}
|
||||
|
||||
generateUidIdentifier(name) {
|
||||
return t.identifier(this.generateUid(name));
|
||||
return identifier(this.generateUid(name));
|
||||
}
|
||||
|
||||
generateUid(name = "temp") {
|
||||
name = t.toIdentifier(name).replace(/^_+/, "").replace(/[0-9]+$/g, "");
|
||||
name = toIdentifier(name).replace(/^_+/, "").replace(/[0-9]+$/g, "");
|
||||
let uid;
|
||||
let i = 0;
|
||||
let i = 1;
|
||||
|
||||
do {
|
||||
uid = this._generateUid(name, i);
|
||||
@@ -378,15 +434,15 @@ class Scope {
|
||||
}
|
||||
|
||||
generateUidIdentifierBasedOnNode(node, defaultName) {
|
||||
return t.identifier(this.generateUidBasedOnNode(node, defaultName));
|
||||
return identifier(this.generateUidBasedOnNode(node, defaultName));
|
||||
}
|
||||
|
||||
isStatic(node) {
|
||||
if (t.isThisExpression(node) || t.isSuper(node)) {
|
||||
if (isThisExpression(node) || isSuper(node) || isTopicReference(node)) {
|
||||
return true;
|
||||
}
|
||||
|
||||
if (t.isIdentifier(node)) {
|
||||
if (isIdentifier(node)) {
|
||||
const binding = this.getBinding(node.name);
|
||||
|
||||
if (binding) {
|
||||
@@ -409,7 +465,7 @@ class Scope {
|
||||
this.push({
|
||||
id
|
||||
});
|
||||
return t.cloneNode(id);
|
||||
return cloneNode(id);
|
||||
}
|
||||
|
||||
return id;
|
||||
@@ -419,7 +475,7 @@ class Scope {
|
||||
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 === "let" || kind === "const");
|
||||
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);
|
||||
@@ -443,7 +499,7 @@ class Scope {
|
||||
}
|
||||
|
||||
dump() {
|
||||
const sep = (0, _repeat.default)("-", 60);
|
||||
const sep = "-".repeat(60);
|
||||
console.log(sep);
|
||||
let scope = this;
|
||||
|
||||
@@ -464,23 +520,23 @@ class Scope {
|
||||
console.log(sep);
|
||||
}
|
||||
|
||||
toArray(node, i, allowArrayLike) {
|
||||
if (t.isIdentifier(node)) {
|
||||
toArray(node, i, arrayLikeIsIterable) {
|
||||
if (isIdentifier(node)) {
|
||||
const binding = this.getBinding(node.name);
|
||||
|
||||
if ((binding == null ? void 0 : binding.constant) && binding.path.isGenericType("Array")) {
|
||||
if (binding != null && binding.constant && binding.path.isGenericType("Array")) {
|
||||
return node;
|
||||
}
|
||||
}
|
||||
|
||||
if (t.isArrayExpression(node)) {
|
||||
if (isArrayExpression(node)) {
|
||||
return node;
|
||||
}
|
||||
|
||||
if (t.isIdentifier(node, {
|
||||
if (isIdentifier(node, {
|
||||
name: "arguments"
|
||||
})) {
|
||||
return t.callExpression(t.memberExpression(t.memberExpression(t.memberExpression(t.identifier("Array"), t.identifier("prototype")), t.identifier("slice")), t.identifier("call")), [node]);
|
||||
return callExpression(memberExpression(memberExpression(memberExpression(identifier("Array"), identifier("prototype")), identifier("slice")), identifier("call")), [node]);
|
||||
}
|
||||
|
||||
let helperName;
|
||||
@@ -489,18 +545,18 @@ class Scope {
|
||||
if (i === true) {
|
||||
helperName = "toConsumableArray";
|
||||
} else if (i) {
|
||||
args.push(t.numericLiteral(i));
|
||||
args.push(numericLiteral(i));
|
||||
helperName = "slicedToArray";
|
||||
} else {
|
||||
helperName = "toArray";
|
||||
}
|
||||
|
||||
if (allowArrayLike) {
|
||||
if (arrayLikeIsIterable) {
|
||||
args.unshift(this.hub.addHelper(helperName));
|
||||
helperName = "maybeArrayLike";
|
||||
}
|
||||
|
||||
return t.callExpression(this.hub.addHelper(helperName), args);
|
||||
return callExpression(this.hub.addHelper(helperName), args);
|
||||
}
|
||||
|
||||
hasLabel(name) {
|
||||
@@ -527,6 +583,7 @@ class Scope {
|
||||
this.registerBinding(path.node.kind, declar);
|
||||
}
|
||||
} else if (path.isClassDeclaration()) {
|
||||
if (path.node.declare) return;
|
||||
this.registerBinding("let", path);
|
||||
} else if (path.isImportDeclaration()) {
|
||||
const specifiers = path.get("specifiers");
|
||||
@@ -546,7 +603,7 @@ class Scope {
|
||||
}
|
||||
|
||||
buildUndefinedNode() {
|
||||
return t.unaryExpression("void", t.numericLiteral(0), true);
|
||||
return unaryExpression("void", numericLiteral(0), true);
|
||||
}
|
||||
|
||||
registerConstantViolation(path) {
|
||||
@@ -628,56 +685,83 @@ class Scope {
|
||||
}
|
||||
|
||||
isPure(node, constantsOnly) {
|
||||
if (t.isIdentifier(node)) {
|
||||
if (isIdentifier(node)) {
|
||||
const binding = this.getBinding(node.name);
|
||||
if (!binding) return false;
|
||||
if (constantsOnly) return binding.constant;
|
||||
return true;
|
||||
} else if (t.isClass(node)) {
|
||||
} 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 (t.isClassBody(node)) {
|
||||
} else if (isClassBody(node)) {
|
||||
for (const method of node.body) {
|
||||
if (!this.isPure(method, constantsOnly)) return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
} else if (t.isBinary(node)) {
|
||||
} else if (isBinary(node)) {
|
||||
return this.isPure(node.left, constantsOnly) && this.isPure(node.right, constantsOnly);
|
||||
} else if (t.isArrayExpression(node)) {
|
||||
} else if (isArrayExpression(node) || isTupleExpression(node)) {
|
||||
for (const elem of node.elements) {
|
||||
if (!this.isPure(elem, constantsOnly)) return false;
|
||||
if (elem !== null && !this.isPure(elem, constantsOnly)) return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
} else if (t.isObjectExpression(node)) {
|
||||
} else if (isObjectExpression(node) || isRecordExpression(node)) {
|
||||
for (const prop of node.properties) {
|
||||
if (!this.isPure(prop, constantsOnly)) return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
} else if (t.isMethod(node)) {
|
||||
} else if (isMethod(node)) {
|
||||
var _node$decorators2;
|
||||
|
||||
if (node.computed && !this.isPure(node.key, constantsOnly)) return false;
|
||||
if (node.kind === "get" || node.kind === "set") return false;
|
||||
|
||||
if (((_node$decorators2 = node.decorators) == null ? void 0 : _node$decorators2.length) > 0) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
} else if (t.isProperty(node)) {
|
||||
} else if (isProperty(node)) {
|
||||
var _node$decorators3;
|
||||
|
||||
if (node.computed && !this.isPure(node.key, constantsOnly)) return false;
|
||||
return this.isPure(node.value, constantsOnly);
|
||||
} else if (t.isUnaryExpression(node)) {
|
||||
|
||||
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 (t.isTaggedTemplateExpression(node)) {
|
||||
return t.matchesPattern(node.tag, "String.raw") && !this.hasBinding("String", true) && this.isPure(node.quasi, constantsOnly);
|
||||
} else if (t.isTemplateLiteral(node)) {
|
||||
} else if (isTaggedTemplateExpression(node)) {
|
||||
return matchesPattern(node.tag, "String.raw") && !this.hasBinding("String", true) && this.isPure(node.quasi, constantsOnly);
|
||||
} else if (isTemplateLiteral(node)) {
|
||||
for (const expression of node.expressions) {
|
||||
if (!this.isPure(expression, constantsOnly)) return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
} else {
|
||||
return t.isPureish(node);
|
||||
return isPureish(node);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -717,19 +801,6 @@ class Scope {
|
||||
this.globals = Object.create(null);
|
||||
this.uids = Object.create(null);
|
||||
this.data = Object.create(null);
|
||||
|
||||
if (path.isFunction()) {
|
||||
if (path.isFunctionExpression() && path.has("id") && !path.get("id").node[t.NOT_LOCAL_BINDING]) {
|
||||
this.registerBinding("local", path.get("id"), path);
|
||||
}
|
||||
|
||||
const params = path.get("params");
|
||||
|
||||
for (const param of params) {
|
||||
this.registerBinding("param", param);
|
||||
}
|
||||
}
|
||||
|
||||
const programParent = this.getProgramParent();
|
||||
if (programParent.crawling) return;
|
||||
const state = {
|
||||
@@ -738,6 +809,21 @@ class Scope {
|
||||
assignments: []
|
||||
};
|
||||
this.crawling = true;
|
||||
|
||||
if (path.type !== "Program" && collectorVisitor._exploded) {
|
||||
for (const visit of collectorVisitor.enter) {
|
||||
visit(path, state);
|
||||
}
|
||||
|
||||
const typeVisitors = collectorVisitor[path.type];
|
||||
|
||||
if (typeVisitors) {
|
||||
for (const visit of typeVisitors.enter) {
|
||||
visit(path, state);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
path.traverse(collectorVisitor, state);
|
||||
this.crawling = false;
|
||||
|
||||
@@ -770,7 +856,9 @@ class Scope {
|
||||
push(opts) {
|
||||
let path = this.path;
|
||||
|
||||
if (!path.isBlockStatement() && !path.isProgram()) {
|
||||
if (path.isPattern()) {
|
||||
path = this.getPatternParent().path;
|
||||
} else if (!path.isBlockStatement() && !path.isProgram()) {
|
||||
path = this.getBlockParent().path;
|
||||
}
|
||||
|
||||
@@ -790,15 +878,15 @@ class Scope {
|
||||
let declarPath = !unique && path.getData(dataKey);
|
||||
|
||||
if (!declarPath) {
|
||||
const declar = t.variableDeclaration(kind, []);
|
||||
const declar = variableDeclaration(kind, []);
|
||||
declar._blockHoist = blockHoist;
|
||||
[declarPath] = path.unshiftContainer("body", [declar]);
|
||||
if (!unique) path.setData(dataKey, declarPath);
|
||||
}
|
||||
|
||||
const declarator = t.variableDeclarator(opts.id, opts.init);
|
||||
declarPath.node.declarations.push(declarator);
|
||||
this.registerBinding(kind, declarPath.get("declarations").pop());
|
||||
const declarator = variableDeclarator(opts.id, opts.init);
|
||||
const len = declarPath.node.declarations.push(declarator);
|
||||
path.scope.registerBinding(kind, declarPath.get("declarations")[len - 1]);
|
||||
}
|
||||
|
||||
getProgramParent() {
|
||||
@@ -837,22 +925,39 @@ class Scope {
|
||||
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 {
|
||||
(0, _defaults.default)(ids, scope.bindings);
|
||||
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() {
|
||||
getAllBindingsOfKind(...kinds) {
|
||||
const ids = Object.create(null);
|
||||
|
||||
for (const kind of arguments) {
|
||||
for (const kind of kinds) {
|
||||
let scope = this;
|
||||
|
||||
do {
|
||||
@@ -880,9 +985,13 @@ class Scope {
|
||||
const binding = scope.getOwnBinding(name);
|
||||
|
||||
if (binding) {
|
||||
if (previousPath && previousPath.isPattern() && previousPath.parentPath.isFunction() && binding.kind !== "param") {} else {
|
||||
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;
|
||||
@@ -913,8 +1022,8 @@ class Scope {
|
||||
if (this.hasOwnBinding(name)) return true;
|
||||
if (this.parentHasBinding(name, noGlobals)) return true;
|
||||
if (this.hasUid(name)) return true;
|
||||
if (!noGlobals && (0, _includes.default)(Scope.globals, name)) return true;
|
||||
if (!noGlobals && (0, _includes.default)(Scope.contextVariables, name)) return true;
|
||||
if (!noGlobals && Scope.globals.includes(name)) return true;
|
||||
if (!noGlobals && Scope.contextVariables.includes(name)) return true;
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -954,5 +1063,5 @@ class Scope {
|
||||
}
|
||||
|
||||
exports.default = Scope;
|
||||
Scope.globals = Object.keys(_globals.default.builtin);
|
||||
Scope.globals = Object.keys(_globals.builtin);
|
||||
Scope.contextVariables = ["arguments", "undefined", "Infinity", "NaN"];
|
||||
+32
-28
@@ -5,17 +5,13 @@ Object.defineProperty(exports, "__esModule", {
|
||||
});
|
||||
exports.default = void 0;
|
||||
|
||||
var _binding = _interopRequireDefault(require("../binding"));
|
||||
var _binding = require("../binding");
|
||||
|
||||
var _helperSplitExportDeclaration = _interopRequireDefault(require("@babel/helper-split-export-declaration"));
|
||||
var _helperSplitExportDeclaration = require("@babel/helper-split-export-declaration");
|
||||
|
||||
var t = _interopRequireWildcard(require("@babel/types"));
|
||||
var t = require("@babel/types");
|
||||
|
||||
function _getRequireWildcardCache() { if (typeof WeakMap !== "function") return null; var cache = new WeakMap(); _getRequireWildcardCache = function () { return cache; }; return cache; }
|
||||
|
||||
function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
|
||||
|
||||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
||||
var _helperEnvironmentVisitor = require("@babel/helper-environment-visitor");
|
||||
|
||||
const renameVisitor = {
|
||||
ReferencedIdentifier({
|
||||
@@ -29,6 +25,10 @@ const renameVisitor = {
|
||||
Scope(path, state) {
|
||||
if (!path.scope.bindingIdentifierEquals(state.oldName, state.binding.identifier)) {
|
||||
path.skip();
|
||||
|
||||
if (path.isMethod()) {
|
||||
(0, _helperEnvironmentVisitor.requeueComputedKeyAndDecorators)(path);
|
||||
}
|
||||
}
|
||||
},
|
||||
|
||||
@@ -57,7 +57,17 @@ class Renamer {
|
||||
return;
|
||||
}
|
||||
|
||||
if (maybeExportDeclar.isExportDefaultDeclaration() && !maybeExportDeclar.get("declaration").node.id) {
|
||||
if (maybeExportDeclar.isExportDefaultDeclaration()) {
|
||||
const {
|
||||
declaration
|
||||
} = maybeExportDeclar.node;
|
||||
|
||||
if (t.isDeclaration(declaration) && !declaration.id) {
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
if (maybeExportDeclar.isExportAllDeclaration()) {
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -65,23 +75,11 @@ class Renamer {
|
||||
}
|
||||
|
||||
maybeConvertFromClassFunctionDeclaration(path) {
|
||||
return;
|
||||
if (!path.isFunctionDeclaration() && !path.isClassDeclaration()) return;
|
||||
if (this.binding.kind !== "hoisted") return;
|
||||
path.node.id = t.identifier(this.oldName);
|
||||
path.node._blockHoist = 3;
|
||||
path.replaceWith(t.variableDeclaration("let", [t.variableDeclarator(t.identifier(this.newName), t.toExpression(path.node))]));
|
||||
return path;
|
||||
}
|
||||
|
||||
maybeConvertFromClassFunctionExpression(path) {
|
||||
return;
|
||||
if (!path.isFunctionExpression() && !path.isClassExpression()) return;
|
||||
if (this.binding.kind !== "local") return;
|
||||
path.node.id = t.identifier(this.oldName);
|
||||
this.binding.scope.parent.push({
|
||||
id: t.identifier(this.newName)
|
||||
});
|
||||
path.replaceWith(t.assignmentExpression("=", t.identifier(this.newName), path.node));
|
||||
return path;
|
||||
}
|
||||
|
||||
rename(block) {
|
||||
@@ -104,7 +102,15 @@ class Renamer {
|
||||
}
|
||||
}
|
||||
|
||||
scope.traverse(block || scope.block, renameVisitor, this);
|
||||
const blockToTraverse = block || scope.block;
|
||||
|
||||
if ((blockToTraverse == null ? void 0 : blockToTraverse.type) === "SwitchStatement") {
|
||||
blockToTraverse.cases.forEach(c => {
|
||||
scope.traverse(c, renameVisitor, this);
|
||||
});
|
||||
} else {
|
||||
scope.traverse(blockToTraverse, renameVisitor, this);
|
||||
}
|
||||
|
||||
if (!block) {
|
||||
scope.removeOwnBinding(oldName);
|
||||
@@ -112,11 +118,9 @@ class Renamer {
|
||||
this.binding.identifier.name = newName;
|
||||
}
|
||||
|
||||
if (binding.type === "hoisted") {}
|
||||
|
||||
if (parentDeclar) {
|
||||
this.maybeConvertFromClassFunctionDeclaration(parentDeclar);
|
||||
this.maybeConvertFromClassFunctionExpression(parentDeclar);
|
||||
this.maybeConvertFromClassFunctionDeclaration(path);
|
||||
this.maybeConvertFromClassFunctionExpression(path);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user