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

Commit 1a4a2d5

Browse files
committed
parse refactoring
1 parent 0b0612d commit 1a4a2d5

File tree

1 file changed

+34
-35
lines changed

1 file changed

+34
-35
lines changed

lib/system.js

Lines changed: 34 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616

1717
var isBrowser = typeof window != 'undefined';
1818
var Loader = global.Reflect && global.Reflect.Loader || require('./loader');
19-
var Promise = global.Promise || require('es6-promise').Promise;
19+
var Promise = global.Promise || require('when/es6-shim/Promise');
2020

2121
// Helpers
2222
// Absolute URL parsing, from https://gist.github.com/Yaffle/1088850
@@ -266,6 +266,14 @@
266266

267267
System.traceurOptions = {modules: 'instantiate'};
268268

269+
function checkForErrors(output, load) {
270+
if (output.errors.length) {
271+
for (var i = 0, l = output.errors.length; i < l; i++)
272+
console.error(output.errors[i]);
273+
throw new Error('Parse of ' + load.name + ', ' + load.address + ' failed, ' + output.errors.length);
274+
}
275+
}
276+
269277
// parse function is used to parse a load record
270278
// Returns an array of ModuleSpecifiers
271279
System.parse = function(load) {
@@ -280,45 +288,29 @@
280288

281289
console.assert(load.source, 'Non-empty source');
282290

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-
292291
var depsList;
293-
(function () {
294-
try {
295-
load.kind = 'declarative';
296292

297-
var compiler = new traceur.Compiler();
298-
var options = System.traceurOptions || {};
299-
var output = compiler.stringToTree({content: load.source, options: options});
300-
checkForErrors(output);
293+
load.kind = 'declarative';
301294

302-
depsList = getImports(output.tree);
303-
output = compiler.treeToTree(output);
304-
checkForErrors(output);
295+
var compiler = new traceur.Compiler();
296+
var options = System.traceurOptions || {};
297+
var output = compiler.stringToTree({content: load.source, options: options});
298+
checkForErrors(output);
305299

306-
output = compiler.treeToString(output);
307-
checkForErrors(output);
308-
var source = output.js;
309-
var sourceMap = output.generatedSourceMap;
300+
depsList = getImports(output.tree);
301+
output = compiler.treeToTree(output);
302+
checkForErrors(output);
310303

311-
if (global.btoa && sourceMap)
312-
source += '\n//# sourceMappingURL=data:application/json;base64,' + btoa(unescape(encodeURIComponent(sourceMap))) + '\n';
304+
output = compiler.treeToString(output);
305+
checkForErrors(output);
306+
var source = output.js;
307+
var sourceMap = output.generatedSourceMap;
308+
309+
if (global.btoa && sourceMap)
310+
source += '\n//# sourceMappingURL=data:application/json;base64,' + btoa(unescape(encodeURIComponent(sourceMap))) + '\n';
311+
312+
__eval(source, global, load);
313313

314-
__eval(source, global, load);
315-
}
316-
catch(e) {
317-
if (e.name == 'SyntaxError' || e.name == 'TypeError')
318-
e.message = 'Evaluating ' + (load.name || load.address) + '\n\t' + e.message;
319-
throw e;
320-
}
321-
}());
322314
return depsList;
323315
}
324316

@@ -384,7 +376,14 @@
384376
// store the registered declaration as load.declare
385377
load.declare = typeof name == 'string' ? declare : deps;
386378
}
387-
eval('var __moduleName = "' + (load.name || '').replace('"', '\"') + '"; (function() { ' + __source + ' \n }).call(__global);');
379+
try {
380+
eval('var __moduleName = "' + (load.name || '').replace('"', '\"') + '"; (function() { ' + __source + ' \n }).call(__global);');
381+
}
382+
catch(e) {
383+
if (e.name == 'SyntaxError' || e.name == 'TypeError')
384+
e.message = 'Evaluating ' + (load.name || load.address) + '\n\t' + e.message;
385+
throw e;
386+
}
388387

389388
System.register = System.__curRegister;
390389
delete System.__curRegister;

0 commit comments

Comments
 (0)