Skip to content

Commit

Permalink
Fix formatting for AssignmentExpression with ClassExpression (#9741)
Browse files Browse the repository at this point in the history
* Fix formatting for asignment with superClass

* Add tests

* Changelog

* For only AssignmentExpression

* Remove unnecesary ifBreak
  • Loading branch information
sosukesuzuki committed Nov 25, 2020
1 parent 114fe2e commit b46710f
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 10 deletions.
27 changes: 27 additions & 0 deletions changelog_unreleased/javascript/9741.md
@@ -0,0 +1,27 @@
#### Fix formatting for AssignmentExpression with ClassExpression (#9741 by @sosukesuzuki)

<!-- prettier-ignore -->
```js
// Input
module.exports = class A extends B {
method() {
console.log("foo");
}
};

// Prettier stable
module.exports = class A extends (
B
) {
method() {
console.log("foo");
}
};

// Prettier master
module.exports = class A extends B {
method() {
console.log("foo");
}
};
```
14 changes: 7 additions & 7 deletions src/language-js/print/class.js
Expand Up @@ -131,13 +131,13 @@ function printList(path, options, print, listName) {
function printSuperClass(path, options, print) {
const printed = path.call(print, "superClass");
const parent = path.getParentNode();
if (parent && parent.type === "AssignmentExpression") {
return concat([
ifBreak("("),
indent(concat([softline, printed])),
softline,
ifBreak(")"),
]);
if (parent.type === "AssignmentExpression") {
return group(
ifBreak(
concat(["(", indent(concat([softline, printed])), softline, ")"]),
printed
)
);
}
return printed;
}
Expand Down
16 changes: 13 additions & 3 deletions tests/js/classes/__snapshots__/jsfmt.spec.js.snap
Expand Up @@ -32,6 +32,12 @@ foo = class extends aaaaaaaa.bbbbbbbb.cccccccc.dddddddd.eeeeeeee.ffffffff.gggggg
}
};
module.exports = class A extends B {
method () {
console.log("foo");
}
};
=====================================output=====================================
aaaaaaaa.bbbbbbbb.cccccccc.dddddddd.eeeeeeee.ffffffff.gggggggg2 = class extends (
aaaaaaaa.bbbbbbbb.cccccccc.dddddddd.eeeeeeee.ffffffff.gggggggg1
Expand All @@ -41,9 +47,7 @@ aaaaaaaa.bbbbbbbb.cccccccc.dddddddd.eeeeeeee.ffffffff.gggggggg2 = class extends
}
};
foo = class extends (
bar
) {
foo = class extends bar {
method() {
console.log("foo");
}
Expand All @@ -65,6 +69,12 @@ foo = class extends (
}
};
module.exports = class A extends B {
method() {
console.log("foo");
}
};
================================================================================
`;

Expand Down
6 changes: 6 additions & 0 deletions tests/js/classes/assignment.js
Expand Up @@ -23,3 +23,9 @@ foo = class extends aaaaaaaa.bbbbbbbb.cccccccc.dddddddd.eeeeeeee.ffffffff.gggggg
console.log("foo");
}
};

module.exports = class A extends B {
method () {
console.log("foo");
}
};

0 comments on commit b46710f

Please sign in to comment.