Skip to content

Commit e681209

Browse files
committed
feature: @putout/printer: ObjectPattern: comma: simplify
1 parent c68ae31 commit e681209

3 files changed

Lines changed: 31 additions & 22 deletions

File tree

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import {types} from '@putout/babel';
2+
import {createTypeChecker} from '#type-checker';
3+
4+
const {
5+
isForOfStatement,
6+
isAssignmentPattern,
7+
} = types;
8+
9+
const isCoupleOption = (a, {couple}) => couple;
10+
11+
export function isInsideForOf({parentPath}) {
12+
return isForOfStatement(parentPath.parentPath.parentPath);
13+
}
14+
15+
export function isPrevAssign(path) {
16+
const prev = path.getPrevSibling();
17+
18+
return isAssignmentPattern(prev.node.value);
19+
}
20+
21+
export const isCommaAfterProperty = createTypeChecker([
22+
['+', isCoupleOption],
23+
['-: key -> -'],
24+
['-', isPrevAssign],
25+
['-: parentPath', isInsideForOf],
26+
['+: node.value.right -> ObjectExpression'],
27+
]);
28+

lib/tokenize/expressions/object-pattern/is.js

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import {hasAssign} from './has.js';
55
const {
66
isObjectExpression,
77
isAssignmentPattern,
8-
isForOfStatement,
98
isFunction,
109
isVariableDeclarator,
1110
isObjectProperty,
@@ -38,16 +37,6 @@ export const isCoupleProperties = ({path, valuePath, property}) => {
3837
return !isObjectProperty(parentPath);
3938
};
4039

41-
export function isInsideForOf({parentPath}) {
42-
return isForOfStatement(parentPath.parentPath.parentPath);
43-
}
44-
45-
export function isPrevAssign(path) {
46-
const prev = path.getPrevSibling();
47-
48-
return isAssignmentPattern(prev.node.value);
49-
}
50-
5140
export function isPrevAssignObject(path) {
5241
const prev = path.getPrevSibling();
5342

lib/tokenize/expressions/object-pattern/object-pattern.js

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import {types} from '@putout/babel';
21
import {wrongShorthand} from './wrong-shorthand.js';
32
import {maybeTypeAnnotation} from '../../maybe/maybe-type-annotation.js';
43
import {printKey} from '../object-expression/print-key.js';
@@ -12,18 +11,15 @@ import {
1211
hasAssignObject,
1312
hasObjectPattern,
1413
} from './has.js';
14+
import {isCommaAfterProperty} from './comma.js';
1515
import {
1616
isCoupleProperties,
1717
isIndent,
1818
isInsideFn,
19-
isInsideForOf,
2019
isNextAssignObject,
21-
isPrevAssign,
2220
isPrevAssignObject,
2321
} from './is.js';
2422

25-
const {isObjectExpression} = types;
26-
2723
export const ObjectPattern = {
2824
print: maybeTypeAnnotation((path, printer, semantics) => {
2925
const shouldIndent = isIndent(path);
@@ -97,12 +93,7 @@ export const ObjectPattern = {
9793
} else if (isAssign) {
9894
print(valuePath);
9995

100-
maybe.print(couple, ',');
101-
maybe.print.newline(couple);
102-
103-
const {right} = valuePath.node;
104-
105-
if (i && !isPrevAssign(property) && !isInsideForOf(path) && isObjectExpression(right)) {
96+
if (isCommaAfterProperty(property, {couple})) {
10697
print(',');
10798
print.newline();
10899
continue;
@@ -137,3 +128,4 @@ export const ObjectPattern = {
137128
print('}');
138129
}),
139130
};
131+

0 commit comments

Comments
 (0)