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:
Mickey Rose 2021-10-28 20:16:22 +02:00 committed by GitHub
parent b1793656e6
commit 99a3fefede
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 14 additions and 13 deletions

View File

@ -41,3 +41,12 @@ export function skipTransparentExprWrappers(
} }
return path; return path;
} }
export function skipTransparentExprWrapperNodes(
node: t.Expression,
): t.Expression {
while (isTransparentExprWrapper(node)) {
node = node.expression;
}
return node;
}

View File

@ -1,6 +1,6 @@
import { types as t, template } from "@babel/core"; import { types as t, template } from "@babel/core";
import { import {
isTransparentExprWrapper, skipTransparentExprWrapperNodes,
skipTransparentExprWrappers, skipTransparentExprWrappers,
} from "@babel/helper-skip-transparent-expression-wrappers"; } from "@babel/helper-skip-transparent-expression-wrappers";
import { willPathCastToBoolean, findOutermostTransparentParent } from "./util"; import { willPathCastToBoolean, findOutermostTransparentParent } from "./util";
@ -8,7 +8,7 @@ import { willPathCastToBoolean, findOutermostTransparentParent } from "./util";
const { ast } = template.expression; const { ast } = template.expression;
function isSimpleMemberExpression(expression) { function isSimpleMemberExpression(expression) {
expression = skipTransparentExprWrappers(expression); expression = skipTransparentExprWrapperNodes(expression);
return ( return (
t.isIdentifier(expression) || t.isIdentifier(expression) ||
t.isSuper(expression) || t.isSuper(expression) ||
@ -103,11 +103,7 @@ export function transform(
const replaceKey = isCall ? "callee" : "object"; const replaceKey = isCall ? "callee" : "object";
const chainWithTypes = node[replaceKey]; const chainWithTypes = node[replaceKey];
let chain = chainWithTypes; const chain = skipTransparentExprWrapperNodes(chainWithTypes);
while (isTransparentExprWrapper(chain)) {
chain = chain.expression;
}
let ref; let ref;
let check; let check;
@ -169,9 +165,7 @@ export function transform(
// i.e. `?.b` in `(a?.b.c)()` // i.e. `?.b` in `(a?.b.c)()`
if (i === 0 && parentIsCall) { if (i === 0 && parentIsCall) {
// `(a?.b)()` to `(a == null ? undefined : a.b.bind(a))()` // `(a?.b)()` to `(a == null ? undefined : a.b.bind(a))()`
const object = skipTransparentExprWrappers( const object = skipTransparentExprWrapperNodes(replacement.object);
replacementPath.get("object"),
).node;
let baseRef; let baseRef;
if (!pureGetters || !isSimpleMemberExpression(object)) { if (!pureGetters || !isSimpleMemberExpression(object)) {
// memoize the context object when getters are not always pure // memoize the context object when getters are not always pure

View File

@ -1,3 +1 @@
var _bar, _ref; ((foo as A).bar as B) == null ? void 0 : ((foo as A).bar as B)(foo.bar, false);
(_bar = ((_ref = (foo as A)).bar as B)) == null ? void 0 : _bar.call(_ref, foo.bar, false);