@putout/printer 1.144.0 → 1.147.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.08, v1.146.0
2
+
3
+ feature:
4
+ - ab62be3 @putout/printer: ArrayExpression: Tuple: String and MemberExpression
5
+
6
+ 2023.06.08, v1.145.0
7
+
8
+ feature:
9
+ - 9d33542 @putout/printer: ArrayExpression: tuple: Identifier and StringLiteral
10
+
1
11
  2023.06.07, v1.144.0
2
12
 
3
13
  feature:
@@ -18,6 +18,8 @@ const {isSimple} = require('@putout/operate');
18
18
  const {
19
19
  isCoupleLines,
20
20
  isStringAndIdentifier,
21
+ isIdentifierAndString,
22
+ isStringAndMember,
21
23
  } = require('../is');
22
24
 
23
25
  const isForOf = ({parentPath}) => parentPath.isForOfStatement();
@@ -258,9 +260,15 @@ function isNewlineBetweenElements(path, {elements}) {
258
260
  if (isStringAndArray(elements))
259
261
  return false;
260
262
 
263
+ if (isStringAndMember(elements))
264
+ return false;
265
+
261
266
  if (isStringAndIdentifier(elements))
262
267
  return false;
263
268
 
269
+ if (isIdentifierAndString(elements))
270
+ return false;
271
+
264
272
  if (isSimpleAndObject(elements))
265
273
  return false;
266
274
 
@@ -31,4 +31,3 @@ module.exports.ArrayPattern = (path, {indent, maybe, print}, options) => {
31
31
  maybe.indent(elements.length && isNewLine);
32
32
  print(']');
33
33
  };
34
-
@@ -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,57 @@
1
+ 'use strict';
2
+
3
+ const {
4
+ isStringLiteral,
5
+ isTemplateLiteral,
6
+ isBinaryExpression,
7
+ } = require('@babel/types');
8
+
9
+ const isBinary = (a) => isBinaryExpression(a);
10
+
11
+ const isStringLike = (a) => {
12
+ if (isStringLiteral(a))
13
+ return true;
14
+
15
+ if (isTemplateLiteral(a))
16
+ return true;
17
+
18
+ return false;
19
+ };
20
+
21
+ module.exports.isConcatenation = (path) => {
22
+ const {parentPath} = path;
23
+
24
+ const {
25
+ operator,
26
+ left,
27
+ right,
28
+ } = path.node;
29
+
30
+ if (operator !== '+')
31
+ return false;
32
+
33
+ if (isStringLike(left) && isStringLike(right) && isBinary(parentPath))
34
+ return true;
35
+
36
+ if (isBinary(left) && isStringLike(right))
37
+ return true;
38
+
39
+ return false;
40
+ };
41
+
42
+ module.exports.concatanate = (path, {print, indent}) => {
43
+ if (!path.parentPath.isBinaryExpression()) {
44
+ indent.inc();
45
+ print.breakline();
46
+ }
47
+
48
+ print('__left');
49
+ print.space();
50
+ print('+');
51
+ print.breakline();
52
+ print('__right');
53
+
54
+ if (!path.parentPath.isBinaryExpression()) {
55
+ indent.dec();
56
+ }
57
+ };
@@ -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,8 @@ 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);
48
+ module.exports.isIdentifierAndString = ([a, b]) => isIdentifier(a) && isStringLiteral(b);
46
49
 
47
50
  const isIfOrStatement = (a) => isIfStatement(a) || isStatement(a);
48
51
  const isForOfOrStatement = (a) => isForOfStatement(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
 
@@ -31,6 +31,7 @@ const {
31
31
  parseLeadingComments,
32
32
  parseTrailingComments,
33
33
  } = require('./comments');
34
+
34
35
  const {parseOverrides} = require('./overrides');
35
36
 
36
37
  const isString = (a) => typeof a === 'string';
@@ -233,7 +234,6 @@ module.exports.tokenize = (ast, overrides) => {
233
234
 
234
235
  const currentIndent = i;
235
236
  parseLeadingComments(path, printer, format);
236
-
237
237
  // this is main thing
238
238
  maybePlugin(currentTraverse, path, printer, semantics);
239
239
  parseTrailingComments(path, printer, format);
@@ -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.144.0",
3
+ "version": "1.147.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",