1 | 'use strict';
|
2 |
|
3 | const t = require('babel-types');
|
4 |
|
5 | function canEvaluate(staticNamespace, exprNode) {
|
6 |
|
7 | if (t.isTemplateLiteral(exprNode)) {
|
8 | for (let idx = -1, len = exprNode.expressions.length; ++idx < len; ) {
|
9 | const expr = exprNode.expressions[idx];
|
10 | if (!canEvaluate(staticNamespace, expr)) {
|
11 | return false;
|
12 | }
|
13 | }
|
14 | return true;
|
15 | }
|
16 |
|
17 |
|
18 | if (t.isLiteral(exprNode)) {
|
19 | return true;
|
20 | }
|
21 |
|
22 |
|
23 | if (t.isUnaryExpression(exprNode) && exprNode.operator === '-') {
|
24 | return canEvaluate(staticNamespace, exprNode.argument);
|
25 | }
|
26 |
|
27 |
|
28 | if (t.isIdentifier(exprNode)) {
|
29 | return (
|
30 | typeof staticNamespace === 'object' &&
|
31 | staticNamespace !== null &&
|
32 | staticNamespace.hasOwnProperty(exprNode.name)
|
33 | );
|
34 | }
|
35 |
|
36 |
|
37 | if (t.isMemberExpression(exprNode)) {
|
38 | return (
|
39 |
|
40 | canEvaluate(staticNamespace, exprNode.object) &&
|
41 |
|
42 | ((t.isIdentifier(exprNode.property) && !exprNode.computed) ||
|
43 |
|
44 | canEvaluate(staticNamespace, exprNode.property))
|
45 | );
|
46 | }
|
47 |
|
48 |
|
49 | if (t.isBinaryExpression(exprNode)) {
|
50 | return (
|
51 | canEvaluate(staticNamespace, exprNode.left) &&
|
52 | canEvaluate(staticNamespace, exprNode.right)
|
53 | );
|
54 | }
|
55 | return false;
|
56 | }
|
57 |
|
58 | module.exports = canEvaluate;
|