Skip to content

Commit

Permalink
Improve formatting for AssignmentExpression with ClassExpression that…
Browse files Browse the repository at this point in the history
… 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
  • Loading branch information
sosukesuzuki committed Oct 8, 2020
1 parent 92ea679 commit bba196a
Show file tree
Hide file tree
Showing 4 changed files with 140 additions and 1 deletion.
33 changes: 33 additions & 0 deletions changelog_unreleased/javascript/pr-9341.md
Original file line number Diff line number Diff line change
@@ -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");
}
};

```
15 changes: 14 additions & 1 deletion src/language-js/printer-estree.js
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
68 changes: 68 additions & 0 deletions tests/js/classes/__snapshots__/jsfmt.spec.js.snap
Original file line number Diff line number Diff line change
@@ -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"]
Expand Down
25 changes: 25 additions & 0 deletions tests/js/classes/assignment.js
Original file line number Diff line number Diff line change
@@ -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");
}
};

0 comments on commit bba196a

Please sign in to comment.