a zip code crypto-currency system good for red ONLY

RequireResolveDependencyParserPlugin.js 2.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. /*
  2. MIT License http://www.opensource.org/licenses/mit-license.php
  3. Author Tobias Koppers @sokra
  4. */
  5. "use strict";
  6. const RequireResolveDependency = require("./RequireResolveDependency");
  7. const RequireResolveContextDependency = require("./RequireResolveContextDependency");
  8. const RequireResolveHeaderDependency = require("./RequireResolveHeaderDependency");
  9. const ContextDependencyHelpers = require("./ContextDependencyHelpers");
  10. class RequireResolveDependencyParserPlugin {
  11. constructor(options) {
  12. this.options = options;
  13. }
  14. apply(parser) {
  15. const options = this.options;
  16. parser.plugin("call require.resolve", (expr) => {
  17. return parser.applyPluginsBailResult("call require.resolve(Weak)", expr, false);
  18. });
  19. parser.plugin("call require.resolveWeak", (expr) => {
  20. return parser.applyPluginsBailResult("call require.resolve(Weak)", expr, true);
  21. });
  22. parser.plugin("call require.resolve(Weak)", (expr, weak) => {
  23. if(expr.arguments.length !== 1) return;
  24. const param = parser.evaluateExpression(expr.arguments[0]);
  25. if(param.isConditional()) {
  26. param.options.forEach((option) => {
  27. const result = parser.applyPluginsBailResult("call require.resolve(Weak):item", expr, option, weak);
  28. if(result === undefined) {
  29. parser.applyPluginsBailResult("call require.resolve(Weak):context", expr, option, weak);
  30. }
  31. });
  32. const dep = new RequireResolveHeaderDependency(expr.callee.range);
  33. dep.loc = expr.loc;
  34. parser.state.current.addDependency(dep);
  35. return true;
  36. } else {
  37. const result = parser.applyPluginsBailResult("call require.resolve(Weak):item", expr, param, weak);
  38. if(result === undefined) {
  39. parser.applyPluginsBailResult("call require.resolve(Weak):context", expr, param, weak);
  40. }
  41. const dep = new RequireResolveHeaderDependency(expr.callee.range);
  42. dep.loc = expr.loc;
  43. parser.state.current.addDependency(dep);
  44. return true;
  45. }
  46. });
  47. parser.plugin("call require.resolve(Weak):item", (expr, param, weak) => {
  48. if(param.isString()) {
  49. const dep = new RequireResolveDependency(param.string, param.range);
  50. dep.loc = expr.loc;
  51. dep.optional = !!parser.scope.inTry;
  52. dep.weak = weak;
  53. parser.state.current.addDependency(dep);
  54. return true;
  55. }
  56. });
  57. parser.plugin("call require.resolve(Weak):context", (expr, param, weak) => {
  58. const dep = ContextDependencyHelpers.create(RequireResolveContextDependency, param.range, param, expr, options);
  59. if(!dep) return;
  60. dep.loc = expr.loc;
  61. dep.optional = !!parser.scope.inTry;
  62. dep.async = weak ? "weak" : false;
  63. parser.state.current.addDependency(dep);
  64. return true;
  65. });
  66. }
  67. }
  68. module.exports = RequireResolveDependencyParserPlugin;