-
Notifications
You must be signed in to change notification settings - Fork 143
/
Copy pathevaluate-json.ts
431 lines (398 loc) · 12.6 KB
/
evaluate-json.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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
import type { NodePath } from '@babel/traverse';
import type {
Identifier,
ObjectExpression,
MemberExpression,
Expression,
ExpressionStatement,
CallExpression,
OptionalMemberExpression,
} from '@babel/types';
import State, { owningPackage } from './state';
import dependencySatisfies from './dependency-satisfies';
import moduleExists from './module-exists';
import getConfig from './get-config';
import { BabelContext } from './babel-context';
type OpValue = string | boolean | number;
const binops: { [operator: string]: any } = {
'||': function (a: OpValue, b: OpValue) {
return a || b;
},
'&&': function (a: OpValue, b: OpValue) {
return a && b;
},
'|': function (a: any, b: any) {
return a | b;
},
'^': function (a: any, b: any) {
return a ^ b;
},
'&': function (a: any, b: any) {
return a & b;
},
'==': function (a: OpValue, b: OpValue) {
// eslint-disable-next-line eqeqeq
return a == b;
},
'!=': function (a: OpValue, b: OpValue) {
// eslint-disable-next-line eqeqeq
return a != b;
},
'===': function (a: OpValue, b: OpValue) {
return a === b;
},
'!==': function (a: OpValue, b: OpValue) {
return a !== b;
},
'<': function (a: OpValue, b: OpValue) {
return a < b;
},
'>': function (a: OpValue, b: OpValue) {
return a > b;
},
'<=': function (a: OpValue, b: OpValue) {
return a <= b;
},
'>=': function (a: OpValue, b: OpValue) {
return a >= b;
},
'<<': function (a: any, b: any) {
return a << b;
},
'>>': function (a: any, b: any) {
return a >> b;
},
'>>>': function (a: any, b: any) {
return a >>> b;
},
'+': function (a: any, b: any) {
return a + b;
},
'-': function (a: any, b: any) {
return a - b;
},
'*': function (a: any, b: any) {
return a * b;
},
'/': function (a: any, b: any) {
return a / b;
},
'%': function (a: any, b: any) {
return a % b;
},
'??': function (a: any, b: any) {
if (a === null || a === undefined) {
return b;
}
return a;
},
};
const unops: { [operator: string]: any } = {
'-': function (a: OpValue) {
return -a;
},
'+': function (a: OpValue) {
return +a;
},
'~': function (a: OpValue) {
return ~a;
},
'!': function (a: OpValue) {
return !a;
},
void: function () {
return undefined;
},
};
export interface ConfidentResult {
confident: true;
value: any;
}
export interface UnknownResult {
confident: false;
}
export type EvaluateResult = ConfidentResult | UnknownResult;
// this is needed to make our strict types work when inter-operating with
// babel's own built-in evaluator
function isConfidentResult(result: { confident: boolean; value: any }): result is ConfidentResult {
return result.confident;
}
export interface EvaluationEnv {
knownPaths?: Map<NodePath, EvaluateResult>;
locals?: { [localVar: string]: any };
state?: State;
}
export class Evaluator {
private knownPaths: Map<NodePath, EvaluateResult>;
private locals: { [localVar: string]: any };
private state: State | undefined;
constructor(env: EvaluationEnv = {}) {
this.knownPaths = env.knownPaths || new Map();
this.locals = env.locals || {};
this.state = env.state;
}
evaluateMember(path: NodePath<MemberExpression | OptionalMemberExpression>, optionalChain: boolean): EvaluateResult {
let propertyPath = assertNotArray(path.get('property'));
let property: EvaluateResult;
if (path.node.computed) {
property = this.evaluate(propertyPath);
} else {
property = this.evaluateKey(propertyPath);
}
if (property.confident) {
let objectPath = path.get('object');
let object = this.evaluate(objectPath);
if (object.confident) {
let confidentObject = object;
let confidentProperty = property;
return {
confident: true,
get value() {
if (optionalChain) {
return confidentObject.value != null
? confidentObject.value[confidentProperty.value]
: confidentObject.value;
} else {
return confidentObject.value[confidentProperty.value];
}
},
};
}
}
return { confident: false };
}
evaluateKey(path: NodePath): EvaluateResult {
let first = this.evaluate(path);
if (first.confident) {
return first;
}
if (path.isIdentifier()) {
return { confident: true, value: path.node.name };
}
return { confident: false };
}
evaluate(path: NodePath): EvaluateResult {
let known = this.knownPaths.get(path);
if (known) {
return known;
}
let result = this.realEvaluate(path);
return result;
}
private realEvaluate(path: NodePath): EvaluateResult {
let builtIn = path.evaluate();
if (isConfidentResult(builtIn)) {
return builtIn;
}
if (path.isMemberExpression()) {
return this.evaluateMember(path, false);
}
// Here we are glossing over the lack of a real OptionalMemberExpression type
// in our @babel/traverse typings.
if (path.node.type === 'OptionalMemberExpression') {
return this.evaluateMember(path as NodePath<OptionalMemberExpression>, true);
}
if (path.isStringLiteral()) {
return { confident: true, value: path.node.value };
}
if (path.isNumericLiteral()) {
return { confident: true, value: path.node.value };
}
if (path.isBooleanLiteral()) {
return { confident: true, value: path.node.value };
}
if (path.isNullLiteral()) {
return { confident: true, value: null };
}
if (path.isObjectExpression()) {
let props = assertArray(path.get('properties')).map(p => {
let key = assertNotArray(p.get('key'));
let keyEvalValue = this.evaluateKey(key);
let value = assertNotArray(p.get('value'));
let valueEvalValue = this.evaluate(value);
return [keyEvalValue, valueEvalValue];
});
for (let [k, v] of props) {
if (!k.confident || !v.confident) {
return { confident: false };
}
}
let confidentProps = props as ConfidentResult[][];
return {
confident: true,
get value() {
let result: any = {};
for (let [k, v] of confidentProps) {
result[k.value] = v.value;
}
return result;
},
};
}
if (path.isArrayExpression()) {
let elements = path.get('elements').map(element => {
return this.evaluate(element as NodePath);
});
if (elements.every(element => element.confident)) {
let confidentElements = elements as ConfidentResult[];
return {
confident: true,
get value() {
return confidentElements.map(element => element.value);
},
};
}
}
if (path.isAssignmentExpression()) {
let leftPath = path.get('left');
if (leftPath.isIdentifier()) {
let rightPath = path.get('right');
let right = this.evaluate(rightPath);
if (right.confident) {
this.locals[leftPath.node.name] = right.value;
return right;
}
}
}
if (path.isCallExpression()) {
let result = this.maybeEvaluateRuntimeConfig(path);
if (result.confident) {
return result;
}
result = this.evaluateMacroCall(path);
if (result.confident) {
return result;
}
}
if (path.isLogicalExpression() || path.isBinaryExpression()) {
let operator = path.node.operator as string;
if (binops[operator]) {
let leftOperand = this.evaluate(path.get('left') as NodePath<Expression>);
if (leftOperand.confident) {
let rightOperand = this.evaluate(path.get('right') as NodePath<Expression>);
if (leftOperand.confident && rightOperand.confident) {
let value = binops[operator](leftOperand.value, rightOperand.value);
return { confident: true, value };
}
}
}
return { confident: false };
}
if (path.isConditionalExpression()) {
let test = this.evaluate(path.get('test'));
if (test.confident) {
let result = test.value ? this.evaluate(path.get('consequent')) : this.evaluate(path.get('alternate'));
if (result.confident) {
return result;
}
}
}
if (path.isUnaryExpression()) {
let operator = path.node.operator as string;
if (unops[operator]) {
let operand = this.evaluate(path.get('argument') as NodePath<Expression>);
if (operand.confident) {
let value = unops[operator](operand.value);
return { confident: true, value };
}
}
return { confident: false };
}
if (path.isIdentifier()) {
if (!this.locals.hasOwnProperty(path.node.name)) {
return { confident: false };
}
return { confident: true, value: this.locals[path.node.name] };
}
return { confident: false };
}
// This handles the presence of our runtime-mode getConfig functions. We want
// to designate them as { confident: true }, because it's important that we
// give feedback even in runtime-mode if the developer is trying to pass
// non-static arguments somewhere they're not supposed to. But we don't
// actually want to calculate their value here because that has been deferred
// to runtime. That's why we've made `value` lazy. It lets us check the
// confidence without actually forcing the value.
private maybeEvaluateRuntimeConfig(path: NodePath<CallExpression>): EvaluateResult {
let callee = path.get('callee');
if (callee.isIdentifier()) {
let { name } = callee.node;
// Does the identifier refer to our runtime config?
if (this.state?.neededRuntimeImports.get(name) === 'config') {
return {
confident: true,
get value() {
throw new Error(`bug in @embroider/macros: didn't expect to need to evaluate this value`);
},
};
}
}
return { confident: false };
}
evaluateMacroCall(path: NodePath<CallExpression>): EvaluateResult {
if (!this.state) {
return { confident: false };
}
let callee = path.get('callee');
if (callee.referencesImport('@embroider/macros', 'dependencySatisfies')) {
return { confident: true, value: dependencySatisfies(path, this.state) };
}
if (callee.referencesImport('@embroider/macros', 'moduleExists')) {
return { confident: true, value: moduleExists(path, this.state) };
}
if (callee.referencesImport('@embroider/macros', 'getConfig')) {
return { confident: true, value: getConfig(path, this.state, 'package') };
}
if (callee.referencesImport('@embroider/macros', 'getOwnConfig')) {
return { confident: true, value: getConfig(path, this.state, 'own') };
}
if (callee.referencesImport('@embroider/macros', 'getGlobalConfig')) {
return { confident: true, value: getConfig(path, this.state, 'getGlobalConfig') };
}
if (callee.referencesImport('@embroider/macros', 'isDevelopingApp')) {
return {
confident: true,
value: Boolean(
this.state.opts.appPackageRoot &&
this.state.opts.isDevelopingPackageRoots.includes(this.state.opts.appPackageRoot)
),
};
}
if (callee.referencesImport('@embroider/macros', 'isDevelopingThisPackage')) {
return {
confident: true,
value: this.state.opts.isDevelopingPackageRoots.includes(owningPackage(path, this.state).root),
};
}
if (callee.referencesImport('@embroider/macros', 'isTesting')) {
let g = getConfig(path, this.state, 'getGlobalConfig') as any;
let e = g && g['@embroider/macros'];
let value = Boolean(e && e.isTesting);
return { confident: true, value };
}
return { confident: false };
}
}
// these next two functions are here because the type definitions we're using
// don't seem to know exactly which NodePath properties are arrays and which
// aren't.
export function assertNotArray<T>(input: T | T[]): T {
if (Array.isArray(input)) {
throw new Error(`bug: not supposed to be an array`);
}
return input;
}
export function assertArray<T>(input: T | T[]): T[] {
if (!Array.isArray(input)) {
throw new Error(`bug: supposed to be an array`);
}
return input;
}
export function buildLiterals(value: unknown | undefined, babelContext: BabelContext): Identifier | ObjectExpression {
if (typeof value === 'undefined') {
return babelContext.types.identifier('undefined');
}
let statement = babelContext.template(`a(${JSON.stringify(value)})`)() as ExpressionStatement;
let expression = statement.expression as CallExpression;
return expression.arguments[0] as ObjectExpression;
}