@putout/printer 18.7.1 → 18.7.2
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 +7 -0
- package/lib/tokenize/statements/return-statement/after-if.js +17 -0
- package/lib/tokenize/statements/return-statement/before-if.js +18 -0
- package/lib/tokenize/statements/return-statement/maybe-space-after-keyword.js +35 -16
- package/lib/tokenize/statements/return-statement/return-statement.js +8 -50
- package/lib/tokenize/type-checker/type-checker.js +2 -2
- package/package.json +1 -1
package/ChangeLog
CHANGED
|
@@ -1,3 +1,10 @@
|
|
|
1
|
+
2026.03.15, v18.7.2
|
|
2
|
+
|
|
3
|
+
feature:
|
|
4
|
+
- 03c11aa @putout/printer: ReturnStatements: maybeSpaceAfterKeyword: simplify
|
|
5
|
+
- 2a0d633 @putout/printer: ReturnStatement: isJSXWithComment
|
|
6
|
+
- 3ee3a68 @putout/printer: ReturnStatement: afterIf
|
|
7
|
+
|
|
1
8
|
2026.03.14, v18.7.1
|
|
2
9
|
|
|
3
10
|
feature:
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import {
|
|
2
|
+
isLast,
|
|
3
|
+
noTrailingComment,
|
|
4
|
+
} from '#is';
|
|
5
|
+
import {createTypeChecker} from '#type-checker';
|
|
6
|
+
|
|
7
|
+
const isInsideIfWithElse = createTypeChecker([
|
|
8
|
+
['-: parentPath -> !IfStatement'],
|
|
9
|
+
['+: parentPath.node.alternate', Boolean],
|
|
10
|
+
]);
|
|
11
|
+
|
|
12
|
+
export const afterIf = createTypeChecker([
|
|
13
|
+
['+', isInsideIfWithElse],
|
|
14
|
+
['-', isLast],
|
|
15
|
+
['-: parentPath', isLast],
|
|
16
|
+
['+', noTrailingComment],
|
|
17
|
+
]);
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import {types} from '@putout/babel';
|
|
2
|
+
import {callWithPrev} from '#is';
|
|
3
|
+
import {hasPrevNewline} from '#mark';
|
|
4
|
+
import {createTypeChecker} from '#type-checker';
|
|
5
|
+
|
|
6
|
+
const {
|
|
7
|
+
isTryStatement,
|
|
8
|
+
isBlockStatement,
|
|
9
|
+
} = types;
|
|
10
|
+
|
|
11
|
+
const isBodyLength = ({parentPath}) => parentPath.node?.body?.length > 2;
|
|
12
|
+
|
|
13
|
+
export const beforeIf = createTypeChecker([
|
|
14
|
+
['+', callWithPrev(isTryStatement)],
|
|
15
|
+
['-: ->', hasPrevNewline],
|
|
16
|
+
['+: ->', isBodyLength],
|
|
17
|
+
['+: ->', callWithPrev(isBlockStatement)],
|
|
18
|
+
]);
|
|
@@ -1,3 +1,9 @@
|
|
|
1
|
+
import {createTypeChecker} from '#type-checker';
|
|
2
|
+
|
|
3
|
+
const isSequenceOption = (a, {roundBraces}) => roundBraces.sequence;
|
|
4
|
+
const isArrowOption = (a, {roundBraces}) => roundBraces.arrow;
|
|
5
|
+
const isNot = (a) => a === '!';
|
|
6
|
+
|
|
1
7
|
export const maybeSpaceAfterKeyword = (path, {print}, semantics) => {
|
|
2
8
|
const {roundBraces} = semantics;
|
|
3
9
|
const {node} = path;
|
|
@@ -5,22 +11,35 @@ export const maybeSpaceAfterKeyword = (path, {print}, semantics) => {
|
|
|
5
11
|
if (!node)
|
|
6
12
|
return;
|
|
7
13
|
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
if (type === 'StringLiteral' || type === 'TemplateLiteral')
|
|
14
|
-
return print.space();
|
|
15
|
-
|
|
16
|
-
if (type === 'ArrayExpression' || type === 'ObjectExpression')
|
|
17
|
-
return print.space();
|
|
18
|
-
|
|
19
|
-
if (type === 'UnaryExpression' && node.operator === '!')
|
|
20
|
-
return print.space();
|
|
21
|
-
|
|
22
|
-
if (type === 'ArrowFunctionExpression' && roundBraces.arrow)
|
|
23
|
-
return print.space();
|
|
14
|
+
if (isOptionalSpace(path, {roundBraces})) {
|
|
15
|
+
print.space();
|
|
16
|
+
return;
|
|
17
|
+
}
|
|
24
18
|
|
|
25
19
|
print(' ');
|
|
26
20
|
};
|
|
21
|
+
|
|
22
|
+
const isSequenceWithOption = createTypeChecker([
|
|
23
|
+
['-: -> !SequenceExpression'],
|
|
24
|
+
['+', isSequenceOption],
|
|
25
|
+
]);
|
|
26
|
+
|
|
27
|
+
const isArrowWithOption = createTypeChecker([
|
|
28
|
+
['-: -> !ArrowFunctionExpression'],
|
|
29
|
+
['+', isArrowOption],
|
|
30
|
+
]);
|
|
31
|
+
|
|
32
|
+
const isUnaryNot = createTypeChecker([
|
|
33
|
+
['-: -> !UnaryExpression'],
|
|
34
|
+
['+: node.operator', isNot],
|
|
35
|
+
]);
|
|
36
|
+
|
|
37
|
+
const isOptionalSpace = createTypeChecker([
|
|
38
|
+
['+: -> StringLiteral'],
|
|
39
|
+
['+: -> TemplateLiteral'],
|
|
40
|
+
['+: -> ArrayExpression'],
|
|
41
|
+
['+: -> ObjectExpression'],
|
|
42
|
+
['+', isUnaryNot],
|
|
43
|
+
['+', isSequenceWithOption],
|
|
44
|
+
['+', isArrowWithOption],
|
|
45
|
+
]);
|
|
@@ -1,33 +1,8 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {hasPrevNewline} from '#mark';
|
|
1
|
+
import {isInsideLabel} from '#is';
|
|
3
2
|
import {createTypeChecker} from '#type-checker';
|
|
4
|
-
import {
|
|
5
|
-
isInsideLabel,
|
|
6
|
-
noTrailingComment,
|
|
7
|
-
isLast,
|
|
8
|
-
callWithPrev,
|
|
9
|
-
} from '#is';
|
|
10
3
|
import {maybeSpaceAfterKeyword} from './maybe-space-after-keyword.js';
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
isJSXElement,
|
|
14
|
-
isTryStatement,
|
|
15
|
-
isBlockStatement,
|
|
16
|
-
} = types;
|
|
17
|
-
|
|
18
|
-
const isBodyLength = ({parentPath}) => parentPath.node?.body?.length > 2;
|
|
19
|
-
|
|
20
|
-
const isInsideIfWithElse = createTypeChecker([
|
|
21
|
-
['-: parentPath -> !IfStatement'],
|
|
22
|
-
['+: parentPath.node.alternate', Boolean],
|
|
23
|
-
]);
|
|
24
|
-
|
|
25
|
-
const beforeIf = createTypeChecker([
|
|
26
|
-
['+', callWithPrev(isTryStatement)],
|
|
27
|
-
['-: ->', hasPrevNewline],
|
|
28
|
-
['+: ->', isBodyLength],
|
|
29
|
-
['+: ->', callWithPrev(isBlockStatement)],
|
|
30
|
-
]);
|
|
4
|
+
import {beforeIf} from './before-if.js';
|
|
5
|
+
import {afterIf} from './after-if.js';
|
|
31
6
|
|
|
32
7
|
export const ReturnStatement = {
|
|
33
8
|
beforeIf,
|
|
@@ -56,30 +31,13 @@ export const ReturnStatement = {
|
|
|
56
31
|
print('__argument');
|
|
57
32
|
print(';');
|
|
58
33
|
},
|
|
59
|
-
afterIf
|
|
60
|
-
if (isInsideIfWithElse(path))
|
|
61
|
-
return true;
|
|
62
|
-
|
|
63
|
-
if (isLast(path))
|
|
64
|
-
return false;
|
|
65
|
-
|
|
66
|
-
if (isLast(path.parentPath))
|
|
67
|
-
return false;
|
|
68
|
-
|
|
69
|
-
return noTrailingComment(path);
|
|
70
|
-
},
|
|
34
|
+
afterIf,
|
|
71
35
|
after(path, {print}) {
|
|
72
36
|
print.newline();
|
|
73
37
|
},
|
|
74
38
|
};
|
|
75
39
|
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
return;
|
|
81
|
-
|
|
82
|
-
const {leadingComments} = arg;
|
|
83
|
-
|
|
84
|
-
return isJSXElement(arg) && leadingComments?.length;
|
|
85
|
-
}
|
|
40
|
+
const isJSXWithComment = createTypeChecker([
|
|
41
|
+
['-: node.argument -> !JSXElement'],
|
|
42
|
+
['+: node.argument.leadingComments ->', Boolean],
|
|
43
|
+
]);
|
|
@@ -14,13 +14,13 @@ export const createTypeChecker = (typeNames, overrides = {}) => {
|
|
|
14
14
|
} = overrides;
|
|
15
15
|
|
|
16
16
|
const tuples = parseTypeNames(typeNames);
|
|
17
|
-
const checkers =
|
|
17
|
+
const checkers = [];
|
|
18
18
|
const results = new Set();
|
|
19
19
|
|
|
20
20
|
for (const [operation, typeName] of tuples) {
|
|
21
21
|
const [result, selector, not] = parseOperation(operation);
|
|
22
22
|
results.add(result);
|
|
23
|
-
checkers.
|
|
23
|
+
checkers.push({
|
|
24
24
|
result,
|
|
25
25
|
selector,
|
|
26
26
|
not,
|
package/package.json
CHANGED