@putout/printer 1.145.0 → 1.148.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,16 @@
1
+ 2023.06.08, v1.148.0
2
+
3
+ fix:
4
+ - 26795a8 @putout/printer: BinaryExpression: Strings
5
+
6
+ feature:
7
+ - 55cb78d @putout/printer: ArrayExpression: plugins
8
+
9
+ 2023.06.08, v1.146.0
10
+
11
+ feature:
12
+ - ab62be3 @putout/printer: ArrayExpression: Tuple: String and MemberExpression
13
+
1
14
  2023.06.08, v1.145.0
2
15
 
3
16
  feature:
@@ -11,6 +11,7 @@ const {
11
11
  isBooleanLiteral,
12
12
  isNullLiteral,
13
13
  isObjectExpression,
14
+ isAwaitExpression,
14
15
  } = require('@babel/types');
15
16
 
16
17
  const {isSimple} = require('@putout/operate');
@@ -19,7 +20,7 @@ const {
19
20
  isCoupleLines,
20
21
  isStringAndIdentifier,
21
22
  isIdentifierAndString,
22
-
23
+ isStringAndMember,
23
24
  } = require('../is');
24
25
 
25
26
  const isForOf = ({parentPath}) => parentPath.isForOfStatement();
@@ -28,7 +29,14 @@ const isStringAndArray = ([a, b]) => isStringLiteral(a) && isArrayExpression(b);
28
29
  const isStringAndString = ([a, b]) => isStringLiteral(a) && isStringLiteral(b);
29
30
  const isIdentifierAndIdentifier = ([a, b]) => isIdentifier(a) && isIdentifier(b);
30
31
  const isInsideArray = (path) => path.parentPath.isArrayExpression();
31
- const isSimpleAndCall = ([a, b]) => isSimple(a) && isCallExpression(b);
32
+
33
+ const isSimpleAndCall = ([a, b]) => {
34
+ if (!isSimple(a))
35
+ return;
36
+
37
+ return isCallExpression(b) || isAwaitExpression(b);
38
+ };
39
+
32
40
  const isBooleanAndSimple = ([a, b]) => isBooleanLiteral(a) && isSimple(b);
33
41
  const isNullAndSimple = ([a, b]) => isNullLiteral(a) && isSimple(b);
34
42
  const isSimpleAndObject = ([a, b]) => isSimple(a) && isObjectExpression(b);
