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