@putout/printer 1.15.4 → 1.16.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,13 @@
1
+ 2023.04.01, v1.16.1
2
+
3
+ feature:
4
+ - 4f2f38a @putout/printer: improve support of ImportSpecifier in ImportDeclaration
5
+
6
+ 2023.03.31, v1.16.0
7
+
8
+ feature:
9
+ - c19ded9 @putout/printer: add support of ClassExpression
10
+
1
11
  2023.03.31, v1.15.4
2
12
 
3
13
  feature:
@@ -4,12 +4,13 @@ const {
4
4
  isFirst,
5
5
  isNext,
6
6
  } = require('./is');
7
+
7
8
  const {
8
9
  markBefore,
9
10
  hasPrevNewline,
10
11
  } = require('./mark');
11
12
 
12
- module.exports.parseLeadingComments = (path, {print, indent}) => {
13
+ module.exports.parseLeadingComments = (path, {print, maybe}) => {
13
14
  const {leadingComments} = path.node;
14
15
 
15
16
  if (!leadingComments || !leadingComments.length)
@@ -18,8 +19,10 @@ module.exports.parseLeadingComments = (path, {print, indent}) => {
18
19
  if (shouldAddNewlineBefore(path))
19
20
  print.linebreak();
20
21
 
22
+ const isClass = !path.isClassMethod();
23
+
21
24
  for (const {type, value} of leadingComments) {
22
- indent();
25
+ maybe.indent(isClass);
23
26
 
24
27
  if (type === 'CommentLine') {
25
28
  print(`//${value}`);
@@ -31,9 +34,10 @@ module.exports.parseLeadingComments = (path, {print, indent}) => {
31
34
  if (type === 'CommentBlock') {
32
35
  print(`/*${value}*/`);
33
36
 
34
- if (path.isStatement()) {
37
+ if (path.isStatement() || path.isClassMethod()) {
35
38
  print.newline();
36
39
  markBefore(path);
40
+ maybe.indent(path.isClassMethod());
37
41
  }
38
42
 
39
43
  continue;
@@ -3,7 +3,17 @@
3
3
  const isArg = (path) => path.parentPath.isFunction();
4
4
 
5
5
  module.exports.AssignmentPattern = (path, {print, maybe}) => {
6
- maybe.print(isArg(path), '__left');
6
+ maybe.print(shouldPrint(path), '__left');
7
7
  print(' = ');
8
8
  print('__right');
9
9
  };
10
+
11
+ function shouldPrint(path) {
12
+ if (isArg(path))
13
+ return true;
14
+
15
+ if (path.parentPath.isArrayPattern())
16
+ return true;
17
+
18
+ return false;
19
+ }
@@ -1,6 +1,9 @@
1
1
  'use strict';
2
2
 
3
- module.exports.ClassDeclaration = (path, {print, indent}) => {
3
+ module.exports.ClassExpression = classVisitor;
4
+ module.exports.ClassDeclaration = classVisitor;
5
+
6
+ function classVisitor(path, {print, indent}) {
4
7
  indent();
5
8
  print('class ');
6
9
  print('__id');
@@ -25,4 +28,4 @@ module.exports.ClassDeclaration = (path, {print, indent}) => {
25
28
 
26
29
  indent.dec();
27
30
  print('}');
28
- };
31
+ }
@@ -3,7 +3,11 @@
3
3
  const functions = require('./functions');
4
4
  const unaryExpressions = require('./unary-expressions');
5
5
  const memberExpressions = require('./member-expressions');
6
- const {ClassDeclaration} = require('./class-declaration');
6
+
7
+ const {
8
+ ClassExpression,
9
+ ClassDeclaration,
10
+ } = require('./class');
7
11
 
8
12
  const {
9
13
  CallExpression,
@@ -44,6 +48,7 @@ module.exports = {
44
48
  AssignmentPattern,
45
49
  BinaryExpression,
46
50
  CallExpression,
51
+ ClassExpression,
47
52
  ClassDeclaration,
48
53
  ConditionalExpression,
49
54
  NewExpression,
@@ -20,6 +20,7 @@ module.exports.ObjectPattern = (path, {indent, print, maybe}) => {
20
20
 
21
21
  const valuePath = property.get('value');
22
22
  const keyPath = property.get('key');
23
+ const isAssign = valuePath.isAssignmentPattern();
23
24
  const {shorthand} = property.node;
24
25
 
25
26
  maybe.indent(is);
@@ -30,7 +31,7 @@ module.exports.ObjectPattern = (path, {indent, print, maybe}) => {
30
31
  print(valuePath);
31
32
  }
32
33
 
33
- if (valuePath.isAssignmentPattern())
34
+ if (isAssign)
34
35
  print(valuePath);
35
36
 
36
37
  if (is) {
@@ -2,42 +2,47 @@
2
2
 
3
3
  const {markAfter} = require('../mark');
4
4
 
5
- module.exports.ImportDeclaration = (path, {print, maybe}) => {
6
- const specifiers = path.get('specifiers');
7
- print('import ');
8
-
9
- for (const [index, spec] of specifiers.entries()) {
10
- if (spec.isImportDefaultSpecifier()) {
11
- print(spec.get('local'));
12
- continue;
13
- }
5
+ module.exports.ImportDeclaration = {
6
+ print(path, {print, maybe}) {
7
+ const specifiers = path.get('specifiers');
8
+ print('import ');
9
+
10
+ let wasSpecifier = false;
11
+ const n = specifiers.length - 1;
14
12
 
15
- if (spec.isImportSpecifier()) {
16
- maybe.print(index, ', ');
17
- print('{');
18
- print(spec.get('imported'));
19
- print('}');
13
+ for (const [index, spec] of specifiers.entries()) {
14
+ if (spec.isImportDefaultSpecifier()) {
15
+ print(spec.get('local'));
16
+ continue;
17
+ }
20
18
 
21
- continue;
19
+ if (spec.isImportSpecifier()) {
20
+ maybe.print(index, ', ');
21
+ maybe.print(!wasSpecifier, '{');
22
+ wasSpecifier = true;
23
+ print(spec.get('imported'));
24
+ maybe.print(index === n, '}');
25
+
26
+ continue;
27
+ }
22
28
  }
23
- }
24
-
25
- if (specifiers.length) {
26
- print.space();
27
- print('from');
28
- print.space();
29
- }
30
-
31
- print('__source');
32
- print(';');
33
- print.newline();
34
-
35
- if (shouldAddNewlineAfter(path)) {
29
+
30
+ if (specifiers.length) {
31
+ print.space();
32
+ print('from');
33
+ print.space();
34
+ }
35
+
36
+ print('__source');
37
+ print(';');
38
+ print.newline();
39
+ },
40
+ afterIf: shouldAddNewlineAfter,
41
+ after(path, {print}) {
36
42
  print.newline();
37
43
  markAfter(path);
38
- }
44
+ },
39
45
  };
40
-
41
46
  function shouldAddNewlineAfter(path) {
42
47
  if (path.getNextSibling().isImportDeclaration())
43
48
  return false;
@@ -1,7 +1,6 @@
1
1
  'use strict';
2
2
 
3
3
  const {round} = Math;
4
-
5
4
  const fullstore = require('fullstore');
6
5
  const isObject = (a) => a && typeof a === 'object';
7
6
  const babelTraverse = require('@babel/traverse').default;
@@ -230,16 +229,13 @@ const createPrint = (path, {traverse, write}) => (maybeLine) => {
230
229
 
231
230
  return write(computed);
232
231
  };
232
+
233
233
  const computePath = (path, maybeLine) => {
234
234
  if (isString(maybeLine) && maybeLine.startsWith(GET))
235
- return get(
236
- path,
237
- maybeLine,
238
- );
235
+ return get(path, maybeLine);
239
236
 
240
237
  if (isObject(maybeLine))
241
238
  return maybeLine;
242
239
 
243
240
  return maybeLine;
244
241
  };
245
-
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@putout/printer",
3
- "version": "1.15.4",
3
+ "version": "1.16.1",
4
4
  "type": "commonjs",
5
5
  "author": "coderaiser <mnemonic.enemy@gmail.com> (https://github.com/coderaiser)",
6
6
  "description": "Easiest possible opinionated Babel AST printer made with ❤️ to use in 🐊Putout",