@@ -260,6 +268,9 @@ function isNewlineBetweenElements(path, {elements}) {
260
268
  if (isStringAndArray(elements))
261
269
  return false;
262
270
 
271
+ if (isStringAndMember(elements))
272
+ return false;
273
+
263
274
  if (isStringAndIdentifier(elements))
264
275
  return false;
265
276
 
@@ -1,5 +1,10 @@
1
1
  'use strict';
2
2
 
3
+ const {
4
+ concatanate,
5
+ isConcatenation,
6
+ } = require('./concatanate');
7
+
3
8
  const BinaryExpression = {
4
9
  condition(path) {
5
10
  const parens = path.node.extra?.parenthesized;
@@ -12,26 +17,29 @@ const BinaryExpression = {
12
17
  before(path, {print}) {
13
18
  print('(');
14
19
  },
15
- print(path, {write, traverse}) {
20
+ print(path, {print, indent, maybe}) {
16
21
  const {operator} = path.node;
17
- const left = path.get('left');
18
- const right = path.get('right');
19
22
 
20
23
  if (operator === 'instanceof') {
21
- traverse(left);
22
- write(' ');
23
- write(operator);
24
- write(' ');
25
- traverse(right);
24
+ print('__left');
25
+ print(' instanceof ');
26
+ print('__right');
26
27
 
27
28
  return;
28
29
  }
29
30
 
30
- traverse(left);
31
- write.space();
32
- write(path.node.operator);
33
- write.space();
34
- traverse(right);
31
+ if (isConcatenation(path))
32
+ return concatanate(path, {
33
+ print,
34
+ indent,
35
+ maybe,
36
+ });
37
+
38
+ print('__left');
39
+ print.space();
40
+ print(path.node.operator);
41
+ print.space();
42
+ print('__right');
35
43
  },
36
44
  after(path, {print}) {
37
45
  print(')');
@@ -0,0 +1,56 @@
1
+ 'use strict';
2
+
3
+ const {
4
+ isStringLiteral,
5
+ isTemplateLiteral,
6
+ isBinaryExpression,
7
+ } = require('@babel/types');
8
+
9
+ const isStringLike = (a) => {
10
+ if (isStringLiteral(a))
11
+ return true;
12
+
13
+ if (isTemplateLiteral(a))
14
+ return true;
15
+
16
+ return false;
17
+ };
18
+
19
+ module.exports.isConcatenation = (path) => {
20
+ const {parentPath} = path;
21
+ const {operator} = path.node;
22
+
23
+ const startLine = path.node.loc?.start.line;
24
+ const endLine = path.node.loc?.end.line;
25
+
26
+ if (startLine === endLine)
27
+ return false;
28
+
29
+ const left = path.get('left');
30
+ const right = path.get('right');
31
+
32
+ if (operator !== '+')
33
+ return false;
34
+
35
+ if (isStringLike(left) && isStringLike(right) && isBinaryExpression(parentPath))
36
+ return true;
37
+
38
+ return isBinaryExpression(left) && isStringLike(right);
39
+ };
40
+
41
+ module.exports.concatanate = (path, {print, indent}) => {
42
+ if (!path.parentPath.isBinaryExpression()) {
43
+ indent.inc();
44
+ print.breakline();
45
+ }
46
+
47
+ print('__left');
48
+ print.space();
49
+ print('+');
50
+ print.breakline();
51
+ print('__right');
52
+
53
+ if (!path.parentPath.isBinaryExpression()) {
54
+ indent.dec();
55
+ }
56
+ };
@@ -17,11 +17,8 @@ const {
17
17
 
18
18
  const {NewExpression} = require('./new-expression');
19
19
 
20
- const {
21
- ObjectExpression,
22
- ObjectProperty,
23
- } = require('./object-expression');
24
-
20
+ const {ObjectExpression} = require('./object-expression/object-expression');
21
+ const {ObjectProperty} = require('./object-expression/object-property');
25
22
  const {ObjectPattern} = require('./object-pattern');
26
23
 
27
24
  const {
@@ -42,7 +39,7 @@ const {TaggedTemplateExpression} = require('./tagged-template-expression');
42
39
  const {
43
40
  BinaryExpression,
44
41
  LogicalExpression,
45
- } = require('./binary-expression');
42
+ } = require('./binary-expression/binary-expression');
46
43
 
47
44
  const {ConditionalExpression} = require('./conditional-expression');
48
45
 
@@ -9,9 +9,9 @@ const {
9
9
  noLeadingComment,
10
10
  hasLeadingComment,
11
11
  exists,
12
- } = require('../is');
12
+ } = require('../../is');
13
13
 
14
- const {parseComments} = require('../comments');
14
+ const {parseComments} = require('../../comments');
15
15
 
16
16
  const isBodyOfArrow = (path) => path.parentPath.node.body === path.node;
17
17
  const isLogical = (path) => path.get('argument').isLogicalExpression();
@@ -86,35 +86,10 @@ function shouldAddNewline(path) {
86
86
  return path.parentPath.parentPath.isSpreadElement();
87
87
  }
88
88
 
89
- module.exports.ObjectProperty = (path, {print, maybe}) => {
90
- const {
91
- shorthand,
92
- computed,
93
- } = path.node;
94
-
95
- const properties = path.parentPath.get('properties');
96
- const isLast = path === properties.at(-1);
97
- const manyLines = !isOneLine(path.parentPath);
98
-
99
- maybe.print(computed, '[');
100
- print('__key');
101
- maybe.print(computed, ']');
102
-
103
- if (!shorthand) {
104
- print(':');
105
- print.space();
106
- print('__value');
107
- }
108
-
109
- if (manyLines)
110
- print(',');
111
- else if (!isLast && properties.length)
112
- print(', ');
113
- };
114
-
115
89
  const ONE_LINE = true;
116
90
  const MANY_LINES = false;
117
91
 
92
+ module.exports.isOneLine = isOneLine;
118
93
  function isOneLine(path) {
119
94
  const {length} = path.get('properties');
120
95
 
@@ -0,0 +1,33 @@
1
+ 'use strict';
2
+
3
+ const {isConcatenation} = require('../binary-expression/concatanate');
4
+ const {isOneLine} = require('./object-expression');
5
+
6
+ module.exports.ObjectProperty = (path, {maybe, traverse, write}) => {
7
+ const {
8
+ shorthand,
9
+ computed,
10
+ } = path.node;
11
+
12
+ const key = path.get('key');
13
+ const value = path.get('value');
14
+
15
+ const properties = path.parentPath.get('properties');
16
+ const isLast = path === properties.at(-1);
17
+ const manyLines = !isOneLine(path.parentPath);
18
+
19
+ maybe.write(computed, '[');
20
+ traverse(key);
21
+ maybe.write(computed, ']');
22
+
23
+ if (!shorthand) {
24
+ write(':');
25
+ maybe.write.space(!isConcatenation(value));
26
+ traverse(value);
27
+ }
28
+
29
+ if (manyLines)
30
+ write(',');
31
+ else if (!isLast && properties.length)
32
+ write(', ');
33
+ };
@@ -7,6 +7,7 @@ const {
7
7
  isStatement,
8
8
  isForOfStatement,
9
9
  isVariableDeclaration,
10
+ isMemberExpression,
10
11
  } = require('@babel/types');
11
12
 
12
13
  const isParentProgram = (path) => path.parentPath?.isProgram();
@@ -43,6 +44,7 @@ function isCoupleLines(path) {
43
44
 
44
45
  module.exports.exists = (a) => a.node;
45
46
  module.exports.isStringAndIdentifier = ([a, b]) => isStringLiteral(a) && isIdentifier(b);
47
+ module.exports.isStringAndMember = ([a, b]) => isStringLiteral(a) && isMemberExpression(b);
46
48
  module.exports.isIdentifierAndString = ([a, b]) => isIdentifier(a) && isStringLiteral(b);
47
49
 
48
50
  const isIfOrStatement = (a) => isIfStatement(a) || isStatement(a);
@@ -12,6 +12,8 @@ const {hasPrevNewline} = require('../../mark');
12
12
  const {isExportDeclaration} = require('@babel/types');
13
13
  const {maybeSpaceAfterKeyword} = require('./maybe-space-after-keyword');
14
14
 
15
+ const {isConcatenation} = require('../../expressions/binary-expression/concatanate');
16
+
15
17
  const isParentBlock = (path) => /Program|BlockStatement|Export/.test(path.parentPath.type);
16
18
  const isInsideBlock = (path) => /^(Program|BlockStatement)$/.test(path.parentPath.type);
17
19
 
@@ -40,7 +42,7 @@ module.exports.VariableDeclaration = {
40
42
  if (exists(init)) {
41
43
  write.space();
42
44
  write('=');
43
- write.space();
45
+ maybe.write.space(!isConcatenation(init));
44
46
  traverse(init);
45
47
  }
46
48
 
@@ -45,6 +45,9 @@ module.exports = {
45
45
  TSUnknownKeyword(path, {write}) {
46
46
  write('unknown');
47
47
  },
48
+ TSObjectKeyword(path, {write}) {
49
+ write('object');
50
+ },
48
51
  TSTupleType(path, {write, traverse, maybe}) {
49
52
  const elementTypes = path.get('elementTypes');
50
53
  const n = elementTypes.length - 1;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@putout/printer",
3
- "version": "1.145.0",
3
+ "version": "1.148.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",