Befor generating
This commit is contained in:
1 parent
3383f4bf4a
commit
1aa1b5f625
6756 files changed
+649946
-1
No files matched your search
Generated
Vendored
+245
@@ -0,0 +1,245 @@
|
||||
'use strict';
|
||||
|
||||
Object.defineProperty(exports, '__esModule', { value: true });
|
||||
|
||||
var helperPluginUtils = require('@babel/helper-plugin-utils');
|
||||
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.isForStatement({
|
||||
test: node
|
||||
}) || parentPath.isWhile({
|
||||
test: node
|
||||
});
|
||||
}
|
||||
function findOutermostTransparentParent(path) {
|
||||
let maybeWrapped = path;
|
||||
path.findParent(p => {
|
||||
if (!helperSkipTransparentExpressionWrappers.isTransparentExprWrapper(p.node)) return true;
|
||||
maybeWrapped = p;
|
||||
});
|
||||
return maybeWrapped;
|
||||
}
|
||||
|
||||
const last = arr => arr[arr.length - 1];
|
||||
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);
|
||||
if (isCall) {
|
||||
node.callee = ref;
|
||||
} else {
|
||||
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 ifNullishVoid = !ifNullishBoolean && core.types.isUnaryExpression(ifNullish, {
|
||||
operator: "void"
|
||||
});
|
||||
const isEvaluationValueIgnored = core.types.isExpressionStatement(replacementPath.parent) && !replacementPath.isCompletionRecord() || core.types.isSequenceExpression(replacementPath.parent) && last(replacementPath.parent.expressions) !== replacementPath.node;
|
||||
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 || ifNullishVoid && isEvaluationValueIgnored ? 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 => {
|
||||
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 != 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.0.0-0 || ^8.0.0-0 || >8.0.0-alpha <8.0.0-beta");
|
||||
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",
|
||||
manipulateOptions: (_, parser) => parser.plugins.push("optionalChaining"),
|
||||
visitor: {
|
||||
"OptionalCallExpression|OptionalMemberExpression"(path) {
|
||||
transform(path, {
|
||||
noDocumentAll,
|
||||
pureGetters
|
||||
});
|
||||
}
|
||||
}
|
||||
};
|
||||
});
|
||||
|
||||
exports.default = index;
|
||||
exports.transform = transform;
|
||||
exports.transformOptionalChain = transformOptionalChain;
|
||||
//# sourceMappingURL=index.js.map
|
||||
Generated
Vendored
+1
@@ -0,0 +1 @@
|
||||
{"version":3,"file":"index.js","sources":["../src/util.ts","../src/transform.ts","../src/index.ts"],"sourcesContent":["import type { NodePath } from \"@babel/core\";\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.isForStatement({ test: node }) ||\n parentPath.isWhile({ 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","import { types as t, template, type NodePath } from \"@babel/core\";\nimport {\n skipTransparentExprWrapperNodes,\n skipTransparentExprWrappers,\n} from \"@babel/helper-skip-transparent-expression-wrappers\";\nimport {\n willPathCastToBoolean,\n findOutermostTransparentParent,\n} from \"./util.ts\";\n\n// TODO(Babel 9): Use .at(-1)\nconst last = <T>(arr: T[]) => arr[arr.length - 1];\n\nfunction isSimpleMemberExpression(\n expression: t.Expression | t.Super,\n): expression is t.Identifier | t.Super | t.MemberExpression {\n expression = skipTransparentExprWrapperNodes(expression);\n return (\n t.isIdentifier(expression) ||\n t.isSuper(expression) ||\n (t.isMemberExpression(expression) &&\n !expression.computed &&\n isSimpleMemberExpression(expression.object))\n );\n}\n\n/**\n * Test if a given optional chain `path` needs to be memoized\n * @param {NodePath} path\n * @returns {boolean}\n */\nfunction needsMemoize(\n path: NodePath<t.OptionalCallExpression | t.OptionalMemberExpression>,\n) {\n let optionalPath: NodePath = path;\n const { scope } = path;\n while (\n optionalPath.isOptionalMemberExpression() ||\n optionalPath.isOptionalCallExpression()\n ) {\n const { node } = optionalPath;\n const childPath = skipTransparentExprWrappers(\n optionalPath.isOptionalMemberExpression()\n ? optionalPath.get(\"object\")\n : optionalPath.get(\"callee\"),\n );\n if (node.optional) {\n return !scope.isStatic(childPath.node);\n }\n\n optionalPath = childPath;\n }\n}\n\nconst NULLISH_CHECK = template.expression(\n `%%check%% === null || %%ref%% === void 0`,\n);\nconst NULLISH_CHECK_NO_DDA = template.expression(`%%check%% == null`);\nconst NULLISH_CHECK_NEG = template.expression(\n `%%check%% !== null && %%ref%% !== void 0`,\n);\nconst NULLISH_CHECK_NO_DDA_NEG = template.expression(`%%check%% != null`);\n\ninterface OptionalChainAssumptions {\n pureGetters: boolean;\n noDocumentAll: boolean;\n}\n\nexport function transformOptionalChain(\n path: NodePath<t.OptionalCallExpression | t.OptionalMemberExpression>,\n { pureGetters, noDocumentAll }: OptionalChainAssumptions,\n replacementPath: NodePath<t.Expression>,\n ifNullish: t.Expression,\n wrapLast?: (value: t.Expression) => t.Expression,\n) {\n const { scope } = path;\n\n // Replace `function (a, x = a.b?.c) {}` to `function (a, x = (() => a.b?.c)() ){}`\n // so the temporary variable can be injected in correct scope\n if (scope.path.isPattern() && needsMemoize(path)) {\n replacementPath.replaceWith(\n template.eLine truncated
|
||||
Reference in new issue
Block a user