@putout/engine-parser 6.0.0 → 6.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +26 -5
- package/lib/custom-parser.js +0 -2
- package/lib/generate.js +0 -1
- package/lib/parser.js +0 -1
- package/lib/parsers/acorn.js +1 -1
- package/lib/parsers/babel/index.js +4 -12
- package/lib/parsers/babel/move-out-directives.js +1 -5
- package/lib/parsers/babel/options.js +0 -1
- package/lib/parsers/babel/plugins.js +0 -1
- package/lib/parsers/espree.js +0 -1
- package/lib/parsers/esprima.js +0 -1
- package/lib/parsers/hermes.js +1 -2
- package/lib/parsers/tenko.js +1 -1
- package/lib/print.js +10 -6
- package/lib/second-chance.js +0 -1
- package/lib/template.js +0 -2
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -39,11 +39,23 @@ You can use brand new [`@putout/printer`](https://github.com/putoutjs/printer) w
|
|
|
39
39
|
|
|
40
40
|
```js
|
|
41
41
|
const ast = parse(source, {
|
|
42
|
-
printer: '
|
|
42
|
+
printer: 'putout',
|
|
43
43
|
});
|
|
44
44
|
|
|
45
45
|
const code = print(ast, {
|
|
46
|
-
printer: '
|
|
46
|
+
printer: 'putout',
|
|
47
|
+
});
|
|
48
|
+
```
|
|
49
|
+
|
|
50
|
+
When you need to pass options, use:
|
|
51
|
+
|
|
52
|
+
```js
|
|
53
|
+
const code = print(ast, {
|
|
54
|
+
printer: ['putout', {
|
|
55
|
+
format: {
|
|
56
|
+
indent: ' ',
|
|
57
|
+
},
|
|
58
|
+
}],
|
|
47
59
|
});
|
|
48
60
|
```
|
|
49
61
|
|
|
@@ -78,11 +90,14 @@ You have two ways to benefit from source map generation:
|
|
|
78
90
|
|
|
79
91
|
```js
|
|
80
92
|
const source = `const hello = 'world';`;
|
|
93
|
+
|
|
81
94
|
const ast = parse(source, {
|
|
82
95
|
sourceFileName: 'hello.js',
|
|
83
96
|
});
|
|
84
97
|
|
|
85
|
-
print(ast, {
|
|
98
|
+
print(ast, {
|
|
99
|
+
sourceMapName: 'hello.map',
|
|
100
|
+
});
|
|
86
101
|
// returns
|
|
87
102
|
`const hello = 'world';
|
|
88
103
|
{"version":3,"sources":["hello.js"],"names":[],"mappings":"AAAA...","file":"hello.map","sourcesContent":["const hello = 'world';"]}`;
|
|
@@ -101,11 +116,17 @@ const ast = babel.parse(source, {
|
|
|
101
116
|
sourceFilename: 'hello.js',
|
|
102
117
|
});
|
|
103
118
|
|
|
104
|
-
generate(ast, {
|
|
119
|
+
generate(ast, {
|
|
120
|
+
sourceMaps: true,
|
|
121
|
+
}, {
|
|
105
122
|
'hello.js': source,
|
|
106
123
|
});
|
|
124
|
+
|
|
107
125
|
// returns
|
|
108
|
-
({
|
|
126
|
+
({
|
|
127
|
+
code,
|
|
128
|
+
map,
|
|
129
|
+
});
|
|
109
130
|
```
|
|
110
131
|
|
|
111
132
|
## Example
|
package/lib/custom-parser.js
CHANGED
|
@@ -7,7 +7,6 @@ const esprima = require('./parsers/esprima');
|
|
|
7
7
|
const tenko = require('./parsers/tenko');
|
|
8
8
|
const hermes = require('./parsers/hermes');
|
|
9
9
|
const secondChance = require('./second-chance');
|
|
10
|
-
|
|
11
10
|
const isObject = (a) => typeof a === 'object';
|
|
12
11
|
|
|
13
12
|
module.exports = (source, parser, {isTS, isFlow, isJSX}) => {
|
|
@@ -55,4 +54,3 @@ function customParse(source, {parser, isTS, isFlow, isJSX}) {
|
|
|
55
54
|
|
|
56
55
|
return require(parser).parse(source);
|
|
57
56
|
}
|
|
58
|
-
|
package/lib/generate.js
CHANGED
package/lib/parser.js
CHANGED
package/lib/parsers/acorn.js
CHANGED
|
@@ -12,6 +12,7 @@ const initAcorn = once(() => {
|
|
|
12
12
|
|
|
13
13
|
module.exports.parse = function acornParse(source) {
|
|
14
14
|
const parser = initAcorn();
|
|
15
|
+
|
|
15
16
|
const options = {
|
|
16
17
|
locations: true,
|
|
17
18
|
comment: true,
|
|
@@ -30,4 +31,3 @@ module.exports.parse = function acornParse(source) {
|
|
|
30
31
|
tokens: tokensToAvoidEsprima,
|
|
31
32
|
};
|
|
32
33
|
};
|
|
33
|
-
|
|
@@ -11,10 +11,8 @@ process.env.BABEL_TYPES_8_BREAKING = true;
|
|
|
11
11
|
|
|
12
12
|
const plugins = require('./plugins');
|
|
13
13
|
const options = require('./options');
|
|
14
|
-
|
|
15
14
|
const moveOutDirectives = require('./move-out-directives');
|
|
16
|
-
|
|
17
|
-
const getFlow = (a) => a.includes('// @flow');
|
|
15
|
+
const getFlow = (a) => !a.indexOf('// @flow');
|
|
18
16
|
const clean = (a) => a.filter(Boolean);
|
|
19
17
|
const initBabel = once(() => require('@babel/parser'));
|
|
20
18
|
const {assign} = Object;
|
|
@@ -24,6 +22,7 @@ const {assign} = Object;
|
|
|
24
22
|
// babel, putout: sourceFilename
|
|
25
23
|
module.exports.parse = function babelParse(source, {sourceFilename, isTS, isJSX = true, isFlow = getFlow(source)}) {
|
|
26
24
|
const {parse} = initBabel();
|
|
25
|
+
|
|
27
26
|
const parserOptions = {
|
|
28
27
|
sourceType: 'module',
|
|
29
28
|
tokens: true,
|
|
@@ -43,7 +42,6 @@ module.exports.parse = function babelParse(source, {sourceFilename, isTS, isJSX
|
|
|
43
42
|
});
|
|
44
43
|
|
|
45
44
|
const ast = parse(source, parserOptions);
|
|
46
|
-
|
|
47
45
|
moveOutDirectives(ast);
|
|
48
46
|
|
|
49
47
|
return ast;
|
|
@@ -55,16 +53,10 @@ function getBabelLangExts({isTS, isFlow, isJSX}) {
|
|
|
55
53
|
];
|
|
56
54
|
|
|
57
55
|
if (isTS)
|
|
58
|
-
return langs.concat([
|
|
59
|
-
'typescript',
|
|
60
|
-
]);
|
|
56
|
+
return langs.concat(['typescript']);
|
|
61
57
|
|
|
62
58
|
if (isFlow)
|
|
63
|
-
return langs.concat([
|
|
64
|
-
'flow',
|
|
65
|
-
'flowComments',
|
|
66
|
-
]);
|
|
59
|
+
return langs.concat(['flow', 'flowComments']);
|
|
67
60
|
|
|
68
61
|
return langs;
|
|
69
62
|
}
|
|
70
|
-
|
|
@@ -18,12 +18,8 @@ module.exports = (ast) => {
|
|
|
18
18
|
|
|
19
19
|
for (const directive of directives) {
|
|
20
20
|
const {value} = directive.value;
|
|
21
|
-
|
|
22
|
-
body.unshift(
|
|
23
|
-
ExpressionStatement(StringLiteral(value)),
|
|
24
|
-
);
|
|
21
|
+
body.unshift(ExpressionStatement(StringLiteral(value)));
|
|
25
22
|
}
|
|
26
23
|
|
|
27
24
|
return ast;
|
|
28
25
|
};
|
|
29
|
-
|
package/lib/parsers/espree.js
CHANGED
package/lib/parsers/esprima.js
CHANGED
package/lib/parsers/hermes.js
CHANGED
|
@@ -1,11 +1,11 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
3
|
const once = require('once');
|
|
4
|
-
|
|
5
4
|
const initHermes = once(() => require('hermes-parser'));
|
|
6
5
|
|
|
7
6
|
module.exports.parse = function hermesParse(source) {
|
|
8
7
|
const parser = initHermes();
|
|
8
|
+
|
|
9
9
|
const options = {
|
|
10
10
|
babel: true,
|
|
11
11
|
allowReturnOutsideFunction: true,
|
|
@@ -18,4 +18,3 @@ module.exports.parse = function hermesParse(source) {
|
|
|
18
18
|
|
|
19
19
|
return result;
|
|
20
20
|
};
|
|
21
|
-
|
package/lib/parsers/tenko.js
CHANGED
|
@@ -5,6 +5,7 @@ const initTenko = once(() => require('tenko'));
|
|
|
5
5
|
|
|
6
6
|
module.exports.parse = (source) => {
|
|
7
7
|
const {Tenko} = initTenko();
|
|
8
|
+
|
|
8
9
|
const {ast} = Tenko(source, {
|
|
9
10
|
goalMode: 'module',
|
|
10
11
|
allowGlobalReturn: true,
|
|
@@ -13,4 +14,3 @@ module.exports.parse = (source) => {
|
|
|
13
14
|
|
|
14
15
|
return ast;
|
|
15
16
|
};
|
|
16
|
-
|
package/lib/print.js
CHANGED
|
@@ -3,16 +3,20 @@
|
|
|
3
3
|
const recast = require('@putout/recast');
|
|
4
4
|
const putoutPrinter = require('@putout/printer');
|
|
5
5
|
const generate = require('@babel/generator').default;
|
|
6
|
+
|
|
6
7
|
const {stringify} = JSON;
|
|
8
|
+
const {isArray} = Array;
|
|
9
|
+
|
|
10
|
+
const maybeArray = (a) => isArray(a) ? a : [a, {}];
|
|
7
11
|
const btoa = (a) => Buffer.from(a, 'binary').toString('base64');
|
|
12
|
+
|
|
8
13
|
const addSourceMap = (sourceMapName, {code, map}) => !sourceMapName ? code : `${code}\n//# sourceMappingURL=data:application/json;charset=utf-8;base64,${btoa(stringify(map))}\n`;
|
|
9
14
|
const fixStrictMode = (a) => a.replace(`\n\n\n'use strict'`, `\n\n'use strict'`);
|
|
10
15
|
|
|
11
16
|
module.exports = (ast, options = {}) => {
|
|
12
|
-
const {
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
} = options;
|
|
17
|
+
const {sourceMapName} = options;
|
|
18
|
+
|
|
19
|
+
const [printer = 'recast', printerOptions] = maybeArray(options.printer);
|
|
16
20
|
|
|
17
21
|
if (printer === 'recast') {
|
|
18
22
|
const printOptions = {
|
|
@@ -21,8 +25,8 @@ module.exports = (ast, options = {}) => {
|
|
|
21
25
|
wrapColumn: Infinity,
|
|
22
26
|
...options,
|
|
23
27
|
};
|
|
24
|
-
const printed = recast.print(ast, printOptions);
|
|
25
28
|
|
|
29
|
+
const printed = recast.print(ast, printOptions);
|
|
26
30
|
const {map} = printed;
|
|
27
31
|
const code = fixStrictMode(printed.code);
|
|
28
32
|
|
|
@@ -35,7 +39,7 @@ module.exports = (ast, options = {}) => {
|
|
|
35
39
|
if (printer === 'babel')
|
|
36
40
|
return babelPrint(ast);
|
|
37
41
|
|
|
38
|
-
return putoutPrinter.print(ast);
|
|
42
|
+
return putoutPrinter.print(ast, printerOptions);
|
|
39
43
|
};
|
|
40
44
|
|
|
41
45
|
function babelPrint(ast) {
|
package/lib/second-chance.js
CHANGED
package/lib/template.js
CHANGED
|
@@ -2,7 +2,6 @@
|
|
|
2
2
|
|
|
3
3
|
const template = require('@babel/template').default;
|
|
4
4
|
const memo = require('nano-memoize');
|
|
5
|
-
|
|
6
5
|
const plugins = require('./parsers/babel/plugins');
|
|
7
6
|
const options = require('./parsers/babel/options');
|
|
8
7
|
|
|
@@ -59,4 +58,3 @@ module.exports.ast.fresh = (value, options) => {
|
|
|
59
58
|
|
|
60
59
|
return result.expression || result;
|
|
61
60
|
};
|
|
62
|
-
|