@putout/printer 1.150.1 → 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,8 @@
1
+ 2023.06.12, v2.0.0
2
+
3
+ feature:
4
+ - b12c1a3 @putout/printer: move comments to semantics
5
+
1
6
  2023.06.12, v1.150.1
2
7
 
3
8
  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) {
@@ -46,6 +46,7 @@ const isShortTwoSimplesInsideCall = (path, short) => {
46
46
  node,
47
47
  parentPath,
48
48
  } = path;
49
+
49
50
  const {elements} = node;
50
51
  const {length} = elements;
51
52
  const [a, b] = elements;
@@ -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,
@@ -29,7 +29,11 @@ const satisfyAfter = satisfy([
29
29
  isNextUp,
30
30
  ]);
31
31
 
32
- const shouldBreakline = satisfy([isNewlineBetweenSiblings, isNotLastBody, isStrictMode]);
32
+ const shouldBreakline = satisfy([
33
+ isNewlineBetweenSiblings,
34
+ isNotLastBody,
35
+ isStrictMode,
36
+ ]);
33
37
 
34
38
  module.exports.ExpressionStatement = {
35
39
  print(path, {indent, print, maybe, store}) {
@@ -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.1",
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",