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 auto-accessors with comments #14038

Merged
merged 3 commits into from Dec 29, 2022
Merged
Show file tree
Hide file tree
Changes from all 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
32 changes: 32 additions & 0 deletions changelog_unreleased/javascript/14038.md
@@ -0,0 +1,32 @@
#### Fix formatting for auto-accessors with comments (#14038 by @fisker)

<!-- prettier-ignore -->
```jsx
// Input
class A {
@dec()
// comment
accessor b;
}

// Prettier stable
class A {
@dec()
accessor // comment
b;
}

// Prettier stable (second format)
class A {
@dec()
accessor; // comment
b;
}

// Prettier main
class A {
@dec()
// comment
accessor b;
}
```
3 changes: 3 additions & 0 deletions src/language-js/comments.js
Expand Up @@ -453,6 +453,9 @@ const propertyLikeNodeTypes = new Set([
"TSAbstractMethodDefinition",
"TSDeclareMethod",
"MethodDefinition",
"ClassAccessorProperty",
"AccessorProperty",
"TSAbstractAccessorProperty",
Copy link
Sponsor Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This seems can't be tested, but other TSAbstract* are listed here.

]);
function handleMethodNameComments({
comment,
Expand Down
1 change: 1 addition & 0 deletions tests/config/format-test.js
Expand Up @@ -72,6 +72,7 @@ const meriyahDisabledTests = new Set([
"static.js",
"with-semicolon-1.js",
"with-semicolon-2.js",
"comments.js",
].map((filename) =>
path.join(__dirname, "../format/js/decorator-auto-accessors", filename)
),
Expand Down
Expand Up @@ -73,6 +73,91 @@ class Foo {
================================================================================
`;

exports[`comments.js [acorn] format 1`] = `
"Unexpected character '@' (2:3)
1 | class A {
> 2 | @dec()
| ^
3 | // comment
4 | accessor b;
5 | }"
`;

exports[`comments.js [espree] format 1`] = `
"Unexpected character '@' (2:3)
1 | class A {
> 2 | @dec()
| ^
3 | // comment
4 | accessor b;
5 | }"
`;

exports[`comments.js - {"semi":false} [acorn] format 1`] = `
"Unexpected character '@' (2:3)
1 | class A {
> 2 | @dec()
| ^
3 | // comment
4 | accessor b;
5 | }"
`;

exports[`comments.js - {"semi":false} [espree] format 1`] = `
"Unexpected character '@' (2:3)
1 | class A {
> 2 | @dec()
| ^
3 | // comment
4 | accessor b;
5 | }"
`;

exports[`comments.js - {"semi":false} format 1`] = `
====================================options=====================================
parsers: ["babel", "typescript", "babel-flow"]
printWidth: 80
semi: false
| printWidth
=====================================input======================================
class A {
@dec()
// comment
accessor b;
}

=====================================output=====================================
class A {
@dec()
// comment
accessor b
}

================================================================================
`;

exports[`comments.js format 1`] = `
====================================options=====================================
parsers: ["babel", "typescript", "babel-flow"]
printWidth: 80
| printWidth
=====================================input======================================
class A {
@dec()
// comment
accessor b;
}

=====================================output=====================================
class A {
@dec()
// comment
accessor b;
}

================================================================================
`;

exports[`computed.js [acorn] format 1`] = `
"Unexpected token (2:12)
1 | class Foo {
Expand Down
5 changes: 5 additions & 0 deletions tests/format/js/decorator-auto-accessors/comments.js
@@ -0,0 +1,5 @@
class A {
@dec()
// comment
accessor b;
}
2 changes: 2 additions & 0 deletions tests/format/js/decorator-auto-accessors/jsfmt.spec.js
Expand Up @@ -9,6 +9,7 @@ const errors = {
"static.js",
"with-semicolon-1.js",
"with-semicolon-2.js",
"comments.js",
],
acorn: [
"basic.js",
Expand All @@ -19,6 +20,7 @@ const errors = {
"static.js",
"with-semicolon-1.js",
"with-semicolon-2.js",
"comments.js",
],
};
run_spec(__dirname, parsers, { errors });
Expand Down