JavascriptParserHelpers.js 3.7 KB

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