Skip to content
This repository was archived by the owner on Jul 13, 2020. It is now read-only.

Commit 036aa62

Browse files
committed
Reimplement System.parse() using traceur.Compiler
1 parent d83a191 commit 036aa62

File tree

3 files changed

+28
-45
lines changed

3 files changed

+28
-45
lines changed

bower.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
"homepage": "https://github.com/ModuleLoader/es6-module-loader",
66
"main": "dist/es6-module-loader-sans-promises.js",
77
"dependencies": {
8-
"traceur": "0.0.45"
8+
"traceur": "0.0.49"
99
},
1010
"keywords": [
1111
"es6",

lib/loader.js

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -107,8 +107,6 @@ function logloads(loads) {
107107
(function() {
108108
var Promise = __global.Promise || require('when/es6-shim/Promise');
109109

110-
var traceur;
111-
112110
var defineProperty;
113111
(function () {
114112
try {

lib/system.js

Lines changed: 27 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -264,8 +264,10 @@
264264
});
265265
// --- <Specific Traceur Parsing Code> ---
266266

267-
// parse function is used to parse a load record
268-
// Returns an array of ModuleSpecifiers
267+
System.traceurOptions = {modules: 'instantiate'};
268+
269+
// parse function is used to parse a load record
270+
// Returns an array of ModuleSpecifiers
269271
System.parse = function(load) {
270272
if (!traceur) {
271273
if (typeof window == 'undefined')
@@ -278,60 +280,45 @@
278280

279281
console.assert(load.source, 'Non-empty source');
280282

281-
var depsList, curSystem, oldSourceMaps, oldModules;
283+
function checkForErrors(output) {
284+
if (output.errors.length) {
285+
output.errors.map(function(error) {
286+
console.error(error);
287+
});
288+
throw new Error('Parse failed, ' + output.errors.length);
289+
}
290+
}
291+
292+
var depsList;
282293
(function () {
283294
try {
284-
285-
var parser = new traceur.syntax.Parser(new traceur.syntax.SourceFile(load.address, load.source));
286-
var body = parser.parseModule();
287-
288295
load.kind = 'declarative';
289-
depsList = getImports(body);
290-
291-
oldSourceMaps = traceur.options.sourceMaps;
292-
oldModules = traceur.options.modules;
293-
294-
traceur.options.sourceMaps = true;
295-
traceur.options.modules = 'instantiate';
296-
297-
var reporter = new traceur.util.ErrorReporter();
298-
299-
reporter.reportMessageInternal = function(location, kind, format, args) {
300-
throw new SyntaxError(kind, location.start && location.start.line_, location.start && location.start.column_);
301-
}
302-
303-
// traceur expects its version of System
304-
curSystem = global.System;
305-
global.System = global.traceurSystem;
306296

307-
var tree = (new traceur.codegeneration.module.AttachModuleNameTransformer(load.name)).transformAny(body);
308-
tree = (new traceur.codegeneration.FromOptionsTransformer(reporter)).transform(tree);
297+
var compiler = new traceur.Compiler();
298+
var options = System.traceurOptions || {};
299+
var output = compiler.stringToTree({content: load.source, options: options});
300+
checkForErrors(output);
309301

310-
var sourceMapGenerator = new traceur.outputgeneration.SourceMapGenerator({ file: load.address });
311-
var options = { sourceMapGenerator: sourceMapGenerator };
302+
depsList = getImports(output.tree);
303+
output = compiler.treeToTree(output);
304+
checkForErrors(output);
312305

313-
var source = traceur.outputgeneration.TreeWriter.write(tree, options);
306+
output = compiler.treeToString(output);
307+
checkForErrors(output);
308+
var source = output.js;
309+
var sourceMap = output.generatedSourceMap;
314310

315-
if (global.btoa)
316-
source += '\n//# sourceMappingURL=data:application/json;base64,' + btoa(unescape(encodeURIComponent(options.sourceMap))) + '\n';
311+
if (global.btoa && sourceMap)
312+
source += '\n//# sourceMappingURL=data:application/json;base64,' + btoa(unescape(encodeURIComponent(sourceMap))) + '\n';
317313

318314
__eval(source, global, load);
319315
}
320316
catch(e) {
321317
if (e.name == 'SyntaxError' || e.name == 'TypeError')
322318
e.message = 'Evaluating ' + (load.name || load.address) + '\n\t' + e.message;
323-
if (curSystem)
324-
global.System = curSystem;
325-
if (oldSourceMaps)
326-
traceur.options.sourceMaps = oldSourceMaps;
327-
if (oldModules)
328-
traceur.options.modules = oldModules;
329319
throw e;
330320
}
331321
}());
332-
global.System = curSystem;
333-
traceur.options.sourceMaps = oldSourceMaps;
334-
traceur.options.modules = oldModules;
335322
return depsList;
336323
}
337324

@@ -385,8 +372,6 @@
385372
if (typeof exports === 'object')
386373
module.exports = System;
387374

388-
if (global.System && global.traceur)
389-
global.traceurSystem = global.System;
390375
global.System = System;
391376
})();
392377

0 commit comments

Comments
 (0)