/
index.ts
55 lines (50 loc) 路 1.49 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
import {
isParenthesizedExpression,
isTSAsExpression,
isTSNonNullExpression,
isTSSatisfiesExpression,
isTSTypeAssertion,
isTypeCastExpression,
} from "@babel/types";
import type * as t from "@babel/types";
import type { NodePath } from "@babel/traverse";
export type TransparentExprWrapper =
| t.TSAsExpression
| t.TSSatisfiesExpression
| 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) ||
isTSSatisfiesExpression(node) ||
isTSTypeAssertion(node) ||
isTSNonNullExpression(node) ||
isTypeCastExpression(node) ||
isParenthesizedExpression(node)
);
}
export function skipTransparentExprWrappers(
path: NodePath<t.Expression>,
): NodePath<t.Expression> {
while (isTransparentExprWrapper(path.node)) {
path = path.get("expression");
}
return path;
}
export function skipTransparentExprWrapperNodes(
node: t.Expression | t.Super,
): t.Expression | t.Super {
while (isTransparentExprWrapper(node)) {
node = node.expression;
}
return node;
}