forked from esnext/es6-module-transpiler
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathimports.js
217 lines (196 loc) · 5.75 KB
/
imports.js
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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
/* jshint node:true, undef:true, unused:true */
var assert = require('assert');
var recast = require('recast');
var types = recast.types;
var n = types.namedTypes;
var ModuleBindingList = require('./module_binding_list');
var ModuleBindingDeclaration = require('./module_binding_declaration');
var ModuleBindingSpecifier = require('./module_binding_specifier');
var utils = require('./utils');
var memo = utils.memo;
var extend = utils.extend;
var sourcePosition = utils.sourcePosition;
/**
* Represents a list of the imports for the given module.
*
* @constructor
* @param {Module} mod
* @extends ModuleBindingList
*/
function ImportDeclarationList(mod) {
ModuleBindingList.call(this, mod);
}
extend(ImportDeclarationList, ModuleBindingList);
/**
* @private
* @param {AST.Node} node
* @return {boolean}
*/
ImportDeclarationList.prototype.isMatchingBinding = function(node) {
return n.ImportDeclaration.check(node);
};
/**
* Gets an import declaration for the given `node`.
*
* @private
* @param {AST.ImportDeclaration} node
* @return {ImportDeclaration}
*/
ImportDeclarationList.prototype.declarationForNode = function(node) {
switch (node.kind) {
case 'default':
return new DefaultImportDeclaration(this.module, node);
case 'named':
return new NamedImportDeclaration(this.module, node);
case undefined:
return new BareImportDeclaration(this.module, node);
default:
assert.ok(false, 'unexpected import kind at ' + sourcePosition(this.module, node) + ': ' + node.kind);
break;
}
};
/**
* Contains information about an import declaration.
*
* @constructor
* @abstract
* @param {Module} mod
* @param {AST.ImportDeclaration} node
* @extends ModuleBindingDeclaration
*/
function ImportDeclaration(mod, node) {
assert.ok(
n.ImportDeclaration.check(node),
'expected an import declaration, got ' + (node && node.type)
);
ModuleBindingDeclaration.call(this, mod, node);
}
extend(ImportDeclaration, ModuleBindingDeclaration);
/**
* Represents a default import of the form
*
* ```js
* import List from 'list';
* ```
*
* @constructor
* @extends ImportDeclaration
* @param {Module} mod
* @param {AST.ImportDeclaration} node
*/
function DefaultImportDeclaration(mod, node) {
assert.equal(node.kind, 'default');
assert.ok(
node.specifiers.length === 1 && node.specifiers[0],
'expected exactly one specifier for a default import, got ' +
node.specifiers.length
);
ImportDeclaration.call(this, mod, node);
}
extend(DefaultImportDeclaration, ImportDeclaration);
/**
* Contains a list of specifier name information for this import.
*
* @type {ImportSpecifier[]}
* @name DefaultImportDeclaration#specifiers
*/
memo(DefaultImportDeclaration.prototype, 'specifiers', /** @this DefaultImportDeclaration */function() {
var specifier = new ImportSpecifier(this, this.node.specifiers[0]);
specifier.from = 'default';
assert.equal(specifier.from, 'default');
return [specifier];
});
/**
* Represents a named import of the form
*
* ```js
* import { sin, cos } from 'math';
* ```
*
* @constructor
* @extends ImportDeclaration
* @param {Module} mod
* @param {AST.ImportDeclaration} node
*/
function NamedImportDeclaration(mod, node) {
assert.equal(node.kind, 'named');
ImportDeclaration.call(this, mod, node);
}
extend(NamedImportDeclaration, ImportDeclaration);
/**
* Contains a list of specifier name information for this import.
*
* @type {ImportSpecifier[]}
* @name NamedImportDeclaration#specifiers
*/
memo(NamedImportDeclaration.prototype, 'specifiers', /** @this NamedImportDeclaration */function() {
var self = this;
return this.node.specifiers.map(function(specifier) {
return new ImportSpecifier(self, specifier);
});
});
/**
* Represents an import with no bindings created in the local scope. These
* imports are of the form `import 'path/to/module'` and are generally included
* only for their side effects.
*
* @constructor
* @extends ImportDeclaration
* @param {Module} mod
* @param {AST.ImportDeclaration} node
*/
function BareImportDeclaration(mod, node) {
assert.ok(
node.kind === undefined && node.specifiers.length === 0,
'expected a bare import at ' + sourcePosition(mod, node) +
', got one with kind=' + node.kind + ' and ' +
node.specifiers.length + ' specifier(s)'
);
ImportDeclaration.call(this, mod, node);
}
extend(BareImportDeclaration, ImportDeclaration);
/**
* Returns an empty set of specifiers.
*
* @type {ImportSpecifier[]}
* @name BareImportDeclaration#specifiers
*/
memo(BareImportDeclaration.prototype, 'specifiers', /** @this BareImportDeclaration */function() {
return [];
});
/**
* Represents an import specifier. The "a" and "b as c" are both import
* specifiers in the following import statement.
*
* import { a, b as c } from "a";
*
* @constructor
* @extends ModuleBindingSpecifier
* @param {ImportDeclaration} declaration
* @param {AST.ImportSpecifier} node
*/
function ImportSpecifier(declaration, node) {
assert.ok(
declaration instanceof ImportDeclaration,
'expected an instance of ImportDeclaration'
);
ModuleBindingSpecifier.call(this, declaration, node);
}
extend(ImportSpecifier, ModuleBindingSpecifier);
/**
* @type {ExportSpecifier}
* @name ImportSpecifier#exportSpecifier
*/
memo(ImportSpecifier.prototype, 'exportSpecifier', /** @this ImportSpecifier */function() {
var source = this.declaration.source;
assert.ok(source, 'import specifiers must have a valid source');
var exportSpecifier = source.exports.findSpecifierByName(this.from);
assert.ok(
exportSpecifier,
'import `' + this.from + '` at ' +
sourcePosition(this.module, this.node) +
' has no matching export in ' + source.relativePath
);
return exportSpecifier;
});
module.exports = ImportDeclarationList;