This commit is contained in:
2023-08-11 10:45:20 +08:00
commit 161ca982f3
31850 changed files with 2706500 additions and 0 deletions

View File

@ -0,0 +1,235 @@
'use strict';
Object.defineProperty(exports, '__esModule', { value: true });
var helperPluginUtils = require('@babel/helper-plugin-utils');
var syntaxOptionalChaining = require('@babel/plugin-syntax-optional-chaining');
var core = require('@babel/core');
var helperSkipTransparentExpressionWrappers = require('@babel/helper-skip-transparent-expression-wrappers');
function willPathCastToBoolean(path) {
const maybeWrapped = findOutermostTransparentParent(path);
const {
node,
parentPath
} = maybeWrapped;
if (parentPath.isLogicalExpression()) {
const {
operator,
right
} = parentPath.node;
if (operator === "&&" || operator === "||" || operator === "??" && node === right) {
return willPathCastToBoolean(parentPath);
}
}
if (parentPath.isSequenceExpression()) {
const {
expressions
} = parentPath.node;
if (expressions[expressions.length - 1] === node) {
return willPathCastToBoolean(parentPath);
} else {
return true;
}
}
return parentPath.isConditional({
test: node
}) || parentPath.isUnaryExpression({
operator: "!"
}) || parentPath.isLoop({
test: node
});
}
function findOutermostTransparentParent(path) {
let maybeWrapped = path;
path.findParent(p => {
if (!helperSkipTransparentExpressionWrappers.isTransparentExprWrapper(p.node)) return true;
maybeWrapped = p;
});
return maybeWrapped;
}
function isSimpleMemberExpression(expression) {
expression = helperSkipTransparentExpressionWrappers.skipTransparentExprWrapperNodes(expression);
return core.types.isIdentifier(expression) || core.types.isSuper(expression) || core.types.isMemberExpression(expression) && !expression.computed && isSimpleMemberExpression(expression.object);
}
function needsMemoize(path) {
let optionalPath = path;
const {
scope
} = path;
while (optionalPath.isOptionalMemberExpression() || optionalPath.isOptionalCallExpression()) {
const {
node
} = optionalPath;
const childPath = helperSkipTransparentExpressionWrappers.skipTransparentExprWrappers(optionalPath.isOptionalMemberExpression() ? optionalPath.get("object") : optionalPath.get("callee"));
if (node.optional) {
return !scope.isStatic(childPath.node);
}
optionalPath = childPath;
}
}
const NULLISH_CHECK = core.template.expression(`%%check%% === null || %%ref%% === void 0`);
const NULLISH_CHECK_NO_DDA = core.template.expression(`%%check%% == null`);
const NULLISH_CHECK_NEG = core.template.expression(`%%check%% !== null && %%ref%% !== void 0`);
const NULLISH_CHECK_NO_DDA_NEG = core.template.expression(`%%check%% != null`);
function transformOptionalChain(path, {
pureGetters,
noDocumentAll
}, replacementPath, ifNullish, wrapLast) {
const {
scope
} = path;
if (scope.path.isPattern() && needsMemoize(path)) {
replacementPath.replaceWith(core.template.expression.ast`(() => ${replacementPath.node})()`);
return;
}
const optionals = [];
let optionalPath = path;
while (optionalPath.isOptionalMemberExpression() || optionalPath.isOptionalCallExpression()) {
const {
node
} = optionalPath;
if (node.optional) {
optionals.push(node);
}
if (optionalPath.isOptionalMemberExpression()) {
optionalPath.node.type = "MemberExpression";
optionalPath = helperSkipTransparentExpressionWrappers.skipTransparentExprWrappers(optionalPath.get("object"));
} else if (optionalPath.isOptionalCallExpression()) {
optionalPath.node.type = "CallExpression";
optionalPath = helperSkipTransparentExpressionWrappers.skipTransparentExprWrappers(optionalPath.get("callee"));
}
}
if (optionals.length === 0) {
return;
}
const checks = [];
let tmpVar;
for (let i = optionals.length - 1; i >= 0; i--) {
const node = optionals[i];
const isCall = core.types.isCallExpression(node);
const chainWithTypes = isCall ? node.callee : node.object;
const chain = helperSkipTransparentExpressionWrappers.skipTransparentExprWrapperNodes(chainWithTypes);
let ref;
let check;
if (isCall && core.types.isIdentifier(chain, {
name: "eval"
})) {
check = ref = chain;
node.callee = core.types.sequenceExpression([core.types.numericLiteral(0), ref]);
} else if (pureGetters && isCall && isSimpleMemberExpression(chain)) {
check = ref = node.callee;
} else if (scope.isStatic(chain)) {
check = ref = chainWithTypes;
} else {
if (!tmpVar || isCall) {
tmpVar = scope.generateUidIdentifierBasedOnNode(chain);
scope.push({
id: core.types.cloneNode(tmpVar)
});
}
ref = tmpVar;
check = core.types.assignmentExpression("=", core.types.cloneNode(tmpVar), chainWithTypes);
isCall ? node.callee = ref : node.object = ref;
}
if (isCall && core.types.isMemberExpression(chain)) {
if (pureGetters && isSimpleMemberExpression(chain)) {
node.callee = chainWithTypes;
} else {
const {
object
} = chain;
let context;
if (core.types.isSuper(object)) {
context = core.types.thisExpression();
} else {
const memoized = scope.maybeGenerateMemoised(object);
if (memoized) {
context = memoized;
chain.object = core.types.assignmentExpression("=", memoized, object);
} else {
context = object;
}
}
node.arguments.unshift(core.types.cloneNode(context));
node.callee = core.types.memberExpression(node.callee, core.types.identifier("call"));
}
}
const data = {
check: core.types.cloneNode(check),
ref: core.types.cloneNode(ref)
};
Object.defineProperty(data, "ref", {
enumerable: false
});
checks.push(data);
}
let result = replacementPath.node;
if (wrapLast) result = wrapLast(result);
const ifNullishBoolean = core.types.isBooleanLiteral(ifNullish);
const ifNullishFalse = ifNullishBoolean && ifNullish.value === false;
const tpl = ifNullishFalse ? noDocumentAll ? NULLISH_CHECK_NO_DDA_NEG : NULLISH_CHECK_NEG : noDocumentAll ? NULLISH_CHECK_NO_DDA : NULLISH_CHECK;
const logicalOp = ifNullishFalse ? "&&" : "||";
const check = checks.map(tpl).reduce((expr, check) => core.types.logicalExpression(logicalOp, expr, check));
replacementPath.replaceWith(ifNullishBoolean ? core.types.logicalExpression(logicalOp, check, result) : core.types.conditionalExpression(check, ifNullish, result));
}
function transform(path, assumptions) {
const {
scope
} = path;
const maybeWrapped = findOutermostTransparentParent(path);
const {
parentPath
} = maybeWrapped;
if (parentPath.isUnaryExpression({
operator: "delete"
})) {
transformOptionalChain(path, assumptions, parentPath, core.types.booleanLiteral(true));
} else {
let wrapLast;
if (parentPath.isCallExpression({
callee: maybeWrapped.node
}) && path.isOptionalMemberExpression()) {
wrapLast = replacement => {
var _baseRef;
const object = helperSkipTransparentExpressionWrappers.skipTransparentExprWrapperNodes(replacement.object);
let baseRef;
if (!assumptions.pureGetters || !isSimpleMemberExpression(object)) {
baseRef = scope.maybeGenerateMemoised(object);
if (baseRef) {
replacement.object = core.types.assignmentExpression("=", baseRef, object);
}
}
return core.types.callExpression(core.types.memberExpression(replacement, core.types.identifier("bind")), [core.types.cloneNode((_baseRef = baseRef) != null ? _baseRef : object)]);
};
}
transformOptionalChain(path, assumptions, path, willPathCastToBoolean(maybeWrapped) ? core.types.booleanLiteral(false) : scope.buildUndefinedNode(), wrapLast);
}
}
var index = helperPluginUtils.declare((api, options) => {
var _api$assumption, _api$assumption2;
api.assertVersion(7);
const {
loose = false
} = options;
const noDocumentAll = (_api$assumption = api.assumption("noDocumentAll")) != null ? _api$assumption : loose;
const pureGetters = (_api$assumption2 = api.assumption("pureGetters")) != null ? _api$assumption2 : loose;
return {
name: "transform-optional-chaining",
inherits: syntaxOptionalChaining.default,
visitor: {
"OptionalCallExpression|OptionalMemberExpression"(path) {
transform(path, {
noDocumentAll,
pureGetters
});
}
}
};
});
exports["default"] = index;
exports.transform = transform;
//# sourceMappingURL=index.js.map

