Skip to content

Commit

Permalink
Continue on parse error, simply print details instead of exiting
Browse files Browse the repository at this point in the history
  • Loading branch information
danielstjules committed Apr 4, 2017
1 parent 64115bf commit 8b11f70
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 6 deletions.
6 changes: 5 additions & 1 deletion lib/inspector.js
Expand Up @@ -55,7 +55,11 @@ class Inspector extends EventEmitter {
this._filePaths.forEach((filePath) => {
var src = fs.readFileSync(filePath, {encoding: 'utf8'});
this._fileContents[filePath] = src.split('\n');
var syntaxTree = parse(src, filePath);
try {
var syntaxTree = parse(src, filePath);
} catch (err) {
return console.error(err.message);
}
this._traversals[filePath] = NodeUtils.getDFSTraversal(syntaxTree);
this._walk(syntaxTree, (nodes) => this._insert(nodes));
});
Expand Down
19 changes: 14 additions & 5 deletions lib/parser.js
Expand Up @@ -12,16 +12,25 @@ var debug = require('./debug');
exports.parse = function(src, filePath) {
debug(`parsing ${filePath}`);
try {
return _parse(src, filePath, 'script');
return attempt(
() => _parse(src, filePath, 'script'),
() => _parse(src, filePath, 'module')
);
} catch (err) {
let ctx = getErrorContext(err, src);
throw new Error(`Couldn't parse ${filePath}: ${err.message}${ctx}`);
}
};

function attempt(...fns) {
for (let i = 0; i < fns.length; i++) {
try {
return _parse(src, filePath, 'module');
return fns[i]();
} catch (err) {
let ctx = getErrorContext(err, src);
throw new Error(`Couldn't parse ${filePath}: ${err.message}${ctx}`);
if (i === fns.length - 1) throw err;
}
}
};
}

function _parse(src, filePath, sourceType) {
return babylon.parse(src, {
Expand Down

0 comments on commit 8b11f70

Please sign in to comment.