Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix formatting for AssignmentExpression with ClassExpression #9741

Merged
merged 5 commits into from Nov 22, 2020
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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");
}
};
```
19 changes: 12 additions & 7 deletions src/language-js/print/class.js
Expand Up @@ -131,13 +131,18 @@ 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([
ifBreak("("),
thorn0 marked this conversation as resolved.
Show resolved Hide resolved
indent(concat([softline, printed])),
softline,
ifBreak(")"),
thorn0 marked this conversation as resolved.
Show resolved Hide resolved
]),
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");
}
};