index.ts 10.5 KB
Newer Older
Rosanny Sihombing's avatar
Rosanny Sihombing committed
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
import {parseExpression, ParserOptions} from '@babel/parser';
import * as b from '@babel/types';
import binaryOperation from './binaryOperation';

export {ParserOptions as BabylonOptions};

export interface ExpressionToConstantOptions {
  constants?: any;
}

export interface Options extends ExpressionToConstantOptions {
  babylon?: ParserOptions;
}
export function expressionToConstant(
  expression: b.Expression,
  options: ExpressionToConstantOptions = {},
): {constant: true; result: any} | {constant: false; result?: void} {
  let constant = true;
  function toConstant(expression: b.Expression): any {
    if (!constant) return;
    if (b.isArrayExpression(expression)) {
      const result = [];
      for (let i = 0; constant && i < expression.elements.length; i++) {
        const element = expression.elements[i];
        if (b.isSpreadElement(element)) {
          const spread = toConstant(element.argument);
          if (!(isSpreadable(spread) && constant)) {
            constant = false;
          } else {
            result.push(...spread);
          }
        } else if (b.isExpression(element)) {
          result.push(toConstant(element));
        } else {
          constant = false;
        }
      }
      return result;
    }
    if (b.isBinaryExpression(expression)) {
      const left = toConstant(expression.left);
      const right = toConstant(expression.right);
      return constant && binaryOperation(expression.operator, left, right);
    }
    if (b.isBooleanLiteral(expression)) {
      return expression.value;
    }
    if (b.isCallExpression(expression)) {
      const args = [];
      for (let i = 0; constant && i < expression.arguments.length; i++) {
        const arg = expression.arguments[i];
        if (b.isSpreadElement(arg)) {
          const spread = toConstant(arg.argument);
          if (!(isSpreadable(spread) && constant)) {
            constant = false;
          } else {
            args.push(...spread);
          }
        } else if (b.isExpression(arg)) {
          args.push(toConstant(arg));
        } else {
          constant = false;
        }
      }
      if (!constant) return;
      if (b.isMemberExpression(expression.callee)) {
        const object = toConstant(expression.callee.object);
        if (!object || !constant) {
          constant = false;
          return;
        }
        const member = expression.callee.computed
          ? toConstant(expression.callee.property)
          : b.isIdentifier(expression.callee.property)
          ? expression.callee.property.name
          : undefined;
        if (member === undefined && !expression.callee.computed) {
          constant = false;
        }
        if (!constant) return;
        if (canCallMethod(object, '' + member)) {
          return object[member].apply(object, args);
        }
      } else {
        if (!b.isExpression(expression.callee)) {
          constant = false;
          return;
        }
        const callee = toConstant(expression.callee);
        if (!constant) return;
        return callee.apply(null, args);
      }
    }
    if (b.isConditionalExpression(expression)) {
      const test = toConstant(expression.test);
      return test
        ? toConstant(expression.consequent)
        : toConstant(expression.alternate);
    }
    if (b.isIdentifier(expression)) {
      if (
        options.constants &&
        {}.hasOwnProperty.call(options.constants, expression.name)
      ) {
        return options.constants[expression.name];
      }
    }
    if (b.isLogicalExpression(expression)) {
      const left = toConstant(expression.left);
      const right = toConstant(expression.right);
      if (constant && expression.operator === '&&') {
        return left && right;
      }
      if (constant && expression.operator === '||') {
        return left || right;
      }
    }
    if (b.isMemberExpression(expression)) {
      const object = toConstant(expression.object);
      if (!object || !constant) {
        constant = false;
        return;
      }
      const member = expression.computed
        ? toConstant(expression.property)
        : b.isIdentifier(expression.property)
        ? expression.property.name
        : undefined;
      if (member === undefined && !expression.computed) {
        constant = false;
      }
      if (!constant) return;
      if ({}.hasOwnProperty.call(object, '' + member) && member[0] !== '_') {
        return object[member];
      }
    }
    if (b.isNullLiteral(expression)) {
      return null;
    }
    if (b.isNumericLiteral(expression)) {
      return expression.value;
    }
    if (b.isObjectExpression(expression)) {
      const result: any = {};
      for (let i = 0; constant && i < expression.properties.length; i++) {
        const property = expression.properties[i];
        if (b.isObjectProperty(property)) {
          if (property.shorthand) {
            constant = false;
            return;
          }
          const key = property.computed
            ? toConstant(property.key)
            : b.isIdentifier(property.key)
            ? property.key.name
            : b.isStringLiteral(property.key)
            ? property.key.value
            : undefined;
          if (!key || key[0] === '_') {
            constant = false;
          }
          if (!constant) return;
          if (b.isExpression(property.value)) {
            const value = toConstant(property.value);
            if (!constant) return;
            result[key] = value;
          } else {
            constant = false;
          }
        } else if (b.isObjectMethod(property)) {
          constant = false;
        } else if (b.isSpreadProperty(property)) {
          const argument = toConstant(property.argument);
          if (!argument) constant = false;
          if (!constant) return;
          Object.assign(result, argument);
        }
      }
      return result;
    }
    if (b.isParenthesizedExpression(expression)) {
      return toConstant(expression.expression);
    }
    if (b.isRegExpLiteral(expression)) {
      return new RegExp(expression.pattern, expression.flags);
    }
    if (b.isSequenceExpression(expression)) {
      for (let i = 0; i < expression.expressions.length - 1 && constant; i++) {
        toConstant(expression.expressions[i]);
      }
      return toConstant(
        expression.expressions[expression.expressions.length - 1],
      );
    }
    if (b.isStringLiteral(expression)) {
      return expression.value;
    }
    // TODO: TaggedTemplateExpression
    if (b.isTemplateLiteral(expression)) {
      let result = '';
      for (let i = 0; i < expression.quasis.length; i++) {
        const quasi = expression.quasis[i];
        result += quasi.value.cooked;
        if (i < expression.expressions.length) {
          result += '' + toConstant(expression.expressions[i]);
        }
      }
      return result;
    }
    if (b.isUnaryExpression(expression)) {
      const argument = toConstant(expression.argument);
      if (!constant) {
        return;
      }
      switch (expression.operator) {
        case '-':
          return -argument;
        case '+':
          return +argument;
        case '!':
          return !argument;
        case '~':
          return ~argument;
        case 'typeof':
          return typeof argument;
        case 'void':
          return void argument;
      }
    }
    constant = false;
  }
  const result = toConstant(expression);
  return constant ? {constant: true, result} : {constant: false};
}
function isSpreadable(value: any): boolean {
  return (
    typeof value === 'string' ||
    Array.isArray(value) ||
    (typeof Set !== 'undefined' && value instanceof Set) ||
    (typeof Map !== 'undefined' && value instanceof Map)
  );
}
function shallowEqual(a: any, b: any) {
  if (a === b) return true;
  if (a && b && typeof a === 'object' && typeof b === 'object') {
    for (let key in a) {
      if (a[key] !== b[key]) {
        return false;
      }
    }
    for (let key in b) {
      if (a[key] !== b[key]) {
        return false;
      }
    }
    return true;
  }
  return false;
}
function canCallMethod(object: any, member: string): boolean {
  switch (typeof object) {
    case 'boolean':
      switch (member) {
        case 'toString':
          return true;
        default:
          return false;
      }
    case 'number':
      switch (member) {
        case 'toExponential':
        case 'toFixed':
        case 'toPrecision':
        case 'toString':
          return true;
        default:
          return false;
      }
    case 'string':
      switch (member) {
        case 'charAt':
        case 'charCodeAt':
        case 'codePointAt':
        case 'concat':
        case 'endsWith':
        case 'includes':
        case 'indexOf':
        case 'lastIndexOf':
        case 'match':
        case 'normalize':
        case 'padEnd':
        case 'padStart':
        case 'repeat':
        case 'replace':
        case 'search':
        case 'slice':
        case 'split':
        case 'startsWith':
        case 'substr':
        case 'substring':
        case 'toLowerCase':
        case 'toUpperCase':
        case 'trim':
          return true;
        default:
          return false;
      }
    default:
      if (object instanceof RegExp) {
        switch (member) {
          case 'test':
          case 'exec':
            return true;
          default:
            return false;
        }
      }
      return {}.hasOwnProperty.call(object, member) && member[0] !== '_';
  }
}

