@putout/printer 18.8.6 → 18.9.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 +6 -0
- package/lib/tokenize/jsx/indent.js +33 -0
- package/lib/tokenize/jsx/jsx-element.js +17 -53
- package/package.json +1 -1
package/ChangeLog
CHANGED
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import {types} from '@putout/babel';
|
|
2
|
+
import {createTypeChecker} from '#type-checker';
|
|
3
|
+
|
|
4
|
+
const {isFunction} = types;
|
|
5
|
+
|
|
6
|
+
export function isNeedIndent(path) {
|
|
7
|
+
if (hasComplexAttribute(path))
|
|
8
|
+
return false;
|
|
9
|
+
|
|
10
|
+
const insideFn = path.parentPath.isArrowFunctionExpression();
|
|
11
|
+
const insideJSX = path.parentPath.isJSXElement();
|
|
12
|
+
const insideCall = path.parentPath.parentPath.isCallExpression();
|
|
13
|
+
|
|
14
|
+
return insideJSX || insideFn && insideCall;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
const isComplexAttribute = createTypeChecker([
|
|
18
|
+
['+: -> JSXSpreadAttribute'],
|
|
19
|
+
['-: node.value -> !JSXExpressionContainer'],
|
|
20
|
+
['+: parentPath.node.attributes.length', '>', 1],
|
|
21
|
+
['+: node.value.expression', isFunction],
|
|
22
|
+
]);
|
|
23
|
+
|
|
24
|
+
function hasComplexAttribute(path) {
|
|
25
|
+
const attributes = path.get('openingElement.attributes');
|
|
26
|
+
|
|
27
|
+
for (const attribute of attributes) {
|
|
28
|
+
if (isComplexAttribute(attribute))
|
|
29
|
+
return true;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
return false;
|
|
33
|
+
}
|
|
@@ -1,13 +1,25 @@
|
|
|
1
1
|
import {types} from '@putout/babel';
|
|
2
|
+
import {createTypeChecker} from '#type-checker';
|
|
3
|
+
import {isNeedIndent} from './indent.js';
|
|
4
|
+
|
|
5
|
+
const isDeeplyNestedJSX = ({parentPath}) => {
|
|
6
|
+
return isJSXElement(parentPath.parentPath?.parentPath?.parentPath);
|
|
7
|
+
};
|
|
8
|
+
|
|
9
|
+
const condition = createTypeChecker([
|
|
10
|
+
'+: node.extra.parenthesized -> +',
|
|
11
|
+
'+: parentPath -> ReturnStatement',
|
|
12
|
+
'+: parentPath -> ParenthesizedExpression',
|
|
13
|
+
'+: parentPath -> ArrowFunctionExpression',
|
|
14
|
+
'+: parentPath -> VariableDeclarator',
|
|
15
|
+
]);
|
|
2
16
|
|
|
3
17
|
const {
|
|
4
18
|
isJSXElement,
|
|
5
|
-
|
|
6
|
-
isFunction,
|
|
7
|
-
isJSXSpreadAttribute,
|
|
19
|
+
isArrowFunctionExpression,
|
|
8
20
|
} = types;
|
|
9
21
|
|
|
10
|
-
const isInsideArrow = ({parentPath}) =>
|
|
22
|
+
const isInsideArrow = ({parentPath}) => isArrowFunctionExpression(parentPath);
|
|
11
23
|
|
|
12
24
|
export const JSXElement = {
|
|
13
25
|
condition,
|
|
@@ -36,7 +48,7 @@ export const JSXElement = {
|
|
|
36
48
|
},
|
|
37
49
|
after(path, {write, indent, maybe}) {
|
|
38
50
|
const {leadingComments} = path.node;
|
|
39
|
-
const isJSX =
|
|
51
|
+
const isJSX = isDeeplyNestedJSX(path);
|
|
40
52
|
|
|
41
53
|
if (isJSX) {
|
|
42
54
|
write.breakline();
|
|
@@ -49,51 +61,3 @@ export const JSXElement = {
|
|
|
49
61
|
maybe.write(!leadingComments?.length, ')');
|
|
50
62
|
},
|
|
51
63
|
};
|
|
52
|
-
|
|
53
|
-
function condition(path) {
|
|
54
|
-
if (path.parentPath.isReturnStatement())
|
|
55
|
-
return true;
|
|
56
|
-
|
|
57
|
-
if (path.parentPath.isParenthesizedExpression())
|
|
58
|
-
return true;
|
|
59
|
-
|
|
60
|
-
if (path.node.extra?.parenthesized)
|
|
61
|
-
return true;
|
|
62
|
-
|
|
63
|
-
if (path.parentPath.isArrowFunctionExpression())
|
|
64
|
-
return true;
|
|
65
|
-
|
|
66
|
-
return path.parentPath.isVariableDeclarator();
|
|
67
|
-
}
|
|
68
|
-
|
|
69
|
-
function hasComplexAttribute(path) {
|
|
70
|
-
const {attributes} = path.node.openingElement;
|
|
71
|
-
|
|
72
|
-
for (const attribute of attributes) {
|
|
73
|
-
const {value} = attribute;
|
|
74
|
-
|
|
75
|
-
if (isJSXSpreadAttribute(attribute))
|
|
76
|
-
return true;
|
|
77
|
-
|
|
78
|
-
if (!isJSXExpressionContainer(value))
|
|
79
|
-
continue;
|
|
80
|
-
|
|
81
|
-
const {expression} = value;
|
|
82
|
-
|
|
83
|
-
if (isFunction(expression))
|
|
84
|
-
return true;
|
|
85
|
-
}
|
|
86
|
-
|
|
87
|
-
return false;
|
|
88
|
-
}
|
|
89
|
-
|
|
90
|
-
function isNeedIndent(path) {
|
|
91
|
-
if (hasComplexAttribute(path))
|
|
92
|
-
return false;
|
|
93
|
-
|
|
94
|
-
const insideFn = path.parentPath.isArrowFunctionExpression();
|
|
95
|
-
const insideJSX = path.parentPath.isJSXElement();
|
|
96
|
-
const insideCall = path.parentPath.parentPath.isCallExpression();
|
|
97
|
-
|
|
98
|
-
return insideJSX || insideFn && insideCall;
|
|
99
|
-
}
|
package/package.json
CHANGED