feat: refactoring project

This commit is contained in:
Carlos
2024-11-23 14:56:07 -05:00
parent f0c2a50c18
commit 1c6db5818d
2351 changed files with 39323 additions and 60326 deletions

View File

@@ -271,12 +271,12 @@ const collectorVisitor = {
for (const param of params) {
path.scope.registerBinding("param", param);
}
if (path.isFunctionExpression() && path.has("id") && !path.get("id").node[NOT_LOCAL_BINDING]) {
if (path.isFunctionExpression() && path.node.id && !path.node.id[NOT_LOCAL_BINDING]) {
path.scope.registerBinding("local", path.get("id"), path);
}
},
ClassExpression(path) {
if (path.has("id") && !path.get("id").node[NOT_LOCAL_BINDING]) {
if (path.node.id && !path.node.id[NOT_LOCAL_BINDING]) {
path.scope.registerBinding("local", path.get("id"), path);
}
},
@@ -290,8 +290,8 @@ class Scope {
this.uid = void 0;
this.path = void 0;
this.block = void 0;
this.labels = void 0;
this.inited = void 0;
this.labels = void 0;
this.bindings = void 0;
this.references = void 0;
this.globals = void 0;
@@ -325,15 +325,6 @@ class Scope {
} 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({
@@ -349,7 +340,8 @@ class Scope {
let uid;
let i = 1;
do {
uid = this._generateUid(name, i);
uid = `_${name}`;
if (i > 1) uid += i;
i++;
} while (this.hasLabel(uid) || this.hasBinding(uid) || this.hasGlobal(uid) || this.hasReference(uid));
const program = this.getProgramParent();
@@ -357,11 +349,6 @@ class Scope {
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);
@@ -405,7 +392,7 @@ class Scope {
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);
throw this.path.hub.buildError(id, `Duplicate declaration "${name}"`, TypeError);
}
}
rename(oldName, newName) {
@@ -418,12 +405,6 @@ class Scope {
}
}
}
_renameFromMap(map, oldName, newName, value) {
if (map[oldName]) {
map[newName] = value;
map[oldName] = null;
}
}
dump() {
const sep = "-".repeat(60);
console.log(sep);
@@ -442,37 +423,6 @@ class Scope {
} 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);
}
@@ -812,20 +762,6 @@ class Scope {
} 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;
}
@@ -958,5 +894,82 @@ class Scope {
exports.default = Scope;
Scope.globals = Object.keys(_globals.builtin);
Scope.contextVariables = ["arguments", "undefined", "Infinity", "NaN"];
{
Scope.prototype._renameFromMap = function _renameFromMap(map, oldName, newName, value) {
if (map[oldName]) {
map[newName] = value;
map[oldName] = null;
}
};
Scope.prototype.traverse = function (node, opts, state) {
(0, _index.default)(node, opts, this, state, this.path);
};
Scope.prototype._generateUid = function _generateUid(name, i) {
let id = name;
if (i > 1) id += i;
return `_${id}`;
};
Scope.prototype.toArray = function 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.path.hub.addHelper(helperName));
helperName = "maybeArrayLike";
}
return callExpression(this.path.hub.addHelper(helperName), args);
};
Scope.prototype.getAllBindingsOfKind = function 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;
};
Object.defineProperties(Scope.prototype, {
parentBlock: {
configurable: true,
enumerable: true,
get() {
return this.path.parent;
}
},
hub: {
configurable: true,
enumerable: true,
get() {
return this.path.hub;
}
}
});
}
//# sourceMappingURL=index.js.map

File diff suppressed because one or more lines are too long

View File

@@ -5,9 +5,13 @@ Object.defineProperty(exports, "__esModule", {
});
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
@@ -45,7 +49,7 @@ const renameVisitor = {
},
"AssignmentExpression|Declaration|VariableDeclarator"(path, state) {
if (path.isVariableDeclaration()) return;
const ids = path.isAssignmentExpression() ? path.getAssignmentIdentifiers() : path.getOuterBindingIdentifiers();
const ids = path.isAssignmentExpression() ? getAssignmentIdentifiers(path.node) : path.getOuterBindingIdentifiers();
for (const name in ids) {
if (name === state.oldName) ids[name].name = state.newName;
}
@@ -99,9 +103,18 @@ class Renamer {
}
}
const blockToTraverse = arguments[0] || scope.block;
(0, _traverseNode.traverseNode)(blockToTraverse, (0, _visitors.explode)(renameVisitor), scope, this, scope.path, {
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;

File diff suppressed because one or more lines are too long