@putout/engine-parser 5.5.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
 
@@ -23,11 +23,35 @@ Any parser should be installed before use, but you can be sure that `@babel/pars
23
23
 
24
24
  ## API
25
25
 
26
- ### print(ast)
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
+ printer: 'babel',
31
+ });
32
+
33
+ const code = print(ast, {
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',
47
+ });
48
+ ```
49
+
50
+ ### print(ast [, options])
27
51
 
28
52
  Print code from `ast`
29
53
 
30
- ### parse(code)
54
+ ### parse(code, [, options])
31
55
 
32
56
  You can add `default options` for custom `parser` you use.
33
57
 
@@ -57,6 +81,7 @@ const source = `const hello = 'world';`;
57
81
  const ast = parse(source, {
58
82
  sourceFileName: 'hello.js',
59
83
  });
84
+
60
85
  print(ast, {sourceMapName: 'hello.map'});
61
86
  // returns
62
87
  `const hello = 'world';
package/lib/parse.js CHANGED
@@ -2,33 +2,34 @@
2
2
 
3
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
- module.exports = parse;
9
-
10
- function parse(source, options) {
7
+ module.exports = (source, options) => {
11
8
  const {
12
9
  parser,
10
+ printer = 'recast',
13
11
  isTS,
14
12
  isFlow,
15
13
  isJSX,
16
14
  sourceFileName,
17
15
  } = options || {};
18
16
 
19
- const ast = recast.parse(source, {
17
+ const cookedParser = getParser({
18
+ parser,
19
+ isTS,
20
+ isFlow,
21
+ isJSX,
20
22
  sourceFileName,
21
- parser: getParser({
22
- parser,
23
- isTS,
24
- isFlow,
25
- isJSX,
26
- sourceFileName,
27
- }),
28
23
  });
29
24
 
30
- return ast;
31
- }
25
+ if (printer !== 'recast')
26
+ return cookedParser.parse(source);
27
+
28
+ return recast.parse(source, {
29
+ sourceFileName,
30
+ parser: cookedParser,
31
+ });
32
+ };
32
33
 
33
34
  function getParser({parser = 'babel', isTS, isFlow, isJSX}) {
34
35
  return {
package/lib/print.js CHANGED
@@ -1,47 +1,49 @@
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
- const {sourceMapName} = options;
16
- const printOptions = {
17
- quote: 'single',
18
- objectCurlySpacing: false,
19
- wrapColumn: Infinity,
20
- ...options,
21
- };
22
- const printed = print(ast, printOptions);
12
+ const {
13
+ sourceMapName,
14
+ printer = 'recast',
15
+ } = options;
23
16
 
24
- if (!canParse(printed.code))
25
- assign(printed, {
26
- code: generate(ast, {
27
- indent: {
28
- style: ' ',
29
- },
30
- }).code,
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,
41
+ function babelPrint(ast) {
42
+ const {code} = generate(ast, {
43
+ indent: {
44
+ style: ' ',
45
+ },
45
46
  });
46
- return !error;
47
+
48
+ return `${code}\n`;
47
49
  }
package/package.json CHANGED
@@ -1,12 +1,11 @@
1
1
  {
2
2
  "name": "@putout/engine-parser",
3
- "version": "5.5.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",