add skipTransparentExprWrapperNodes helper (#13687)
* overload `skipTransparentExprWrappers` * fixes isSimpleMemberExpression in @babel/plugin-proposal-optional-chaining * use skipTransparentExprWrappers<Expression> instead of the NodePath overload where possible in @babel/plugin-proposal-optional-chaining * Revert "overload `skipTransparentExprWrappers`" This reverts commit fad47596a64a8d33aadacb7695dbf1aa60a528ae. * add helper `skipTransparentExprWrapperNodes` * avoids constructing NodePaths when only the Node is needed * fixes isSimpleMemberExpression in @babel/plugin-proposal-optional-chaining * use this new helper instead of skipTransparentExprWrappers where possible in @babel/plugin-proposal-optional-chaining
This commit is contained in:
parent
b1793656e6
commit
99a3fefede
@ -41,3 +41,12 @@ export function skipTransparentExprWrappers(
|
||||
}
|
||||
return path;
|
||||
}
|
||||
|
||||
export function skipTransparentExprWrapperNodes(
|
||||
node: t.Expression,
|
||||
): t.Expression {
|
||||
while (isTransparentExprWrapper(node)) {
|
||||
node = node.expression;
|
||||
}
|
||||
return node;
|
||||
}
|
||||
|
||||
@ -1,6 +1,6 @@
|
||||
import { types as t, template } from "@babel/core";
|
||||
import {
|
||||
isTransparentExprWrapper,
|
||||
skipTransparentExprWrapperNodes,
|
||||
skipTransparentExprWrappers,
|
||||
} from "@babel/helper-skip-transparent-expression-wrappers";
|
||||
import { willPathCastToBoolean, findOutermostTransparentParent } from "./util";
|
||||
@ -8,7 +8,7 @@ import { willPathCastToBoolean, findOutermostTransparentParent } from "./util";
|
||||
const { ast } = template.expression;
|
||||
|
||||
function isSimpleMemberExpression(expression) {
|
||||
expression = skipTransparentExprWrappers(expression);
|
||||
expression = skipTransparentExprWrapperNodes(expression);
|
||||
return (
|
||||
t.isIdentifier(expression) ||
|
||||
t.isSuper(expression) ||
|
||||
@ -103,11 +103,7 @@ export function transform(
|
||||
const replaceKey = isCall ? "callee" : "object";
|
||||
|
||||
const chainWithTypes = node[replaceKey];
|
||||
let chain = chainWithTypes;
|
||||
|
||||
while (isTransparentExprWrapper(chain)) {
|
||||
chain = chain.expression;
|
||||
}
|
||||
const chain = skipTransparentExprWrapperNodes(chainWithTypes);
|
||||
|
||||
let ref;
|
||||
let check;
|
||||
@ -169,9 +165,7 @@ export function transform(
|
||||
// i.e. `?.b` in `(a?.b.c)()`
|
||||
if (i === 0 && parentIsCall) {
|
||||
// `(a?.b)()` to `(a == null ? undefined : a.b.bind(a))()`
|
||||
const object = skipTransparentExprWrappers(
|
||||
replacementPath.get("object"),
|
||||
).node;
|
||||
const object = skipTransparentExprWrapperNodes(replacement.object);
|
||||
let baseRef;
|
||||
if (!pureGetters || !isSimpleMemberExpression(object)) {
|
||||
// memoize the context object when getters are not always pure
|
||||
|
||||
@ -1,3 +1 @@
|
||||
var _bar, _ref;
|
||||
|
||||
(_bar = ((_ref = (foo as A)).bar as B)) == null ? void 0 : _bar.call(_ref, foo.bar, false);
|
||||
((foo as A).bar as B) == null ? void 0 : ((foo as A).bar as B)(foo.bar, false);
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user