@putout/printer 1.15.3 → 1.16.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,13 @@
1
+ 2023.03.31, v1.16.0
2
+
3
+ feature:
4
+ - c19ded9 @putout/printer: add support of ClassExpression
5
+
6
+ 2023.03.31, v1.15.4
7
+
8
+ feature:
9
+ - b4076e9 @putout/printer: add support of TSAnyKeyword
10
+
1
11
  2023.03.31, v1.15.3
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;
@@ -8,6 +8,7 @@ const isForOf = ({parentPath}) => parentPath.isForOfStatement();
8
8
  const SECOND = 1;
9
9
 
10
10
  const isStringAndArray = ([a, b]) => a?.isStringLiteral() && b?.isArrayExpression();
11
+ const isArrayParent = (path) => path.parentPath.isArrayExpression();
11
12
  const isTwoLongStrings = ([a, b]) => {
12
13
  const LONG_STRING = 20;
13
14
 
@@ -46,7 +47,9 @@ module.exports.ArrayExpression = (path, {print, maybe, indent}) => {
46
47
  if (!isTwoLongStrings(elements))
47
48
  maybe.indent.dec(!shouldIncreaseIndent);
48
49
 
49
- if (path.parentPath.isArrayExpression() && isStringAndArray(path.parentPath.get('elements'))) {
50
+ const parentElements = path.parentPath.get('elements');
51
+
52
+ if (isArrayParent(path) && isStringAndArray(parentElements)) {
50
53
  indent.dec();
51
54
  maybe.indent(elements.length && isNewLine);
52
55
  indent.inc();
@@ -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) {
@@ -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
-
@@ -20,6 +20,9 @@ module.exports = {
20
20
  TSNumberKeyword(path, {write}) {
21
21
  write('number');
22
22
  },
23
+ TSAnyKeyword(path, {write}) {
24
+ write('any');
25
+ },
23
26
  TSTypeAnnotation(path, {print}) {
24
27
  print(':');
25
28
  print.space();
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@putout/printer",
3
- "version": "1.15.3",
3
+ "version": "1.16.0",
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",