@putout/engine-parser 5.6.0 → 6.0.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 CHANGED
@@ -1,7 +1,7 @@
1
1
  # @putout/engine-parser [![NPM version][NPMIMGURL]][NPMURL]
2
2
 
3
3
  [NPMIMGURL]: https://img.shields.io/npm/v/@putout/engine-parser.svg?style=flat&longCache=true
4
- [NPMURL]: https://npmjs.org/package/@putout/engine-parser"npm"
4
+ [NPMURL]: https://npmjs.org/package/@putout/engine-parser "npm"
5
5
 
6
6
  🐊[**Putout**](https://github.com/coderaiser/putout) engine that parses input.
7
7
 
@@ -27,11 +27,23 @@ You can avoid using [`recast`](https://github.com/putoutjs/recast) and speed up
27
27
 
28
28
  ```js
29
29
  const ast = parse(source, {
30
- recast: false,
30
+ printer: 'babel',
31
31
  });
32
32
 
33
33
  const code = print(ast, {
34
- recast: false,
34
+ printer: 'babel',
35
+ });
36
+ ```
37
+
38
+ You can use brand new [`@putout/printer`](https://github.com/putoutjs/printer) which formats code according to [`eslint-plugin-putout`](https://github.com/coderaiser/putout/tree/master/packages/eslint-plugin-putout#readme) rules but without using **ESLint**.
39
+
40
+ ```js
41
+ const ast = parse(source, {
42
+ printer: '@putout/printer',
43
+ });
44
+
45
+ const code = print(ast, {
46
+ printer: '@putout/printer',
35
47
  });
36
48
  ```
37
49
 
package/lib/parse.js CHANGED
@@ -1,18 +1,17 @@
1
1
  'use strict';
2
2
 
3
- const {parse} = require('@putout/recast');
3
+ const recast = require('@putout/recast');
4
4
  const toBabel = require('estree-to-babel');
5
-
6
5
  const customParser = require('./custom-parser');
7
6
 
8
7
  module.exports = (source, options) => {
9
8
  const {
10
9
  parser,
10
+ printer = 'recast',
11
11
  isTS,
12
12
  isFlow,
13
13
  isJSX,
14
14
  sourceFileName,
15
- recast = true,
16
15
  } = options || {};
17
16
 
18
17
  const cookedParser = getParser({
@@ -23,10 +22,10 @@ module.exports = (source, options) => {
23
22
  sourceFileName,
24
23
  });
25
24
 
26
- if (!recast)
25
+ if (printer !== 'recast')
27
26
  return cookedParser.parse(source);
28
27
 
29
- return parse(source, {
28
+ return recast.parse(source, {
30
29
  sourceFileName,
31
30
  parser: cookedParser,
32
31
  });
package/lib/print.js CHANGED
@@ -1,52 +1,43 @@
1
1
  'use strict';
2
2
 
3
- const {print} = require('@putout/recast');
3
+ const recast = require('@putout/recast');
4
+ const putoutPrinter = require('@putout/printer');
4
5
  const generate = require('@babel/generator').default;
5
- const tryCatch = require('try-catch');
6
-
7
- const parse = require('./parse');
8
6
  const {stringify} = JSON;
9
7
  const btoa = (a) => Buffer.from(a, 'binary').toString('base64');
10
- const {assign} = Object;
11
8
  const addSourceMap = (sourceMapName, {code, map}) => !sourceMapName ? code : `${code}\n//# sourceMappingURL=data:application/json;charset=utf-8;base64,${btoa(stringify(map))}\n`;
12
9
  const fixStrictMode = (a) => a.replace(`\n\n\n'use strict'`, `\n\n'use strict'`);
13
10
 
14
11
  module.exports = (ast, options = {}) => {
15
12
  const {
16
- recast = true,
17
13
  sourceMapName,
14
+ printer = 'recast',
18
15
  } = options;
19
16
 
20
- const printOptions = {
21
- quote: 'single',
22
- objectCurlySpacing: false,
23
- wrapColumn: Infinity,
24
- ...options,
25
- };
26
- const printed = print(ast, printOptions);
27
-
28
- if (!recast || !canParse(printed.code))
29
- assign(printed, {
30
- code: babelPrint(ast),
17
+ if (printer === 'recast') {
18
+ const printOptions = {
19
+ quote: 'single',
20
+ objectCurlySpacing: false,
21
+ wrapColumn: Infinity,
22
+ ...options,
23
+ };
24
+ const printed = recast.print(ast, printOptions);
25
+
26
+ const {map} = printed;
27
+ const code = fixStrictMode(printed.code);
28
+
29
+ return addSourceMap(sourceMapName, {
30
+ code,
31
+ map,
31
32
  });
33
+ }
32
34
 
33
- const {map} = printed;
34
- const code = fixStrictMode(printed.code);
35
+ if (printer === 'babel')
36
+ return babelPrint(ast);
35
37
 
36
- return addSourceMap(sourceMapName, {
37
- code,
38
- map,
39
- });
38
+ return putoutPrinter.print(ast);
40
39
  };
41
40
 
42
- function canParse(source) {
43
- const [error] = tryCatch(parse, source, {
44
- isTS: true,
45
- });
46
-
47
- return !error;
48
- }
49
-
50
41
  function babelPrint(ast) {
51
42
  const {code} = generate(ast, {
52
43
  indent: {
package/package.json CHANGED
@@ -1,12 +1,11 @@
1
1
  {
2
2
  "name": "@putout/engine-parser",
3
- "version": "5.6.0",
3
+ "version": "6.0.0",
4
4
  "type": "commonjs",
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",
8
8
  "main": "./lib/parser.js",
9
- "commitType": "colon",
10
9
  "release": false,
11
10
  "tag": false,
12
11
  "changelog": false,
@@ -38,6 +37,7 @@
38
37
  "@babel/parser": "^7.19.0",
39
38
  "@babel/template": "^7.18.10",
40
39
  "@babel/types": "^7.19.0",
40
+ "@putout/printer": "^1.20.0",
41
41
  "@putout/recast": "^1.12.1",
42
42
  "estree-to-babel": "^5.0.0",
43
43
  "nano-memoize": "^2.0.0",
@@ -55,7 +55,7 @@
55
55
  "c8": "^7.5.0",
56
56
  "eslint": "^8.0.1",
57
57
  "eslint-plugin-n": "^15.2.4",
58
- "eslint-plugin-putout": "^16.0.0",
58
+ "eslint-plugin-putout": "^17.0.0",
59
59
  "espree": "^9.0.0",
60
60
  "esprima": "^4.0.1",
61
61
  "hermes-parser": "^0.9.0",