a zip code crypto-currency system good for red ONLY

HarmonyImportSpecifierDependency.js 3.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. /*
  2. MIT License http://www.opensource.org/licenses/mit-license.php
  3. Author Tobias Koppers @sokra
  4. */
  5. "use strict";
  6. const NullDependency = require("./NullDependency");
  7. class HarmonyImportSpecifierDependency extends NullDependency {
  8. constructor(importDependency, importedVar, id, name, range, strictExportPresence) {
  9. super();
  10. this.importDependency = importDependency;
  11. this.importedVar = importedVar;
  12. this.id = id;
  13. this.name = name;
  14. this.range = range;
  15. this.strictExportPresence = strictExportPresence;
  16. this.namespaceObjectAsContext = false;
  17. this.callArgs = undefined;
  18. this.call = undefined;
  19. this.directImport = undefined;
  20. }
  21. get type() {
  22. return "harmony import specifier";
  23. }
  24. getReference() {
  25. if(!this.importDependency.module) return null;
  26. return {
  27. module: this.importDependency.module,
  28. importedNames: this.id && !this.namespaceObjectAsContext ? [this.id] : true
  29. };
  30. }
  31. getWarnings() {
  32. if(this.strictExportPresence) {
  33. return [];
  34. }
  35. return this._getErrors();
  36. }
  37. getErrors() {
  38. if(this.strictExportPresence) {
  39. return this._getErrors();
  40. }
  41. return [];
  42. }
  43. _getErrors() {
  44. const importedModule = this.importDependency.module;
  45. if(!importedModule || !importedModule.meta || !importedModule.meta.harmonyModule) {
  46. return;
  47. }
  48. if(!this.id) {
  49. return;
  50. }
  51. if(importedModule.isProvided(this.id) !== false) {
  52. return;
  53. }
  54. const idIsNotNameMessage = this.id !== this.name ? ` (imported as '${this.name}')` : "";
  55. const errorMessage = `"export '${this.id}'${idIsNotNameMessage} was not found in '${this.importDependency.userRequest}'`;
  56. const err = new Error(errorMessage);
  57. err.hideStack = true;
  58. return [err];
  59. }
  60. updateHash(hash) {
  61. super.updateHash(hash);
  62. const importedModule = this.importDependency.module;
  63. hash.update((importedModule && importedModule.id) + "");
  64. hash.update((importedModule && this.id) + "");
  65. hash.update((importedModule && this.importedVar) + "");
  66. hash.update((importedModule && this.id && importedModule.isUsed(this.id)) + "");
  67. hash.update((importedModule && (!importedModule.meta || importedModule.meta.harmonyModule)) + "");
  68. hash.update((importedModule && (importedModule.used + JSON.stringify(importedModule.usedExports))) + "");
  69. }
  70. }
  71. HarmonyImportSpecifierDependency.Template = class HarmonyImportSpecifierDependencyTemplate {
  72. apply(dep, source) {
  73. const content = this.getContent(dep);
  74. source.replace(dep.range[0], dep.range[1] - 1, content);
  75. }
  76. getContent(dep) {
  77. const importedModule = dep.importDependency.module;
  78. const defaultImport = dep.directImport && dep.id === "default" && !(importedModule && (!importedModule.meta || importedModule.meta.harmonyModule));
  79. const shortHandPrefix = this.getShortHandPrefix(dep);
  80. const importedVar = dep.importedVar;
  81. const importedVarSuffix = this.getImportVarSuffix(dep, defaultImport, importedModule);
  82. if(dep.call && defaultImport) {
  83. return `${shortHandPrefix}${importedVar}_default()`;
  84. }
  85. if(dep.call && dep.id) {
  86. return `${shortHandPrefix}Object(${importedVar}${importedVarSuffix})`;
  87. }
  88. return `${shortHandPrefix}${importedVar}${importedVarSuffix}`;
  89. }
  90. getImportVarSuffix(dep, defaultImport, importedModule) {
  91. if(defaultImport) {
  92. return "_default.a";
  93. }
  94. if(dep.id) {
  95. const used = importedModule ? importedModule.isUsed(dep.id) : dep.id;
  96. const optionalComment = dep.id !== used ? " /* " + dep.id + " */" : "";
  97. return `[${JSON.stringify(used)}${optionalComment}]`;
  98. }
  99. return "";
  100. }
  101. getShortHandPrefix(dep) {
  102. if(!dep.shorthand) {
  103. return "";
  104. }
  105. return dep.name + ": ";
  106. }
  107. };
  108. module.exports = HarmonyImportSpecifierDependency;