@putout/engine-parser 12.0.0 → 12.2.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
@@ -25,15 +25,12 @@ Any parser should be installed before use, but you can be sure that `@babel/pars
25
25
 
26
26
  By default [`@putout/printer`](https://github.com/putoutjs/printer) used. It formats code according to [`eslint-plugin-putout`](https://github.com/coderaiser/putout/tree/master/packages/eslint-plugin-putout#readme) rules but without using **ESLint**.
27
27
 
28
- But you can also use [`recast`](https://github.com/putoutjs/recast) or `babel` with:
28
+ But you can also use `babel` with:
29
29
 
30
30
  ```js
31
- const ast = parse(source, {
32
- printer: 'recast',
33
- });
34
-
31
+ const ast = parse(source);
35
32
  const code = print(ast, {
36
- printer: 'recast',
33
+ printer: 'babel',
37
34
  });
38
35
  ```
39
36
 
@@ -47,6 +44,12 @@ const code = print(ast, {
47
44
  },
48
45
  }],
49
46
  });
47
+
48
+ print(ast, {
49
+ printer: ['babel', {
50
+ alginSpaces: false, // when you don't want to add spaces on empty lines
51
+ }],
52
+ });
50
53
  ```
51
54
 
52
55
  ### print(ast [, options])
@@ -13,7 +13,7 @@ module.exports.parse = function babelParse(source, {sourceFileName, isTS, isJSX
13
13
  const parserOptions = {
14
14
  sourceFileName,
15
15
  sourceType: 'module',
16
- tokens: false,
16
+ tokens: true,
17
17
  ...options,
18
18
  errorRecovery: isRecovery,
19
19
  plugins: clean([
package/lib/print.js CHANGED
@@ -1,7 +1,7 @@
1
1
  'use strict';
2
2
 
3
3
  const putoutPrinter = require('@putout/printer');
4
- const {generate} = require('@putout/babel');
4
+ const babelPrinter = require('./printers/babel');
5
5
 
6
6
  const {isArray} = Array;
7
7
 
@@ -11,17 +11,10 @@ module.exports = (ast, options = {}) => {
11
11
  const [printer = 'putout', printerOptions] = maybeArray(options.printer);
12
12
 
13
13
  if (printer === 'babel')
14
- return babelPrint(ast);
14
+ return babelPrinter.print(ast, {
15
+ ...options,
16
+ ...printerOptions,
17
+ });
15
18
 
16
19
  return putoutPrinter.print(ast, printerOptions);
17
20
  };
18
-
19
- function babelPrint(ast) {
20
- const {code} = generate(ast, {
21
- indent: {
22
- style: ' ',
23
- },
24
- });
25
-
26
- return `${code}\n`;
27
- }
@@ -0,0 +1,32 @@
1
+ 'use strict';
2
+
3
+ const {generate} = require('@putout/babel');
4
+ const align = require('align-spaces');
5
+
6
+ const defaultOptions = {
7
+ alignSpaces: true,
8
+ };
9
+
10
+ module.exports.print = (ast, options) => {
11
+ const {source, alignSpaces} = {
12
+ ...defaultOptions,
13
+ ...options,
14
+ };
15
+
16
+ let {code} = generate(ast, {
17
+ ...source && {
18
+ experimental_preserveFormat: true,
19
+ retainLines: true,
20
+ },
21
+ }, source);
22
+
23
+ if (code[0] === '\n')
24
+ code = code.trimStart();
25
+
26
+ code += '\n';
27
+
28
+ if (!alignSpaces)
29
+ return code;
30
+
31
+ return align(code);
32
+ };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@putout/engine-parser",
3
- "version": "12.0.0",
3
+ "version": "12.2.0",
4
4
  "type": "commonjs",
5
5
  "author": "coderaiser <mnemonic.enemy@gmail.com> (https://github.com/coderaiser)",
6
6
  "description": "🐊Putout parser",
@@ -35,6 +35,7 @@
35
35
  "dependencies": {
36
36
  "@putout/babel": "^3.0.0",
37
37
  "@putout/printer": "^12.0.0",
38
+ "align-spaces": "^1.0.4",
38
39
  "estree-to-babel": "^10.0.0",
39
40
  "nano-memoize": "^3.0.11",
40
41
  "once": "^1.4.0",