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