File diff suppressed because one or more lines are too long

View File

@ -0,0 +1,170 @@
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.transform = transform;
exports.transformOptionalChain = transformOptionalChain;
var _core = require("@babel/core");
var _helperSkipTransparentExpressionWrappers = require("@babel/helper-skip-transparent-expression-wrappers");
var _util = require("./util");
function isSimpleMemberExpression(expression) {
expression = (0, _helperSkipTransparentExpressionWrappers.skipTransparentExprWrapperNodes)(expression);
return _core.types.isIdentifier(expression) || _core.types.isSuper(expression) || _core.types.isMemberExpression(expression) && !expression.computed && isSimpleMemberExpression(expression.object);
}
function needsMemoize(path) {
let optionalPath = path;
const {
scope
} = path;
while (optionalPath.isOptionalMemberExpression() || optionalPath.isOptionalCallExpression()) {
const {
node
} = optionalPath;
const childPath = (0, _helperSkipTransparentExpressionWrappers.skipTransparentExprWrappers)(optionalPath.isOptionalMemberExpression() ? optionalPath.get("object") : optionalPath.get("callee"));
if (node.optional) {
return !scope.isStatic(childPath.node);
}
optionalPath = childPath;
}
}
const NULLISH_CHECK = _core.template.expression(`%%check%% === null || %%ref%% === void 0`);
const NULLISH_CHECK_NO_DDA = _core.template.expression(`%%check%% == null`);
const NULLISH_CHECK_NEG = _core.template.expression(`%%check%% !== null && %%ref%% !== void 0`);
const NULLISH_CHECK_NO_DDA_NEG = _core.template.expression(`%%check%% != null`);
function transformOptionalChain(path, {
pureGetters,
noDocumentAll
}, replacementPath, ifNullish, wrapLast) {
const {
scope
} = path;
if (scope.path.isPattern() && needsMemoize(path)) {
replacementPath.replaceWith(_core.template.expression.ast`(() => ${replacementPath.node})()`);
return;
}
const optionals = [];
let optionalPath = path;
while (optionalPath.isOptionalMemberExpression() || optionalPath.isOptionalCallExpression()) {
const {
node
} = optionalPath;
if (node.optional) {
optionals.push(node);
}
if (optionalPath.isOptionalMemberExpression()) {
optionalPath.node.type = "MemberExpression";
optionalPath = (0, _helperSkipTransparentExpressionWrappers.skipTransparentExprWrappers)(optionalPath.get("object"));
} else if (optionalPath.isOptionalCallExpression()) {
optionalPath.node.type = "CallExpression";
optionalPath = (0, _helperSkipTransparentExpressionWrappers.skipTransparentExprWrappers)(optionalPath.get("callee"));
}
}
if (optionals.length === 0) {
return;
}
const checks = [];
let tmpVar;
for (let i = optionals.length - 1; i >= 0; i--) {
const node = optionals[i];
const isCall = _core.types.isCallExpression(node);
const chainWithTypes = isCall ? node.callee : node.object;
const chain = (0, _helperSkipTransparentExpressionWrappers.skipTransparentExprWrapperNodes)(chainWithTypes);
let ref;
let check;
if (isCall && _core.types.isIdentifier(chain, {
name: "eval"
})) {
check = ref = chain;
node.callee = _core.types.sequenceExpression([_core.types.numericLiteral(0), ref]);
} else if (pureGetters && isCall && isSimpleMemberExpression(chain)) {
check = ref = node.callee;
} else if (scope.isStatic(chain)) {
check = ref = chainWithTypes;
} else {
if (!tmpVar || isCall) {
tmpVar = scope.generateUidIdentifierBasedOnNode(chain);
scope.push({
id: _core.types.cloneNode(tmpVar)
});
}
ref = tmpVar;
check = _core.types.assignmentExpression("=", _core.types.cloneNode(tmpVar), chainWithTypes);
isCall ? node.callee = ref : node.object = ref;
}
if (isCall && _core.types.isMemberExpression(chain)) {
if (pureGetters && isSimpleMemberExpression(chain)) {
node.callee = chainWithTypes;
} else {
const {
object
} = chain;
let context;
if (_core.types.isSuper(object)) {
context = _core.types.thisExpression();
} else {
const memoized = scope.maybeGenerateMemoised(object);
if (memoized) {
context = memoized;
chain.object = _core.types.assignmentExpression("=", memoized, object);
} else {
context = object;
}
}
node.arguments.unshift(_core.types.cloneNode(context));
node.callee = _core.types.memberExpression(node.callee, _core.types.identifier("call"));
}
}
const data = {
check: _core.types.cloneNode(check),
ref: _core.types.cloneNode(ref)
};
Object.defineProperty(data, "ref", {
enumerable: false
});
checks.push(data);
}
let result = replacementPath.node;
if (wrapLast) result = wrapLast(result);
const ifNullishBoolean = _core.types.isBooleanLiteral(ifNullish);
const ifNullishFalse = ifNullishBoolean && ifNullish.value === false;
const tpl = ifNullishFalse ? noDocumentAll ? NULLISH_CHECK_NO_DDA_NEG : NULLISH_CHECK_NEG : noDocumentAll ? NULLISH_CHECK_NO_DDA : NULLISH_CHECK;
const logicalOp = ifNullishFalse ? "&&" : "||";
const check = checks.map(tpl).reduce((expr, check) => _core.types.logicalExpression(logicalOp, expr, check));
replacementPath.replaceWith(ifNullishBoolean ? _core.types.logicalExpression(logicalOp, check, result) : _core.types.conditionalExpression(check, ifNullish, result));
}
function transform(path, assumptions) {
const {
scope
} = path;
const maybeWrapped = (0, _util.findOutermostTransparentParent)(path);
const {
parentPath
} = maybeWrapped;
if (parentPath.isUnaryExpression({
operator: "delete"
})) {
transformOptionalChain(path, assumptions, parentPath, _core.types.booleanLiteral(true));
} else {
let wrapLast;
if (parentPath.isCallExpression({
callee: maybeWrapped.node
}) && path.isOptionalMemberExpression()) {
wrapLast = replacement => {
var _baseRef;
const object = (0, _helperSkipTransparentExpressionWrappers.skipTransparentExprWrapperNodes)(replacement.object);
let baseRef;
if (!assumptions.pureGetters || !isSimpleMemberExpression(object)) {
baseRef = scope.maybeGenerateMemoised(object);
if (baseRef) {
replacement.object = _core.types.assignmentExpression("=", baseRef, object);
}
}
return _core.types.callExpression(_core.types.memberExpression(replacement, _core.types.identifier("bind")), [_core.types.cloneNode((_baseRef = baseRef) != null ? _baseRef : object)]);
};
}
transformOptionalChain(path, assumptions, path, (0, _util.willPathCastToBoolean)(maybeWrapped) ? _core.types.booleanLiteral(false) : scope.buildUndefinedNode(), wrapLast);
}
}
//# sourceMappingURL=transform.js.map

