@babel/plugin-transform-optional-chaining 7.22.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +22 -0
- package/README.md +19 -0
- package/lib/index.js +217 -0
- package/lib/index.js.map +1 -0
- package/lib/package.json +1 -0
- package/lib/transform.js +151 -0
- package/lib/transform.js.map +1 -0
- package/lib/util.js +51 -0
- package/lib/util.js.map +1 -0
- package/package.json +38 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2014-present Sebastian McKenzie and other contributors
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
|
6
|
+
a copy of this software and associated documentation files (the
|
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
|
11
|
+
the following conditions:
|
|
12
|
+
|
|
13
|
+
The above copyright notice and this permission notice shall be
|
|
14
|
+
included in all copies or substantial portions of the Software.
|
|
15
|
+
|
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
# @babel/plugin-transform-optional-chaining
|
|
2
|
+
|
|
3
|
+
> Transform optional chaining operators into a series of nil checks
|
|
4
|
+
|
|
5
|
+
See our website [@babel/plugin-transform-optional-chaining](https://babeljs.io/docs/en/babel-plugin-transform-optional-chaining) for more information.
|
|
6
|
+
|
|
7
|
+
## Install
|
|
8
|
+
|
|
9
|
+
Using npm:
|
|
10
|
+
|
|
11
|
+
```sh
|
|
12
|
+
npm install --save-dev @babel/plugin-transform-optional-chaining
|
|
13
|
+
```
|
|
14
|
+
|
|
15
|
+
or using yarn:
|
|
16
|
+
|
|
17
|
+
```sh
|
|
18
|
+
yarn add @babel/plugin-transform-optional-chaining --dev
|
|
19
|
+
```
|
package/lib/index.js
ADDED
|
@@ -0,0 +1,217 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, '__esModule', { value: true });
|
|
4
|
+
|
|
5
|
+
var helperPluginUtils = require('@babel/helper-plugin-utils');
|
|
6
|
+
var syntaxOptionalChaining = require('@babel/plugin-syntax-optional-chaining');
|
|
7
|
+
var core = require('@babel/core');
|
|
8
|
+
var helperSkipTransparentExpressionWrappers = require('@babel/helper-skip-transparent-expression-wrappers');
|
|
9
|
+
|
|
10
|
+
function willPathCastToBoolean(path) {
|
|
11
|
+
const maybeWrapped = findOutermostTransparentParent(path);
|
|
12
|
+
const {
|
|
13
|
+
node,
|
|
14
|
+
parentPath
|
|
15
|
+
} = maybeWrapped;
|
|
16
|
+
if (parentPath.isLogicalExpression()) {
|
|
17
|
+
const {
|
|
18
|
+
operator,
|
|
19
|
+
right
|
|
20
|
+
} = parentPath.node;
|
|
21
|
+
if (operator === "&&" || operator === "||" || operator === "??" && node === right) {
|
|
22
|
+
return willPathCastToBoolean(parentPath);
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
if (parentPath.isSequenceExpression()) {
|
|
26
|
+
const {
|
|
27
|
+
expressions
|
|
28
|
+
} = parentPath.node;
|
|
29
|
+
if (expressions[expressions.length - 1] === node) {
|
|
30
|
+
return willPathCastToBoolean(parentPath);
|
|
31
|
+
} else {
|
|
32
|
+
return true;
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
return parentPath.isConditional({
|
|
36
|
+
test: node
|
|
37
|
+
}) || parentPath.isUnaryExpression({
|
|
38
|
+
operator: "!"
|
|
39
|
+
}) || parentPath.isLoop({
|
|
40
|
+
test: node
|
|
41
|
+
});
|
|
42
|
+
}
|
|
43
|
+
function findOutermostTransparentParent(path) {
|
|
44
|
+
let maybeWrapped = path;
|
|
45
|
+
path.findParent(p => {
|
|
46
|
+
if (!helperSkipTransparentExpressionWrappers.isTransparentExprWrapper(p.node)) return true;
|
|
47
|
+
maybeWrapped = p;
|
|
48
|
+
});
|
|
49
|
+
return maybeWrapped;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
const {
|
|
53
|
+
ast
|
|
54
|
+
} = core.template.expression;
|
|
55
|
+
function isSimpleMemberExpression(expression) {
|
|
56
|
+
expression = helperSkipTransparentExpressionWrappers.skipTransparentExprWrapperNodes(expression);
|
|
57
|
+
return core.types.isIdentifier(expression) || core.types.isSuper(expression) || core.types.isMemberExpression(expression) && !expression.computed && isSimpleMemberExpression(expression.object);
|
|
58
|
+
}
|
|
59
|
+
function needsMemoize(path) {
|
|
60
|
+
let optionalPath = path;
|
|
61
|
+
const {
|
|
62
|
+
scope
|
|
63
|
+
} = path;
|
|
64
|
+
while (optionalPath.isOptionalMemberExpression() || optionalPath.isOptionalCallExpression()) {
|
|
65
|
+
const {
|
|
66
|
+
node
|
|
67
|
+
} = optionalPath;
|
|
68
|
+
const childPath = helperSkipTransparentExpressionWrappers.skipTransparentExprWrappers(optionalPath.isOptionalMemberExpression() ? optionalPath.get("object") : optionalPath.get("callee"));
|
|
69
|
+
if (node.optional) {
|
|
70
|
+
return !scope.isStatic(childPath.node);
|
|
71
|
+
}
|
|
72
|
+
optionalPath = childPath;
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
function transform(path, {
|
|
76
|
+
pureGetters,
|
|
77
|
+
noDocumentAll
|
|
78
|
+
}) {
|
|
79
|
+
const {
|
|
80
|
+
scope
|
|
81
|
+
} = path;
|
|
82
|
+
const maybeWrapped = findOutermostTransparentParent(path);
|
|
83
|
+
const {
|
|
84
|
+
parentPath
|
|
85
|
+
} = maybeWrapped;
|
|
86
|
+
const willReplacementCastToBoolean = willPathCastToBoolean(maybeWrapped);
|
|
87
|
+
let isDeleteOperation = false;
|
|
88
|
+
const parentIsCall = parentPath.isCallExpression({
|
|
89
|
+
callee: maybeWrapped.node
|
|
90
|
+
}) && path.isOptionalMemberExpression();
|
|
91
|
+
const optionals = [];
|
|
92
|
+
let optionalPath = path;
|
|
93
|
+
if (scope.path.isPattern() && needsMemoize(optionalPath)) {
|
|
94
|
+
path.replaceWith(core.template.ast`(() => ${path.node})()`);
|
|
95
|
+
return;
|
|
96
|
+
}
|
|
97
|
+
while (optionalPath.isOptionalMemberExpression() || optionalPath.isOptionalCallExpression()) {
|
|
98
|
+
const {
|
|
99
|
+
node
|
|
100
|
+
} = optionalPath;
|
|
101
|
+
if (node.optional) {
|
|
102
|
+
optionals.push(node);
|
|
103
|
+
}
|
|
104
|
+
if (optionalPath.isOptionalMemberExpression()) {
|
|
105
|
+
optionalPath.node.type = "MemberExpression";
|
|
106
|
+
optionalPath = helperSkipTransparentExpressionWrappers.skipTransparentExprWrappers(optionalPath.get("object"));
|
|
107
|
+
} else if (optionalPath.isOptionalCallExpression()) {
|
|
108
|
+
optionalPath.node.type = "CallExpression";
|
|
109
|
+
optionalPath = helperSkipTransparentExpressionWrappers.skipTransparentExprWrappers(optionalPath.get("callee"));
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
let replacementPath = path;
|
|
113
|
+
if (parentPath.isUnaryExpression({
|
|
114
|
+
operator: "delete"
|
|
115
|
+
})) {
|
|
116
|
+
replacementPath = parentPath;
|
|
117
|
+
isDeleteOperation = true;
|
|
118
|
+
}
|
|
119
|
+
for (let i = optionals.length - 1; i >= 0; i--) {
|
|
120
|
+
const node = optionals[i];
|
|
121
|
+
const isCall = core.types.isCallExpression(node);
|
|
122
|
+
const chainWithTypes = isCall ? node.callee : node.object;
|
|
123
|
+
const chain = helperSkipTransparentExpressionWrappers.skipTransparentExprWrapperNodes(chainWithTypes);
|
|
124
|
+
let ref;
|
|
125
|
+
let check;
|
|
126
|
+
if (isCall && core.types.isIdentifier(chain, {
|
|
127
|
+
name: "eval"
|
|
128
|
+
})) {
|
|
129
|
+
check = ref = chain;
|
|
130
|
+
node.callee = core.types.sequenceExpression([core.types.numericLiteral(0), ref]);
|
|
131
|
+
} else if (pureGetters && isCall && isSimpleMemberExpression(chain)) {
|
|
132
|
+
check = ref = node.callee;
|
|
133
|
+
} else {
|
|
134
|
+
ref = scope.maybeGenerateMemoised(chain);
|
|
135
|
+
if (ref) {
|
|
136
|
+
check = core.types.assignmentExpression("=", core.types.cloneNode(ref), chainWithTypes);
|
|
137
|
+
isCall ? node.callee = ref : node.object = ref;
|
|
138
|
+
} else {
|
|
139
|
+
check = ref = chainWithTypes;
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
if (isCall && core.types.isMemberExpression(chain)) {
|
|
143
|
+
if (pureGetters && isSimpleMemberExpression(chain)) {
|
|
144
|
+
node.callee = chainWithTypes;
|
|
145
|
+
} else {
|
|
146
|
+
const {
|
|
147
|
+
object
|
|
148
|
+
} = chain;
|
|
149
|
+
let context;
|
|
150
|
+
if (core.types.isSuper(object)) {
|
|
151
|
+
context = core.types.thisExpression();
|
|
152
|
+
} else {
|
|
153
|
+
const memoized = scope.maybeGenerateMemoised(object);
|
|
154
|
+
if (memoized) {
|
|
155
|
+
context = memoized;
|
|
156
|
+
chain.object = core.types.assignmentExpression("=", memoized, object);
|
|
157
|
+
} else {
|
|
158
|
+
context = object;
|
|
159
|
+
}
|
|
160
|
+
}
|
|
161
|
+
node.arguments.unshift(core.types.cloneNode(context));
|
|
162
|
+
node.callee = core.types.memberExpression(node.callee, core.types.identifier("call"));
|
|
163
|
+
}
|
|
164
|
+
}
|
|
165
|
+
let replacement = replacementPath.node;
|
|
166
|
+
if (i === 0 && parentIsCall) {
|
|
167
|
+
var _baseRef;
|
|
168
|
+
const object = helperSkipTransparentExpressionWrappers.skipTransparentExprWrapperNodes(replacement.object);
|
|
169
|
+
let baseRef;
|
|
170
|
+
if (!pureGetters || !isSimpleMemberExpression(object)) {
|
|
171
|
+
baseRef = scope.maybeGenerateMemoised(object);
|
|
172
|
+
if (baseRef) {
|
|
173
|
+
replacement.object = core.types.assignmentExpression("=", baseRef, object);
|
|
174
|
+
}
|
|
175
|
+
}
|
|
176
|
+
replacement = core.types.callExpression(core.types.memberExpression(replacement, core.types.identifier("bind")), [core.types.cloneNode((_baseRef = baseRef) != null ? _baseRef : object)]);
|
|
177
|
+
}
|
|
178
|
+
if (willReplacementCastToBoolean) {
|
|
179
|
+
const nonNullishCheck = noDocumentAll ? ast`${core.types.cloneNode(check)} != null` : ast`
|
|
180
|
+
${core.types.cloneNode(check)} !== null && ${core.types.cloneNode(ref)} !== void 0`;
|
|
181
|
+
replacementPath.replaceWith(core.types.logicalExpression("&&", nonNullishCheck, replacement));
|
|
182
|
+
replacementPath = helperSkipTransparentExpressionWrappers.skipTransparentExprWrappers(replacementPath.get("right"));
|
|
183
|
+
} else {
|
|
184
|
+
const nullishCheck = noDocumentAll ? ast`${core.types.cloneNode(check)} == null` : ast`
|
|
185
|
+
${core.types.cloneNode(check)} === null || ${core.types.cloneNode(ref)} === void 0`;
|
|
186
|
+
const returnValue = isDeleteOperation ? ast`true` : ast`void 0`;
|
|
187
|
+
replacementPath.replaceWith(core.types.conditionalExpression(nullishCheck, returnValue, replacement));
|
|
188
|
+
replacementPath = helperSkipTransparentExpressionWrappers.skipTransparentExprWrappers(replacementPath.get("alternate"));
|
|
189
|
+
}
|
|
190
|
+
}
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
var index = helperPluginUtils.declare((api, options) => {
|
|
194
|
+
var _api$assumption, _api$assumption2;
|
|
195
|
+
api.assertVersion(7);
|
|
196
|
+
const {
|
|
197
|
+
loose = false
|
|
198
|
+
} = options;
|
|
199
|
+
const noDocumentAll = (_api$assumption = api.assumption("noDocumentAll")) != null ? _api$assumption : loose;
|
|
200
|
+
const pureGetters = (_api$assumption2 = api.assumption("pureGetters")) != null ? _api$assumption2 : loose;
|
|
201
|
+
return {
|
|
202
|
+
name: "transform-optional-chaining",
|
|
203
|
+
inherits: syntaxOptionalChaining.default,
|
|
204
|
+
visitor: {
|
|
205
|
+
"OptionalCallExpression|OptionalMemberExpression"(path) {
|
|
206
|
+
transform(path, {
|
|
207
|
+
noDocumentAll,
|
|
208
|
+
pureGetters
|
|
209
|
+
});
|
|
210
|
+
}
|
|
211
|
+
}
|
|
212
|
+
};
|
|
213
|
+
});
|
|
214
|
+
|
|
215
|
+
exports["default"] = index;
|
|
216
|
+
exports.transform = transform;
|
|
217
|
+
//# sourceMappingURL=index.js.map
|
package/lib/index.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sources":["../src/util.ts","../src/transform.ts","../src/index.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","import { types as t, template } from \"@babel/core\";\nimport type { NodePath } from \"@babel/traverse\";\nimport {\n skipTransparentExprWrapperNodes,\n skipTransparentExprWrappers,\n} from \"@babel/helper-skip-transparent-expression-wrappers\";\nimport { willPathCastToBoolean, findOutermostTransparentParent } from \"./util\";\n\nconst { ast } = template.expression;\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 // @ts-expect-error isOptionalMemberExpression does not work with NodePath union\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\nexport function transform(\n path: NodePath<t.OptionalCallExpression | t.OptionalMemberExpression>,\n {\n pureGetters,\n noDocumentAll,\n }: { pureGetters: boolean; noDocumentAll: boolean },\n) {\n const { scope } = path;\n // maybeWrapped points to the outermost transparent expression wrapper\n // or the path itself\n const maybeWrapped = findOutermostTransparentParent(path);\n const { parentPath } = maybeWrapped;\n const willReplacementCastToBoolean = willPathCastToBoolean(maybeWrapped);\n let isDeleteOperation = false;\n const parentIsCall =\n parentPath.isCallExpression({ callee: maybeWrapped.node }) &&\n // note that the first condition must implies that `path.optional` is `true`,\n // otherwise the parentPath should be an OptionalCallExpression\n path.isOptionalMemberExpression();\n\n const optionals = [];\n\n let optionalPath = path;\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(optionalPath)) {\n path.replaceWith(template.ast`(() => ${path.node})()` as t.Statement);\n // The injected optional chain will be queued and eventually transformed when visited\n return;\n }\n while (\n optionalPath.isOptionalMemberExpression() ||\n optionalPath.isOptionalCallExpression()\n ) {\n const { node } = optionalPath;\n if (node.optional) {\n optionals.push(node);\n }\n // @ts-expect-error isOptionalMemberExpression does not work with NodePath union\n if (optionalPath.isOptionalMemberExpression()) {\n // @ts-expect-error todo(flow->ts) avoid changing more type\n optionalPath.node.type = \"MemberExpression\";\n // @ts-expect-error todo(flow->ts)\n optionalPath = skipTransparentExprWrappers(optionalPath.get(\"object\"));\n } else if (optionalPath.isOptionalCallExpression()) {\n // @ts-expect-error todo(flow->ts) avoid changing more type\n optionalPath.node.type = \"CallExpression\";\n // @ts-expect-error todo(flow->ts)\n optionalPath = skipTransparentExprWrappers(optionalPath.get(\"callee\"));\n }\n }\n\n // todo: Improve replacementPath typings\n let replacementPath: NodePath<any> = path;\n if (parentPath.isUnaryExpression({ operator: \"delete\" })) {\n replacementPath = parentPath;\n isDeleteOperation = true;\n }\n for (let i = optionals.length - 1; i >= 0; i--) {\n const node = optionals[i] as unknown as\n | t.MemberExpression\n | t.CallExpression;\n\n const isCall = t.isCallExpression(node);\n\n const chainWithTypes = isCall\n ? // V8 intrinsics must not be an optional call\n (node.callee as t.Expression)\n : node.object;\n const chain = skipTransparentExprWrapperNodes(chainWithTypes);\n\n let ref;\n let check;\n if (isCall && t.isIdentifier(chain, { name: \"eval\" })) {\n check = ref = chain;\n // `eval?.()` is an indirect eval call transformed to `(0,eval)()`\n node.callee = t.sequenceExpression([t.numericLiteral(0), ref]);\n } else if (pureGetters && isCall && isSimpleMemberExpression(chain)) {\n // If we assume getters are pure (avoiding a Function#call) and we are at the call,\n // we can avoid a needless memoize. We only do this if the callee is a simple member\n // expression, to avoid multiple calls to nested call expressions.\n check = ref = node.callee;\n } else {\n ref = scope.maybeGenerateMemoised(chain);\n if (ref) {\n check = t.assignmentExpression(\n \"=\",\n t.cloneNode(ref),\n // Here `chainWithTypes` MUST NOT be cloned because it could be\n // updated when generating the memoised context of a call\n // expression. It must be an Expression when `ref` is an identifier\n // eslint-disable-next-line @typescript-eslint/no-unnecessary-type-assertion\n chainWithTypes as t.Expression,\n );\n\n isCall ? (node.callee = ref) : (node.object = ref);\n } else {\n check = ref = chainWithTypes;\n }\n }\n\n // Ensure call expressions have the proper `this`\n // `foo.bar()` has context `foo`.\n if (isCall && t.isMemberExpression(chain)) {\n if (pureGetters && isSimpleMemberExpression(chain)) {\n // To avoid a Function#call, we can instead re-grab the property from the context object.\n // `a.?b.?()` translates roughly to `_a.b != null && _a.b()`\n node.callee = chainWithTypes;\n } else {\n // Otherwise, we need to memoize the context object, and change the call into a Function#call.\n // `a.?b.?()` translates roughly to `(_b = _a.b) != null && _b.call(_a)`\n const { object } = chain;\n let context: t.Expression;\n if (t.isSuper(object)) {\n context = t.thisExpression();\n } else {\n const memoized = scope.maybeGenerateMemoised(object);\n if (memoized) {\n context = memoized;\n chain.object = t.assignmentExpression(\"=\", memoized, object);\n } else {\n context = object;\n }\n }\n\n node.arguments.unshift(t.cloneNode(context));\n // @ts-expect-error node.callee can not be an V8IntrinsicIdentifier: V8 intrinsic is disallowed in optional chain\n node.callee = t.memberExpression(node.callee, t.identifier(\"call\"));\n }\n }\n let replacement = replacementPath.node;\n // Ensure (a?.b)() has proper `this`\n // The `parentIsCall` is constant within loop, we should check i === 0\n // to ensure that it is only applied to the first optional chain element\n // i.e. `?.b` in `(a?.b.c)()`\n if (i === 0 && parentIsCall) {\n // `(a?.b)()` to `(a == null ? undefined : a.b.bind(a))()`\n // object must not be Super as super?.foo is invalid\n const object = skipTransparentExprWrapperNodes(\n replacement.object,\n // eslint-disable-next-line @typescript-eslint/no-unnecessary-type-assertion\n ) as any as t.Expression;\n let baseRef;\n if (!pureGetters || !isSimpleMemberExpression(object)) {\n // memoize the context object when getters are not always pure\n // or the object is not a simple member expression\n // `(a?.b.c)()` to `(a == null ? undefined : (_a$b = a.b).c.bind(_a$b))()`\n baseRef = scope.maybeGenerateMemoised(object);\n if (baseRef) {\n replacement.object = t.assignmentExpression(\"=\", baseRef, object);\n }\n }\n replacement = t.callExpression(\n t.memberExpression(replacement, t.identifier(\"bind\")),\n [t.cloneNode(baseRef ?? object)],\n );\n }\n\n if (willReplacementCastToBoolean) {\n // `if (a?.b) {}` transformed to `if (a != null && a.b) {}`\n // we don't need to return `void 0` because the returned value will\n // eventually cast to boolean.\n const nonNullishCheck = noDocumentAll\n ? ast`${t.cloneNode(check)} != null`\n : ast`\n ${t.cloneNode(check)} !== null && ${t.cloneNode(ref)} !== void 0`;\n replacementPath.replaceWith(\n t.logicalExpression(\"&&\", nonNullishCheck, replacement),\n );\n replacementPath = skipTransparentExprWrappers(\n // @ts-expect-error todo(flow->ts)\n replacementPath.get(\"right\"),\n );\n } else {\n const nullishCheck = noDocumentAll\n ? ast`${t.cloneNode(check)} == null`\n : ast`\n ${t.cloneNode(check)} === null || ${t.cloneNode(ref)} === void 0`;\n\n const returnValue = isDeleteOperation ? ast`true` : ast`void 0`;\n replacementPath.replaceWith(\n t.conditionalExpression(nullishCheck, returnValue, replacement),\n );\n replacementPath = skipTransparentExprWrappers(\n // @ts-expect-error todo(flow->ts)\n replacementPath.get(\"alternate\"),\n );\n }\n }\n}\n","import { declare } from \"@babel/helper-plugin-utils\";\nimport syntaxOptionalChaining from \"@babel/plugin-syntax-optional-chaining\";\nimport { transform } from \"./transform\";\nimport type { NodePath } from \"@babel/traverse\";\nimport type * as t from \"@babel/types\";\n\nexport interface Options {\n loose?: boolean;\n}\nexport default declare((api, options: Options) => {\n api.assertVersion(7);\n\n const { loose = false } = options;\n const noDocumentAll = api.assumption(\"noDocumentAll\") ?? loose;\n const pureGetters = api.assumption(\"pureGetters\") ?? loose;\n\n return {\n name: \"transform-optional-chaining\",\n inherits: syntaxOptionalChaining.default,\n\n visitor: {\n \"OptionalCallExpression|OptionalMemberExpression\"(\n path: NodePath<t.OptionalCallExpression | t.OptionalMemberExpression>,\n ) {\n transform(path, { noDocumentAll, pureGetters });\n },\n },\n };\n});\n\nexport { transform };\n"],"names":["willPathCastToBoolean","path","maybeWrapped","findOutermostTransparentParent","node","parentPath","isLogicalExpression","operator","right","isSequenceExpression","expressions","length","isConditional","test","isUnaryExpression","isLoop","findParent","p","isTransparentExprWrapper","ast","template","expression","isSimpleMemberExpression","skipTransparentExprWrapperNodes","t","isIdentifier","isSuper","isMemberExpression","computed","object","needsMemoize","optionalPath","scope","isOptionalMemberExpression","isOptionalCallExpression","childPath","skipTransparentExprWrappers","get","optional","isStatic","transform","pureGetters","noDocumentAll","willReplacementCastToBoolean","isDeleteOperation","parentIsCall","isCallExpression","callee","optionals","isPattern","replaceWith","push","type","replacementPath","i","isCall","chainWithTypes","chain","ref","check","name","sequenceExpression","numericLiteral","maybeGenerateMemoised","assignmentExpression","cloneNode","context","thisExpression","memoized","arguments","unshift","memberExpression","identifier","replacement","_baseRef","baseRef","callExpression","nonNullishCheck","logicalExpression","nullishCheck","returnValue","conditionalExpression","declare","api","options","_api$assumption","_api$assumption2","assertVersion","loose","assumption","inherits","syntaxOptionalChaining","default","visitor","OptionalCallExpression|OptionalMemberExpression"],"mappings":";;;;;;;;;AAiBO,SAASA,qBAAqBA,CAACC,IAAc,EAAW;AAC7D,EAAA,MAAMC,YAAY,GAAGC,8BAA8B,CAACF,IAAI,CAAC,CAAA;EACzD,MAAM;IAAEG,IAAI;AAAEC,IAAAA,UAAAA;AAAW,GAAC,GAAGH,YAAY,CAAA;AACzC,EAAA,IAAIG,UAAU,CAACC,mBAAmB,EAAE,EAAE;IACpC,MAAM;MAAEC,QAAQ;AAAEC,MAAAA,KAAAA;KAAO,GAAGH,UAAU,CAACD,IAAI,CAAA;AAC3C,IAAA,IACEG,QAAQ,KAAK,IAAI,IACjBA,QAAQ,KAAK,IAAI,IAChBA,QAAQ,KAAK,IAAI,IAAIH,IAAI,KAAKI,KAAM,EACrC;MACA,OAAOR,qBAAqB,CAACK,UAAU,CAAC,CAAA;AAC1C,KAAA;AACF,GAAA;AACA,EAAA,IAAIA,UAAU,CAACI,oBAAoB,EAAE,EAAE;IACrC,MAAM;AAAEC,MAAAA,WAAAA;KAAa,GAAGL,UAAU,CAACD,IAAI,CAAA;IACvC,IAAIM,WAAW,CAACA,WAAW,CAACC,MAAM,GAAG,CAAC,CAAC,KAAKP,IAAI,EAAE;MAChD,OAAOJ,qBAAqB,CAACK,UAAU,CAAC,CAAA;AAC1C,KAAC,MAAM;AAIL,MAAA,OAAO,IAAI,CAAA;AACb,KAAA;AACF,GAAA;EACA,OACEA,UAAU,CAACO,aAAa,CAAC;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AA,OAAO,EAAE;MACP,iDAAiDC,CAC/C7F,IAAqE,EACrE;QACAuC,SAAS,CAACvC,IAAI,EAAE;UAAEyC,aAAa;AAAED,UAAAA,WAAAA;AAAY,SAAC,CAAC,CAAA;AACjD,OAAA;AACF,KAAA;GACD,CAAA;AACH,CAAC,CAAC;;;;;"}
|
package/lib/package.json
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{ "type": "script" }
|
package/lib/transform.js
ADDED
|
@@ -0,0 +1,151 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.transform = transform;
|
|
7
|
+
var _core = require("@babel/core");
|
|
8
|
+
var _helperSkipTransparentExpressionWrappers = require("@babel/helper-skip-transparent-expression-wrappers");
|
|
9
|
+
var _util = require("./util");
|
|
10
|
+
const {
|
|
11
|
+
ast
|
|
12
|
+
} = _core.template.expression;
|
|
13
|
+
function isSimpleMemberExpression(expression) {
|
|
14
|
+
expression = (0, _helperSkipTransparentExpressionWrappers.skipTransparentExprWrapperNodes)(expression);
|
|
15
|
+
return _core.types.isIdentifier(expression) || _core.types.isSuper(expression) || _core.types.isMemberExpression(expression) && !expression.computed && isSimpleMemberExpression(expression.object);
|
|
16
|
+
}
|
|
17
|
+
function needsMemoize(path) {
|
|
18
|
+
let optionalPath = path;
|
|
19
|
+
const {
|
|
20
|
+
scope
|
|
21
|
+
} = path;
|
|
22
|
+
while (optionalPath.isOptionalMemberExpression() || optionalPath.isOptionalCallExpression()) {
|
|
23
|
+
const {
|
|
24
|
+
node
|
|
25
|
+
} = optionalPath;
|
|
26
|
+
const childPath = (0, _helperSkipTransparentExpressionWrappers.skipTransparentExprWrappers)(optionalPath.isOptionalMemberExpression() ? optionalPath.get("object") : optionalPath.get("callee"));
|
|
27
|
+
if (node.optional) {
|
|
28
|
+
return !scope.isStatic(childPath.node);
|
|
29
|
+
}
|
|
30
|
+
optionalPath = childPath;
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
function transform(path, {
|
|
34
|
+
pureGetters,
|
|
35
|
+
noDocumentAll
|
|
36
|
+
}) {
|
|
37
|
+
const {
|
|
38
|
+
scope
|
|
39
|
+
} = path;
|
|
40
|
+
const maybeWrapped = (0, _util.findOutermostTransparentParent)(path);
|
|
41
|
+
const {
|
|
42
|
+
parentPath
|
|
43
|
+
} = maybeWrapped;
|
|
44
|
+
const willReplacementCastToBoolean = (0, _util.willPathCastToBoolean)(maybeWrapped);
|
|
45
|
+
let isDeleteOperation = false;
|
|
46
|
+
const parentIsCall = parentPath.isCallExpression({
|
|
47
|
+
callee: maybeWrapped.node
|
|
48
|
+
}) && path.isOptionalMemberExpression();
|
|
49
|
+
const optionals = [];
|
|
50
|
+
let optionalPath = path;
|
|
51
|
+
if (scope.path.isPattern() && needsMemoize(optionalPath)) {
|
|
52
|
+
path.replaceWith(_core.template.ast`(() => ${path.node})()`);
|
|
53
|
+
return;
|
|
54
|
+
}
|
|
55
|
+
while (optionalPath.isOptionalMemberExpression() || optionalPath.isOptionalCallExpression()) {
|
|
56
|
+
const {
|
|
57
|
+
node
|
|
58
|
+
} = optionalPath;
|
|
59
|
+
if (node.optional) {
|
|
60
|
+
optionals.push(node);
|
|
61
|
+
}
|
|
62
|
+
if (optionalPath.isOptionalMemberExpression()) {
|
|
63
|
+
optionalPath.node.type = "MemberExpression";
|
|
64
|
+
optionalPath = (0, _helperSkipTransparentExpressionWrappers.skipTransparentExprWrappers)(optionalPath.get("object"));
|
|
65
|
+
} else if (optionalPath.isOptionalCallExpression()) {
|
|
66
|
+
optionalPath.node.type = "CallExpression";
|
|
67
|
+
optionalPath = (0, _helperSkipTransparentExpressionWrappers.skipTransparentExprWrappers)(optionalPath.get("callee"));
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
let replacementPath = path;
|
|
71
|
+
if (parentPath.isUnaryExpression({
|
|
72
|
+
operator: "delete"
|
|
73
|
+
})) {
|
|
74
|
+
replacementPath = parentPath;
|
|
75
|
+
isDeleteOperation = true;
|
|
76
|
+
}
|
|
77
|
+
for (let i = optionals.length - 1; i >= 0; i--) {
|
|
78
|
+
const node = optionals[i];
|
|
79
|
+
const isCall = _core.types.isCallExpression(node);
|
|
80
|
+
const chainWithTypes = isCall ? node.callee : node.object;
|
|
81
|
+
const chain = (0, _helperSkipTransparentExpressionWrappers.skipTransparentExprWrapperNodes)(chainWithTypes);
|
|
82
|
+
let ref;
|
|
83
|
+
let check;
|
|
84
|
+
if (isCall && _core.types.isIdentifier(chain, {
|
|
85
|
+
name: "eval"
|
|
86
|
+
})) {
|
|
87
|
+
check = ref = chain;
|
|
88
|
+
node.callee = _core.types.sequenceExpression([_core.types.numericLiteral(0), ref]);
|
|
89
|
+
} else if (pureGetters && isCall && isSimpleMemberExpression(chain)) {
|
|
90
|
+
check = ref = node.callee;
|
|
91
|
+
} else {
|
|
92
|
+
ref = scope.maybeGenerateMemoised(chain);
|
|
93
|
+
if (ref) {
|
|
94
|
+
check = _core.types.assignmentExpression("=", _core.types.cloneNode(ref), chainWithTypes);
|
|
95
|
+
isCall ? node.callee = ref : node.object = ref;
|
|
96
|
+
} else {
|
|
97
|
+
check = ref = chainWithTypes;
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
if (isCall && _core.types.isMemberExpression(chain)) {
|
|
101
|
+
if (pureGetters && isSimpleMemberExpression(chain)) {
|
|
102
|
+
node.callee = chainWithTypes;
|
|
103
|
+
} else {
|
|
104
|
+
const {
|
|
105
|
+
object
|
|
106
|
+
} = chain;
|
|
107
|
+
let context;
|
|
108
|
+
if (_core.types.isSuper(object)) {
|
|
109
|
+
context = _core.types.thisExpression();
|
|
110
|
+
} else {
|
|
111
|
+
const memoized = scope.maybeGenerateMemoised(object);
|
|
112
|
+
if (memoized) {
|
|
113
|
+
context = memoized;
|
|
114
|
+
chain.object = _core.types.assignmentExpression("=", memoized, object);
|
|
115
|
+
} else {
|
|
116
|
+
context = object;
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
node.arguments.unshift(_core.types.cloneNode(context));
|
|
120
|
+
node.callee = _core.types.memberExpression(node.callee, _core.types.identifier("call"));
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
let replacement = replacementPath.node;
|
|
124
|
+
if (i === 0 && parentIsCall) {
|
|
125
|
+
var _baseRef;
|
|
126
|
+
const object = (0, _helperSkipTransparentExpressionWrappers.skipTransparentExprWrapperNodes)(replacement.object);
|
|
127
|
+
let baseRef;
|
|
128
|
+
if (!pureGetters || !isSimpleMemberExpression(object)) {
|
|
129
|
+
baseRef = scope.maybeGenerateMemoised(object);
|
|
130
|
+
if (baseRef) {
|
|
131
|
+
replacement.object = _core.types.assignmentExpression("=", baseRef, object);
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
replacement = _core.types.callExpression(_core.types.memberExpression(replacement, _core.types.identifier("bind")), [_core.types.cloneNode((_baseRef = baseRef) != null ? _baseRef : object)]);
|
|
135
|
+
}
|
|
136
|
+
if (willReplacementCastToBoolean) {
|
|
137
|
+
const nonNullishCheck = noDocumentAll ? ast`${_core.types.cloneNode(check)} != null` : ast`
|
|
138
|
+
${_core.types.cloneNode(check)} !== null && ${_core.types.cloneNode(ref)} !== void 0`;
|
|
139
|
+
replacementPath.replaceWith(_core.types.logicalExpression("&&", nonNullishCheck, replacement));
|
|
140
|
+
replacementPath = (0, _helperSkipTransparentExpressionWrappers.skipTransparentExprWrappers)(replacementPath.get("right"));
|
|
141
|
+
} else {
|
|
142
|
+
const nullishCheck = noDocumentAll ? ast`${_core.types.cloneNode(check)} == null` : ast`
|
|
143
|
+
${_core.types.cloneNode(check)} === null || ${_core.types.cloneNode(ref)} === void 0`;
|
|
144
|
+
const returnValue = isDeleteOperation ? ast`true` : ast`void 0`;
|
|
145
|
+
replacementPath.replaceWith(_core.types.conditionalExpression(nullishCheck, returnValue, replacement));
|
|
146
|
+
replacementPath = (0, _helperSkipTransparentExpressionWrappers.skipTransparentExprWrappers)(replacementPath.get("alternate"));
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
//# sourceMappingURL=transform.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"names":["_core","require","_helperSkipTransparentExpressionWrappers","_util","ast","template","expression","isSimpleMemberExpression","skipTransparentExprWrapperNodes","t","isIdentifier","isSuper","isMemberExpression","computed","object","needsMemoize","path","optionalPath","scope","isOptionalMemberExpression","isOptionalCallExpression","node","childPath","skipTransparentExprWrappers","get","optional","isStatic","transform","pureGetters","noDocumentAll","maybeWrapped","findOutermostTransparentParent","parentPath","willReplacementCastToBoolean","willPathCastToBoolean","isDeleteOperation","parentIsCall","isCallExpression","callee","optionals","isPattern","replaceWith","push","type","replacementPath","isUnaryExpression","operator","i","length","isCall","chainWithTypes","chain","ref","check","name","sequenceExpression","numericLiteral","maybeGenerateMemoised","assignmentExpression","cloneNode","context","thisExpression","memoized","arguments","unshift","memberExpression","identifier","replacement","_baseRef","baseRef","callExpression","nonNullishCheck","logicalExpression","nullishCheck","returnValue","conditionalExpression"],"sources":["../src/transform.ts"],"sourcesContent":["import { types as t, template } from \"@babel/core\";\nimport type { NodePath } from \"@babel/traverse\";\nimport {\n skipTransparentExprWrapperNodes,\n skipTransparentExprWrappers,\n} from \"@babel/helper-skip-transparent-expression-wrappers\";\nimport { willPathCastToBoolean, findOutermostTransparentParent } from \"./util\";\n\nconst { ast } = template.expression;\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 // @ts-expect-error isOptionalMemberExpression does not work with NodePath union\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\nexport function transform(\n path: NodePath<t.OptionalCallExpression | t.OptionalMemberExpression>,\n {\n pureGetters,\n noDocumentAll,\n }: { pureGetters: boolean; noDocumentAll: boolean },\n) {\n const { scope } = path;\n // maybeWrapped points to the outermost transparent expression wrapper\n // or the path itself\n const maybeWrapped = findOutermostTransparentParent(path);\n const { parentPath } = maybeWrapped;\n const willReplacementCastToBoolean = willPathCastToBoolean(maybeWrapped);\n let isDeleteOperation = false;\n const parentIsCall =\n parentPath.isCallExpression({ callee: maybeWrapped.node }) &&\n // note that the first condition must implies that `path.optional` is `true`,\n // otherwise the parentPath should be an OptionalCallExpression\n path.isOptionalMemberExpression();\n\n const optionals = [];\n\n let optionalPath = path;\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(optionalPath)) {\n path.replaceWith(template.ast`(() => ${path.node})()` as t.Statement);\n // The injected optional chain will be queued and eventually transformed when visited\n return;\n }\n while (\n optionalPath.isOptionalMemberExpression() ||\n optionalPath.isOptionalCallExpression()\n ) {\n const { node } = optionalPath;\n if (node.optional) {\n optionals.push(node);\n }\n // @ts-expect-error isOptionalMemberExpression does not work with NodePath union\n if (optionalPath.isOptionalMemberExpression()) {\n // @ts-expect-error todo(flow->ts) avoid changing more type\n optionalPath.node.type = \"MemberExpression\";\n // @ts-expect-error todo(flow->ts)\n optionalPath = skipTransparentExprWrappers(optionalPath.get(\"object\"));\n } else if (optionalPath.isOptionalCallExpression()) {\n // @ts-expect-error todo(flow->ts) avoid changing more type\n optionalPath.node.type = \"CallExpression\";\n // @ts-expect-error todo(flow->ts)\n optionalPath = skipTransparentExprWrappers(optionalPath.get(\"callee\"));\n }\n }\n\n // todo: Improve replacementPath typings\n let replacementPath: NodePath<any> = path;\n if (parentPath.isUnaryExpression({ operator: \"delete\" })) {\n replacementPath = parentPath;\n isDeleteOperation = true;\n }\n for (let i = optionals.length - 1; i >= 0; i--) {\n const node = optionals[i] as unknown as\n | t.MemberExpression\n | t.CallExpression;\n\n const isCall = t.isCallExpression(node);\n\n const chainWithTypes = isCall\n ? // V8 intrinsics must not be an optional call\n (node.callee as t.Expression)\n : node.object;\n const chain = skipTransparentExprWrapperNodes(chainWithTypes);\n\n let ref;\n let check;\n if (isCall && t.isIdentifier(chain, { name: \"eval\" })) {\n check = ref = chain;\n // `eval?.()` is an indirect eval call transformed to `(0,eval)()`\n node.callee = t.sequenceExpression([t.numericLiteral(0), ref]);\n } else if (pureGetters && isCall && isSimpleMemberExpression(chain)) {\n // If we assume getters are pure (avoiding a Function#call) and we are at the call,\n // we can avoid a needless memoize. We only do this if the callee is a simple member\n // expression, to avoid multiple calls to nested call expressions.\n check = ref = node.callee;\n } else {\n ref = scope.maybeGenerateMemoised(chain);\n if (ref) {\n check = t.assignmentExpression(\n \"=\",\n t.cloneNode(ref),\n // Here `chainWithTypes` MUST NOT be cloned because it could be\n // updated when generating the memoised context of a call\n // expression. It must be an Expression when `ref` is an identifier\n // eslint-disable-next-line @typescript-eslint/no-unnecessary-type-assertion\n chainWithTypes as t.Expression,\n );\n\n isCall ? (node.callee = ref) : (node.object = ref);\n } else {\n check = ref = chainWithTypes;\n }\n }\n\n // Ensure call expressions have the proper `this`\n // `foo.bar()` has context `foo`.\n if (isCall && t.isMemberExpression(chain)) {\n if (pureGetters && isSimpleMemberExpression(chain)) {\n // To avoid a Function#call, we can instead re-grab the property from the context object.\n // `a.?b.?()` translates roughly to `_a.b != null && _a.b()`\n node.callee = chainWithTypes;\n } else {\n // Otherwise, we need to memoize the context object, and change the call into a Function#call.\n // `a.?b.?()` translates roughly to `(_b = _a.b) != null && _b.call(_a)`\n const { object } = chain;\n let context: t.Expression;\n if (t.isSuper(object)) {\n context = t.thisExpression();\n } else {\n const memoized = scope.maybeGenerateMemoised(object);\n if (memoized) {\n context = memoized;\n chain.object = t.assignmentExpression(\"=\", memoized, object);\n } else {\n context = object;\n }\n }\n\n node.arguments.unshift(t.cloneNode(context));\n // @ts-expect-error node.callee can not be an V8IntrinsicIdentifier: V8 intrinsic is disallowed in optional chain\n node.callee = t.memberExpression(node.callee, t.identifier(\"call\"));\n }\n }\n let replacement = replacementPath.node;\n // Ensure (a?.b)() has proper `this`\n // The `parentIsCall` is constant within loop, we should check i === 0\n // to ensure that it is only applied to the first optional chain element\n // i.e. `?.b` in `(a?.b.c)()`\n if (i === 0 && parentIsCall) {\n // `(a?.b)()` to `(a == null ? undefined : a.b.bind(a))()`\n // object must not be Super as super?.foo is invalid\n const object = skipTransparentExprWrapperNodes(\n replacement.object,\n // eslint-disable-next-line @typescript-eslint/no-unnecessary-type-assertion\n ) as any as t.Expression;\n let baseRef;\n if (!pureGetters || !isSimpleMemberExpression(object)) {\n // memoize the context object when getters are not always pure\n // or the object is not a simple member expression\n // `(a?.b.c)()` to `(a == null ? undefined : (_a$b = a.b).c.bind(_a$b))()`\n baseRef = scope.maybeGenerateMemoised(object);\n if (baseRef) {\n replacement.object = t.assignmentExpression(\"=\", baseRef, object);\n }\n }\n replacement = t.callExpression(\n t.memberExpression(replacement, t.identifier(\"bind\")),\n [t.cloneNode(baseRef ?? object)],\n );\n }\n\n if (willReplacementCastToBoolean) {\n // `if (a?.b) {}` transformed to `if (a != null && a.b) {}`\n // we don't need to return `void 0` because the returned value will\n // eventually cast to boolean.\n const nonNullishCheck = noDocumentAll\n ? ast`${t.cloneNode(check)} != null`\n : ast`\n ${t.cloneNode(check)} !== null && ${t.cloneNode(ref)} !== void 0`;\n replacementPath.replaceWith(\n t.logicalExpression(\"&&\", nonNullishCheck, replacement),\n );\n replacementPath = skipTransparentExprWrappers(\n // @ts-expect-error todo(flow->ts)\n replacementPath.get(\"right\"),\n );\n } else {\n const nullishCheck = noDocumentAll\n ? ast`${t.cloneNode(check)} == null`\n : ast`\n ${t.cloneNode(check)} === null || ${t.cloneNode(ref)} === void 0`;\n\n const returnValue = isDeleteOperation ? ast`true` : ast`void 0`;\n replacementPath.replaceWith(\n t.conditionalExpression(nullishCheck, returnValue, replacement),\n );\n replacementPath = skipTransparentExprWrappers(\n // @ts-expect-error todo(flow->ts)\n replacementPath.get(\"alternate\"),\n );\n }\n }\n}\n"],"mappings":";;;;;;AAAA,IAAAA,KAAA,GAAAC,OAAA;AAEA,IAAAC,wCAAA,GAAAD,OAAA;AAIA,IAAAE,KAAA,GAAAF,OAAA;AAEA,MAAM;EAAEG;AAAI,CAAC,GAAGC,cAAQ,CAACC,UAAU;AAEnC,SAASC,wBAAwBA,CAC/BD,UAAkC,EACyB;EAC3DA,UAAU,GAAG,IAAAE,wEAA+B,EAACF,UAAU,CAAC;EACxD,OACEG,WAAC,CAACC,YAAY,CAACJ,UAAU,CAAC,IAC1BG,WAAC,CAACE,OAAO,CAACL,UAAU,CAAC,IACpBG,WAAC,CAACG,kBAAkB,CAACN,UAAU,CAAC,IAC/B,CAACA,UAAU,CAACO,QAAQ,IACpBN,wBAAwB,CAACD,UAAU,CAACQ,MAAM,CAAE;AAElD;AAOA,SAASC,YAAYA,CACnBC,IAAqE,EACrE;EACA,IAAIC,YAAsB,GAAGD,IAAI;EACjC,MAAM;IAAEE;EAAM,CAAC,GAAGF,IAAI;EACtB,OACEC,YAAY,CAACE,0BAA0B,EAAE,IACzCF,YAAY,CAACG,wBAAwB,EAAE,EACvC;IACA,MAAM;MAAEC;IAAK,CAAC,GAAGJ,YAAY;IAC7B,MAAMK,SAAS,GAAG,IAAA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AAG,CAAE,aAAY;MACvER,eAAe,CAACH,WAAW,CACzBhC,WAAC,CAAC+D,iBAAiB,CAAC,IAAI,EAAED,eAAe,EAAEJ,WAAW,CAAC,CACxD;MACDvB,eAAe,GAAG,IAAArB,oEAA2B,EAE3CqB,eAAe,CAACpB,GAAG,CAAC,OAAO,CAAC,CAC7B;IACH,CAAC,MAAM;MACL,MAAMiD,YAAY,GAAG5C,aAAa,GAC9BzB,GAAI,GAAEK,WAAC,CAACkD,SAAS,CAACN,KAAK,CAAE,UAAS,GAClCjD,GAAI;AACd,cAAcK,WAAC,CAACkD,SAAS,CAACN,KAAK,CAAE,gBAAe5C,WAAC,CAACkD,SAAS,CAACP,GAAG,CAAE,aAAY;MAEvE,MAAMsB,WAAW,GAAGvC,iBAAiB,GAAG/B,GAAI,MAAK,GAAGA,GAAI,QAAO;MAC/DwC,eAAe,CAACH,WAAW,CACzBhC,WAAC,CAACkE,qBAAqB,CAACF,YAAY,EAAEC,WAAW,EAAEP,WAAW,CAAC,CAChE;MACDvB,eAAe,GAAG,IAAArB,oEAA2B,EAE3CqB,eAAe,CAACpB,GAAG,CAAC,WAAW,CAAC,CACjC;IACH;EACF;AACF"}
|
package/lib/util.js
ADDED
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.findOutermostTransparentParent = findOutermostTransparentParent;
|
|
7
|
+
exports.willPathCastToBoolean = willPathCastToBoolean;
|
|
8
|
+
var _helperSkipTransparentExpressionWrappers = require("@babel/helper-skip-transparent-expression-wrappers");
|
|
9
|
+
function willPathCastToBoolean(path) {
|
|
10
|
+
const maybeWrapped = findOutermostTransparentParent(path);
|
|
11
|
+
const {
|
|
12
|
+
node,
|
|
13
|
+
parentPath
|
|
14
|
+
} = maybeWrapped;
|
|
15
|
+
if (parentPath.isLogicalExpression()) {
|
|
16
|
+
const {
|
|
17
|
+
operator,
|
|
18
|
+
right
|
|
19
|
+
} = parentPath.node;
|
|
20
|
+
if (operator === "&&" || operator === "||" || operator === "??" && node === right) {
|
|
21
|
+
return willPathCastToBoolean(parentPath);
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
if (parentPath.isSequenceExpression()) {
|
|
25
|
+
const {
|
|
26
|
+
expressions
|
|
27
|
+
} = parentPath.node;
|
|
28
|
+
if (expressions[expressions.length - 1] === node) {
|
|
29
|
+
return willPathCastToBoolean(parentPath);
|
|
30
|
+
} else {
|
|
31
|
+
return true;
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
return parentPath.isConditional({
|
|
35
|
+
test: node
|
|
36
|
+
}) || parentPath.isUnaryExpression({
|
|
37
|
+
operator: "!"
|
|
38
|
+
}) || parentPath.isLoop({
|
|
39
|
+
test: node
|
|
40
|
+
});
|
|
41
|
+
}
|
|
42
|
+
function findOutermostTransparentParent(path) {
|
|
43
|
+
let maybeWrapped = path;
|
|
44
|
+
path.findParent(p => {
|
|
45
|
+
if (!(0, _helperSkipTransparentExpressionWrappers.isTransparentExprWrapper)(p.node)) return true;
|
|
46
|
+
maybeWrapped = p;
|
|
47
|
+
});
|
|
48
|
+
return maybeWrapped;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
//# sourceMappingURL=util.js.map
|
package/lib/util.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
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,EAAE,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,EAAE,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"}
|
package/package.json
ADDED
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@babel/plugin-transform-optional-chaining",
|
|
3
|
+
"version": "7.22.0",
|
|
4
|
+
"description": "Transform optional chaining operators into a series of nil checks",
|
|
5
|
+
"repository": {
|
|
6
|
+
"type": "git",
|
|
7
|
+
"url": "https://github.com/babel/babel.git",
|
|
8
|
+
"directory": "packages/babel-plugin-transform-optional-chaining"
|
|
9
|
+
},
|
|
10
|
+
"homepage": "https://babel.dev/docs/en/next/babel-plugin-transform-optional-chaining",
|
|
11
|
+
"license": "MIT",
|
|
12
|
+
"publishConfig": {
|
|
13
|
+
"access": "public"
|
|
14
|
+
},
|
|
15
|
+
"main": "./lib/index.js",
|
|
16
|
+
"keywords": [
|
|
17
|
+
"babel-plugin"
|
|
18
|
+
],
|
|
19
|
+
"dependencies": {
|
|
20
|
+
"@babel/helper-plugin-utils": "^7.21.5",
|
|
21
|
+
"@babel/helper-skip-transparent-expression-wrappers": "^7.20.0",
|
|
22
|
+
"@babel/plugin-syntax-optional-chaining": "^7.8.3"
|
|
23
|
+
},
|
|
24
|
+
"peerDependencies": {
|
|
25
|
+
"@babel/core": "^7.0.0-0"
|
|
26
|
+
},
|
|
27
|
+
"devDependencies": {
|
|
28
|
+
"@babel/core": "^7.22.0",
|
|
29
|
+
"@babel/helper-plugin-test-runner": "^7.18.6",
|
|
30
|
+
"@babel/plugin-transform-block-scoping": "^7.21.0",
|
|
31
|
+
"@babel/traverse": "^7.22.0"
|
|
32
|
+
},
|
|
33
|
+
"engines": {
|
|
34
|
+
"node": ">=6.9.0"
|
|
35
|
+
},
|
|
36
|
+
"author": "The Babel Team (https://babel.dev/team)",
|
|
37
|
+
"type": "commonjs"
|
|
38
|
+
}
|