@putout/engine-parser 15.0.4 → 15.0.6
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 +4 -3
- package/lib/custom-parser.js +11 -9
- package/lib/generate.js +3 -5
- package/lib/parse.js +4 -5
- package/lib/parser.js +4 -10
- package/lib/parsers/{acorn.js → acorn.cjs} +1 -1
- package/lib/parsers/babel/index.js +5 -9
- package/lib/parsers/babel/options.js +3 -7
- package/lib/parsers/babel/plugins.js +1 -3
- package/lib/parsers/{espree.js → espree.cjs} +2 -1
- package/lib/parsers/{esprima.js → esprima.cjs} +2 -1
- package/lib/parsers/{hermes.js → hermes.cjs} +1 -2
- package/lib/parsers/{tenko.js → tenko.cjs} +1 -1
- package/lib/print.js +5 -7
- package/lib/printers/babel.js +3 -5
- package/lib/second-chance.js +2 -4
- package/lib/template.js +22 -17
- package/lib/try-throw-with-reason.js +2 -4
- package/package.json +7 -7
package/README.md
CHANGED
|
@@ -82,8 +82,8 @@ To generate sourcemap using babel generator, you should use babel parser before.
|
|
|
82
82
|
This is low level transformation, because `Babel` doesn't preserve any formatting.
|
|
83
83
|
|
|
84
84
|
```js
|
|
85
|
-
|
|
86
|
-
|
|
85
|
+
import {generate} from '@putout/engine-parser';
|
|
86
|
+
import {parse} from '@putout/engine-parser/babel';
|
|
87
87
|
|
|
88
88
|
const ast = parse(source, {
|
|
89
89
|
sourceFilename: 'hello.js',
|
|
@@ -103,7 +103,8 @@ generate(ast, {sourceMaps: true}, {
|
|
|
103
103
|
## Example
|
|
104
104
|
|
|
105
105
|
```js
|
|
106
|
-
|
|
106
|
+
import {parse} from '@putout/engine-parser';
|
|
107
|
+
|
|
107
108
|
const parser = 'acorn';
|
|
108
109
|
|
|
109
110
|
const code = parse('var t = "hello"', {
|
package/lib/custom-parser.js
CHANGED
|
@@ -1,19 +1,19 @@
|
|
|
1
|
-
|
|
1
|
+
import module from 'node:module';
|
|
2
|
+
import * as acorn from './parsers/acorn.cjs';
|
|
3
|
+
import * as babel from './parsers/babel/index.js';
|
|
4
|
+
import * as espree from './parsers/espree.cjs';
|
|
5
|
+
import * as esprima from './parsers/esprima.cjs';
|
|
6
|
+
import * as tenko from './parsers/tenko.cjs';
|
|
7
|
+
import * as hermes from './parsers/hermes.cjs';
|
|
8
|
+
import {secondChance} from './second-chance.js';
|
|
2
9
|
|
|
3
|
-
const acorn = require('./parsers/acorn');
|
|
4
|
-
const babel = require('./parsers/babel');
|
|
5
|
-
const espree = require('./parsers/espree');
|
|
6
|
-
const esprima = require('./parsers/esprima');
|
|
7
|
-
const tenko = require('./parsers/tenko');
|
|
8
|
-
const hermes = require('./parsers/hermes');
|
|
9
|
-
const secondChance = require('./second-chance');
|
|
10
10
|
const isObject = (a) => typeof a === 'object';
|
|
11
11
|
|
|
12
12
|
const MESSAGES = [
|
|
13
13
|
'has already been declared',
|
|
14
14
|
];
|
|
15
15
|
|
|
16
|
-
|
|
16
|
+
export default (source, parser, {isTS, isJSX, printer}) => {
|
|
17
17
|
const options = {
|
|
18
18
|
parser,
|
|
19
19
|
printer,
|
|
@@ -68,5 +68,7 @@ function customParse(source, {parser, printer, isTS, isJSX, isRecovery}) {
|
|
|
68
68
|
if (parser === 'hermes')
|
|
69
69
|
return hermes.parse(source);
|
|
70
70
|
|
|
71
|
+
const require = module.createRequire(import.meta.url);
|
|
72
|
+
|
|
71
73
|
return require(parser).parse(source);
|
|
72
74
|
}
|
package/lib/generate.js
CHANGED
|
@@ -1,9 +1,7 @@
|
|
|
1
|
-
|
|
1
|
+
import {generate as babelGenerate} from '@putout/babel';
|
|
2
2
|
|
|
3
|
-
const
|
|
4
|
-
|
|
5
|
-
module.exports = (node, options, sourceMaps) => {
|
|
6
|
-
return generate(node, {
|
|
3
|
+
export const generate = (node, options, sourceMaps) => {
|
|
4
|
+
return babelGenerate(node, {
|
|
7
5
|
comments: false,
|
|
8
6
|
...options,
|
|
9
7
|
}, sourceMaps);
|
package/lib/parse.js
CHANGED
|
@@ -1,12 +1,11 @@
|
|
|
1
|
-
|
|
1
|
+
import {estreeToBabel} from 'estree-to-babel';
|
|
2
|
+
import customParser from './custom-parser.js';
|
|
3
|
+
import {tryThrowWithReason} from './try-throw-with-reason.js';
|
|
2
4
|
|
|
3
|
-
const {estreeToBabel} = require('estree-to-babel');
|
|
4
|
-
const customParser = require('./custom-parser');
|
|
5
|
-
const {tryThrowWithReason} = require('./try-throw-with-reason');
|
|
6
5
|
const {assign} = Object;
|
|
7
6
|
const isString = (a) => typeof a === 'string';
|
|
8
7
|
|
|
9
|
-
|
|
8
|
+
export const parse = (source, options) => {
|
|
10
9
|
check(source);
|
|
11
10
|
|
|
12
11
|
const {
|
package/lib/parser.js
CHANGED
|
@@ -1,11 +1,5 @@
|
|
|
1
|
-
|
|
1
|
+
export * from './print.js';
|
|
2
|
+
export * from './parse.js';
|
|
3
|
+
export * from './generate.js';
|
|
4
|
+
export * from './template.js';
|
|
2
5
|
|
|
3
|
-
const print = require('./print');
|
|
4
|
-
const parse = require('./parse');
|
|
5
|
-
const generate = require('./generate');
|
|
6
|
-
const template = require('./template');
|
|
7
|
-
|
|
8
|
-
module.exports.print = print;
|
|
9
|
-
module.exports.parse = parse;
|
|
10
|
-
module.exports.generate = generate;
|
|
11
|
-
module.exports.template = template;
|
|
@@ -1,15 +1,12 @@
|
|
|
1
|
-
|
|
1
|
+
import {parse as babelParse} from '@putout/babel';
|
|
2
|
+
import plugins from './plugins.js';
|
|
3
|
+
import * as options from './options.js';
|
|
2
4
|
|
|
3
|
-
const once = require('once');
|
|
4
|
-
|
|
5
|
-
const plugins = require('./plugins');
|
|
6
|
-
const options = require('./options');
|
|
7
5
|
const {assign} = Object;
|
|
8
6
|
const getFlow = (a) => !a.indexOf('// @flow');
|
|
9
7
|
const clean = (a) => a.filter(Boolean);
|
|
10
|
-
const initBabel = once(() => require('@putout/babel'));
|
|
11
8
|
|
|
12
|
-
|
|
9
|
+
export const parse = (source, overrides) => {
|
|
13
10
|
const {
|
|
14
11
|
sourceFileName,
|
|
15
12
|
isTS,
|
|
@@ -19,7 +16,6 @@ module.exports.parse = function babelParse(source, overrides) {
|
|
|
19
16
|
printer,
|
|
20
17
|
} = overrides;
|
|
21
18
|
|
|
22
|
-
const {parse} = initBabel();
|
|
23
19
|
const parserOptions = {
|
|
24
20
|
sourceFileName,
|
|
25
21
|
sourceType: 'module',
|
|
@@ -41,7 +37,7 @@ module.exports.parse = function babelParse(source, overrides) {
|
|
|
41
37
|
tokens: true,
|
|
42
38
|
});
|
|
43
39
|
|
|
44
|
-
const ast =
|
|
40
|
+
const ast = babelParse(source, parserOptions);
|
|
45
41
|
|
|
46
42
|
ast.program.extra.__putout_printer = printer;
|
|
47
43
|
return ast;
|
|
@@ -1,7 +1,3 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
allowReturnOutsideFunction: true,
|
|
5
|
-
allowUndeclaredExports: true,
|
|
6
|
-
allowImportExportEverywhere: true,
|
|
7
|
-
};
|
|
1
|
+
export const allowReturnOutsideFunction = true;
|
|
2
|
+
export const allowUndeclaredExports = true;
|
|
3
|
+
export const allowImportExportEverywhere = true;
|
|
@@ -1,9 +1,10 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
3
|
const once = require('once');
|
|
4
|
+
|
|
4
5
|
const initEspree = once(() => require('espree'));
|
|
5
6
|
|
|
6
|
-
module.exports.parse =
|
|
7
|
+
module.exports.parse = (source) => {
|
|
7
8
|
const {parse} = initEspree();
|
|
8
9
|
const preventUsingEsprima = true;
|
|
9
10
|
|
|
@@ -1,9 +1,10 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
3
|
const once = require('once');
|
|
4
|
+
|
|
4
5
|
const initEsprima = once(() => require('esprima'));
|
|
5
6
|
|
|
6
|
-
module.exports.parse =
|
|
7
|
+
module.exports.parse = (source) => {
|
|
7
8
|
const {parse} = initEsprima();
|
|
8
9
|
|
|
9
10
|
return parse(source, {
|
|
@@ -3,9 +3,8 @@
|
|
|
3
3
|
const once = require('once');
|
|
4
4
|
const initHermes = once(() => require('hermes-parser'));
|
|
5
5
|
|
|
6
|
-
module.exports.parse =
|
|
6
|
+
module.exports.parse = (source) => {
|
|
7
7
|
const parser = initHermes();
|
|
8
|
-
|
|
9
8
|
const options = {
|
|
10
9
|
babel: true,
|
|
11
10
|
allowReturnOutsideFunction: true,
|
|
@@ -1,11 +1,11 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
3
|
const once = require('once');
|
|
4
|
+
|
|
4
5
|
const initTenko = once(() => require('tenko'));
|
|
5
6
|
|
|
6
7
|
module.exports.parse = (source) => {
|
|
7
8
|
const {Tenko} = initTenko();
|
|
8
|
-
|
|
9
9
|
const {ast} = Tenko(source, {
|
|
10
10
|
goalMode: 'module',
|
|
11
11
|
allowGlobalReturn: true,
|
package/lib/print.js
CHANGED
|
@@ -1,20 +1,18 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
const putoutPrinter = require('@putout/printer');
|
|
4
|
-
const babelPrinter = require('./printers/babel');
|
|
1
|
+
import {print as putoutPrint} from '@putout/printer';
|
|
2
|
+
import {print as babelPrint} from './printers/babel.js';
|
|
5
3
|
|
|
6
4
|
const {isArray} = Array;
|
|
7
5
|
|
|
8
6
|
const maybeArray = (a) => isArray(a) ? a : [a, {}];
|
|
9
7
|
|
|
10
|
-
|
|
8
|
+
export const print = (ast, options = {}) => {
|
|
11
9
|
const [printer = 'putout', printerOptions] = maybeArray(options.printer);
|
|
12
10
|
|
|
13
11
|
if (printer === 'babel')
|
|
14
|
-
return
|
|
12
|
+
return babelPrint(ast, {
|
|
15
13
|
...options,
|
|
16
14
|
...printerOptions,
|
|
17
15
|
});
|
|
18
16
|
|
|
19
|
-
return
|
|
17
|
+
return putoutPrint(ast, printerOptions);
|
|
20
18
|
};
|
package/lib/printers/babel.js
CHANGED
|
@@ -1,13 +1,11 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
const {generate} = require('@putout/babel');
|
|
4
|
-
const align = require('align-spaces');
|
|
1
|
+
import {generate} from '@putout/babel';
|
|
2
|
+
import align from 'align-spaces';
|
|
5
3
|
|
|
6
4
|
const defaultOptions = {
|
|
7
5
|
alignSpaces: true,
|
|
8
6
|
};
|
|
9
7
|
|
|
10
|
-
|
|
8
|
+
export const print = (ast, options) => {
|
|
11
9
|
const {source, alignSpaces} = {
|
|
12
10
|
...defaultOptions,
|
|
13
11
|
...options,
|
package/lib/second-chance.js
CHANGED
|
@@ -1,8 +1,6 @@
|
|
|
1
|
-
|
|
1
|
+
import {tryCatch} from 'try-catch';
|
|
2
2
|
|
|
3
|
-
const
|
|
4
|
-
|
|
5
|
-
module.exports = (fn, source, messages, args) => {
|
|
3
|
+
export const secondChance = (fn, source, messages, args) => {
|
|
6
4
|
const [a, ...others] = args;
|
|
7
5
|
const [errorA, resultA] = tryCatch(fn, source, a);
|
|
8
6
|
|
package/lib/template.js
CHANGED
|
@@ -1,9 +1,14 @@
|
|
|
1
|
-
|
|
1
|
+
import {
|
|
2
|
+
types,
|
|
3
|
+
template as babelTemplate,
|
|
4
|
+
} from '@putout/babel';
|
|
5
|
+
import nano from 'nano-memoize';
|
|
6
|
+
import plugins from './parsers/babel/plugins.js';
|
|
7
|
+
import * as options from './parsers/babel/options.js';
|
|
2
8
|
|
|
3
|
-
const {
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
const options = require('./parsers/babel/options');
|
|
9
|
+
const {
|
|
10
|
+
nanomemoize = nano,
|
|
11
|
+
} = nano;
|
|
7
12
|
|
|
8
13
|
const defaults = {
|
|
9
14
|
...options,
|
|
@@ -29,8 +34,8 @@ const extractExpression = (a) => {
|
|
|
29
34
|
return a;
|
|
30
35
|
};
|
|
31
36
|
|
|
32
|
-
|
|
33
|
-
const fn =
|
|
37
|
+
export const template = nanomemoize((value, options) => {
|
|
38
|
+
const fn = babelTemplate(value, {
|
|
34
39
|
...defaults,
|
|
35
40
|
...options,
|
|
36
41
|
});
|
|
@@ -41,8 +46,10 @@ module.exports = nanomemoize((value, options) => {
|
|
|
41
46
|
};
|
|
42
47
|
});
|
|
43
48
|
|
|
44
|
-
|
|
45
|
-
|
|
49
|
+
template.extractExpression = extractExpression;
|
|
50
|
+
|
|
51
|
+
template.ast = nanomemoize((value, options) => {
|
|
52
|
+
const result = babelTemplate.ast(value, {
|
|
46
53
|
...defaults,
|
|
47
54
|
...options,
|
|
48
55
|
});
|
|
@@ -50,8 +57,8 @@ module.exports.ast = nanomemoize((value, options) => {
|
|
|
50
57
|
return extractExpression(result);
|
|
51
58
|
});
|
|
52
59
|
|
|
53
|
-
|
|
54
|
-
const result =
|
|
60
|
+
template.program = nanomemoize((value, options) => {
|
|
61
|
+
const result = babelTemplate.program(value, {
|
|
55
62
|
...defaults,
|
|
56
63
|
...options,
|
|
57
64
|
});
|
|
@@ -59,8 +66,8 @@ module.exports.program = nanomemoize((value, options) => {
|
|
|
59
66
|
return result;
|
|
60
67
|
});
|
|
61
68
|
|
|
62
|
-
|
|
63
|
-
const result =
|
|
69
|
+
template.program.ast = nanomemoize((value, options) => {
|
|
70
|
+
const result = babelTemplate.program.ast(value, {
|
|
64
71
|
...defaults,
|
|
65
72
|
...options,
|
|
66
73
|
});
|
|
@@ -68,13 +75,11 @@ module.exports.program.ast = nanomemoize((value, options) => {
|
|
|
68
75
|
return result;
|
|
69
76
|
});
|
|
70
77
|
|
|
71
|
-
|
|
72
|
-
const result =
|
|
78
|
+
template.ast.fresh = (value, options) => {
|
|
79
|
+
const result = babelTemplate.ast(value, {
|
|
73
80
|
...defaults,
|
|
74
81
|
...options,
|
|
75
82
|
});
|
|
76
83
|
|
|
77
84
|
return extractExpression(result);
|
|
78
85
|
};
|
|
79
|
-
|
|
80
|
-
module.exports.extractExpression = extractExpression;
|
|
@@ -1,8 +1,6 @@
|
|
|
1
|
-
|
|
1
|
+
import {tryCatch} from 'try-catch';
|
|
2
2
|
|
|
3
|
-
const
|
|
4
|
-
|
|
5
|
-
module.exports.tryThrowWithReason = (fn, ...args) => {
|
|
3
|
+
export const tryThrowWithReason = (fn, ...args) => {
|
|
6
4
|
const [error, result] = tryCatch(fn, ...args);
|
|
7
5
|
|
|
8
6
|
if (error) {
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@putout/engine-parser",
|
|
3
|
-
"version": "15.0.
|
|
4
|
-
"type": "
|
|
3
|
+
"version": "15.0.6",
|
|
4
|
+
"type": "module",
|
|
5
5
|
"author": "coderaiser <mnemonic.enemy@gmail.com> (https://github.com/coderaiser)",
|
|
6
6
|
"description": "🐊Putout parser",
|
|
7
7
|
"homepage": "https://github.com/coderaiser/putout/tree/master/packages/engine-parser#readme",
|
|
@@ -11,12 +11,12 @@
|
|
|
11
11
|
"changelog": false,
|
|
12
12
|
"exports": {
|
|
13
13
|
".": "./lib/parser.js",
|
|
14
|
-
"./acorn": "./lib/parsers/acorn.
|
|
14
|
+
"./acorn": "./lib/parsers/acorn.cjs",
|
|
15
15
|
"./babel": "./lib/parsers/babel/index.js",
|
|
16
16
|
"./babel/options": "./lib/parsers/babel/options.js",
|
|
17
17
|
"./babel/plugins": "./lib/parsers/babel/plugins.js",
|
|
18
|
-
"./espree": "./lib/parsers/espree.
|
|
19
|
-
"./esprima": "./lib/parsers/esprima.
|
|
18
|
+
"./espree": "./lib/parsers/espree.cjs",
|
|
19
|
+
"./esprima": "./lib/parsers/esprima.cjs"
|
|
20
20
|
},
|
|
21
21
|
"repository": {
|
|
22
22
|
"type": "git",
|
|
@@ -34,7 +34,7 @@
|
|
|
34
34
|
},
|
|
35
35
|
"dependencies": {
|
|
36
36
|
"@putout/babel": "^5.0.0",
|
|
37
|
-
"@putout/printer": "^
|
|
37
|
+
"@putout/printer": "^17.0.0",
|
|
38
38
|
"align-spaces": "^2.0.0",
|
|
39
39
|
"estree-to-babel": "^11.0.3",
|
|
40
40
|
"nano-memoize": "^3.0.11",
|
|
@@ -58,7 +58,7 @@
|
|
|
58
58
|
"esprima": "^4.0.1",
|
|
59
59
|
"hermes-parser": "^0.33.0",
|
|
60
60
|
"just-camel-case": "^6.2.0",
|
|
61
|
-
"madrun": "^
|
|
61
|
+
"madrun": "^12.0.0",
|
|
62
62
|
"montag": "^1.0.0",
|
|
63
63
|
"nodemon": "^3.0.1",
|
|
64
64
|
"putout": "*",
|