@putout/engine-parser 5.4.1 → 5.6.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 +15 -2
- package/lib/parse.js +16 -14
- package/lib/parsers/acorn.js +1 -1
- package/lib/parsers/espree.js +1 -1
- package/lib/print.js +29 -34
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -23,11 +23,23 @@ Any parser should be installed before use, but you can be sure that `@babel/pars
|
|
|
23
23
|
|
|
24
24
|
## API
|
|
25
25
|
|
|
26
|
-
|
|
26
|
+
You can avoid using [`recast`](https://github.com/putoutjs/recast) and speed up parsing and printing a bit using only Babel using:
|
|
27
|
+
|
|
28
|
+
```js
|
|
29
|
+
const ast = parse(source, {
|
|
30
|
+
recast: false,
|
|
31
|
+
});
|
|
32
|
+
|
|
33
|
+
const code = print(ast, {
|
|
34
|
+
recast: false,
|
|
35
|
+
});
|
|
36
|
+
```
|
|
37
|
+
|
|
38
|
+
### print(ast [, options])
|
|
27
39
|
|
|
28
40
|
Print code from `ast`
|
|
29
41
|
|
|
30
|
-
### parse(code)
|
|
42
|
+
### parse(code, [, options])
|
|
31
43
|
|
|
32
44
|
You can add `default options` for custom `parser` you use.
|
|
33
45
|
|
|
@@ -57,6 +69,7 @@ const source = `const hello = 'world';`;
|
|
|
57
69
|
const ast = parse(source, {
|
|
58
70
|
sourceFileName: 'hello.js',
|
|
59
71
|
});
|
|
72
|
+
|
|
60
73
|
print(ast, {sourceMapName: 'hello.map'});
|
|
61
74
|
// returns
|
|
62
75
|
`const hello = 'world';
|
package/lib/parse.js
CHANGED
|
@@ -1,34 +1,36 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
|
-
const
|
|
3
|
+
const {parse} = require('@putout/recast');
|
|
4
4
|
const toBabel = require('estree-to-babel');
|
|
5
5
|
|
|
6
6
|
const customParser = require('./custom-parser');
|
|
7
7
|
|
|
8
|
-
module.exports =
|
|
9
|
-
|
|
10
|
-
function parse(source, options) {
|
|
8
|
+
module.exports = (source, options) => {
|
|
11
9
|
const {
|
|
12
10
|
parser,
|
|
13
11
|
isTS,
|
|
14
12
|
isFlow,
|
|
15
13
|
isJSX,
|
|
16
14
|
sourceFileName,
|
|
15
|
+
recast = true,
|
|
17
16
|
} = options || {};
|
|
18
17
|
|
|
19
|
-
const
|
|
18
|
+
const cookedParser = getParser({
|
|
19
|
+
parser,
|
|
20
|
+
isTS,
|
|
21
|
+
isFlow,
|
|
22
|
+
isJSX,
|
|
20
23
|
sourceFileName,
|
|
21
|
-
parser: getParser({
|
|
22
|
-
parser,
|
|
23
|
-
isTS,
|
|
24
|
-
isFlow,
|
|
25
|
-
isJSX,
|
|
26
|
-
sourceFileName,
|
|
27
|
-
}),
|
|
28
24
|
});
|
|
29
25
|
|
|
30
|
-
|
|
31
|
-
|
|
26
|
+
if (!recast)
|
|
27
|
+
return cookedParser.parse(source);
|
|
28
|
+
|
|
29
|
+
return parse(source, {
|
|
30
|
+
sourceFileName,
|
|
31
|
+
parser: cookedParser,
|
|
32
|
+
});
|
|
33
|
+
};
|
|
32
34
|
|
|
33
35
|
function getParser({parser = 'babel', isTS, isFlow, isJSX}) {
|
|
34
36
|
return {
|
package/lib/parsers/acorn.js
CHANGED
package/lib/parsers/espree.js
CHANGED
package/lib/print.js
CHANGED
|
@@ -2,13 +2,21 @@
|
|
|
2
2
|
|
|
3
3
|
const {print} = require('@putout/recast');
|
|
4
4
|
const generate = require('@babel/generator').default;
|
|
5
|
-
const
|
|
6
|
-
|
|
7
|
-
const
|
|
5
|
+
const tryCatch = require('try-catch');
|
|
6
|
+
|
|
7
|
+
const parse = require('./parse');
|
|
8
8
|
const {stringify} = JSON;
|
|
9
|
+
const btoa = (a) => Buffer.from(a, 'binary').toString('base64');
|
|
9
10
|
const {assign} = Object;
|
|
11
|
+
const addSourceMap = (sourceMapName, {code, map}) => !sourceMapName ? code : `${code}\n//# sourceMappingURL=data:application/json;charset=utf-8;base64,${btoa(stringify(map))}\n`;
|
|
12
|
+
const fixStrictMode = (a) => a.replace(`\n\n\n'use strict'`, `\n\n'use strict'`);
|
|
13
|
+
|
|
10
14
|
module.exports = (ast, options = {}) => {
|
|
11
|
-
const {
|
|
15
|
+
const {
|
|
16
|
+
recast = true,
|
|
17
|
+
sourceMapName,
|
|
18
|
+
} = options;
|
|
19
|
+
|
|
12
20
|
const printOptions = {
|
|
13
21
|
quote: 'single',
|
|
14
22
|
objectCurlySpacing: false,
|
|
@@ -17,13 +25,9 @@ module.exports = (ast, options = {}) => {
|
|
|
17
25
|
};
|
|
18
26
|
const printed = print(ast, printOptions);
|
|
19
27
|
|
|
20
|
-
if (
|
|
28
|
+
if (!recast || !canParse(printed.code))
|
|
21
29
|
assign(printed, {
|
|
22
|
-
code:
|
|
23
|
-
indent: {
|
|
24
|
-
style: ' ',
|
|
25
|
-
},
|
|
26
|
-
}).code,
|
|
30
|
+
code: babelPrint(ast),
|
|
27
31
|
});
|
|
28
32
|
|
|
29
33
|
const {map} = printed;
|
|
@@ -34,30 +38,21 @@ module.exports = (ast, options = {}) => {
|
|
|
34
38
|
map,
|
|
35
39
|
});
|
|
36
40
|
};
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
return false;
|
|
43
|
-
|
|
44
|
-
while (--i) {
|
|
45
|
-
const current = source[i];
|
|
46
|
-
|
|
47
|
-
if (current === ')') {
|
|
48
|
-
brackets.push(current);
|
|
49
|
-
continue;
|
|
50
|
-
}
|
|
51
|
-
|
|
52
|
-
if (current === '(') {
|
|
53
|
-
brackets.pop();
|
|
54
|
-
continue;
|
|
55
|
-
}
|
|
56
|
-
}
|
|
57
|
-
const current = source[i];
|
|
41
|
+
|
|
42
|
+
function canParse(source) {
|
|
43
|
+
const [error] = tryCatch(parse, source, {
|
|
44
|
+
isTS: true,
|
|
45
|
+
});
|
|
58
46
|
|
|
59
|
-
|
|
60
|
-
|
|
47
|
+
return !error;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
function babelPrint(ast) {
|
|
51
|
+
const {code} = generate(ast, {
|
|
52
|
+
indent: {
|
|
53
|
+
style: ' ',
|
|
54
|
+
},
|
|
55
|
+
});
|
|
61
56
|
|
|
62
|
-
return
|
|
57
|
+
return `${code}\n`;
|
|
63
58
|
}
|