@putout/printer 1.28.0 → 1.29.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,10 @@
1
+ 2023.04.07, v1.29.0
2
+
3
+ feature:
4
+ - b1dfcf3 @putout/printer: ObjectPattern: improve support of not ObjectProperty value !== Identifier
5
+ - 53a44db @putout/printer: improve chaining support
6
+ - a552062 @putout/printer: newlines
7
+
1
8
  2023.04.07, v1.28.0
2
9
 
3
10
  feature:
@@ -1,15 +1,16 @@
1
1
  'use strict';
2
2
 
3
3
  const {stringify} = JSON;
4
-
5
4
  const {TYPES} = require('../types');
6
5
  const toSnakeCase = require('just-snake-case');
6
+
7
7
  const {
8
8
  LOG,
9
9
  LOG_ALL,
10
10
  LOG_TOKENS,
11
11
  DEBUG,
12
12
  } = process.env;
13
+
13
14
  const {codeFrameColumns} = require('@babel/code-frame');
14
15
 
15
16
  module.exports.createDebug = (tokens) => (a) => {
@@ -24,9 +25,13 @@ module.exports.createDebug = (tokens) => (a) => {
24
25
 
25
26
  module.exports.createLog = ({newline = '\n', store = createStore()} = {}) => ({type, value}) => {
26
27
  if (LOG_TOKENS) {
27
- console.log(codeFrameColumns(stringify({type, value}), {}, {
28
+ console.log(codeFrameColumns(stringify({
29
+ type,
30
+ value,
31
+ }), {}, {
28
32
  highlightCode: true,
29
33
  }));
34
+
30
35
  return;
31
36
  }
32
37
 
@@ -34,6 +39,7 @@ module.exports.createLog = ({newline = '\n', store = createStore()} = {}) => ({t
34
39
  console.log(codeFrameColumns(value, {}, {
35
40
  highlightCode: true,
36
41
  }));
42
+
37
43
  return;
38
44
  }
39
45
 
@@ -42,6 +48,7 @@ module.exports.createLog = ({newline = '\n', store = createStore()} = {}) => ({t
42
48
  console.log(codeFrameColumns(store(), {}, {
43
49
  highlightCode: true,
44
50
  }));
51
+
45
52
  return;
46
53
  }
47
54
 
@@ -61,9 +68,9 @@ function createStore() {
61
68
  }
62
69
 
63
70
  const result = chunks.join('');
71
+
64
72
  chunks = [];
65
73
 
66
74
  return result;
67
75
  };
68
76
  }
69
-
@@ -200,11 +200,8 @@ function isTwoStringsDifferentLength(strings) {
200
200
  if (!a?.isStringLiteral() || !b?.isStringLiteral())
201
201
  return false;
202
202
 
203
- const aLength = a
204
- .node.value.length;
205
-
206
- const bLength = b
207
- .node.value.length;
203
+ const aLength = a.node.value.length;
204
+ const bLength = b.node.value.length;
208
205
 
209
206
  return round(bLength / aLength) > 2;
210
207
  }
@@ -35,8 +35,7 @@ const CallExpression = {
35
35
 
36
36
  if (isParentCall) {
37
37
  indent.dec();
38
- print.newline();
39
- print.indent();
38
+ print.breakline();
40
39
  }
41
40
 
42
41
  print(')');
@@ -1,15 +1,13 @@
1
1
  'use strict';
2
2
 
3
3
  const {hasPrevNewline} = require('../mark');
4
- const isFirst = (path) => !path.getPrevSibling().node;
4
+ const isFirst = (path) => !path
5
+ .getPrevSibling().node;
5
6
 
6
7
  module.exports.FunctionExpression = (path, {print, maybe, write, traverse}) => {
7
8
  const {node} = path;
8
9
 
9
- const {
10
- generator,
11
- async,
12
- } = node;
10
+ const {generator, async} = node;
13
11
 
14
12
  maybe.print(async, 'async ');
15
13
  print('function');
@@ -4,6 +4,7 @@ const {
4
4
  isIfStatement,
5
5
  isIdentifier,
6
6
  isThisExpression,
7
+ isUnaryExpression,
7
8
  } = require('@babel/types');
8
9
 
9
10
  const {
@@ -23,7 +24,7 @@ module.exports.MemberExpression = (path, {print, indent, maybe}) => {
23
24
  return;
24
25
  }
25
26
 
26
- const isChain = looksLikeChain(path);
27
+ const isChain = isLooksLikeChain(path);
27
28
  maybe.indent.inc(isChain);
28
29
 
29
30
  if (isChain) {
@@ -52,7 +53,7 @@ module.exports.OptionalMemberExpression = (path, {print}) => {
52
53
  print('__property');
53
54
  };
54
55
 
55
- function looksLikeChain(path) {
56
+ function isLooksLikeChain(path) {
56
57
  const {parentPath} = path;
57
58
 
58
59
  if (parentPath.parentPath.isStatement() && !parentPath.parentPath.isExpressionStatement())
@@ -61,6 +62,9 @@ function looksLikeChain(path) {
61
62
  if (path.find(isIfStatement))
62
63
  return false;
63
64
 
65
+ if (path.find(isUnaryExpression))
66
+ return false;
67
+
64
68
  const isMember = ({parentPath}) => parentPath.parentPath.isMemberExpression();
65
69
  const isExpression = ({parentPath}) => parentPath.parentPath.isExpressionStatement();
66
70
  const itMember = isMember(path);
@@ -69,9 +73,6 @@ function looksLikeChain(path) {
69
73
  if (parentPath.isLiteral())
70
74
  return false;
71
75
 
72
- if (parentPath.isUnaryExpression())
73
- return false;
74
-
75
76
  if (!itMember && !path.parentPath.isExpressionStatement() && !parentPath.isCallExpression())
76
77
  return false;
77
78
 
@@ -81,7 +82,10 @@ function looksLikeChain(path) {
81
82
  if (parentPath.get('callee') !== path)
82
83
  return false;
83
84
 
84
- if (compare(parentPath, '__a.__b(__args);') && !itMember && !itExpression)
85
+ if (compare(path.parentPath.parentPath, '(__args) => __b.__c(__args).__d()'))
86
+ return false;
87
+
88
+ if (compare(parentPath, '__a.__b(__args);') && !itExpression && !itMember)
85
89
  return false;
86
90
 
87
91
  if (compare(parentPath, '__a.__b.__c(__args)') && !itMember)
@@ -90,6 +94,9 @@ function looksLikeChain(path) {
90
94
  if (compare(parentPath, '__a.__b.__c = __d'))
91
95
  return false;
92
96
 
97
+ if (compare(parentPath, '(__args) => __a.__b(__args).__c'))
98
+ return false;
99
+
93
100
  const {__a, __b} = getTemplateValues(parentPath, '__a.__b(__args)');
94
101
  const aType = __a?.type;
95
102
  const bType = __b?.type;
@@ -2,15 +2,15 @@
2
2
 
3
3
  const {isCoupleLines} = require('../is');
4
4
  const {isFunction} = require('@babel/types');
5
+ const isBodyOfArrow = (path) => path.parentPath.node.body === path.node;
5
6
 
6
- const isBodyOfArrow = (path) => path
7
- .parentPath.node.body === path.node;
8
7
  const isLogical = (path) => path
9
8
  .get('argument').isLogicalExpression();
9
+
10
10
  const isValue = (path) => path
11
11
  .get('properties.0.value').node;
12
- const isIf = (path) => path
13
- .parentPath.parentPath.isIfStatement();
12
+
13
+ const isIf = (path) => path.parentPath.parentPath.isIfStatement();
14
14
  const isParentExpression = (path) => path.parentPath.isExpressionStatement();
15
15
 
16
16
  const isForOf = (path) => {
@@ -67,8 +67,7 @@ function shouldAddNewline(path) {
67
67
  if (!path.parentPath.isLogicalExpression())
68
68
  return false;
69
69
 
70
- return path
71
- .parentPath.parentPath.isSpreadElement();
70
+ return path.parentPath.parentPath.isSpreadElement();
72
71
  }
73
72
  module.exports.ObjectProperty = (path, {print, maybe}) => {
74
73
  const {
@@ -123,4 +122,3 @@ function isParens(path) {
123
122
 
124
123
  return false;
125
124
  }
126
-
@@ -1,5 +1,6 @@
1
1
  'use strict';
2
2
 
3
+ const {isIdentifier} = require('@babel/types');
3
4
  const isForOf = (path) => path.parentPath?.parentPath?.parentPath?.isForOfStatement();
4
5
 
5
6
  module.exports.ObjectPattern = (path, {indent, print, maybe}) => {
@@ -51,7 +52,12 @@ module.exports.ObjectPattern = (path, {indent, print, maybe}) => {
51
52
 
52
53
  function checkLength(properties) {
53
54
  for (const prop of properties) {
54
- if (prop.node.value.name.length > 4)
55
+ const {value} = prop.node;
56
+
57
+ if (!isIdentifier(value))
58
+ continue;
59
+
60
+ if (value.name.length > 4)
55
61
  return true;
56
62
  }
57
63
 
@@ -1,10 +1,12 @@
1
1
  'use strict';
2
2
 
3
3
  const {isMarkedAfter} = require('./mark');
4
+
4
5
  const {
5
6
  isStringLiteral,
6
7
  isIdentifier,
7
8
  } = require('@babel/types');
9
+
8
10
  const isParentProgram = (path) => path.parentPath?.isProgram();
9
11
  const isParentBlock = (path) => path.parentPath.isBlockStatement();
10
12
 
@@ -44,5 +46,4 @@ function isCoupleLines(path) {
44
46
  function isNextCoupleLines(path) {
45
47
  return isCoupleLines(path.getNextSibling());
46
48
  }
47
-
48
49
  module.exports.isStringAndIdentifier = ([a, b]) => isStringLiteral(a) && isIdentifier(b);
@@ -35,9 +35,7 @@ module.exports.ForOfStatement = {
35
35
  indent.inc();
36
36
  print.newline();
37
37
  traverse(bodyPath);
38
-
39
38
  maybe.markAfter(isMarkedAfter(bodyPath), path);
40
-
41
39
  indent.dec();
42
40
  maybe.print.newline(isNext(path));
43
41
 
@@ -6,7 +6,9 @@ const {
6
6
  } = require('../mark');
7
7
 
8
8
  const {isFirst} = require('../is');
9
- const isEmptyConsequent = (path) => path.get('consequent').isEmptyStatement();
9
+
10
+ const isEmptyConsequent = (path) => path
11
+ .get('consequent').isEmptyStatement();
10
12
 
11
13
  module.exports.IfStatement = {
12
14
  before: (path, {print}) => {
@@ -32,7 +32,11 @@ module.exports = {
32
32
  DebuggerStatement,
33
33
  Program(path, {print}) {
34
34
  print('__interpreter');
35
- path.get('body').forEach(print);
35
+
36
+ path
37
+ .get('body')
38
+ .forEach(print);
39
+
36
40
  print.newline();
37
41
  },
38
42
  EmptyStatement(path, {write}) {
@@ -99,5 +99,6 @@ const isNextAssign = (path) => {
99
99
  if (!nextPath.isExpressionStatement())
100
100
  return false;
101
101
 
102
- return nextPath.get('expression').isAssignmentExpression();
102
+ return nextPath
103
+ .get('expression').isAssignmentExpression();
103
104
  };
@@ -3,12 +3,20 @@
3
3
  module.exports = {
4
4
  TSTypeParameterDeclaration(path, {print}) {
5
5
  print('<');
6
- path.get('params').forEach(print);
6
+
7
+ path
8
+ .get('params')
9
+ .forEach(print);
10
+
7
11
  print('>');
8
12
  },
9
13
  TSTypeParameterInstantiation(path, {print}) {
10
14
  print('<');
11
- path.get('params').forEach(print);
15
+
16
+ path
17
+ .get('params')
18
+ .forEach(print);
19
+
12
20
  print('>');
13
21
  },
14
22
  TSTypeReference(path, {print}) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@putout/printer",
3
- "version": "1.28.0",
3
+ "version": "1.29.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",