forked from esnext/es6-module-transpiler
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodule_binding_list.js
244 lines (212 loc) · 6.02 KB
/
module_binding_list.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
/* jshint node:true, undef:true, unused:true */
var assert = require('assert');
var utils = require('./utils');
var memo = utils.memo;
var sourcePosition = utils.sourcePosition;
/**
* Represents a list of bindings for the given module. This corresponds to the
* shared functionality from `ExportsList` and `ImportsList` from the ES6 spec.
*
* @abstract
* @constructor
* @param {Module} mod
*/
function ModuleBindingList(mod) {
Object.defineProperties(this, {
/**
* @name ModuleBindingList#_nodes
* @type {AST.ImportDeclaration[]|AST.ExportDeclaration[]}
* @private
*/
_nodes: {
value: []
},
/**
* @name ModuleBindingList#module
* @type {Module}
*/
module: {
value: mod
}
});
}
/**
* Add all the binding declarations from the given scope body. Generally this
* should be the Program node's `body` property, an array of statements.
*
* @param {AST.Program} program
*/
ModuleBindingList.prototype.readProgram = function(program) {
var body = program.body;
for (var i = 0; i < body.length; i++) {
if (this.isMatchingBinding(body[i])) {
this.addDeclaration(body[i]);
}
}
};
/**
* Adds a declaration to the list.
*
* @private
* @param {AST.ImportDeclaration|AST.ExportDeclaration} node
*/
ModuleBindingList.prototype.addDeclaration = function(node) {
assert.ok(
this.isMatchingBinding(node),
'expected node to be an declaration, but got ' +
(node && node.type)
);
this._nodes.push(node);
// reset the cache
delete this.declarations;
delete this.specifiers;
delete this.modules;
};
/**
* Gets the associated module's scope.
*
* @type {Scope}
* @name ModuleBindingList#moduleScope
*/
memo(ModuleBindingList.prototype, 'moduleScope', /** @this ModuleBindingList */function() {
return this.module.scope;
});
/**
* Gets all the modules referenced by the declarations in this list.
*
* @type {Module[]}
* @name ModuleBindingList#modules
*/
memo(ModuleBindingList.prototype, 'modules', /** @this ModuleBindingList */function() {
var modules = [];
this.declarations.forEach(function(declaration) {
if (declaration.source && modules.indexOf(declaration.source) < 0) {
modules.push(declaration.source);
}
});
return modules;
});
/**
* Finds the specifier that creates the local binding given by `name`, if one
* exists. Otherwise `null` is returned.
*
* @private
* @param {string} name
* @return {?ModuleBindingSpecifier}
*/
ModuleBindingList.prototype.findSpecifierByName = function(name) {
for (var i = 0, length = this.declarations.length; i < length; i++) {
var specifier = this.declarations[i].findSpecifierByName(name);
if (specifier) { return specifier; }
}
return null;
};
/**
* Finds the specifier whose identifier is the given identifier, if one exists.
* Otherwise `null` is returned.
*
* @private
* @param {AST.Identifier} identifier
* @return {?ModuleBindingSpecifier}
*/
ModuleBindingList.prototype.findSpecifierByIdentifier = function(identifier) {
for (var i = 0, length = this.declarations.length; i < length; i++) {
var specifier = this.declarations[i].findSpecifierByIdentifier(identifier);
if (specifier && specifier.identifier === identifier) {
return specifier;
}
}
return null;
};
/**
* @param {NodePath} referencePath
* @return {?ModuleBindingSpecifier}
*/
ModuleBindingList.prototype.findSpecifierForReference = function(referencePath) {
var declaration = this.findDeclarationForReference(referencePath);
if (!declaration) {
return null;
}
var specifier = this.findSpecifierByIdentifier(declaration.node);
assert.ok(
specifier,
'no specifier found for `' + referencePath.node.name + '`! this should not happen!'
);
return specifier;
};
/**
* @private
*/
ModuleBindingList.prototype.findDeclarationForReference = function(referencePath) {
// Check names to avoid traversing scopes for all references.
if (this.names.indexOf(referencePath.node.name) < 0) {
return null;
}
var node = referencePath.node;
var declaringScope = referencePath.scope.lookup(node.name);
assert.ok(
declaringScope,
'`' + node.name + '` at ' + sourcePosition(this.module, node) +
' cannot be bound if it is not declared'
);
// Bindings are at the top level, so if this isn't then it's shadowing.
if (!declaringScope.isGlobal) {
return null;
}
var declarations = declaringScope.getBindings()[node.name];
if (!declarations || declarations.length !== 1) {
throw new SyntaxError(
'expected one declaration for `' + node.name +
'`, at ' + sourcePosition(this.module, node) +
' but found ' + (declarations ? declarations.length : 'none')
);
}
return declarations[0];
};
/**
* Generate a string representing this object to aid debugging.
*
* @return {string}
*/
ModuleBindingList.prototype.inspect = function() {
var result = '#<' + this.constructor.name;
result += ' module=' + this.module.relativePath;
if (this.declarations.length > 0) {
result += ' declarations=' + this.declarations.map(function(imp) {
return imp.inspect();
}).join(', ');
}
result += '>';
return result;
};
/**
* @see ModuleBindingList#inspect
*/
ModuleBindingList.prototype.toString = ModuleBindingList.prototype.inspect;
/**
* Contains a list of declarations.
*
* @type {(ImportDeclaration[]|ExportDeclaration[])}
* @name ModuleBindingList#declarations
*/
memo(ModuleBindingList.prototype, 'declarations', /** @this ModuleBindingList */function() {
var self = this;
return this._nodes.map(function(child) {
return self.declarationForNode(child);
});
});
/**
* Contains a combined list of names for all the declarations contained in this
* list.
*
* @type {string[]}
* @name ModuleBindingList#names
*/
memo(ModuleBindingList.prototype, 'names', /** @this ModuleBindingList */function() {
return this.declarations.reduce(function(names, decl) {
return names.concat(decl.specifiers.map(function(specifier) {
return specifier.name;
}));
}, []);
});
module.exports = ModuleBindingList;