@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 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
- ### 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
+ 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 recast = require('@putout/recast');
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 = parse;
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 ast = recast.parse(source, {
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
- return ast;
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 {
@@ -15,7 +15,7 @@ module.exports.parse = function acornParse(source) {
15
15
  const options = {
16
16
  locations: true,
17
17
  comment: true,
18
- ecmaVersion: 2_020,
18
+ ecmaVersion: 2023,
19
19
  sourceType: 'module',
20
20
  allowAwaitOutsideFunction: true,
21
21
  allowReturnOutsideFunction: true,
@@ -11,7 +11,7 @@ module.exports.parse = function espreeParse(source) {
11
11
  loc: true,
12
12
  tokens: preventUsingEsprima,
13
13
  comment: true,
14
- ecmaVersion: 2_020,
14
+ ecmaVersion: 2023,
15
15
  sourceType: 'module',
16
16
  ecmaFeatures: {
17
17
  jsx: true,
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 fixStrictMode = (a) => a.replace(`\n\n\n'use strict'`, `\n\n'use strict'`);
6
- const btoa = (a) => Buffer.from(a, 'binary').toString('base64');
7
- const addSourceMap = (sourceMapName, {code, map}) => !sourceMapName ? code : `${code}\n//# sourceMappingURL=data:application/json;charset=utf-8;base64,${btoa(stringify(map))}\n`;
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 {sourceMapName} = options;
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 (checkBrackets(printed.code))
28
+ if (!recast || !canParse(printed.code))
21
29
  assign(printed, {
22
- code: generate(ast, {
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
- function checkBrackets(source) {
38
- const brackets = [];
39
- let i = source.length;
40
-
41
- if (!i)
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
- if (current === '(')
60
- brackets.pop();
47
+ return !error;
48
+ }
49
+
50
+ function babelPrint(ast) {
51
+ const {code} = generate(ast, {
52
+ indent: {
53
+ style: ' ',
54
+ },
55
+ });
61
56
 
62
- return brackets.length;
57
+ return `${code}\n`;
63
58
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@putout/engine-parser",
3
- "version": "5.4.1",
3
+ "version": "5.6.0",
4
4
  "type": "commonjs",
5
5
  "author": "coderaiser <mnemonic.enemy@gmail.com> (https://github.com/coderaiser)",
6
6
  "description": "🐊Putout parser",