@putout/printer 1.69.0 → 1.70.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/ChangeLog CHANGED
@@ -1,3 +1,14 @@
1
+ 2023.04.25, v1.70.0
2
+
3
+ feature:
4
+ - 054212f @putout/printer: improve handling of SpreadElement newline in ObjectExpression
5
+ - b61a9e9 package: @putout/plugin-printer v1.0.0
6
+
7
+ 2023.04.24, v1.69.1
8
+
9
+ fix:
10
+ - be3dc7d @putout/printer: newline after ObjectProperty
11
+
1
12
  2023.04.23, v1.69.0
2
13
 
3
14
  feature:
package/README.md CHANGED
@@ -5,7 +5,7 @@
5
5
 
6
6
  Prints [**Babel AST**](https://github.com/coderaiser/estree-to-babel) to readable **JavaScript**.
7
7
 
8
- - ☝️ Similar to **Recast**, but simpler and easier in maintenance, since it supports only **Babel**.
8
+ - ☝️ Similar to **Recast**, but [twice faster](#speed-comparison), also simpler and easier in maintenance, since it supports only **Babel**.
9
9
  - ☝️ As opinionated as **Prettier**, but has more user-friendly output and works directly with **AST**.
10
10
  - ☝️ Like **ESLint** but works directly with **Babel AST**.
11
11
  - ☝️ Easily extandable with help of [Overrides](h#overrides).
@@ -22,6 +22,27 @@ Supports:
22
22
  npm i @putout/printer
23
23
  ```
24
24
 
25
+ ## 🐊 Support of Printer
26
+
27
+ **Printer** has first class support from 🐊**Putout** with help of [`@putout/plugin-printer`](https://github.com/coderaiser/putout/tree/master/packages/plugin-printer#putoutplugin-printer-). So install:
28
+
29
+ ```sh
30
+ npm i @putout/plugin-printer -aD
31
+ ```
32
+
33
+ And update `.putout.json`:
34
+
35
+ ```json
36
+ {
37
+ "printer": "putout",
38
+ "plugins": [
39
+ "printer"
40
+ ]
41
+ }
42
+ ```
43
+
44
+ To benefit from it.
45
+
25
46
  ## API
26
47
 
27
48
  ```js
@@ -158,6 +179,41 @@ print(ast, {
158
179
 
159
180
  This is the same as `print('__left')` but more low-level, and supports only objects.
160
181
 
182
+ ## Speed Comparison
183
+
184
+ About speed, for file `speed.js`:
185
+
186
+ ```js
187
+ const putout = require('putout');
188
+ const {readFileSync} = require('fs');
189
+ const parser = require('@babel/parser');
190
+
191
+ const code = readFileSync('./lib/tokenize/tokenize.js', 'utf8');
192
+ const ast = parser.parse(code);
193
+
194
+ speed('recast');
195
+ speed('putout');
196
+
197
+ function speed(printer) {
198
+ console.time(printer);
199
+
200
+ for (let i = 0; i < 1000; i++) {
201
+ putout(code, {
202
+ printer,
203
+ plugins: [
204
+ 'remove-unused-variables',
205
+ ],
206
+ });
207
+ }
208
+
209
+ console.timeEnd(printer);
210
+ }
211
+ ```
212
+
213
+ With contents of [`tokenize.js`](https://github.com/putoutjs/printer/blob/v1.69.1/lib/tokenize/tokenize.js), we have:
214
+
215
+ ![image](https://user-images.githubusercontent.com/1573141/234004942-8f890da3-a145-425f-9040-25924dcfba7b.png)
216
+
161
217
  ## License
162
218
 
163
219
  MIT
@@ -34,4 +34,3 @@ module.exports.FunctionExpression = (path, {print, maybe, write, traverse}) => {
34
34
  print(') ');
35
35
  print('__body');
36
36
  };
37
-
@@ -11,4 +11,3 @@ module.exports.FunctionExpression = FunctionExpression;
11
11
  module.exports.ArrowFunctionExpression = ArrowFunctionExpression;
12
12
  module.exports.ClassMethod = ClassMethod;
13
13
  module.exports.ObjectMethod = ObjectMethod;
14
-
@@ -28,10 +28,12 @@ module.exports.ObjectExpression = (path, {print, maybe, indent}) => {
28
28
 
29
29
  for (const property of properties) {
30
30
  if (property.isSpreadElement()) {
31
- maybe.indent(length > 1 || isLogical(property));
31
+ const logical = isLogical(property);
32
+
33
+ maybe.indent(length > 1 || logical || manyLines);
32
34
  print(property);
33
35
 
34
- if (length > 1) {
36
+ if (!logical && (length > 1 || manyLines)) {
35
37
  print(',');
36
38
  print.newline();
37
39
  }
@@ -86,4 +86,4 @@ module.exports.satisfy = (conditions) => (path) => {
86
86
  return false;
87
87
  };
88
88
 
89
- module.exports.noTrailingComment = (path) => !path.node.trailingComments;
89
+ module.exports.noTrailingComment = (path) => !path.node.trailingComments?.length;
@@ -6,9 +6,14 @@ const {
6
6
  isLast,
7
7
  exists,
8
8
  } = require('../is');
9
- const {isObjectMethod} = require('@babel/types');
9
+
10
+ const {
11
+ isArrowFunctionExpression,
12
+ isObjectMethod,
13
+ } = require('@babel/types');
10
14
 
11
15
  const isFirstStatement = (path) => path.get('body.0')?.isStatement();
16
+ const isMethodOrArrow = (path) => isArrowFunctionExpression(path) || isObjectMethod(path);
12
17
 
13
18
  module.exports.BlockStatement = {
14
19
  print(path, {indent, maybe, print}) {
@@ -49,7 +54,7 @@ function shouldAddNewlineAfter(path) {
49
54
  if (!isNext(path) && !isNext(path.parentPath) && isParentProgram(path.parentPath))
50
55
  return false;
51
56
 
52
- if (path.find(isObjectMethod))
57
+ if (path.parentPath.isIfStatement() && path.find(isMethodOrArrow))
53
58
  return true;
54
59
 
55
60
  if (parentPath.isStatement() && !path.node.body.length && !isNext(parentPath))
@@ -17,8 +17,7 @@ module.exports.ForOfStatement = {
17
17
  return !isFirst(path) && !hasPrevNewline(path);
18
18
  },
19
19
  before(path, {print}) {
20
- print.indent();
21
- print.newline();
20
+ print.linebreak();
22
21
  markBefore(path);
23
22
  },
24
23
  print(path, {indent, print, maybe, traverse}) {
@@ -47,8 +46,7 @@ module.exports.ForOfStatement = {
47
46
  },
48
47
  afterIf: isNext,
49
48
  after(path, {print}) {
50
- print.indent();
51
- print.newline();
49
+ print.linebreak();
52
50
  markAfter(path);
53
51
  },
54
52
  };
@@ -13,8 +13,7 @@ module.exports.ReturnStatement = {
13
13
  return !hasPrevNewline(path) && isBodyLength(path) || isPrevBody(path);
14
14
  },
15
15
  before(path, {print}) {
16
- print.indent();
17
- print.newline();
16
+ print.linebreak();
18
17
  },
19
18
  print(path, {indent, traverse, print}) {
20
19
  indent();
package/package.json CHANGED
@@ -1,9 +1,9 @@
1
1
  {
2
2
  "name": "@putout/printer",
3
- "version": "1.69.0",
3
+ "version": "1.70.0",
4
4
  "type": "commonjs",
5
5
  "author": "coderaiser <mnemonic.enemy@gmail.com> (https://github.com/coderaiser)",
6
- "description": "Easiest possible opinionated Babel AST printer made with ❤️ to use in 🐊Putout",
6
+ "description": "Simplest possible opinionated Babel AST printer fro 🐊Putout",
7
7
  "homepage": "https://github.com/putoutjs/printer#readme",
8
8
  "main": "./lib/printer.js",
9
9
  "exports": {
@@ -45,6 +45,7 @@
45
45
  "generate"
46
46
  ],
47
47
  "devDependencies": {
48
+ "@putout/plugin-printer": "^1.0.0",
48
49
  "@putout/plugin-react-hooks": "^5.0.0",
49
50
  "@putout/test": "^6.0.1",
50
51
  "acorn": "^8.8.2",
@@ -58,8 +59,7 @@
58
59
  "mock-require": "^3.0.3",
59
60
  "montag": "^1.0.0",
60
61
  "nodemon": "^2.0.1",
61
- "putout": "*",
62
- "putout-plugin-printer": "./rules",
62
+ "putout": "29",
63
63
  "supertape": "^8.0.0",
64
64
  "try-catch": "^3.0.0"
65
65
  },