forked from esnext/es6-module-transpiler
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodule.js
192 lines (173 loc) · 4.08 KB
/
module.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
/* jshint node:true, undef:true, unused:true */
var assert = require('assert');
var fs = require('fs');
var Path = require('path');
var esprima = require('esprima');
var recast = require('recast');
var types = recast.types;
var n = types.namedTypes;
var b = types.builders;
var NodePath = recast.types.NodePath;
var ImportDeclarationList = require('./imports');
var ExportDeclarationList = require('./exports');
var utils = require('./utils');
var memo = utils.memo;
var endsWith = utils.endsWith;
/**
* Represents a JavaScript module at a particular location on disk.
*
* @param {string} path
* @param {string} relativePath
* @param {Container} container
* @constructor
*/
function Module(path, relativePath, container) {
Object.defineProperties(this, {
/**
* @type {string}
* @name Module#path
*/
path: {
value: path,
enumerable: true,
writable: false
},
/**
* @type {string}
* @name Module#relativePath
*/
relativePath: {
value: relativePath,
enumerable: true,
writable: false
},
/**
* @type {Container}
* @name Module#container
*/
container: {
value: container,
enumerable: true,
writable: false
}
});
}
/**
* Clears the cached data for this module.
*/
Module.prototype.reload = function() {
delete this.src;
delete this.ast;
delete this.imports;
delete this.exports;
delete this.scope;
};
/**
* The list of imports declared by this module.
*
* @type {ImportDeclarationList}
* @name Module#imports
*/
memo(Module.prototype, 'imports', /** @this Module */function() {
var result = new ImportDeclarationList(this);
result.readProgram(this.ast.program);
return result;
});
/**
* The list of exports declared by this module.
*
* @type {ExportDeclarationList}
* @name Module#exports
*/
memo(Module.prototype, 'exports', /** @this Module */function() {
var result = new ExportDeclarationList(this);
result.readProgram(this.ast.program);
return result;
});
/**
* This module's scope.
*
* @type {Scope}
* @name Module#scope
*/
memo(Module.prototype, 'scope', /** @this Module */function() {
return new NodePath(this.ast).get('program').get('body').scope;
});
/**
* This module's source code represented as an abstract syntax tree.
*
* @type {File}
* @name Module#ast
*/
memo(Module.prototype, 'ast', /** @this Module */function() {
return recast.parse(
this.src, {
esprima: esprima,
sourceFileName: Path.basename(this.path)
}
);
});
/**
* This module's source code.
*
* @type {String}
* @name Module#src
*/
memo(Module.prototype, 'src', /** @this Module */function() {
return fs.readFileSync(this.path).toString();
});
/**
* A reference to the options from this module's container.
*
* @type {object}
* @name Module#options
*/
memo(Module.prototype, 'options', /** @this Module */function() {
return this.container.options;
});
/**
* This module's relative name, like {#relativePath} but without the extension.
* This may be modified by a Container if this Module is part of a Container.
*
* @type {string}
* @name Module#name
*/
memo(Module.prototype, 'name', /** @this Module */function() {
var relativePath = this.relativePath;
if (endsWith(relativePath, '.js')) {
return relativePath.slice(0, -3);
} else {
return relativePath;
}
});
/**
* A string suitable for a JavaScript identifier named for this module.
*
* @type {string}
* @name Module#id
*/
memo(Module.prototype, 'id', /** @this Module */function() {
return this.name.replace(/[^\w$_]/g, '$') + '$$';
});
/**
* Gets a Module by path relative to this module.
*
* @param {string} sourcePath
* @return {Module}
*/
Module.prototype.getModule = function(sourcePath) {
return this.container.getModule(sourcePath, this);
};
/**
* Generate a descriptive string suitable for debugging.
*
* @return {string}
*/
Module.prototype.inspect = function() {
return '#<' + this.constructor.name + ' ' + this.relativePath + '>';
};
/**
* @see Module#inspect
*/
Module.prototype.toString = Module.prototype.inspect;
module.exports = Module;