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.

154 lines
4.6 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 HarmonyExportExpressionDependency = require("./HarmonyExportExpressionDependency");
  7. const HarmonyImportSideEffectDependency = require("./HarmonyImportSideEffectDependency");
  8. const HarmonyExportHeaderDependency = require("./HarmonyExportHeaderDependency");
  9. const HarmonyExportSpecifierDependency = require("./HarmonyExportSpecifierDependency");
  10. const HarmonyExportImportedSpecifierDependency = require("./HarmonyExportImportedSpecifierDependency");
  11. const ConstDependency = require("./ConstDependency");
  12. module.exports = class HarmonyExportDependencyParserPlugin {
  13. constructor(moduleOptions) {
  14. this.strictExportPresence = moduleOptions.strictExportPresence;
  15. }
  16. apply(parser) {
  17. parser.hooks.export.tap(
  18. "HarmonyExportDependencyParserPlugin",
  19. statement => {
  20. const dep = new HarmonyExportHeaderDependency(
  21. statement.declaration && statement.declaration.range,
  22. statement.range
  23. );
  24. dep.loc = Object.create(statement.loc);
  25. dep.loc.index = -1;
  26. parser.state.current.addDependency(dep);
  27. return true;
  28. }
  29. );
  30. parser.hooks.exportImport.tap(
  31. "HarmonyExportDependencyParserPlugin",
  32. (statement, source) => {
  33. parser.state.lastHarmonyImportOrder =
  34. (parser.state.lastHarmonyImportOrder || 0) + 1;
  35. const clearDep = new ConstDependency("", statement.range);
  36. clearDep.loc = Object.create(statement.loc);
  37. clearDep.loc.index = -1;
  38. parser.state.current.addDependency(clearDep);
  39. const sideEffectDep = new HarmonyImportSideEffectDependency(
  40. source,
  41. parser.state.module,
  42. parser.state.lastHarmonyImportOrder,
  43. parser.state.harmonyParserScope
  44. );
  45. sideEffectDep.loc = Object.create(statement.loc);
  46. sideEffectDep.loc.index = -1;
  47. parser.state.current.addDependency(sideEffectDep);
  48. return true;
  49. }
  50. );
  51. parser.hooks.exportExpression.tap(
  52. "HarmonyExportDependencyParserPlugin",
  53. (statement, expr) => {
  54. const comments = parser.getComments([
  55. statement.range[0],
  56. expr.range[0]
  57. ]);
  58. const dep = new HarmonyExportExpressionDependency(
  59. parser.state.module,
  60. expr.range,
  61. statement.range,
  62. comments
  63. .map(c => {
  64. switch (c.type) {
  65. case "Block":
  66. return `/*${c.value}*/`;
  67. case "Line":
  68. return `//${c.value}\n`;
  69. }
  70. return "";
  71. })
  72. .join("")
  73. );
  74. dep.loc = Object.create(statement.loc);
  75. dep.loc.index = -1;
  76. parser.state.current.addDependency(dep);
  77. return true;
  78. }
  79. );
  80. parser.hooks.exportDeclaration.tap(
  81. "HarmonyExportDependencyParserPlugin",
  82. statement => {}
  83. );
  84. parser.hooks.exportSpecifier.tap(
  85. "HarmonyExportDependencyParserPlugin",
  86. (statement, id, name, idx) => {
  87. const rename = parser.scope.renames.get(id);
  88. let dep;
  89. const harmonyNamedExports = (parser.state.harmonyNamedExports =
  90. parser.state.harmonyNamedExports || new Set());
  91. harmonyNamedExports.add(name);
  92. if (rename === "imported var") {
  93. const settings = parser.state.harmonySpecifier.get(id);
  94. dep = new HarmonyExportImportedSpecifierDependency(
  95. settings.source,
  96. parser.state.module,
  97. settings.sourceOrder,
  98. parser.state.harmonyParserScope,
  99. settings.id,
  100. name,
  101. harmonyNamedExports,
  102. null,
  103. this.strictExportPresence
  104. );
  105. } else {
  106. dep = new HarmonyExportSpecifierDependency(
  107. parser.state.module,
  108. id,
  109. name
  110. );
  111. }
  112. dep.loc = Object.create(statement.loc);
  113. dep.loc.index = idx;
  114. parser.state.current.addDependency(dep);
  115. return true;
  116. }
  117. );
  118. parser.hooks.exportImportSpecifier.tap(
  119. "HarmonyExportDependencyParserPlugin",
  120. (statement, source, id, name, idx) => {
  121. const harmonyNamedExports = (parser.state.harmonyNamedExports =
  122. parser.state.harmonyNamedExports || new Set());
  123. let harmonyStarExports = null;
  124. if (name) {
  125. harmonyNamedExports.add(name);
  126. } else {
  127. harmonyStarExports = parser.state.harmonyStarExports =
  128. parser.state.harmonyStarExports || [];
  129. }
  130. const dep = new HarmonyExportImportedSpecifierDependency(
  131. source,
  132. parser.state.module,
  133. parser.state.lastHarmonyImportOrder,
  134. parser.state.harmonyParserScope,
  135. id,
  136. name,
  137. harmonyNamedExports,
  138. harmonyStarExports && harmonyStarExports.slice(),
  139. this.strictExportPresence
  140. );
  141. if (harmonyStarExports) {
  142. harmonyStarExports.push(dep);
  143. }
  144. dep.loc = Object.create(statement.loc);
  145. dep.loc.index = idx;
  146. parser.state.current.addDependency(dep);
  147. return true;
  148. }
  149. );
  150. }
  151. };