Skip to content

Commit 440ff85

Browse files
committed
feature: @putout/engine-parser: handle absense of source
1 parent fa005ad commit 440ff85

File tree

2 files changed

+16
-0
lines changed

2 files changed

+16
-0
lines changed

packages/engine-parser/lib/parse.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,11 @@ const {estreeToBabel} = require('estree-to-babel');
44
const customParser = require('./custom-parser');
55
const {tryThrowWithReason} = require('./try-throw-with-reason');
66
const {assign} = Object;
7+
const isString = (a) => typeof a === 'string';
78

89
module.exports = (source, options) => {
10+
check(source);
11+
912
const {
1013
parser,
1114
printer = 'putout',
@@ -41,3 +44,8 @@ const getParser = ({parser = 'babel', isTS, isJSX, printer}) => ({
4144
return ast;
4245
},
4346
});
47+
48+
function check(source) {
49+
if (!isString(source))
50+
throw Error(`☝️ Looks like type of 'source' is not 'string', but '${typeof source}'`);
51+
}

packages/engine-parser/lib/parse.spec.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,3 +123,11 @@ test('putout: engine-parser: parse: set extra.__putout_printer: babel', (t) => {
123123
t.equal(__putout_printer, 'babel');
124124
t.end();
125125
});
126+
127+
test('putout: engine-parser: parse: tuple', (t) => {
128+
const source = [`const hello = 'world';`];
129+
const [error] = tryCatch(parse, source);
130+
131+
t.equal(error.message, `☝️ Looks like type of 'source' is not 'string', but 'object'`);
132+
t.end();
133+
});

0 commit comments

Comments
 (0)