@putout/printer 1.28.0 → 1.30.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,15 @@
1
+ 2023.04.10, v1.30.0
2
+
3
+ feature:
4
+ - 27b22b3 @putout/printer: add support of JSXElement
5
+
6
+ 2023.04.07, v1.29.0
7
+
8
+ feature:
9
+ - b1dfcf3 @putout/printer: ObjectPattern: improve support of not ObjectProperty value !== Identifier
10
+ - 53a44db @putout/printer: improve chaining support
11
+ - a552062 @putout/printer: newlines
12
+
1
13
  2023.04.07, v1.28.0
2
14
 
3
15
  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(')');
@@ -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,10 @@
2
2
 
3
3
  const {isCoupleLines} = require('../is');
4
4
  const {isFunction} = require('@babel/types');
5
-
6
- const isBodyOfArrow = (path) => path
7
- .parentPath.node.body === path.node;
8
- const isLogical = (path) => path
9
- .get('argument').isLogicalExpression();
10
- const isValue = (path) => path
11
- .get('properties.0.value').node;
12
- const isIf = (path) => path
13
- .parentPath.parentPath.isIfStatement();
5
+ const isBodyOfArrow = (path) => path.parentPath.node.body === path.node;
6
+ const isLogical = (path) => path.get('argument').isLogicalExpression();
7
+ const isValue = (path) => path.get('properties.0.value').node;
8
+ const isIf = (path) => path.parentPath.parentPath.isIfStatement();
14
9
  const isParentExpression = (path) => path.parentPath.isExpressionStatement();
15
10
 
16
11
  const isForOf = (path) => {
@@ -67,8 +62,7 @@ function shouldAddNewline(path) {
67
62
  if (!path.parentPath.isLogicalExpression())
68
63
  return false;
69
64
 
70
- return path
71
- .parentPath.parentPath.isSpreadElement();
65
+ return path.parentPath.parentPath.isSpreadElement();
72
66
  }
73
67
  module.exports.ObjectProperty = (path, {print, maybe}) => {
74
68
  const {
@@ -123,4 +117,3 @@ function isParens(path) {
123
117
 
124
118
  return false;
125
119
  }
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);
@@ -0,0 +1,23 @@
1
+ 'use strict';
2
+
3
+ const {JSXElement} = require('./jsx-element');
4
+
5
+ module.exports = {
6
+ JSXElement,
7
+ JSXOpeningElement(path, {print}) {
8
+ print('<');
9
+ print('__name');
10
+ print('>');
11
+ },
12
+ JSXIdentifier(path, {write}) {
13
+ write(path.node.name);
14
+ },
15
+ JSXText(path, {write}) {
16
+ write(path.node.value);
17
+ },
18
+ JSXClosingElement(path, {print}) {
19
+ print('</');
20
+ print('__name');
21
+ print('>');
22
+ },
23
+ };
@@ -0,0 +1,25 @@
1
+ 'use strict';
2
+
3
+ module.exports.JSXElement = {
4
+ condition,
5
+ before(path, {write, indent}) {
6
+ write('(');
7
+ write.breakline();
8
+ indent();
9
+ indent.inc();
10
+ },
11
+ print(path, {print, traverse}) {
12
+ print('__openingElement');
13
+ path.get('children').map(traverse);
14
+ print('__closingElement');
15
+ },
16
+ after(path, {write, indent}) {
17
+ indent.dec();
18
+ write.breakline();
19
+ write(')');
20
+ },
21
+ };
22
+
23
+ function condition(path) {
24
+ return path.parentPath.isReturnStatement();
25
+ }
@@ -27,10 +27,11 @@ function objectPlugin(plugin, path, printer) {
27
27
  const {
28
28
  print,
29
29
  split,
30
+ condition,
30
31
  before = split,
31
- beforeIf,
32
+ beforeIf = condition,
32
33
  after = split,
33
- afterIf,
34
+ afterIf = condition,
34
35
  } = plugin;
35
36
 
36
37
  if (beforeIf?.(path, printer)) {
@@ -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
 
@@ -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
  };
@@ -8,6 +8,7 @@ const expressions = require('./expressions');
8
8
  const statements = require('./statements');
9
9
  const literals = require('./literals');
10
10
  const typescript = require('./typescript');
11
+ const jsx = require('./jsx');
11
12
  const {TYPES} = require('../types');
12
13
 
13
14
  const {
@@ -38,6 +39,7 @@ const traversers = {
38
39
  ...statements,
39
40
  ...literals,
40
41
  ...typescript,
42
+ ...jsx,
41
43
  };
42
44
 
43
45
  const GET = '__';
@@ -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.30.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",