File diff suppressed because one or more lines are too long

View File

@ -0,0 +1,51 @@
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.findOutermostTransparentParent = findOutermostTransparentParent;
exports.willPathCastToBoolean = willPathCastToBoolean;
var _helperSkipTransparentExpressionWrappers = require("@babel/helper-skip-transparent-expression-wrappers");
function willPathCastToBoolean(path) {
const maybeWrapped = findOutermostTransparentParent(path);
const {
node,
parentPath
} = maybeWrapped;
if (parentPath.isLogicalExpression()) {
const {
operator,
right
} = parentPath.node;
if (operator === "&&" || operator === "||" || operator === "??" && node === right) {
return willPathCastToBoolean(parentPath);
}
}
if (parentPath.isSequenceExpression()) {
const {
expressions
} = parentPath.node;
if (expressions[expressions.length - 1] === node) {
return willPathCastToBoolean(parentPath);
} else {
return true;
}
}
return parentPath.isConditional({
test: node
}) || parentPath.isUnaryExpression({
operator: "!"
}) || parentPath.isLoop({
test: node
});
}
function findOutermostTransparentParent(path) {
let maybeWrapped = path;
path.findParent(p => {
if (!(0, _helperSkipTransparentExpressionWrappers.isTransparentExprWrapper)(p.node)) return true;
maybeWrapped = p;
});
return maybeWrapped;
}
//# sourceMappingURL=util.js.map

