1 | /*
|
---|
2 | MIT License http://www.opensource.org/licenses/mit-license.php
|
---|
3 | Author Tobias Koppers @sokra
|
---|
4 | */
|
---|
5 |
|
---|
6 | "use strict";
|
---|
7 |
|
---|
8 | const UnsupportedFeatureWarning = require("../UnsupportedFeatureWarning");
|
---|
9 | const ConstDependency = require("../dependencies/ConstDependency");
|
---|
10 | const BasicEvaluatedExpression = require("./BasicEvaluatedExpression");
|
---|
11 |
|
---|
12 | /** @typedef {import("estree").Expression} ExpressionNode */
|
---|
13 | /** @typedef {import("estree").Node} Node */
|
---|
14 | /** @typedef {import("./JavascriptParser")} JavascriptParser */
|
---|
15 |
|
---|
16 | /**
|
---|
17 | * @param {JavascriptParser} parser the parser
|
---|
18 | * @param {string} value the const value
|
---|
19 | * @param {string[]=} runtimeRequirements runtime requirements
|
---|
20 | * @returns {function(ExpressionNode): true} plugin function
|
---|
21 | */
|
---|
22 | exports.toConstantDependency = (parser, value, runtimeRequirements) => {
|
---|
23 | return function constDependency(expr) {
|
---|
24 | const dep = new ConstDependency(value, expr.range, runtimeRequirements);
|
---|
25 | dep.loc = expr.loc;
|
---|
26 | parser.state.module.addPresentationalDependency(dep);
|
---|
27 | return true;
|
---|
28 | };
|
---|
29 | };
|
---|
30 |
|
---|
31 | /**
|
---|
32 | * @param {string} value the string value
|
---|
33 | * @returns {function(ExpressionNode): BasicEvaluatedExpression} plugin function
|
---|
34 | */
|
---|
35 | exports.evaluateToString = value => {
|
---|
36 | return function stringExpression(expr) {
|
---|
37 | return new BasicEvaluatedExpression().setString(value).setRange(expr.range);
|
---|
38 | };
|
---|
39 | };
|
---|
40 |
|
---|
41 | /**
|
---|
42 | * @param {number} value the number value
|
---|
43 | * @returns {function(ExpressionNode): BasicEvaluatedExpression} plugin function
|
---|
44 | */
|
---|
45 | exports.evaluateToNumber = value => {
|
---|
46 | return function stringExpression(expr) {
|
---|
47 | return new BasicEvaluatedExpression().setNumber(value).setRange(expr.range);
|
---|
48 | };
|
---|
49 | };
|
---|
50 |
|
---|
51 | /**
|
---|
52 | * @param {boolean} value the boolean value
|
---|
53 | * @returns {function(ExpressionNode): BasicEvaluatedExpression} plugin function
|
---|
54 | */
|
---|
55 | exports.evaluateToBoolean = value => {
|
---|
56 | return function booleanExpression(expr) {
|
---|
57 | return new BasicEvaluatedExpression()
|
---|
58 | .setBoolean(value)
|
---|
59 | .setRange(expr.range);
|
---|
60 | };
|
---|
61 | };
|
---|
62 |
|
---|
63 | /**
|
---|
64 | * @param {string} identifier identifier
|
---|
65 | * @param {string} rootInfo rootInfo
|
---|
66 | * @param {function(): string[]} getMembers getMembers
|
---|
67 | * @param {boolean|null=} truthy is truthy, null if nullish
|
---|
68 | * @returns {function(ExpressionNode): BasicEvaluatedExpression} callback
|
---|
69 | */
|
---|
70 | exports.evaluateToIdentifier = (identifier, rootInfo, getMembers, truthy) => {
|
---|
71 | return function identifierExpression(expr) {
|
---|
72 | let evaluatedExpression = new BasicEvaluatedExpression()
|
---|
73 | .setIdentifier(identifier, rootInfo, getMembers)
|
---|
74 | .setSideEffects(false)
|
---|
75 | .setRange(expr.range);
|
---|
76 | switch (truthy) {
|
---|
77 | case true:
|
---|
78 | evaluatedExpression.setTruthy();
|
---|
79 | evaluatedExpression.setNullish(false);
|
---|
80 | break;
|
---|
81 | case null:
|
---|
82 | evaluatedExpression.setFalsy();
|
---|
83 | evaluatedExpression.setNullish(true);
|
---|
84 | break;
|
---|
85 | case false:
|
---|
86 | evaluatedExpression.setFalsy();
|
---|
87 | break;
|
---|
88 | }
|
---|
89 |
|
---|
90 | return evaluatedExpression;
|
---|
91 | };
|
---|
92 | };
|
---|
93 |
|
---|
94 | exports.expressionIsUnsupported = (parser, message) => {
|
---|
95 | return function unsupportedExpression(expr) {
|
---|
96 | const dep = new ConstDependency("(void 0)", expr.range, null);
|
---|
97 | dep.loc = expr.loc;
|
---|
98 | parser.state.module.addPresentationalDependency(dep);
|
---|
99 | if (!parser.state.module) return;
|
---|
100 | parser.state.module.addWarning(
|
---|
101 | new UnsupportedFeatureWarning(message, expr.loc)
|
---|
102 | );
|
---|
103 | return true;
|
---|
104 | };
|
---|
105 | };
|
---|
106 |
|
---|
107 | exports.skipTraversal = () => true;
|
---|
108 |
|
---|
109 | exports.approve = () => true;
|
---|