Skip to content

Commit

Permalink
Fix formatting for auto-accessors with comments (#14038)
Browse files Browse the repository at this point in the history
Fixes #14036
  • Loading branch information
fisker committed Dec 29, 2022
1 parent d798b55 commit ef707da
Show file tree
Hide file tree
Showing 6 changed files with 128 additions and 0 deletions.
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",
]);
function handleMethodNameComments({
comment,
Expand Down
1 change: 1 addition & 0 deletions tests/config/format-test.js
Expand Up @@ -73,6 +73,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

0 comments on commit ef707da

Please sign in to comment.