diff --git a/changelog_unreleased/javascript/14038.md b/changelog_unreleased/javascript/14038.md new file mode 100644 index 000000000000..b52a5aa34d96 --- /dev/null +++ b/changelog_unreleased/javascript/14038.md @@ -0,0 +1,32 @@ +#### Fix formatting for auto-accessors with comments (#14038 by @fisker) + + +```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; +} +``` diff --git a/src/language-js/comments.js b/src/language-js/comments.js index cb32c25a13f0..560c278192b9 100644 --- a/src/language-js/comments.js +++ b/src/language-js/comments.js @@ -453,6 +453,9 @@ const propertyLikeNodeTypes = new Set([ "TSAbstractMethodDefinition", "TSDeclareMethod", "MethodDefinition", + "ClassAccessorProperty", + "AccessorProperty", + "TSAbstractAccessorProperty", ]); function handleMethodNameComments({ comment, diff --git a/tests/config/format-test.js b/tests/config/format-test.js index 3574fddf1e40..90a11e752bbe 100644 --- a/tests/config/format-test.js +++ b/tests/config/format-test.js @@ -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) ), diff --git a/tests/format/js/decorator-auto-accessors/__snapshots__/jsfmt.spec.js.snap b/tests/format/js/decorator-auto-accessors/__snapshots__/jsfmt.spec.js.snap index a6850f56f69b..2263bc534b61 100644 --- a/tests/format/js/decorator-auto-accessors/__snapshots__/jsfmt.spec.js.snap +++ b/tests/format/js/decorator-auto-accessors/__snapshots__/jsfmt.spec.js.snap @@ -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 { diff --git a/tests/format/js/decorator-auto-accessors/comments.js b/tests/format/js/decorator-auto-accessors/comments.js new file mode 100644 index 000000000000..73640ec2a1a4 --- /dev/null +++ b/tests/format/js/decorator-auto-accessors/comments.js @@ -0,0 +1,5 @@ +class A { + @dec() + // comment + accessor b; +} diff --git a/tests/format/js/decorator-auto-accessors/jsfmt.spec.js b/tests/format/js/decorator-auto-accessors/jsfmt.spec.js index 85acc64686de..8c402a09ea0b 100644 --- a/tests/format/js/decorator-auto-accessors/jsfmt.spec.js +++ b/tests/format/js/decorator-auto-accessors/jsfmt.spec.js @@ -9,6 +9,7 @@ const errors = { "static.js", "with-semicolon-1.js", "with-semicolon-2.js", + "comments.js", ], acorn: [ "basic.js", @@ -19,6 +20,7 @@ const errors = { "static.js", "with-semicolon-1.js", "with-semicolon-2.js", + "comments.js", ], }; run_spec(__dirname, parsers, { errors });