forked from esnext/es6-module-transpiler
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexports.js
339 lines (303 loc) · 9.07 KB
/
exports.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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
/* 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 DeclarationInfo = require('./declaration_info');
var utils = require('./utils');
var memo = utils.memo;
var extend = utils.extend;
var sourcePosition = utils.sourcePosition;
/**
* Represents a list of the exports for the given module.
*
* @constructor
* @extends ModuleBindingList
* @param {Module} mod
*/
function ExportDeclarationList(mod) {
ModuleBindingList.call(this, mod);
}
extend(ExportDeclarationList, ModuleBindingList);
/**
* @private
* @param {AST.Declaration} node
* @return {boolean}
*/
ExportDeclarationList.prototype.isMatchingBinding = function(node) {
return n.ExportDeclaration.check(node);
};
/**
* Gets an export declaration for the given `node`.
*
* @private
* @param {AST.ExportDeclaration} node
* @return {ExportDeclaration}
*/
ExportDeclarationList.prototype.declarationForNode = function(node) {
if (node.default) {
return new DefaultExportDeclaration(this.module, node);
} else if (n.VariableDeclaration.check(node.declaration)) {
return new VariableExportDeclaration(this.module, node);
} else if (n.FunctionDeclaration.check(node.declaration)) {
return new FunctionExportDeclaration(this.module, node);
} else if (n.ExportBatchSpecifier.check(node.specifiers[0])) {
throw new Error(
'`export *` found at ' + sourcePosition(this.module, node) +
' is not supported, please use `export { … }` instead'
);
} else {
return new NamedExportDeclaration(this.module, node);
}
};
/**
* @param {NodePath} referencePath
* @return {?ExportSpecifier}
*/
ExportDeclarationList.prototype.findSpecifierForReference = function(referencePath) {
if (n.ExportSpecifier.check(referencePath.parent.node) && referencePath.parent.parent.node.source) {
// This is a direct export from another module, e.g. `export { foo } from 'foo'`.
return /** @type {ExportSpecifier} */this.findSpecifierByIdentifier(referencePath.node);
}
var declaration = this.findDeclarationForReference(referencePath);
if (!declaration) {
return null;
}
var specifier = /** @type {ExportSpecifier} */this.findSpecifierByName(declaration.node.name);
assert.ok(
specifier,
'no specifier found for `' + referencePath.node.name + '`! this should not happen!'
);
return specifier;
};
/**
* Contains information about an export declaration.
*
* @constructor
* @abstract
* @extends ModuleBindingDeclaration
* @param {Module} mod
* @param {ExportDeclaration} node
*/
function ExportDeclaration(mod, node) {
assert.ok(
n.ExportDeclaration.check(node),
'expected an export declaration, got ' + (node && node.type)
);
ModuleBindingDeclaration.call(this, mod, node);
}
extend(ExportDeclaration, ModuleBindingDeclaration);
/**
* Returns a string description suitable for debugging.
*
* @return {string}
*/
ExportDeclaration.prototype.inspect = function() {
return recast.print(this.node).code;
};
/**
* @see ExportDeclaration#inspect
*/
ExportDeclaration.prototype.toString = ExportDeclaration.prototype.inspect;
/**
* Represents an export declaration of the form:
*
* export default foo;
*
* @constructor
* @extends ExportDeclaration
* @param {Module} mod
* @param {AST.ExportDeclaration} node
*/
function DefaultExportDeclaration(mod, node) {
ExportDeclaration.call(this, mod, node);
}
extend(DefaultExportDeclaration, ExportDeclaration);
/**
* Contains a list of specifier name information for this export.
*
* @type {ExportSpecifier[]}
* @name DefaultExportSpecifier#specifiers
*/
memo(DefaultExportDeclaration.prototype, 'specifiers', /** @this DefaultExportDeclaration */function() {
var specifier = new DefaultExportSpecifier(this, this.node.declaration);
return [specifier];
});
/**
* Represents an export declaration of the form:
*
* export { foo, bar };
*
* @constructor
* @extends ExportDeclaration
* @param {Module} mod
* @param {AST.ExportDeclaration} node
*/
function NamedExportDeclaration(mod, node) {
ExportDeclaration.call(this, mod, node);
}
extend(NamedExportDeclaration, ExportDeclaration);
/**
* Contains a list of specifier name information for this export.
*
* @type {ExportSpecifier[]}
* @name NamedExportDeclaration#specifiers
*/
memo(NamedExportDeclaration.prototype, 'specifiers', /** @this NamedExportDeclaration */function() {
var self = this;
return this.node.specifiers.map(function(specifier) {
return new ExportSpecifier(self, specifier);
});
});
/**
* Represents an export declaration of the form:
*
* export var foo = 1;
*
* @constructor
* @extends ExportDeclaration
* @param {Module} mod
* @param {AST.ExportDeclaration} node
*/
function VariableExportDeclaration(mod, node) {
ExportDeclaration.call(this, mod, node);
}
extend(VariableExportDeclaration, ExportDeclaration);
/**
* Gets the list of export specifiers for this declaration.
*
* @type {ExportSpecifier[]}
* @name VariableExportDeclaration#specifiers
*/
memo(VariableExportDeclaration.prototype, 'specifiers', /** @this VariableExportDeclaration */function() {
var self = this;
return this.node.declaration.declarations.map(function(declarator) {
return new ExportSpecifier(self, declarator);
});
});
/**
* Represents an export declaration of the form:
*
* export function foo() {}
*
* @constructor
* @extends ExportDeclaration
* @param {Module} mod
* @param {AST.ExportDeclaration} node
*/
function FunctionExportDeclaration(mod, node) {
ExportDeclaration.call(this, mod, node);
}
extend(FunctionExportDeclaration, ExportDeclaration);
/**
* Gets the list of export specifiers for this declaration.
*
* @type {ExportSpecifier[]}
* @name FunctionExportDeclaration#specifiers
*/
memo(FunctionExportDeclaration.prototype, 'specifiers', /** @this FunctionExportDeclaration */function() {
return [new ExportSpecifier(this, this.node.declaration)];
});
/**
* Represents an export specifier in an export declaration.
*
* @constructor
* @extends ModuleBindingSpecifier
* @param {ExportDeclaration} declaration
* @param {AST.Node} node
*/
function ExportSpecifier(declaration, node) {
ModuleBindingSpecifier.call(this, declaration, node);
}
extend(ExportSpecifier, ModuleBindingSpecifier);
/**
* Contains the local declaration info for this export specifier. For example,
* in this module:
*
* var a = 1;
* export { a };
*
* The module declaration info for the `a` export specifier is the variable
* declaration plus the `a` identifier in its first declarator.
*
* @type {?DeclarationInfo}
* @name ExportSpecifier#moduleDeclaration
*/
memo(ExportSpecifier.prototype, 'moduleDeclaration', /** @this ExportSpecifier */function() {
if (this.declaration.source) {
// This is part of a direct export, e.g. `export { ... } from '...'`, so
// there is no declaration as part of this module.
return null;
}
var bindings = this.moduleScope.getBindings();
var identifierPaths = bindings[this.from];
assert.ok(
identifierPaths && identifierPaths.length === 1,
'expected exactly one declaration for export `' +
this.from + '` at ' + sourcePosition(this.module, this.node) +
', found ' + (identifierPaths ? identifierPaths.length : 'none')
);
var identifierPath = identifierPaths[0];
var declarationInfo = DeclarationInfo.forIdentifierPath(identifierPath);
assert.ok(
declarationInfo,
'cannot detect declaration for `' +
identifierPath.node.name + '`, found parent.type `' +
identifierPath.parent.node.type + '`'
);
return declarationInfo;
});
/**
* Represents an export specifier in a default export declaration.
*
* @constructor
* @extends ExportSpecifier
* @param {ExportDeclaration} declaration
* @param {AST.Expression} node
*/
function DefaultExportSpecifier(declaration, node) {
ExportSpecifier.call(this, declaration, node);
}
extend(DefaultExportSpecifier, ExportSpecifier);
/**
* The node of a default export specifier is an expression, not a specifier.
*
* @type {AST.Expression}
*/
DefaultExportSpecifier.prototype.node = null;
/**
* Default export specifier names are always "default".
*
* @type {string}
* @name DefaultExportSpecifier#name
* @default "default"
*/
DefaultExportSpecifier.prototype.name = 'default';
/**
* Default export specifiers do not bind to a local identifier.
*
* @type {?Identifier}
* @name DefaultExportSpecifier#identifier
* @default null
*/
DefaultExportSpecifier.prototype.identifier = null;
/**
* Default export specifiers do not have a local bound name.
*
* @type {?string}
* @name DefaultExportSpecifier#from
* @default null
*/
DefaultExportSpecifier.prototype.from = null;
/**
* Default export specifiers do not have a local declaration.
*
* @type {?DeclarationInfo}
* @name DefaultExportSpecifier#moduleDeclaration
* @default null
*/
DefaultExportSpecifier.prototype.moduleDeclaration = null;
module.exports = ExportDeclarationList;