Skip to content

Commit

Permalink
overload skipTransparentExprWrappers
Browse files Browse the repository at this point in the history
* fixes isSimpleMemberExpression
  in @babel/plugin-proposal-optional-chaining

* use skipTransparentExprWrappers<Expression> instead of the NodePath
  overload where possible in @babel/plugin-proposal-optional-chaining
  • Loading branch information
lightmare committed Aug 20, 2021
1 parent 976bfbb commit fad4759
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 21 deletions.
@@ -1,3 +1,4 @@
import { NodePath } from "@babel/traverse";
import {
isParenthesizedExpression,
isTSAsExpression,
Expand All @@ -7,7 +8,6 @@ import {
} from "@babel/types";

import type * as t from "@babel/types";
import type { NodePath } from "@babel/traverse";

export type TransparentExprWrapper =
| t.TSAsExpression
Expand All @@ -33,11 +33,25 @@ export function isTransparentExprWrapper(
);
}

type ExprNodeOrPath = t.Expression | NodePath<t.Expression>;

export function skipTransparentExprWrappers(expr: t.Expression): t.Expression;

export function skipTransparentExprWrappers(
expr: NodePath<t.Expression>,
): NodePath<t.Expression>;

export function skipTransparentExprWrappers(
path: NodePath<t.Expression>,
): NodePath<t.Expression> {
while (isTransparentExprWrapper(path.node)) {
path = path.get("expression");
expr: ExprNodeOrPath,
): ExprNodeOrPath {
if (expr instanceof NodePath) {
while (isTransparentExprWrapper(expr.node)) {
expr = expr.get("expression");
}
} else {
while (isTransparentExprWrapper(expr)) {
expr = expr.expression;
}
}
return path;
return expr;
}
15 changes: 3 additions & 12 deletions packages/babel-plugin-proposal-optional-chaining/src/transform.js
@@ -1,8 +1,5 @@
import { types as t, template } from "@babel/core";
import {
isTransparentExprWrapper,
skipTransparentExprWrappers,
} from "@babel/helper-skip-transparent-expression-wrappers";
import { skipTransparentExprWrappers } from "@babel/helper-skip-transparent-expression-wrappers";
import { willPathCastToBoolean, findOutermostTransparentParent } from "./util";

const { ast } = template.expression;
Expand Down Expand Up @@ -103,11 +100,7 @@ export function transform(
const replaceKey = isCall ? "callee" : "object";

const chainWithTypes = node[replaceKey];
let chain = chainWithTypes;

while (isTransparentExprWrapper(chain)) {
chain = chain.expression;
}
const chain = skipTransparentExprWrappers(chainWithTypes);

let ref;
let check;
Expand Down Expand Up @@ -169,9 +162,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 = skipTransparentExprWrappers(replacement.object);
let baseRef;
if (!pureGetters || !isSimpleMemberExpression(object)) {
// memoize the context object when getters are not always pure
Expand Down
@@ -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);

0 comments on commit fad4759

Please sign in to comment.