forked from angular/angular
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
257 lines (227 loc) · 9.69 KB
/
index.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
// Entry point for Node.
var fs = require('fs');
var glob = require('glob');
var path = require('path');
var traceur = require('traceur');
var assert = require('assert');
exports.RUNTIME_PATH = traceur.RUNTIME_PATH;
var TRACEUR_PATH = traceur.RUNTIME_PATH.replace('traceur-runtime.js', 'traceur.js');
var SELF_SOURCE_REGEX = /transpiler\/src/;
var SELF_COMPILE_OPTIONS = {
modules: 'register',
memberVariables: false,
moduleName: true,
script: false // parse as a module
};
var needsReload = true;
var oldSystemGet = System.get;
exports.reloadSources = function() {
needsReload = true;
};
exports.compile = function compile(options, paths, source) {
if (needsReload) {
reloadCompiler();
needsReload = false;
}
var inputPath, outputPath, moduleName;
if (typeof paths === 'string') {
inputPath = outputPath = paths;
} else {
inputPath = paths.inputPath;
outputPath = paths.inputPath;
moduleName = paths.moduleName;
}
outputPath = outputPath || inputPath;
moduleName = moduleName || inputPath;
moduleName = moduleName.replace(/\.\w*$/, '');
var localOptions = extend(options, {
moduleName: moduleName
});
var CompilerCls = System.get('transpiler/src/compiler').Compiler;
var compiler = new CompilerCls(localOptions);
var result = {
js: compiler.compile(source, inputPath, outputPath),
sourceMap: null
};
var sourceMapString = compiler.getSourceMap();
if (sourceMapString) {
result.sourceMap = JSON.parse(sourceMapString);
}
if (localOptions.outputLanguage === 'es6' && source.indexOf('$traceurRuntime') === -1) {
assert(result.js.indexOf('$traceurRuntime') === -1,
'Transpile to ES6 must not add references to $traceurRuntime, '
+ inputPath + ' is transpiled to:\n' + result.js);
}
return result;
};
exports.init = function() {
if (needsReload) {
reloadCompiler();
needsReload = false;
}
}
// Transpile and evaluate the code in `src`.
// Use existing traceur to compile our sources.
function reloadCompiler() {
loadModule(TRACEUR_PATH, false);
glob.sync(__dirname + '/src/**/*.js').forEach(function(fileName) {
loadModule(fileName, true);
});
// Traceur modules are register with the ".js" extension but we don't want
// to add it to all the import statements.
System.get = function get(normalizedName) {
var m = oldSystemGet.call(this, normalizedName);
if (!m && normalizedName.indexOf('traceur') == 0) {
m = oldSystemGet.call(this, normalizedName + '.js');
}
return m;
};
useRttsAssertModuleForConvertingTypesToExpressions();
supportSuperCallsInEs6Patch();
convertTypesToExpressionsInEs6Patch();
removeNonStaticFieldDeclarationsInEs6Patch();
disableGetterSetterAssertionPatch();
patchCommonJSModuleTransformerToSupportExportStar();
}
function loadModule(filepath, transpile) {
var data = fs.readFileSync(filepath, 'utf8');
if (!data) {
throw new Error('Failed to import ' + filepath);
}
if (transpile) {
var moduleName = path.normalize(filepath)
.replace(__dirname, 'transpiler')
.replace(/\\/g, '/')
.replace(/\.\w*$/, '');
data = (new traceur.NodeCompiler(
extend(SELF_COMPILE_OPTIONS, { moduleName: moduleName } )
)).compile(data, filepath, filepath);
}
('global', eval)(data);
}
function extend(source, props) {
var res = {};
for (var prop in source) {
res[prop] = source[prop];
}
for (var prop in props) {
res[prop] = props[prop];
}
return res;
}
// TODO(tbosch): remove when traceur is fixed.
// see https://github.com/google/traceur-compiler/issues/1700
function supportSuperCallsInEs6Patch() {
var traceurVersion = System.map['traceur'];
var ParseTreeMapWriter = System.get(traceurVersion+'/src/outputgeneration/ParseTreeMapWriter').ParseTreeMapWriter;
var _enterBranch = ParseTreeMapWriter.prototype.enterBranch;
ParseTreeMapWriter.prototype.enterBranch = function(location) {
if (!location.start) {
// This would throw...
return;
}
return _enterBranch.apply(this, arguments);
}
}
// TODO(tbosch): Remove when traceur is fixed.
// see https://github.com/google/traceur-compiler/issues/1699
function convertTypesToExpressionsInEs6Patch() {
var traceurVersion = System.map['traceur'];
var TypeToExpressionTransformer = System.get(traceurVersion+'/src/codegeneration/TypeToExpressionTransformer').TypeToExpressionTransformer;
var PureES6Transformer = System.get(traceurVersion+'/src/codegeneration/PureES6Transformer').PureES6Transformer;
var UniqueIdentifierGenerator = System.get(traceurVersion+'/src/codegeneration/UniqueIdentifierGenerator').UniqueIdentifierGenerator;
var _transform = PureES6Transformer.prototype.transform;
PureES6Transformer.prototype.transform = function() {
if (!this._patched) {
this._patched = true;
this.treeTransformers_.splice(0,0, function(tree) {
return new TypeToExpressionTransformer(new UniqueIdentifierGenerator(), this.reporter_).transformAny(tree);
});
}
return _transform.apply(this, arguments);
};
}
// TODO(tbosch): Don't write field declarations in classes when we output to ES6.
// This just patches the writer and does not support moving initializers to the constructor.
// See src/codegeneration/ClassTransformer.js for how to support initializers as well.
// see https://github.com/google/traceur-compiler/issues/1708
function removeNonStaticFieldDeclarationsInEs6Patch() {
var traceurVersion = System.map['traceur'];
var ParseTreeWriter = System.get(traceurVersion+'/src/outputgeneration/ParseTreeWriter').ParseTreeWriter;
var options = System.get(traceurVersion + "/src/Options.js").options;
var _visitPropertyVariableDeclaration = ParseTreeWriter.prototype.visitPropertyVariableDeclaration;
ParseTreeWriter.prototype.visitPropertyVariableDeclaration = function() {
if (options.outputLanguage !== 'es6') {
return _visitPropertyVariableDeclaration.apply(this, arguments);
}
};
}
// TODO(tbosch): Disable getter/setters for assertions until traceur has a flag
// that allows to disable them while keeping assertions and member fields enabled.
// see https://github.com/google/traceur-compiler/issues/1625
// Why:
// - traceur uses field names based on numbers, which can lead to collisions when creating a subclass in a separate compiler run.
// - this rename of fields makes debugging via the repl harder (e.g. via DevTools console)
// - this rename can break JSON conversion of instances
function disableGetterSetterAssertionPatch() {
var traceurVersion = System.map['traceur'];
var MemberVariableTransformer = System.get(traceurVersion+'/src/codegeneration/MemberVariableTransformer').MemberVariableTransformer;
var AnonBlock = System.get(traceurVersion+'/src/syntax/trees/ParseTrees.js').AnonBlock;
MemberVariableTransformer.prototype.transformPropertyVariableDeclaration = function(tree) {
return new AnonBlock(tree.location, []);
}
}
// TODO(tbosch): Get all types from `assert` module and not from `$traceurRuntime`.
// With this a transpile to ES6 does no more include the `$traceurRuntime`.
// see https://github.com/google/traceur-compiler/issues/1706
function useRttsAssertModuleForConvertingTypesToExpressions() {
var traceurVersion = System.map['traceur'];
var original = System.get(traceurVersion+'/src/codegeneration/TypeToExpressionTransformer').TypeToExpressionTransformer;
var patch = System.get('transpiler/src/patch/TypeToExpressionTransformer').TypeToExpressionTransformer;
for (var prop in patch.prototype) {
original.prototype[prop] = patch.prototype[prop];
}
var TypeAssertionTransformer = System.get(traceurVersion+'/src/codegeneration/TypeAssertionTransformer').TypeAssertionTransformer;
var createIdentifierExpression = System.get(traceurVersion+'/src/codegeneration/ParseTreeFactory').createIdentifierExpression;
var parseExpression = System.get(traceurVersion+'/src/codegeneration/PlaceholderParser.js').parseExpression;
TypeAssertionTransformer.prototype.transformBindingElementParameter_ = function(element, typeAnnotation) {
// Copied from https://github.com/google/traceur-compiler/commits/master/src/codegeneration/TypeAssertionTransformer.js
if (!element.binding.isPattern()) {
if (typeAnnotation) {
this.paramTypes_.atLeastOneParameterTyped = true;
} else {
// PATCH start
typeAnnotation = parseExpression(["assert.type.any"]);
// PATCH end
}
this.paramTypes_.arguments.push(
createIdentifierExpression(element.binding.identifierToken),
typeAnnotation);
return;
}
// NYI
}
}
// TODO(tbosch): patch exports for CommonJS to support `export * from ...`
// see https://github.com/google/traceur-compiler/issues/1042
function patchCommonJSModuleTransformerToSupportExportStar() {
var traceurVersion = System.map['traceur'];
var CommonJsModuleTransformer = System.get(traceurVersion+'/src/codegeneration/CommonJsModuleTransformer').CommonJsModuleTransformer;
var parseStatement = System.get(traceurVersion+'/src/codegeneration/PlaceholderParser.js').parseStatement;
var prependStatements = System.get(traceurVersion+"/src/codegeneration/PrependStatements.js").prependStatements;
var _wrapModule = CommonJsModuleTransformer.prototype.wrapModule;
CommonJsModuleTransformer.prototype.wrapModule = function(statements) {
if (this.hasStarExports()) {
var last = statements[statements.length - 1];
statements = statements.slice(0, -1);
var exportObject = last.expression;
if (exportObject.propertyNameAndValues) {
throw new Error('Don\'t support export * with named exports right now...');
}
statements.push(parseStatement(['module.exports = ', ';'], exportObject));
return statements;
} else {
return _wrapModule.apply(this, arguments);
}
}
}