@putout/printer 1.150.0 → 2.0.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.06.12, v2.0.0
2
+
3
+ feature:
4
+ - b12c1a3 @putout/printer: move comments to semantics
5
+
6
+ 2023.06.12, v1.150.1
7
+
8
+ feature:
9
+ - 51ee3e9 @putout/printer: ArrayExpression: newline
10
+
1
11
  2023.06.09, v1.150.0
2
12
 
3
13
  feature:
package/README.md CHANGED
@@ -80,12 +80,12 @@ write(ast, {
80
80
  indent: ' ',
81
81
  newline: '\n',
82
82
  space: ' ',
83
- comments: true,
84
83
  splitter: '\n',
85
84
  roundBraceOpen: '(',
86
85
  roundBraceClose: ')',
87
86
  },
88
87
  semantics: {
88
+ comments: true,
89
89
  maxSpecifiersInOneLine: 2,
90
90
  maxElementsInOneLine: 3,
91
91
  },
@@ -6,8 +6,9 @@ const {
6
6
  } = require('./is');
7
7
 
8
8
  const {markBefore} = require('./mark');
9
+ const {isVariableDeclarator} = require('@babel/types');
9
10
 
10
- module.exports.parseLeadingComments = (path, {print, maybe, indent}, format) => {
11
+ module.exports.parseLeadingComments = (path, {print, maybe, indent}, format = {}) => {
11
12
  if (!format.comments)
12
13
  return;
13
14
 
@@ -20,7 +21,7 @@ module.exports.parseLeadingComments = (path, {print, maybe, indent}, format) =>
20
21
  return;
21
22
 
22
23
  const insideFn = path.parentPath.isFunction();
23
- const isProperty = path.isObjectProperty();
24
+ const isProperty = path.isObjectProperty() || isVariableDeclarator(path);
24
25
  const isIndent = !path.isClassMethod() && !insideFn && !isProperty;
25
26
 
26
27
  for (const {type, value} of leadingComments) {
@@ -22,6 +22,7 @@ const {
22
22
  isIdentifierAndString,
23
23
  isStringAndMember,
24
24
  } = require('../../is');
25
+
25
26
  const MAX_ARRAY_ELEMENTS_WHEN_USED_AS_ARGUMENT = 4;
26
27
 
27
28
  const isForOf = ({parentPath}) => parentPath.isForOfStatement();
@@ -39,8 +40,13 @@ const isStringAndArray = ([a, b]) => {
39
40
  };
40
41
 
41
42
  const isStringAndString = ([a, b]) => isStringLiteral(a) && isStringLiteral(b);
43
+
42
44
  const isShortTwoSimplesInsideCall = (path, short) => {
43
- const {node, parentPath} = path;
45
+ const {
46
+ node,
47
+ parentPath,
48
+ } = path;
49
+
44
50
  const {elements} = node;
45
51
  const {length} = elements;
46
52
  const [a, b] = elements;
@@ -48,11 +54,12 @@ const isShortTwoSimplesInsideCall = (path, short) => {
48
54
  if (!parentPath.isCallExpression())
49
55
  return false;
50
56
 
51
- if (!isSimple(a) || !isSimple(b))
57
+ if (!isStringLiteral(a) || !isStringLiteral(b))
52
58
  return false;
53
59
 
54
60
  return length < short;
55
61
  };
62
+
56
63
  const isIdentifierAndIdentifier = ([a, b]) => isIdentifier(a) && isIdentifier(b);
57
64
  const isInsideArray = (path) => path.parentPath.isArrayExpression();
58
65
 
@@ -66,6 +73,7 @@ const isSimpleAndCall = ([a, b]) => {
66
73
  const isBooleanAndSimple = ([a, b]) => isBooleanLiteral(a) && isSimple(b);
67
74
  const isNullAndSimple = ([a, b]) => isNullLiteral(a) && isSimple(b);
68
75
  const isSimpleAndObject = ([a, b]) => isSimple(a) && isObjectExpression(b);
76
+
69
77
  const isStringAndObject = (elements) => {
70
78
  const first = elements.at(0);
71
79
  const last = elements.at(-1);
@@ -350,4 +358,3 @@ const isOneElementCall = (path, {elements}) => {
350
358
 
351
359
  return !isCallExpression(elements[0]);
352
360
  };
353
-
@@ -19,7 +19,6 @@ function initFormat(format) {
19
19
  indent: ' ',
20
20
  newline: '\n',
21
21
  space: ' ',
22
- comments: true,
23
22
  splitter: '\n',
24
23
  roundBraceOpen: '(',
25
24
  roundBraceClose: ')',
@@ -29,6 +28,7 @@ function initFormat(format) {
29
28
 
30
29
  function initSemantics(semantics = {}) {
31
30
  return {
31
+ comments: true,
32
32
  maxSpecifiersInOneLine: 2,
33
33
  maxElementsInOneLine: 5,
34
34
  ...semantics,
@@ -1,7 +1,11 @@
1
1
  'use strict';
2
2
 
3
3
  module.exports.maybeSpaceAfterKeyword = (path, {write}) => {
4
- const {id} = path.node.declarations[0];
4
+ const {declarations} = path.node;
5
+ const {id} = declarations[0];
6
+
7
+ if (declarations.length > 1)
8
+ return;
5
9
 
6
10
  if (id.type === 'ArrayPattern' || id.type === 'ObjectPattern')
7
11
  return write.space();
@@ -13,6 +13,7 @@ const {isExportDeclaration} = require('@babel/types');
13
13
  const {maybeSpaceAfterKeyword} = require('./maybe-space-after-keyword');
14
14
 
15
15
  const {isConcatenation} = require('../../expressions/binary-expression/concatanate');
16
+ const {parseLeadingComments} = require('../../comments');
16
17
 
17
18
  const isParentBlock = (path) => /Program|BlockStatement|Export/.test(path.parentPath.type);
18
19
  const isInsideBlock = (path) => /^(Program|BlockStatement)$/.test(path.parentPath.type);
@@ -22,9 +23,9 @@ module.exports.VariableDeclaration = {
22
23
  before(path, {print}) {
23
24
  print.breakline();
24
25
  },
25
- print(path, {maybe, store, write, traverse}) {
26
+ print(path, {maybe, store, write, traverse, print, indent}, semantics) {
26
27
  maybe.indent(isInsideBlock(path));
27
- write(String(path.node.kind));
28
+ write(path.node.kind);
28
29
  maybeSpaceAfterKeyword(path, {
29
30
  write,
30
31
  });
@@ -32,6 +33,9 @@ module.exports.VariableDeclaration = {
32
33
  const declarations = path.get('declarations');
33
34
  const n = declarations.length - 1;
34
35
 
36
+ maybe.indent.inc(n);
37
+ maybe.print.breakline(n);
38
+
35
39
  for (const [index, declaration] of declarations.entries()) {
36
40
  const id = declaration.get('id');
37
41
  const init = declaration.get('init');
@@ -46,10 +50,20 @@ module.exports.VariableDeclaration = {
46
50
  traverse(init);
47
51
  }
48
52
 
49
- maybe.write(notLast, ',');
50
- maybe.write.space(notLast);
53
+ if (notLast) {
54
+ const next = declarations[index + 1];
55
+
56
+ write(',');
57
+ parseLeadingComments(next, {
58
+ print,
59
+ maybe,
60
+ indent,
61
+ }, semantics);
62
+ maybe.write.breakline(!next.node.leadingComments);
63
+ }
51
64
  }
52
65
 
66
+ maybe.indent.dec(n);
53
67
  maybe.write(isParentBlock(path), ';');
54
68
 
55
69
  let wasNewline = false;
@@ -233,10 +233,10 @@ module.exports.tokenize = (ast, overrides) => {
233
233
  maybeThrow(!currentTraverse, path, `Node type '{{ type }}' is not supported yet: '{{ path }}'`);
234
234
 
235
235
  const currentIndent = i;
236
- parseLeadingComments(path, printer, format);
236
+ parseLeadingComments(path, printer, semantics);
237
237
  // this is main thing
238
238
  maybePlugin(currentTraverse, path, printer, semantics);
239
- parseTrailingComments(path, printer, format);
239
+ parseTrailingComments(path, printer, semantics);
240
240
  maybeThrow(i !== currentIndent, path, `☝️Looks like indent level changed after token visitor: '{{ type }}', for code: '{{ path }}'`);
241
241
 
242
242
  debug(path.type);
@@ -19,7 +19,7 @@ module.exports.TSTypeLiteral = (path, {indent, traverse, write, maybe}) => {
19
19
  maybe.write(is, ';');
20
20
 
21
21
  if (is && isNext(member))
22
- write.newline(is);
22
+ write.newline();
23
23
  }
24
24
 
25
25
  if (is) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@putout/printer",
3
- "version": "1.150.0",
3
+ "version": "2.0.0",
4
4
  "type": "commonjs",
5
5
  "author": "coderaiser <mnemonic.enemy@gmail.com> (https://github.com/coderaiser)",
6
6
  "description": "Simplest possible opinionated Babel AST printer for 🐊Putout",