@putout/engine-parser 5.6.0 → 6.1.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,35 @@ 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',
43
+ });
44
+
45
+ const code = print(ast, {
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
+ }],
35
59
  });
36
60
  ```
37
61
 
@@ -66,11 +90,14 @@ You have two ways to benefit from source map generation:
66
90
 
67
91
  ```js
68
92
  const source = `const hello = 'world';`;
93
+
69
94
  const ast = parse(source, {
70
95
  sourceFileName: 'hello.js',
71
96
  });
72
97
 
73
- print(ast, {sourceMapName: 'hello.map'});
98
+ print(ast, {
99
+ sourceMapName: 'hello.map',
100
+ });
74
101
  // returns
75
102
  `const hello = 'world';
76
103
  {"version":3,"sources":["hello.js"],"names":[],"mappings":"AAAA...","file":"hello.map","sourcesContent":["const hello = 'world';"]}`;
@@ -89,11 +116,17 @@ const ast = babel.parse(source, {
89
116
  sourceFilename: 'hello.js',
90
117
  });
91
118
 
92
- generate(ast, {sourceMaps: true}, {
119
+ generate(ast, {
120
+ sourceMaps: true,
121
+ }, {
93
122
  'hello.js': source,
94
123
  });
124
+
95
125
  // returns
96
- ({code, map});
126
+ ({
127
+ code,
128
+ map,
129
+ });
97
130
  ```
98
131
 
99
132
  ## Example
@@ -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
@@ -9,4 +9,3 @@ module.exports = (node, options, sourceMaps) => {
9
9
  ...options,
10
10
  }, sourceMaps);
11
11
  };
12
-
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/parser.js CHANGED
@@ -9,4 +9,3 @@ module.exports.print = print;
9
9
  module.exports.parse = parse;
10
10
  module.exports.generate = generate;
11
11
  module.exports.template = template;
12
-
@@ -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,9 +11,7 @@ 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
15
  const getFlow = (a) => a.includes('// @flow');
18
16
  const clean = (a) => a.filter(Boolean);
19
17
  const initBabel = once(() => require('@babel/parser'));
@@ -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
-
@@ -5,4 +5,3 @@ module.exports = {
5
5
  allowUndeclaredExports: true,
6
6
  allowImportExportEverywhere: true,
7
7
  };
8
-
@@ -15,4 +15,3 @@ module.exports = [
15
15
  'throwExpressions',
16
16
  'recordAndTuple',
17
17
  ];
18
-
@@ -18,4 +18,3 @@ module.exports.parse = function espreeParse(source) {
18
18
  },
19
19
  });
20
20
  };
21
-
@@ -14,4 +14,3 @@ module.exports.parse = function esprimaParse(source) {
14
14
  jsx: true,
15
15
  });
16
16
  };
17
-
@@ -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
-
@@ -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
@@ -1,52 +1,47 @@
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
6
 
7
- const parse = require('./parse');
8
7
  const {stringify} = JSON;
8
+ const {isArray} = Array;
9
+
10
+ const maybeArray = (a) => isArray(a) ? a : [a, {}];
9
11
  const btoa = (a) => Buffer.from(a, 'binary').toString('base64');
10
- const {assign} = Object;
12
+
11
13
  const addSourceMap = (sourceMapName, {code, map}) => !sourceMapName ? code : `${code}\n//# sourceMappingURL=data:application/json;charset=utf-8;base64,${btoa(stringify(map))}\n`;
12
14
  const fixStrictMode = (a) => a.replace(`\n\n\n'use strict'`, `\n\n'use strict'`);
13
15
 
14
16
  module.exports = (ast, options = {}) => {
15
- const {
16
- recast = true,
17
- sourceMapName,
18
- } = options;
17
+ const {sourceMapName} = options;
19
18
 
20
- const printOptions = {
21
- quote: 'single',
22
- objectCurlySpacing: false,
23
- wrapColumn: Infinity,
24
- ...options,
25
- };
26
- const printed = print(ast, printOptions);
19
+ const [printer = 'recast', printerOptions] = maybeArray(options.printer);
27
20
 
28
- if (!recast || !canParse(printed.code))
29
- assign(printed, {
30
- code: babelPrint(ast),
21
+ if (printer === 'recast') {
22
+ const printOptions = {
23
+ quote: 'single',
24
+ objectCurlySpacing: false,
25
+ wrapColumn: Infinity,
26
+ ...options,
27
+ };
28
+
29
+ const printed = recast.print(ast, printOptions);
30
+ const {map} = printed;
31
+ const code = fixStrictMode(printed.code);
32
+
33
+ return addSourceMap(sourceMapName, {
34
+ code,
35
+ map,
31
36
  });
37
+ }
32
38
 
33
- const {map} = printed;
34
- const code = fixStrictMode(printed.code);
39
+ if (printer === 'babel')
40
+ return babelPrint(ast);
35
41
 
36
- return addSourceMap(sourceMapName, {
37
- code,
38
- map,
39
- });
42
+ return putoutPrinter.print(ast, printerOptions);
40
43
  };
41
44
 
42
- function canParse(source) {
43
- const [error] = tryCatch(parse, source, {
44
- isTS: true,
45
- });
46
-
47
- return !error;
48
- }
49
-
50
45
  function babelPrint(ast) {
51
46
  const {code} = generate(ast, {
52
47
  indent: {
@@ -15,4 +15,3 @@ module.exports = (fn, source, a, b) => {
15
15
 
16
16
  throw errorA;
17
17
  };
18
-
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
-
package/package.json CHANGED
@@ -1,12 +1,11 @@
1
1
  {
2
2
  "name": "@putout/engine-parser",
3
- "version": "5.6.0",
3
+ "version": "6.1.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",