const EMPTY_OBJECT = {};
let lastSrc = '';
let lastConstants = EMPTY_OBJECT;
let lastOptions = EMPTY_OBJECT;
let lastResult: any = null;
let lastWasConstant = false;
export function isConstant(
  src: string,
  constants: any = EMPTY_OBJECT,
  options: ParserOptions = EMPTY_OBJECT,
) {
  if (
    lastSrc === src &&
    shallowEqual(lastConstants, constants) &&
    shallowEqual(lastOptions, options)
  ) {
    return lastWasConstant;
  }
  lastSrc = src;
  lastConstants = constants;
  let ast: b.Expression | void;
  try {
    ast = parseExpression(src, options);
  } catch (ex) {
    return (lastWasConstant = false);
  }
  const {result, constant} = expressionToConstant(ast, {constants});
  lastResult = result;
  return (lastWasConstant = constant);
}
export function toConstant(
  src: string,
  constants: any = EMPTY_OBJECT,
  options: ParserOptions = EMPTY_OBJECT,
) {
  if (!isConstant(src, constants, options)) {
    throw new Error(JSON.stringify(src) + ' is not constant.');
  }
  return lastResult;
}

export default isConstant;

module.exports = isConstant;
module.exports.default = isConstant;
module.exports.expressionToConstant = expressionToConstant;
module.exports.isConstant = isConstant;
module.exports.toConstant = toConstant;