@putout/printer 1.69.1 → 1.70.1

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.1
2
+
3
+ feature:
4
+ - 1cdcf93 @putout/printer: add support of ImportNamespaceSpecifier
5
+
6
+ 2023.04.25, v1.70.0
7
+
8
+ feature:
9
+ - 054212f @putout/printer: improve handling of SpreadElement newline in ObjectExpression
10
+ - b61a9e9 package: @putout/plugin-printer v1.0.0
11
+
1
12
  2023.04.24, v1.69.1
2
13
 
3
14
  fix:
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
@@ -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
  }
@@ -7,9 +7,13 @@ const {
7
7
  exists,
8
8
  } = require('../is');
9
9
 
10
- const {isArrowFunctionExpression} = require('@babel/types');
10
+ const {
11
+ isArrowFunctionExpression,
12
+ isObjectMethod,
13
+ } = require('@babel/types');
11
14
 
12
15
  const isFirstStatement = (path) => path.get('body.0')?.isStatement();
16
+ const isMethodOrArrow = (path) => isArrowFunctionExpression(path) || isObjectMethod(path);
13
17
 
14
18
  module.exports.BlockStatement = {
15
19
  print(path, {indent, maybe, print}) {
@@ -50,7 +54,7 @@ function shouldAddNewlineAfter(path) {
50
54
  if (!isNext(path) && !isNext(path.parentPath) && isParentProgram(path.parentPath))
51
55
  return false;
52
56
 
53
- if (path.parentPath.isIfStatement() && path.find(isArrowFunctionExpression))
57
+ if (path.parentPath.isIfStatement() && path.find(isMethodOrArrow))
54
58
  return true;
55
59
 
56
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
  };
@@ -17,6 +17,11 @@ module.exports.ImportDeclaration = {
17
17
  continue;
18
18
  }
19
19
 
20
+ if (spec.isImportNamespaceSpecifier()) {
21
+ print('* as ');
22
+ print(spec.get('local'));
23
+ }
24
+
20
25
  if (spec.isImportSpecifier()) {
21
26
  maybe.print(index, ', ');
22
27
  maybe.print(!wasSpecifier, '{');
@@ -9,7 +9,7 @@ const {ReturnStatement} = require('./return-statement');
9
9
  const TryStatements = require('./try-statements');
10
10
  const {DebuggerStatement} = require('./debugger-statement');
11
11
  const {ForStatement} = require('./for-statement');
12
- const importDeclarations = require('./import-declarations');
12
+ const importDeclarations = require('./import-declaration');
13
13
  const exportDeclarations = require('./export-declarations');
14
14
  const {WhileStatement} = require('./while-statement');
15
15
  const {SwitchStatement} = require('./switch-statement');
@@ -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.1",
3
+ "version": "1.70.1",
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
  },