Skip to content

Commit 4f41b2f

Browse files
committed
feature: @putout/engine-parser: mark used printer in ast
1 parent afbc492 commit 4f41b2f

File tree

4 files changed

+36
-2
lines changed

4 files changed

+36
-2
lines changed

packages/engine-parser/lib/parse.js

Lines changed: 9 additions & 1 deletion
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 {assign} = Object;
56

67
module.exports = (source, options) => {
78
const {
@@ -23,11 +24,18 @@ module.exports = (source, options) => {
2324

2425
const getParser = ({parser = 'babel', isTS, isJSX, printer}) => ({
2526
parse(source) {
27+
const options = {};
28+
29+
if (printer === 'babel')
30+
assign(options, {
31+
convertParens: false,
32+
});
33+
2634
const ast = toBabel(customParser(source, parser, {
2735
isTS,
2836
isJSX,
2937
printer,
30-
}));
38+
}), options);
3139

3240
return ast;
3341
},

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

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,3 +102,24 @@ test('putout: engine-parser: parse + generate = sourcemap', (t) => {
102102
t.ok(map, 'returns map');
103103
t.end();
104104
});
105+
106+
test('putout: engine-parser: parse: set extra.__putout_printer', (t) => {
107+
const source = `const hello = 'world';`;
108+
const ast = parse(source);
109+
const {__putout_printer} = ast.program.extra;
110+
111+
t.equal(__putout_printer, 'putout');
112+
t.end();
113+
});
114+
115+
test('putout: engine-parser: parse: set extra.__putout_printer: babel', (t) => {
116+
const source = `const hello = 'world';`;
117+
const ast = parse(source, {
118+
printer: 'babel',
119+
});
120+
121+
const {__putout_printer} = ast.program.extra;
122+
123+
t.equal(__putout_printer, 'babel');
124+
t.end();
125+
});

packages/engine-parser/lib/parsers/babel/index.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,10 @@ module.exports.parse = function babelParse(source, overrides) {
4141
createParenthesizedExpressions: true,
4242
});
4343

44-
return parse(source, parserOptions);
44+
const ast = parse(source, parserOptions);
45+
46+
ast.program.extra.__putout_printer = printer;
47+
return ast;
4548
};
4649

4750
function getBabelLangExts({isTS, isFlow, isJSX}) {

packages/engine-parser/lib/printers/babel.spec.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,7 @@ test('putout: parser: print: printer: babel: preserve format: parens: parser: no
134134

135135
test('putout: parser: print: printer: babel: preserve format: parens', (t) => {
136136
const source = montag`
137+
a && (a = b);
137138
const a: (boolean | number)[] = [false, 1];
138139
`;
139140

@@ -148,6 +149,7 @@ test('putout: parser: print: printer: babel: preserve format: parens', (t) => {
148149
});
149150

150151
const expected = montag`
152+
a && (a = b);
151153
const a: (boolean | number)[] = [false, 1];\n
152154
`;
153155

0 commit comments

Comments
 (0)