You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

103 lines
2.8 KiB

4 years ago
  1. /*
  2. MIT License http://www.opensource.org/licenses/mit-license.php
  3. Author Tobias Koppers @sokra
  4. */
  5. "use strict";
  6. const path = require("path");
  7. const BasicEvaluatedExpression = require("./BasicEvaluatedExpression");
  8. const ConstDependency = require("./dependencies/ConstDependency");
  9. const UnsupportedFeatureWarning = require("./UnsupportedFeatureWarning");
  10. const ParserHelpers = exports;
  11. ParserHelpers.addParsedVariableToModule = (parser, name, expression) => {
  12. if (!parser.state.current.addVariable) return false;
  13. var deps = [];
  14. parser.parse(expression, {
  15. current: {
  16. addDependency: dep => {
  17. dep.userRequest = name;
  18. deps.push(dep);
  19. }
  20. },
  21. module: parser.state.module
  22. });
  23. parser.state.current.addVariable(name, expression, deps);
  24. return true;
  25. };
  26. ParserHelpers.requireFileAsExpression = (context, pathToModule) => {
  27. var moduleJsPath = path.relative(context, pathToModule);
  28. if (!/^[A-Z]:/i.test(moduleJsPath)) {
  29. moduleJsPath = "./" + moduleJsPath.replace(/\\/g, "/");
  30. }
  31. return "require(" + JSON.stringify(moduleJsPath) + ")";
  32. };
  33. ParserHelpers.toConstantDependency = (parser, value) => {
  34. return function constDependency(expr) {
  35. var dep = new ConstDependency(value, expr.range, false);
  36. dep.loc = expr.loc;
  37. parser.state.current.addDependency(dep);
  38. return true;
  39. };
  40. };
  41. ParserHelpers.toConstantDependencyWithWebpackRequire = (parser, value) => {
  42. return function constDependencyWithWebpackRequire(expr) {
  43. var dep = new ConstDependency(value, expr.range, true);
  44. dep.loc = expr.loc;
  45. parser.state.current.addDependency(dep);
  46. return true;
  47. };
  48. };
  49. ParserHelpers.evaluateToString = value => {
  50. return function stringExpression(expr) {
  51. return new BasicEvaluatedExpression().setString(value).setRange(expr.range);
  52. };
  53. };
  54. ParserHelpers.evaluateToBoolean = value => {
  55. return function booleanExpression(expr) {
  56. return new BasicEvaluatedExpression()
  57. .setBoolean(value)
  58. .setRange(expr.range);
  59. };
  60. };
  61. ParserHelpers.evaluateToIdentifier = (identifier, truthy) => {
  62. return function identifierExpression(expr) {
  63. let evex = new BasicEvaluatedExpression()
  64. .setIdentifier(identifier)
  65. .setRange(expr.range);
  66. if (truthy === true) {
  67. evex = evex.setTruthy();
  68. } else if (truthy === false) {
  69. evex = evex.setFalsy();
  70. }
  71. return evex;
  72. };
  73. };
  74. ParserHelpers.expressionIsUnsupported = (parser, message) => {
  75. return function unsupportedExpression(expr) {
  76. var dep = new ConstDependency("(void 0)", expr.range, false);
  77. dep.loc = expr.loc;
  78. parser.state.current.addDependency(dep);
  79. if (!parser.state.module) return;
  80. parser.state.module.warnings.push(
  81. new UnsupportedFeatureWarning(parser.state.module, message, expr.loc)
  82. );
  83. return true;
  84. };
  85. };
  86. ParserHelpers.skipTraversal = function skipTraversal() {
  87. return true;
  88. };
  89. ParserHelpers.approve = function approve() {
  90. return true;
  91. };