View File

@ -0,0 +1 @@
{"version":3,"names":["_helperSkipTransparentExpressionWrappers","require","willPathCastToBoolean","path","maybeWrapped","findOutermostTransparentParent","node","parentPath","isLogicalExpression","operator","right","isSequenceExpression","expressions","length","isConditional","test","isUnaryExpression","isLoop","findParent","p","isTransparentExprWrapper"],"sources":["../src/util.ts"],"sourcesContent":["import type { NodePath } from \"@babel/traverse\";\nimport { isTransparentExprWrapper } from \"@babel/helper-skip-transparent-expression-wrappers\";\n/**\n * Test if a NodePath will be cast to boolean when evaluated.\n * It respects transparent expression wrappers defined in\n * \"@babel/helper-skip-transparent-expression-wrappers\"\n *\n * @example\n * // returns true\n * const nodePathADotB = NodePath(\"if (a.b) {}\").get(\"test\"); // a.b\n * willPathCastToBoolean(nodePathADotB)\n * @example\n * // returns false\n * willPathCastToBoolean(NodePath(\"a.b\"))\n * @param {NodePath} path\n * @returns {boolean}\n */\nexport function willPathCastToBoolean(path: NodePath): boolean {\n const maybeWrapped = findOutermostTransparentParent(path);\n const { node, parentPath } = maybeWrapped;\n if (parentPath.isLogicalExpression()) {\n const { operator, right } = parentPath.node;\n if (\n operator === \"&&\" ||\n operator === \"||\" ||\n (operator === \"??\" && node === right)\n ) {\n return willPathCastToBoolean(parentPath);\n }\n }\n if (parentPath.isSequenceExpression()) {\n const { expressions } = parentPath.node;\n if (expressions[expressions.length - 1] === node) {\n return willPathCastToBoolean(parentPath);\n } else {\n // if it is in the middle of a sequence expression, we don't\n // care the return value so just cast to boolean for smaller\n // output\n return true;\n }\n }\n return (\n parentPath.isConditional({ test: node }) ||\n parentPath.isUnaryExpression({ operator: \"!\" }) ||\n parentPath.isLoop({ test: node })\n );\n}\n\n/**\n * Return the outermost transparent expression wrapper of a given path,\n * otherwise returns path itself.\n * @example\n * const nodePathADotB = NodePath(\"(a.b as any)\").get(\"expression\"); // a.b\n * // returns NodePath(\"(a.b as any)\")\n * findOutermostTransparentParent(nodePathADotB);\n * @param {NodePath} path\n * @returns {NodePath}\n */\nexport function findOutermostTransparentParent(path: NodePath): NodePath {\n let maybeWrapped = path;\n path.findParent(p => {\n if (!isTransparentExprWrapper(p.node)) return true;\n maybeWrapped = p;\n });\n return maybeWrapped;\n}\n"],"mappings":";;;;;;;AACA,IAAAA,wCAAA,GAAAC,OAAA;AAgBO,SAASC,qBAAqBA,CAACC,IAAc,EAAW;EAC7D,MAAMC,YAAY,GAAGC,8BAA8B,CAACF,IAAI,CAAC;EACzD,MAAM;IAAEG,IAAI;IAAEC;EAAW,CAAC,GAAGH,YAAY;EACzC,IAAIG,UAAU,CAACC,mBAAmB,CAAC,CAAC,EAAE;IACpC,MAAM;MAAEC,QAAQ;MAAEC;IAAM,CAAC,GAAGH,UAAU,CAACD,IAAI;IAC3C,IACEG,QAAQ,KAAK,IAAI,IACjBA,QAAQ,KAAK,IAAI,IAChBA,QAAQ,KAAK,IAAI,IAAIH,IAAI,KAAKI,KAAM,EACrC;MACA,OAAOR,qBAAqB,CAACK,UAAU,CAAC;IAC1C;EACF;EACA,IAAIA,UAAU,CAACI,oBAAoB,CAAC,CAAC,EAAE;IACrC,MAAM;MAAEC;IAAY,CAAC,GAAGL,UAAU,CAACD,IAAI;IACvC,IAAIM,WAAW,CAACA,WAAW,CAACC,MAAM,GAAG,CAAC,CAAC,KAAKP,IAAI,EAAE;MAChD,OAAOJ,qBAAqB,CAACK,UAAU,CAAC;IAC1C,CAAC,MAAM;MAIL,OAAO,IAAI;IACb;EACF;EACA,OACEA,UAAU,CAACO,aAAa,CAAC;IAAEC,IAAI,EAAET;EAAK,CAAC,CAAC,IACxCC,UAAU,CAACS,iBAAiB,CAAC;IAAEP,QAAQ,EAAE;EAAI,CAAC,CAAC,IAC/CF,UAAU,CAACU,MAAM,CAAC;IAAEF,IAAI,EAAET;EAAK,CAAC,CAAC;AAErC;AAYO,SAASD,8BAA8BA,CAACF,IAAc,EAAY;EACvE,IAAIC,YAAY,GAAGD,IAAI;EACvBA,IAAI,CAACe,UAAU,CAACC,CAAC,IAAI;IACnB,IAAI,CAAC,IAAAC,iEAAwB,EAACD,CAAC,CAACb,IAAI,CAAC,EAAE,OAAO,IAAI;IAClDF,YAAY,GAAGe,CAAC;EAClB,CAAC,CAAC;EACF,OAAOf,YAAY;AACrB"}