Skip to content

Commit e67a827

Browse files
committed
feature: @putout/printer: ObjectPattern: Assign: improve
1 parent f3e9f67 commit e67a827

2 files changed

Lines changed: 17 additions & 6 deletions

File tree

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,5 @@ const {
22
loc = {
33
line: 0,
44
},
5-
id} = error;
5+
id,
6+
} = error;

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

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
'use strict';
22

33
const {types} = require('@putout/babel');
4-
54
const {wrongShorthand} = require('./wrong-shortand');
65

76
const {
@@ -15,13 +14,20 @@ const {moreThenMaxPropertiesInOneLine} = require('./more-then-max-properties-in-
1514
const {maybeTypeAnnotation} = require('../../maybe/maybe-type-annotation');
1615
const {moreThenMaxPropertiesLengthInOneLine} = require('./more-then-max-properties-length-in-one-line');
1716
const {printKey} = require('../object-expression/print-key');
18-
const isInsideFn = (path) => path.parentPath.isFunction();
17+
18+
const isInsideFn = (path) => {
19+
if (isFunction(path.parentPath))
20+
return true;
21+
22+
return isFunction(path.parentPath.parentPath);
23+
};
1924

2025
const {
2126
isIdentifier,
2227
isObjectPattern,
2328
isAssignmentPattern,
2429
isVariableDeclarator,
30+
isFunction,
2531
} = types;
2632

2733
function isIndent(path) {
@@ -67,8 +73,8 @@ module.exports.ObjectPattern = {
6773
});
6874

6975
const hasObject = n && hasObjectPattern(properties);
70-
7176
const notInsideFn = !isInsideFn(path);
77+
7278
maybe.print.newline(is && notInsideFn);
7379

7480
for (const [i, property] of properties.entries()) {
@@ -81,6 +87,9 @@ module.exports.ObjectPattern = {
8187
continue;
8288
}
8389

90+
const prev = property.getPrevSibling();
91+
const prevAssign = i && isAssignmentPattern(prev.node.value);
92+
8493
const valuePath = property.get('value');
8594
const keyPath = property.get('key');
8695
const isAssign = valuePath.isAssignmentPattern();
@@ -92,7 +101,7 @@ module.exports.ObjectPattern = {
92101
valuePath,
93102
});
94103

95-
maybe.indent(is && notInsideFn);
104+
maybe.indent((prevAssign || is) && notInsideFn);
96105
maybe.print.breakline(couple);
97106

98107
printKey(property, printer);
@@ -108,7 +117,7 @@ module.exports.ObjectPattern = {
108117
maybe.print.newline(couple);
109118
}
110119

111-
if (is || hasObject) {
120+
if (is || hasObject || prevAssign && notInsideFn) {
112121
print(',');
113122
print.newline();
114123

@@ -209,3 +218,4 @@ function isFunctionParam({parentPath}) {
209218

210219
return parentPath.parentPath.isFunction();
211220
}
221+

0 commit comments

Comments
 (0)