UI for Zipcoin Blue

HarmonyImportDependencyParserPlugin.js 5.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. /*
  2. MIT License http://www.opensource.org/licenses/mit-license.php
  3. Author Tobias Koppers @sokra
  4. */
  5. "use strict";
  6. const HarmonyImportDependency = require("./HarmonyImportDependency");
  7. const HarmonyImportSpecifierDependency = require("./HarmonyImportSpecifierDependency");
  8. const HarmonyAcceptImportDependency = require("./HarmonyAcceptImportDependency");
  9. const HarmonyAcceptDependency = require("./HarmonyAcceptDependency");
  10. const HarmonyModulesHelpers = require("./HarmonyModulesHelpers");
  11. module.exports = class HarmonyImportDependencyParserPlugin {
  12. constructor(moduleOptions) {
  13. this.strictExportPresence = moduleOptions.strictExportPresence;
  14. this.strictThisContextOnImports = moduleOptions.strictThisContextOnImports;
  15. }
  16. apply(parser) {
  17. parser.plugin("import", (statement, source) => {
  18. const dep = new HarmonyImportDependency(source, HarmonyModulesHelpers.getNewModuleVar(parser.state, source), statement.range);
  19. dep.loc = statement.loc;
  20. parser.state.current.addDependency(dep);
  21. parser.state.lastHarmonyImport = dep;
  22. return true;
  23. });
  24. parser.plugin("import specifier", (statement, source, id, name) => {
  25. parser.scope.definitions.length--;
  26. parser.scope.renames[`$${name}`] = "imported var";
  27. if(!parser.state.harmonySpecifier) parser.state.harmonySpecifier = {};
  28. parser.state.harmonySpecifier[`$${name}`] = [parser.state.lastHarmonyImport, HarmonyModulesHelpers.getModuleVar(parser.state, source), id];
  29. return true;
  30. });
  31. parser.plugin("expression imported var", (expr) => {
  32. const name = expr.name;
  33. const settings = parser.state.harmonySpecifier[`$${name}`];
  34. const dep = new HarmonyImportSpecifierDependency(settings[0], settings[1], settings[2], name, expr.range, this.strictExportPresence);
  35. dep.shorthand = parser.scope.inShorthand;
  36. dep.directImport = true;
  37. dep.loc = expr.loc;
  38. parser.state.current.addDependency(dep);
  39. return true;
  40. });
  41. parser.plugin("expression imported var.*", (expr) => {
  42. const name = expr.object.name;
  43. const settings = parser.state.harmonySpecifier[`$${name}`];
  44. if(settings[2] !== null)
  45. return false;
  46. const dep = new HarmonyImportSpecifierDependency(settings[0], settings[1], expr.property.name || expr.property.value, name, expr.range, this.strictExportPresence);
  47. dep.shorthand = parser.scope.inShorthand;
  48. dep.directImport = false;
  49. dep.loc = expr.loc;
  50. parser.state.current.addDependency(dep);
  51. return true;
  52. });
  53. if(this.strictThisContextOnImports) {
  54. // only in case when we strictly follow the spec we need a special case here
  55. parser.plugin("call imported var.*", (expr) => {
  56. if(expr.callee.type !== "MemberExpression") return;
  57. if(expr.callee.object.type !== "Identifier") return;
  58. const name = expr.callee.object.name;
  59. const settings = parser.state.harmonySpecifier[`$${name}`];
  60. if(settings[2] !== null)
  61. return false;
  62. const dep = new HarmonyImportSpecifierDependency(settings[0], settings[1], expr.callee.property.name || expr.callee.property.value, name, expr.callee.range, this.strictExportPresence);
  63. dep.shorthand = parser.scope.inShorthand;
  64. dep.directImport = false;
  65. dep.namespaceObjectAsContext = true;
  66. dep.loc = expr.callee.loc;
  67. parser.state.current.addDependency(dep);
  68. if(expr.arguments)
  69. parser.walkExpressions(expr.arguments);
  70. return true;
  71. });
  72. }
  73. parser.plugin("call imported var", (expr) => {
  74. const args = expr.arguments;
  75. const fullExpr = expr;
  76. expr = expr.callee;
  77. if(expr.type !== "Identifier") return;
  78. const name = expr.name;
  79. const settings = parser.state.harmonySpecifier[`$${name}`];
  80. const dep = new HarmonyImportSpecifierDependency(settings[0], settings[1], settings[2], name, expr.range, this.strictExportPresence);
  81. dep.directImport = true;
  82. dep.callArgs = args;
  83. dep.call = fullExpr;
  84. dep.loc = expr.loc;
  85. parser.state.current.addDependency(dep);
  86. if(args)
  87. parser.walkExpressions(args);
  88. return true;
  89. });
  90. parser.plugin("hot accept callback", (expr, requests) => {
  91. const dependencies = requests
  92. .filter(request => HarmonyModulesHelpers.checkModuleVar(parser.state, request))
  93. .map(request => {
  94. const dep = new HarmonyAcceptImportDependency(request, HarmonyModulesHelpers.getModuleVar(parser.state, request), expr.range);
  95. dep.loc = expr.loc;
  96. parser.state.current.addDependency(dep);
  97. return dep;
  98. });
  99. if(dependencies.length > 0) {
  100. const dep = new HarmonyAcceptDependency(expr.range, dependencies, true);
  101. dep.loc = expr.loc;
  102. parser.state.current.addDependency(dep);
  103. }
  104. });
  105. parser.plugin("hot accept without callback", (expr, requests) => {
  106. const dependencies = requests
  107. .filter(request => HarmonyModulesHelpers.checkModuleVar(parser.state, request))
  108. .map(request => {
  109. const dep = new HarmonyAcceptImportDependency(request, HarmonyModulesHelpers.getModuleVar(parser.state, request), expr.range);
  110. dep.loc = expr.loc;
  111. parser.state.current.addDependency(dep);
  112. return dep;
  113. });
  114. if(dependencies.length > 0) {
  115. const dep = new HarmonyAcceptDependency(expr.range, dependencies, false);
  116. dep.loc = expr.loc;
  117. parser.state.current.addDependency(dep);
  118. }
  119. });
  120. }
  121. };