UI for Zipcoin Blue

HarmonyExportDependencyParserPlugin.js 3.9KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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 HarmonyExportHeaderDependency = require("./HarmonyExportHeaderDependency");
  8. const HarmonyExportSpecifierDependency = require("./HarmonyExportSpecifierDependency");
  9. const HarmonyExportImportedSpecifierDependency = require("./HarmonyExportImportedSpecifierDependency");
  10. const HarmonyImportDependency = require("./HarmonyImportDependency");
  11. const HarmonyModulesHelpers = require("./HarmonyModulesHelpers");
  12. module.exports = class HarmonyExportDependencyParserPlugin {
  13. apply(parser) {
  14. parser.plugin("export", statement => {
  15. const dep = new HarmonyExportHeaderDependency(statement.declaration && statement.declaration.range, statement.range);
  16. dep.loc = Object.create(statement.loc);
  17. dep.loc.index = -1;
  18. parser.state.current.addDependency(dep);
  19. return true;
  20. });
  21. parser.plugin("export import", (statement, source) => {
  22. const dep = new HarmonyImportDependency(source, HarmonyModulesHelpers.getNewModuleVar(parser.state, source), statement.range);
  23. dep.loc = Object.create(statement.loc);
  24. dep.loc.index = -1;
  25. parser.state.current.addDependency(dep);
  26. parser.state.lastHarmonyImport = dep;
  27. return true;
  28. });
  29. parser.plugin("export expression", (statement, expr) => {
  30. const dep = new HarmonyExportExpressionDependency(parser.state.module, expr.range, statement.range);
  31. dep.loc = Object.create(statement.loc);
  32. dep.loc.index = -1;
  33. parser.state.current.addDependency(dep);
  34. return true;
  35. });
  36. parser.plugin("export declaration", statement => {});
  37. parser.plugin("export specifier", (statement, id, name, idx) => {
  38. const rename = parser.scope.renames[`$${id}`];
  39. let dep;
  40. const harmonyNamedExports = parser.state.harmonyNamedExports = parser.state.harmonyNamedExports || new Set();
  41. harmonyNamedExports.add(name);
  42. if(rename === "imported var") {
  43. const settings = parser.state.harmonySpecifier[`$${id}`];
  44. dep = new HarmonyExportImportedSpecifierDependency(parser.state.module, settings[0], settings[1], settings[2], name, harmonyNamedExports, null);
  45. } else {
  46. const immutable = statement.declaration && isImmutableStatement(statement.declaration);
  47. const hoisted = statement.declaration && isHoistedStatement(statement.declaration);
  48. dep = new HarmonyExportSpecifierDependency(parser.state.module, id, name, !immutable || hoisted ? -2 : (statement.range[1] + 0.5), immutable);
  49. }
  50. dep.loc = Object.create(statement.loc);
  51. dep.loc.index = idx;
  52. parser.state.current.addDependency(dep);
  53. return true;
  54. });
  55. parser.plugin("export import specifier", (statement, source, id, name, idx) => {
  56. const harmonyNamedExports = parser.state.harmonyNamedExports = parser.state.harmonyNamedExports || new Set();
  57. let harmonyStarExports = null;
  58. if(name) {
  59. harmonyNamedExports.add(name);
  60. } else {
  61. harmonyStarExports = parser.state.harmonyStarExports = parser.state.harmonyStarExports || [];
  62. }
  63. const dep = new HarmonyExportImportedSpecifierDependency(parser.state.module, parser.state.lastHarmonyImport, HarmonyModulesHelpers.getModuleVar(parser.state, source), id, name, harmonyNamedExports, harmonyStarExports && harmonyStarExports.slice());
  64. if(harmonyStarExports) {
  65. harmonyStarExports.push(dep);
  66. }
  67. dep.loc = Object.create(statement.loc);
  68. dep.loc.index = idx;
  69. parser.state.current.addDependency(dep);
  70. return true;
  71. });
  72. }
  73. };
  74. function isImmutableStatement(statement) {
  75. if(statement.type === "FunctionDeclaration") return true;
  76. if(statement.type === "ClassDeclaration") return true;
  77. if(statement.type === "VariableDeclaration" && statement.kind === "const") return true;
  78. return false;
  79. }
  80. function isHoistedStatement(statement) {
  81. if(statement.type === "FunctionDeclaration") return true;
  82. return false;
  83. }