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} Expression */
|
---|
13 | /** @typedef {import("estree").Node} Node */
|
---|
14 | /** @typedef {import("estree").SourceLocation} SourceLocation */
|
---|
15 | /** @typedef {import("./JavascriptParser")} JavascriptParser */
|
---|
16 | /** @typedef {import("./JavascriptParser").Range} Range */
|
---|
17 |
|
---|
18 | /**
|
---|
19 | * @param {JavascriptParser} parser the parser
|
---|
20 | * @param {string} value the const value
|
---|
21 | * @param {(string[] | null)=} runtimeRequirements runtime requirements
|
---|
22 | * @returns {function(Expression): true} plugin function
|
---|
23 | */
|
---|
24 | module.exports.toConstantDependency = (parser, value, runtimeRequirements) =>
|
---|
25 | function constDependency(expr) {
|
---|
26 | const dep = new ConstDependency(
|
---|
27 | value,
|
---|
28 | /** @type {Range} */ (expr.range),
|
---|
29 | runtimeRequirements
|
---|
30 | );
|
---|
31 | dep.loc = /** @type {SourceLocation} */ (expr.loc);
|
---|
32 | parser.state.module.addPresentationalDependency(dep);
|
---|
33 | return true;
|
---|
34 | };
|
---|
35 |
|
---|
36 | /**
|
---|
37 | * @param {string} value the string value
|
---|
38 | * @returns {function(Expression): BasicEvaluatedExpression} plugin function
|
---|
39 | */
|
---|
40 | module.exports.evaluateToString = value =>
|
---|
41 | function stringExpression(expr) {
|
---|
42 | return new BasicEvaluatedExpression()
|
---|
43 | .setString(value)
|
---|
44 | .setRange(/** @type {Range} */ (expr.range));
|
---|
45 | };
|
---|
46 |
|
---|
47 | /**
|
---|
48 | * @param {number} value the number value
|
---|
49 | * @returns {function(Expression): BasicEvaluatedExpression} plugin function
|
---|
50 | */
|
---|
51 | module.exports.evaluateToNumber = value =>
|
---|
52 | function stringExpression(expr) {
|
---|
53 | return new BasicEvaluatedExpression()
|
---|
54 | .setNumber(value)
|
---|
55 | .setRange(/** @type {Range} */ (expr.range));
|
---|
56 | };
|
---|
57 |
|
---|
58 | /**
|
---|
59 | * @param {boolean} value the boolean value
|
---|
60 | * @returns {function(Expression): BasicEvaluatedExpression} plugin function
|
---|
61 | */
|
---|
62 | module.exports.evaluateToBoolean = value =>
|
---|
63 | function booleanExpression(expr) {
|
---|
64 | return new BasicEvaluatedExpression()
|
---|
65 | .setBoolean(value)
|
---|
66 | .setRange(/** @type {Range} */ (expr.range));
|
---|
67 | };
|
---|
68 |
|
---|
69 | /**
|
---|
70 | * @param {string} identifier identifier
|
---|
71 | * @param {string} rootInfo rootInfo
|
---|
72 | * @param {function(): string[]} getMembers getMembers
|
---|
73 | * @param {boolean|null=} truthy is truthy, null if nullish
|
---|
74 | * @returns {function(Expression): BasicEvaluatedExpression} callback
|
---|
75 | */
|
---|
76 | module.exports.evaluateToIdentifier = (
|
---|
77 | identifier,
|
---|
78 | rootInfo,
|
---|
79 | getMembers,
|
---|
80 | truthy
|
---|
81 | ) =>
|
---|
82 | function identifierExpression(expr) {
|
---|
83 | const evaluatedExpression = new BasicEvaluatedExpression()
|
---|
84 | .setIdentifier(identifier, rootInfo, getMembers)
|
---|
85 | .setSideEffects(false)
|
---|
86 | .setRange(/** @type {Range} */ (expr.range));
|
---|
87 | switch (truthy) {
|
---|
88 | case true:
|
---|
89 | evaluatedExpression.setTruthy();
|
---|
90 | break;
|
---|
91 | case null:
|
---|
92 | evaluatedExpression.setNullish(true);
|
---|
93 | break;
|
---|
94 | case false:
|
---|
95 | evaluatedExpression.setFalsy();
|
---|
96 | break;
|
---|
97 | }
|
---|
98 |
|
---|
99 | return evaluatedExpression;
|
---|
100 | };
|
---|
101 |
|
---|
102 | /**
|
---|
103 | * @param {JavascriptParser} parser the parser
|
---|
104 | * @param {string} message the message
|
---|
105 | * @returns {function(Expression): boolean | undefined} callback to handle unsupported expression
|
---|
106 | */
|
---|
107 | module.exports.expressionIsUnsupported = (parser, message) =>
|
---|
108 | function unsupportedExpression(expr) {
|
---|
109 | const dep = new ConstDependency(
|
---|
110 | "(void 0)",
|
---|
111 | /** @type {Range} */ (expr.range),
|
---|
112 | null
|
---|
113 | );
|
---|
114 | dep.loc = /** @type {SourceLocation} */ (expr.loc);
|
---|
115 | parser.state.module.addPresentationalDependency(dep);
|
---|
116 | if (!parser.state.module) return;
|
---|
117 | parser.state.module.addWarning(
|
---|
118 | new UnsupportedFeatureWarning(
|
---|
119 | message,
|
---|
120 | /** @type {SourceLocation} */ (expr.loc)
|
---|
121 | )
|
---|
122 | );
|
---|
123 | return true;
|
---|
124 | };
|
---|
125 |
|
---|
126 | module.exports.skipTraversal = () => true;
|
---|
127 |
|
---|
128 | module.exports.approve = () => true;
|
---|