Improve formatting for AssignmentExpression with ClassExpression that has long superClass (#9341)

* Improve formatting for AssignmentExpression with ClassExpression that has long superClass

Fix by prettier

* Add changelog

* Remove unnecesary concat call

* Mention abount Google Closure Library in changelog
This commit is contained in:
Sosuke Suzuki authored and GitHub committed 2020-10-08 20:11:15 +09:00
1 parent 92ea679a9d
commit bba196abc8
4 files changed
+140 -1

No files matched your search

@@ -0,0 +1,33 @@
#### Improve formatting for `AssignmentExpression` with `ClassExpression` that has a long super class (#9341 by @sosukesuzuki)
This improves the formatting for [Google Closure Library Namespace](https://developers.google.com/closure/library/docs/introduction#names).
<!-- prettier-ignore -->
```js
// Input
aaaaaaaa.bbbbbbbb.cccccccc.dddddddd.eeeeeeee.ffffffff.gggggggg2 = class extends (
aaaaaaaa.bbbbbbbb.cccccccc.dddddddd.eeeeeeee.ffffffff.gggggggg1
) {
method () {
console.log("foo");
}
};
// Prettier stable
aaaaaaaa.bbbbbbbb.cccccccc.dddddddd.eeeeeeee.ffffffff.gggggggg2 = class extends aaaaaaaa
.bbbbbbbb.cccccccc.dddddddd.eeeeeeee.ffffffff.gggggggg1 {
method() {
console.log("foo");
}
};
// Prettier master
aaaaaaaa.bbbbbbbb.cccccccc.dddddddd.eeeeeeee.ffffffff.gggggggg2 = class extends (
aaaaaaaa.bbbbbbbb.cccccccc.dddddddd.eeeeeeee.ffffffff.gggggggg1
) {
method() {
console.log("foo");
}
};
```
+14 -1
View File
@@ -4322,9 +4322,22 @@ function printClass(path, options, print) {
}
if (n.superClass) {
const printSuperClass = (path) => {
const printed = path.call(print, "superClass");
const parent = path.getParentNode();
if (parent && parent.type === "AssignmentExpression") {
return concat([
ifBreak("("),
indent(concat([softline, printed])),
softline,
ifBreak(")"),
]);
}
return printed;
};
const printed = concat([
"extends ",
path.call(print, "superClass"),
printSuperClass(path),
path.call(print, "superTypeParameters"),
]);
const printedWithComments = path.call(
@@ -1,5 +1,73 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP
exports[`assignment.js format 1`] = `
====================================options=====================================
parsers: ["babel", "flow", "typescript"]
printWidth: 80
| printWidth
=====================================input======================================
aaaaaaaa.bbbbbbbb.cccccccc.dddddddd.eeeeeeee.ffffffff.gggggggg2 = class extends (
aaaaaaaa.bbbbbbbb.cccccccc.dddddddd.eeeeeeee.ffffffff.gggggggg1
) {
method () {
console.log("foo");
}
};
foo = class extends bar {
method() {
console.log("foo");
}
};
aaaaaaaa.bbbbbbbb.cccccccc.dddddddd.eeeeeeee.ffffffff.gggggggg2 = class extends bar {
method() {
console.log("foo");
}
};
foo = class extends aaaaaaaa.bbbbbbbb.cccccccc.dddddddd.eeeeeeee.ffffffff.gggggggg2 {
method() {
console.log("foo");
}
};
=====================================output=====================================
aaaaaaaa.bbbbbbbb.cccccccc.dddddddd.eeeeeeee.ffffffff.gggggggg2 = class extends (
aaaaaaaa.bbbbbbbb.cccccccc.dddddddd.eeeeeeee.ffffffff.gggggggg1
) {
method() {
console.log("foo");
}
};
foo = class extends (
bar
) {
method() {
console.log("foo");
}
};
aaaaaaaa.bbbbbbbb.cccccccc.dddddddd.eeeeeeee.ffffffff.gggggggg2 = class extends (
bar
) {
method() {
console.log("foo");
}
};
foo = class extends (
aaaaaaaa.bbbbbbbb.cccccccc.dddddddd.eeeeeeee.ffffffff.gggggggg2
) {
method() {
console.log("foo");
}
};
================================================================================
`;
exports[`binary.js format 1`] = `
====================================options=====================================
parsers: ["babel", "flow", "typescript"]
+25
View File
@@ -0,0 +1,25 @@
aaaaaaaa.bbbbbbbb.cccccccc.dddddddd.eeeeeeee.ffffffff.gggggggg2 = class extends (
aaaaaaaa.bbbbbbbb.cccccccc.dddddddd.eeeeeeee.ffffffff.gggggggg1
) {
method () {
console.log("foo");
}
};
foo = class extends bar {
method() {
console.log("foo");
}
};
aaaaaaaa.bbbbbbbb.cccccccc.dddddddd.eeeeeeee.ffffffff.gggggggg2 = class extends bar {
method() {
console.log("foo");
}
};
foo = class extends aaaaaaaa.bbbbbbbb.cccccccc.dddddddd.eeeeeeee.ffffffff.gggggggg2 {
method() {
console.log("foo");
}
};