Skip to content

Commit 61b6b16

Browse files
committed
convert @babel/helper-skip-transparent-expression-wrappers to typescript
* 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
1 parent 482e33b commit 61b6b16

6 files changed

Lines changed: 58 additions & 43 deletions

File tree

  • packages
    • babel-helper-skip-transparent-expression-wrappers/src
    • babel-plugin-bugfix-v8-spread-parameters-in-optional-chaining/src
    • babel-plugin-proposal-optional-chaining

packages/babel-helper-skip-transparent-expression-wrappers/src/index.js

Lines changed: 0 additions & 27 deletions
This file was deleted.
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
import * as t from "@babel/types";
2+
import { NodePath } from "@babel/traverse";
3+
4+
export type TransparentExprWrapper =
5+
| t.TSAsExpression
6+
| t.TSTypeAssertion
7+
| t.TSNonNullExpression
8+
| t.TypeCastExpression
9+
| t.ParenthesizedExpression;
10+
11+
// A transparent expression wrapper is an AST node that most plugins will wish
12+
// to skip, as its presence does not affect the behaviour of the code. This
13+
// includes expressions used for types, and extra parenthesis. For example, in
14+
// (a as any)(), this helper can be used to skip the TSAsExpression when
15+
// determining the callee.
16+
export function isTransparentExprWrapper(
17+
node: t.Node,
18+
): node is TransparentExprWrapper {
19+
return (
20+
t.isTSAsExpression(node) ||
21+
t.isTSTypeAssertion(node) ||
22+
t.isTSNonNullExpression(node) ||
23+
t.isTypeCastExpression(node) ||
24+
t.isParenthesizedExpression(node)
25+
);
26+
}
27+
28+
type ExprNodeOrPath = t.Expression | NodePath<t.Expression>;
29+
30+
export function skipTransparentExprWrappers(expr: t.Expression): t.Expression;
31+
32+
export function skipTransparentExprWrappers(
33+
expr: NodePath<t.Expression>,
34+
): NodePath<t.Expression>;
35+
36+
export function skipTransparentExprWrappers(
37+
expr: ExprNodeOrPath,
38+
): ExprNodeOrPath {
39+
if (expr instanceof NodePath) {
40+
while (isTransparentExprWrapper(expr.node)) {
41+
expr = expr.get("expression");
42+
}
43+
} else {
44+
while (isTransparentExprWrapper(expr)) {
45+
expr = expr.expression;
46+
}
47+
}
48+
return expr;
49+
}

packages/babel-plugin-bugfix-v8-spread-parameters-in-optional-chaining/src/util.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ function matchAffectedArguments(argumentNodes) {
2222
export function shouldTransform(
2323
path: NodePath<t.OptionalMemberExpression | t.OptionalCallExpression>,
2424
): boolean {
25-
let optionalPath = path;
25+
let optionalPath: NodePath<t.Expression> = path;
2626
const chains = [];
2727
while (
2828
optionalPath.isOptionalMemberExpression() ||

packages/babel-plugin-proposal-optional-chaining/src/transform.js

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,5 @@
11
import { types as t, template } from "@babel/core";
2-
import {
3-
isTransparentExprWrapper,
4-
skipTransparentExprWrappers,
5-
} from "@babel/helper-skip-transparent-expression-wrappers";
2+
import { skipTransparentExprWrappers } from "@babel/helper-skip-transparent-expression-wrappers";
63
import { willPathCastToBoolean, findOutermostTransparentParent } from "./util";
74

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

105102
const chainWithTypes = node[replaceKey];
106-
let chain = chainWithTypes;
107-
108-
while (isTransparentExprWrapper(chain)) {
109-
chain = chain.expression;
110-
}
103+
const chain = skipTransparentExprWrappers(chainWithTypes);
111104

112105
let ref;
113106
let check;
@@ -169,9 +162,7 @@ export function transform(
169162
// i.e. `?.b` in `(a?.b.c)()`
170163
if (i === 0 && parentIsCall) {
171164
// `(a?.b)()` to `(a == null ? undefined : a.b.bind(a))()`
172-
const object = skipTransparentExprWrappers(
173-
replacementPath.get("object"),
174-
).node;
165+
const object = skipTransparentExprWrappers(replacement.object);
175166
let baseRef;
176167
if (!pureGetters || !isSimpleMemberExpression(object)) {
177168
// memoize the context object when getters are not always pure
Original file line numberDiff line numberDiff line change
@@ -1,3 +1 @@
1-
var _bar, _ref;
2-
3-
(_bar = ((_ref = (foo as A)).bar as B)) == null ? void 0 : _bar.call(_ref, foo.bar, false);
1+
((foo as A).bar as B) == null ? void 0 : ((foo as A).bar as B)(foo.bar, false);

tsconfig.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
"./packages/babel-helper-optimise-call-expression/src/**/*.ts",
2121
"./packages/babel-helper-replace-supers/src/**/*.ts",
2222
"./packages/babel-helper-simple-access/src/**/*.ts",
23+
"./packages/babel-helper-skip-transparent-expression-wrappers/src/**/*.ts",
2324
"./packages/babel-helper-split-export-declaration/src/**/*.ts",
2425
"./packages/babel-helper-transform-fixture-test-runner/src/**/*.ts",
2526
"./packages/babel-helper-validator-identifier/src/**/*.ts",
@@ -94,6 +95,9 @@
9495
"@babel/helper-simple-access": [
9596
"./packages/babel-helper-simple-access/src"
9697
],
98+
"@babel/helper-skip-transparent-expression-wrappers": [
99+
"./packages/babel-helper-skip-transparent-expression-wrappers/src"
100+
],
97101
"@babel/helper-split-export-declaration": [
98102
"./packages/babel-helper-split-export-declaration/src"
99103
],

0 commit comments

Comments
 (0)