@putout/printer 1.56.0 → 1.58.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.04.19, v1.58.0
2
+
3
+ feature:
4
+ - da02030 @putout/printer: improve support of comments inside JSX
5
+
6
+ 2023.04.19, v1.57.0
7
+
8
+ feature:
9
+ - 8f4ddc4 @putout/printer: add support of JSXMemberExpression
10
+
1
11
  2023.04.19, v1.56.0
2
12
 
3
13
  feature:
@@ -10,12 +10,19 @@ module.exports.parseLeadingComments = (path, {print, maybe}) => {
10
10
  if (!leadingComments || !leadingComments.length)
11
11
  return;
12
12
 
13
- const isClass = !path.isClassMethod();
13
+ const insideFn = path.parentPath.isFunction();
14
+ const isIndent = !path.isClassMethod() && !insideFn;
14
15
 
15
16
  for (const {type, value} of leadingComments) {
16
- maybe.indent(isClass);
17
+ maybe.indent(isIndent);
17
18
 
18
19
  if (type === 'CommentLine') {
20
+ maybe.indent.inc(insideFn);
21
+ maybe.indent.inc(insideFn);
22
+ maybe.print.breakline(insideFn);
23
+ maybe.indent.dec(insideFn);
24
+ maybe.indent.dec(insideFn);
25
+
19
26
  print(`//${value}`);
20
27
  print.newline();
21
28
 
@@ -51,6 +51,7 @@ function CallExpression(path, {indent, print, maybe, traverse}) {
51
51
 
52
52
  print(')');
53
53
  }
54
+
54
55
  module.exports.OptionalCallExpression = CallExpression;
55
56
  module.exports.CallExpression = CallExpression;
56
57
 
@@ -1,18 +1,25 @@
1
1
  'use strict';
2
2
 
3
+ const {isNext} = require('../is');
4
+
3
5
  module.exports.ClassExpression = classVisitor;
4
6
  module.exports.ClassDeclaration = classVisitor;
5
7
 
6
- module.exports.ClassDeclaration = (path, {print, indent}) => {
8
+ module.exports.ClassDeclaration = (path, {print, indent, maybe, write}) => {
7
9
  indent();
8
10
 
9
11
  classVisitor(path, {
10
12
  print,
11
13
  indent,
14
+ maybe,
12
15
  });
16
+
17
+ if (!path.parentPath.isExportDeclaration() && isNext(path)) {
18
+ write.newline();
19
+ }
13
20
  };
14
21
 
15
- function classVisitor(path, {print, indent}) {
22
+ function classVisitor(path, {print, indent, maybe}) {
16
23
  print('class ');
17
24
  print('__id');
18
25
  print('__typeParameters');
@@ -25,6 +32,7 @@ function classVisitor(path, {print, indent}) {
25
32
  }
26
33
 
27
34
  if (node.superClass) {
35
+ maybe.print.space(path.node.id);
28
36
  print('extends ');
29
37
  print('__superClass');
30
38
  }
@@ -37,7 +37,14 @@ module.exports.ArrowFunctionExpression = {
37
37
 
38
38
  print.space();
39
39
  print('=>');
40
- print.space();
40
+
41
+ const body = path.get('body');
42
+
43
+ const insideCall = path.parentPath.isCallExpression();
44
+ const isJSX = body.isJSXElement();
45
+
46
+ maybe.print.space(!insideCall && isJSX);
47
+ maybe.print.space(!isJSX);
41
48
  print('__body');
42
49
  },
43
50
  after(path, {write}) {
@@ -2,8 +2,20 @@
2
2
 
3
3
  const {markAfter} = require('../../mark');
4
4
  const {isNext} = require('../../is');
5
+ const isFirst = (path) => !path.getPrevSibling().node;
5
6
 
6
7
  module.exports.FunctionDeclaration = {
8
+ beforeIf(path) {
9
+ if (isFirst(path))
10
+ return false;
11
+
12
+ const prev = path.getPrevSibling();
13
+
14
+ return prev.isClassDeclaration();
15
+ },
16
+ before(path, {write}) {
17
+ write.newline();
18
+ },
7
19
  print(path, {print, maybe}) {
8
20
  const {async} = path.node;
9
21
 
@@ -62,6 +62,7 @@ function shouldAddNewline(path) {
62
62
 
63
63
  return path.parentPath.parentPath.isSpreadElement();
64
64
  }
65
+
65
66
  module.exports.ObjectProperty = (path, {print, maybe}) => {
66
67
  const {
67
68
  shorthand,
@@ -30,6 +30,7 @@ function printUnary(path, name, {print}) {
30
30
  print(`${name} `);
31
31
  print('__argument');
32
32
  }
33
+
33
34
  const isWord = (a) => /delete|typeof/.test(a);
34
35
 
35
36
  function unaryExpression(path, {maybe, traverse}) {
@@ -45,6 +45,7 @@ function isCoupleLines(path) {
45
45
 
46
46
  return end > start;
47
47
  }
48
+
48
49
  module.exports.exists = (a) => a.node;
49
50
  module.exports.isStringAndIdentifier = ([a, b]) => isStringLiteral(a) && isIdentifier(b);
50
51
 
@@ -22,6 +22,11 @@ module.exports = {
22
22
  JSXText(path, {write}) {
23
23
  write(path.node.value);
24
24
  },
25
+ JSXMemberExpression(path, {print}) {
26
+ print('__object');
27
+ print('.');
28
+ print('__property');
29
+ },
25
30
  JSXSpreadAttribute(path, {print, maybe}) {
26
31
  const isNewline = isCoupleLines(path.parentPath);
27
32
  maybe.indent.inc(isNewline);
@@ -7,10 +7,25 @@ module.exports.JSXElement = {
7
7
  indent.inc();
8
8
  write.breakline();
9
9
  },
10
- print(path, {print, traverse}) {
10
+ print(path, {print, traverse, indent, maybe}) {
11
+ const insideFn = path.parentPath.isArrowFunctionExpression();
12
+ const insideCall = path.parentPath.parentPath.isCallExpression();
13
+
14
+ if (insideFn && insideCall) {
15
+ indent.inc();
16
+ indent.inc();
17
+ indent();
18
+ }
19
+
11
20
  print('__openingElement');
12
21
  path.get('children').map(traverse);
13
22
  print('__closingElement');
23
+
24
+ if (insideFn && insideCall) {
25
+ indent.dec();
26
+ maybe.print.breakline(insideCall);
27
+ indent.dec();
28
+ }
14
29
  },
15
30
  after(path, {write, indent}) {
16
31
  indent.dec();
@@ -2,27 +2,29 @@
2
2
 
3
3
  const {isCoupleLines} = require('../is');
4
4
 
5
- module.exports.JSXOpeningElement = (path, {print, maybe}) => {
6
- print('<');
7
- print('__name');
8
-
9
- const coupleLines = isCoupleLines(path);
10
- const noCoupleLines = !coupleLines;
11
- const shouldIndent = coupleLines && path.parentPath.parentPath.isJSXElement();
12
-
13
- maybe.indent.inc(shouldIndent);
14
-
15
- for (const attr of path.get('attributes')) {
16
- maybe.print.space(noCoupleLines);
17
- print(attr);
18
- }
19
-
20
- if (isCoupleLines(path))
21
- print.breakline();
22
-
23
- if (path.node.selfClosing)
24
- print('/');
25
-
26
- print('>');
27
- maybe.indent.dec(shouldIndent);
5
+ module.exports.JSXOpeningElement = {
6
+ print(path, {print, maybe}) {
7
+ print('<');
8
+ print('__name');
9
+
10
+ const coupleLines = isCoupleLines(path);
11
+ const noCoupleLines = !coupleLines;
12
+ const shouldIndent = coupleLines && path.parentPath.parentPath.isJSXElement();
13
+
14
+ maybe.indent.inc(shouldIndent);
15
+
16
+ for (const attr of path.get('attributes')) {
17
+ maybe.print.space(noCoupleLines);
18
+ print(attr);
19
+ }
20
+
21
+ if (isCoupleLines(path))
22
+ print.breakline();
23
+
24
+ if (path.node.selfClosing)
25
+ print('/');
26
+
27
+ print('>');
28
+ maybe.indent.dec(shouldIndent);
29
+ },
28
30
  };
@@ -14,11 +14,13 @@ function markBefore(path) {
14
14
  function markAfter(path) {
15
15
  path[WATER_MARK_AFTER] = true;
16
16
  }
17
+
17
18
  module.exports.isMarkedAfter = isMarkedAfter;
18
19
 
19
20
  function isMarkedAfter(path) {
20
21
  return path[WATER_MARK_AFTER];
21
22
  }
23
+
22
24
  module.exports.hasPrevNewline = (path) => {
23
25
  return isMarkedAfter(path.getPrevSibling());
24
26
  };
@@ -89,6 +89,7 @@ function isNextCoupleLines(path) {
89
89
 
90
90
  return isCoupleLines(next);
91
91
  }
92
+
92
93
  const isLast = (path) => path.parentPath?.isProgram() && !isNext(path);
93
94
 
94
95
  function shouldAddNewlineBefore(path) {
@@ -115,6 +116,7 @@ function shouldAddNewlineBefore(path) {
115
116
  function isFirst(path) {
116
117
  return path.node === path.parentPath.node.body?.[0];
117
118
  }
119
+
118
120
  const isNextAssign = (path) => {
119
121
  const nextPath = path.getNextSibling();
120
122
 
@@ -52,6 +52,7 @@ function initFormat(format) {
52
52
  indent: ' ',
53
53
  };
54
54
  }
55
+
55
56
  const createAddToken = (tokens) => {
56
57
  const log = createLog();
57
58
 
@@ -237,6 +238,7 @@ function printIndent(i, indent) {
237
238
 
238
239
  return result;
239
240
  }
241
+
240
242
  const createPrint = (path, {traverse, write}) => (maybeLine) => {
241
243
  if (maybeLine === path)
242
244
  return null;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@putout/printer",
3
- "version": "1.56.0",
3
+ "version": "1.58.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",