forked from babel/babel
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.ts
57 lines (50 loc) 路 1.55 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
import { NodePath } from "@babel/traverse";
import {
isParenthesizedExpression,
isTSAsExpression,
isTSNonNullExpression,
isTSTypeAssertion,
isTypeCastExpression,
} from "@babel/types";
import type * as t from "@babel/types";
export type TransparentExprWrapper =
| t.TSAsExpression
| t.TSTypeAssertion
| t.TSNonNullExpression
| t.TypeCastExpression
| t.ParenthesizedExpression;
// A transparent expression wrapper is an AST node that most plugins will wish
// to skip, as its presence does not affect the behaviour of the code. This
// includes expressions used for types, and extra parenthesis. For example, in
// (a as any)(), this helper can be used to skip the TSAsExpression when
// determining the callee.
export function isTransparentExprWrapper(
node: t.Node,
): node is TransparentExprWrapper {
return (
isTSAsExpression(node) ||
isTSTypeAssertion(node) ||
isTSNonNullExpression(node) ||
isTypeCastExpression(node) ||
isParenthesizedExpression(node)
);
}
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(
expr: ExprNodeOrPath,
): ExprNodeOrPath {
if (expr instanceof NodePath) {
while (isTransparentExprWrapper(expr.node)) {
expr = expr.get("expression");
}
} else {
while (isTransparentExprWrapper(expr)) {
expr = expr.expression;
}
}
return expr;
}