/
estree-utils.ts
174 lines (159 loc) · 3.81 KB
/
estree-utils.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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
import * as JS from "estree";
import { isValidJSIdentifierName } from "./jsvariable";
export function member(obj: JS.Expression, prop: string): JS.MemberExpression {
const dotNotation = isValidJSIdentifierName(prop);
return {
type: "MemberExpression",
computed: !dotNotation,
object: obj,
property: dotNotation ? identifier(prop) : literal(prop)
};
}
export function methodCall(
e: JS.Expression,
method: string,
args: JS.Expression[]
): JS.CallExpression {
return {
type: "CallExpression",
callee: member(e, method),
arguments: args
};
}
/** Generate an arrow function in JS
*
* @description This will execute all the expressions, returning the
* last one. */
export function arrowFunction(
args: JS.Pattern[],
body: JS.Expression[]
): JS.ArrowFunctionExpression {
if (body.length === 0) {
throw new Error(`Empty body`);
}
if (body.length === 1) {
const expr = body[0];
return {
type: "ArrowFunctionExpression",
params: args,
body: expr,
generator: false,
expression: true,
async: false
};
} else {
const middle: JS.Statement[] = body.slice(0, -1).map(e => ({
type: "ExpressionStatement",
expression: e
}));
const returning: JS.Statement = {
type: "ReturnStatement",
argument: body[body.length - 1]
};
return {
type: "ArrowFunctionExpression",
params: args,
body: {
type: "BlockStatement",
body: [...middle, returning]
},
generator: false,
expression: false,
async: false
};
}
}
export function literal(value: number | string | boolean | null): JS.Literal {
return {
type: "Literal",
value
};
}
export function identifier(name: string): JS.Identifier {
return {
type: "Identifier",
name
};
}
export function op1(
operator: JS.UnaryOperator,
argument: JS.Expression
): JS.UnaryExpression {
return { type: "UnaryExpression", operator, prefix: true, argument };
}
export function op(
operator: JS.BinaryOperator,
left: JS.Expression,
right: JS.Expression
): JS.BinaryExpression {
return { type: "BinaryExpression", operator, left, right };
}
export function primitiveCall(
name: string,
...args: JS.Expression[]
): JS.Expression {
return {
type: "CallExpression",
callee: identifier(name),
arguments: args
};
}
interface Property {
key: string;
value: JS.Expression;
}
export function objectExpression(properties: Property[]): JS.ObjectExpression {
return {
type: "ObjectExpression",
properties: properties.map(p => {
const validIdentifier = isValidJSIdentifierName(p.key);
return {
type: "Property",
kind: "init",
method: false,
computed: false,
shorthand:
validIdentifier &&
p.value.type === "Identifier" &&
p.value.name === p.key,
key: validIdentifier ? identifier(p.key) : literal(p.key),
value: p.value
};
})
};
}
export function requireModule(name: string): JS.Expression {
return {
type: "CallExpression",
callee: identifier("require"),
arguments: [literal(name)]
};
}
export function requireNames(names: string[], source: string): JS.Statement {
return {
type: "VariableDeclaration",
kind: "const",
declarations: [
{
type: "VariableDeclarator",
id: {
type: "ObjectPattern",
properties: names.map(name => ({
type: "Property",
kind: "init",
key: identifier(name),
value: identifier(name),
computed: false,
method: false,
shorthand: true
}))
},
init: {
type: "CallExpression",
callee: identifier("require"),
arguments: [literal(source)]
}
}
]
};
}