This commit is contained in:
2022-07-18 02:50:52 +00:00
parent befd344ab0
commit 06181b34d6
8569 changed files with 818704 additions and 352705 deletions
+51 -30
View File
@@ -5,19 +5,35 @@ Object.defineProperty(exports, "__esModule", {
});
exports.default = _default;
var _helperGetFunctionArity = _interopRequireDefault(require("@babel/helper-get-function-arity"));
var _template = require("@babel/template");
var _template = _interopRequireDefault(require("@babel/template"));
var _t = require("@babel/types");
var t = _interopRequireWildcard(require("@babel/types"));
const {
NOT_LOCAL_BINDING,
cloneNode,
identifier,
isAssignmentExpression,
isAssignmentPattern,
isFunction,
isIdentifier,
isLiteral,
isNullLiteral,
isObjectMethod,
isObjectProperty,
isRegExpLiteral,
isRestElement,
isTemplateLiteral,
isVariableDeclarator,
toBindingIdentifierName
} = _t;
function _getRequireWildcardCache() { if (typeof WeakMap !== "function") return null; var cache = new WeakMap(); _getRequireWildcardCache = function () { return cache; }; return cache; }
function getFunctionArity(node) {
const count = node.params.findIndex(param => isAssignmentPattern(param) || isRestElement(param));
return count === -1 ? node.params.length : count;
}
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 buildPropertyMethodAssignmentWrapper = (0, _template.default)(`
const buildPropertyMethodAssignmentWrapper = _template.default.statement(`
(function (FUNCTION_KEY) {
function FUNCTION_ID() {
return FUNCTION_KEY.apply(this, arguments);
@@ -30,7 +46,8 @@ const buildPropertyMethodAssignmentWrapper = (0, _template.default)(`
return FUNCTION_ID;
})(FUNCTION)
`);
const buildGeneratorPropertyMethodAssignmentWrapper = (0, _template.default)(`
const buildGeneratorPropertyMethodAssignmentWrapper = _template.default.statement(`
(function (FUNCTION_KEY) {
function* FUNCTION_ID() {
return yield* FUNCTION_KEY.apply(this, arguments);
@@ -43,6 +60,7 @@ const buildGeneratorPropertyMethodAssignmentWrapper = (0, _template.default)(`
return FUNCTION_ID;
})(FUNCTION)
`);
const visitor = {
"ReferencedIdentifier|BindingIdentifier"(path, state) {
if (path.node.name !== state.name) return;
@@ -55,15 +73,15 @@ const visitor = {
};
function getNameFromLiteralId(id) {
if (t.isNullLiteral(id)) {
if (isNullLiteral(id)) {
return "null";
}
if (t.isRegExpLiteral(id)) {
if (isRegExpLiteral(id)) {
return `_${id.pattern}_${id.flags}`;
}
if (t.isTemplateLiteral(id)) {
if (isTemplateLiteral(id)) {
return id.quasis.map(quasi => quasi.value.raw).join("");
}
@@ -79,7 +97,7 @@ function wrap(state, method, id, scope) {
if (scope.hasBinding(id.name) && !scope.hasGlobal(id.name)) {
scope.rename(id.name);
} else {
if (!t.isFunction(method)) return;
if (!isFunction(method)) return;
let build = buildPropertyMethodAssignmentWrapper;
if (method.generator) {
@@ -93,7 +111,7 @@ function wrap(state, method, id, scope) {
}).expression;
const params = template.callee.body.body[0].params;
for (let i = 0, len = (0, _helperGetFunctionArity.default)(method); i < len; i++) {
for (let i = 0, len = getFunctionArity(method); i < len; i++) {
params.push(scope.generateUidIdentifier("x"));
}
@@ -110,7 +128,6 @@ function visit(node, name, scope) {
selfAssignment: false,
selfReference: false,
outerDeclar: scope.getBindingIdentifier(name),
references: [],
name: name
};
const binding = scope.getOwnBinding(name);
@@ -131,26 +148,26 @@ function _default({
parent,
scope,
id
}, localBinding = false) {
}, localBinding = false, supportUnicodeId = false) {
if (node.id) return;
if ((t.isObjectProperty(parent) || t.isObjectMethod(parent, {
if ((isObjectProperty(parent) || isObjectMethod(parent, {
kind: "method"
})) && (!parent.computed || t.isLiteral(parent.key))) {
})) && (!parent.computed || isLiteral(parent.key))) {
id = parent.key;
} else if (t.isVariableDeclarator(parent)) {
} else if (isVariableDeclarator(parent)) {
id = parent.id;
if (t.isIdentifier(id) && !localBinding) {
if (isIdentifier(id) && !localBinding) {
const binding = scope.parent.getBinding(id.name);
if (binding && binding.constant && scope.getBinding(id.name) === binding) {
node.id = t.cloneNode(id);
node.id[t.NOT_LOCAL_BINDING] = true;
node.id = cloneNode(id);
node.id[NOT_LOCAL_BINDING] = true;
return;
}
}
} else if (t.isAssignmentExpression(parent, {
} else if (isAssignmentExpression(parent, {
operator: "="
})) {
id = parent.left;
@@ -160,9 +177,9 @@ function _default({
let name;
if (id && t.isLiteral(id)) {
if (id && isLiteral(id)) {
name = getNameFromLiteralId(id);
} else if (id && t.isIdentifier(id)) {
} else if (id && isIdentifier(id)) {
name = id.name;
}
@@ -170,9 +187,13 @@ function _default({
return;
}
name = t.toBindingIdentifierName(name);
id = t.identifier(name);
id[t.NOT_LOCAL_BINDING] = true;
if (!supportUnicodeId && isFunction(node) && /[\uD800-\uDFFF]/.test(name)) {
return;
}
name = toBindingIdentifierName(name);
const newId = identifier(name);
newId[NOT_LOCAL_BINDING] = true;
const state = visit(node, name, scope);
return wrap(state, node, id, scope) || node;
return wrap(state, node, newId, scope) || node;
}