forked from esnext/es6-module-transpiler
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodule_binding_declaration.js
133 lines (116 loc) · 3.24 KB
/
module_binding_declaration.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
/* jshint node:true, undef:true, unused:true */
var assert = require('assert');
var recast = require('recast');
var types = recast.types;
var n = types.namedTypes;
var utils = require('./utils');
var memo = utils.memo;
/**
* Contains information about a module binding declaration. This corresponds to
* the shared functionality of `ExportDeclaration` and `ImportDeclaration` in
* the ES6 spec.
*
* @constructor
* @abstract
* @param {Module} mod
* @param {AST.ImportDeclaration|AST.ExportDeclaration} node
*/
function ModuleBindingDeclaration(mod, node) {
assert.ok(
n.ImportDeclaration.check(node) || n.ExportDeclaration.check(node),
'expected an import or export declaration, got ' + (node && node.type)
);
Object.defineProperties(this, {
/**
* @name ModuleBindingDeclaration#node
* @type {AST.ImportDeclaration|AST.ExportDeclaration}
*/
node: {
value: node
},
/**
* @name ModuleBindingDeclaration#module
* @type {Module}
*/
module: {
value: mod
}
});
}
/**
* Finds the specifier that creates the local binding given by `name`, if one
* exists. Otherwise `null` is returned.
*
* @param {string} name
* @return {?ModuleBindingSpecifier}
*/
ModuleBindingDeclaration.prototype.findSpecifierByName = function(name) {
var specifiers = this.specifiers;
for (var i = 0, length = specifiers.length; i < length; i++) {
var specifier = specifiers[i];
if (specifier.name === name) {
return specifier;
}
}
return null;
};
/**
* @param {AST.Identifier} identifier
* @return {?ModuleBindingSpecifier}
*/
ModuleBindingDeclaration.prototype.findSpecifierByIdentifier = function(identifier) {
for (var i = 0, length = this.specifiers.length; i < length; i++) {
var specifier = this.specifiers[i];
if (specifier.identifier === identifier) {
return specifier;
}
}
return null;
};
/**
* Gets the raw path of the `from` part of the declaration, if present. For
* example:
*
* ```js
* import { map } from "array";
* ```
*
* The source path for the above declaration is "array".
*
* @type {?string}
* @name ModuleBindingDeclaration#sourcePath
*/
memo(ModuleBindingDeclaration.prototype, 'sourcePath', /** @this ModuleBindingDeclaration */function() {
return this.node.source ? this.node.source.value : null;
});
/**
* Gets a reference to the module referenced by this declaration.
*
* @type {Module}
* @name ModuleBindingDeclaration#source
*/
memo(ModuleBindingDeclaration.prototype, 'source', /** @this ModuleBindingDeclaration */function() {
return this.sourcePath ? this.module.getModule(this.sourcePath) : null;
});
/**
* Gets the containing module's scope.
*
* @type {Scope}
* @name ModuleBindingDeclaration#moduleScope
*/
memo(ModuleBindingDeclaration.prototype, 'moduleScope', /** @this ModuleBindingDeclaration */function() {
return this.module.scope;
});
/**
* Generate a string representing this object to aid debugging.
*
* @return {string}
*/
ModuleBindingDeclaration.prototype.inspect = function() {
return recast.print(this.node).code;
};
/**
* @see ModuleBindingDeclaration#inspect
*/
ModuleBindingDeclaration.prototype.toString = ModuleBindingDeclaration.prototype.inspect;
module.exports = ModuleBindingDeclaration;