Skip to content

Commit 925a5de

Browse files
committed
featur: @putout/engine-parser: add reason when cannot parse
1 parent 7151359 commit 925a5de

File tree

3 files changed

+24
-2
lines changed

3 files changed

+24
-2
lines changed

packages/engine-parser/lib/parse.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
const toBabel = require('estree-to-babel');
44
const customParser = require('./custom-parser');
5+
const {tryThrowWithReason} = require('./try-throw-with-reason');
56
const {assign} = Object;
67

78
module.exports = (source, options) => {
@@ -12,14 +13,14 @@ module.exports = (source, options) => {
1213
isJSX,
1314
} = options || {};
1415

15-
const cookedParser = getParser({
16+
const {parse} = getParser({
1617
printer,
1718
parser,
1819
isTS,
1920
isJSX,
2021
});
2122

22-
return cookedParser.parse(source);
23+
return tryThrowWithReason(parse, source);
2324
};
2425

2526
const getParser = ({parser = 'babel', isTS, isJSX, printer}) => ({
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
'use strict';
2+
3+
const tryCatch = require('try-catch');
4+
5+
module.exports.tryThrowWithReason = (fn, ...args) => {
6+
const [error, result] = tryCatch(fn, ...args);
7+
8+
if (error) {
9+
error.reason = 'parse';
10+
throw error;
11+
}
12+
13+
return result;
14+
};

packages/engine-parser/test/parser.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,13 @@ test('putout: parser: no-flow', (t) => {
195195
t.end();
196196
});
197197

198+
test('putout: parser: broken', (t) => {
199+
const [error] = tryCatch(putout, 'console.lo(');
200+
201+
t.equal(error.reason, 'parse');
202+
t.end();
203+
});
204+
198205
test('putout: parser: typescript', (t) => {
199206
const {code} = putout(fixture.typescript, {
200207
isTS: true,

0 commit comments

Comments
 (0)