From ea4a5fc30a3f43d65cc3519f1adb5a5e6d9175f5 Mon Sep 17 00:00:00 2001 From: fisker Cheung Date: Thu, 29 Dec 2022 19:06:50 +0800 Subject: [PATCH 1/5] Fix comments inside JSX attributes --- tests/config/format-test.js | 1 + tests/format/jsx/comments/in-tags.js | 43 ++++++++++++++++++++++++++++ 2 files changed, 44 insertions(+) diff --git a/tests/config/format-test.js b/tests/config/format-test.js index c5703e99ea36..cbcdd244f769 100644 --- a/tests/config/format-test.js +++ b/tests/config/format-test.js @@ -42,6 +42,7 @@ const unstableTests = new Map( "js/for/continue-and-break-comment-without-blocks.js", "typescript/satisfies-operators/comments-unstable.ts", ["js/identifier/parentheses/let.js", (options) => options.semi === false], + "jsx/comments/in-tags.js", ].map((fixture) => { const [file, isUnstable = () => true] = Array.isArray(fixture) ? fixture diff --git a/tests/format/jsx/comments/in-tags.js b/tests/format/jsx/comments/in-tags.js index e06914134ef6..5bb78d6096e7 100644 --- a/tests/format/jsx/comments/in-tags.js +++ b/tests/format/jsx/comments/in-tags.js @@ -26,3 +26,46 @@
; + +
+ {foo} +
; + +
+ {foo} +
; + +
+ {foo} +
; + +
+ {foo} +
; + +
+ {foo} +
; + +
+ {foo} +
; From bd46a210c0c97300943746839a83fdc78120ecfd Mon Sep 17 00:00:00 2001 From: fisker Cheung Date: Thu, 29 Dec 2022 19:10:55 +0800 Subject: [PATCH 2/5] Move tests --- src/language-js/print/jsx.js | 9 ++- tests/config/format-test.js | 2 +- .../comments/__snapshots__/jsfmt.spec.js.snap | 76 +++++++++++++++++++ tests/format/jsx/comments/in-attributes.js | 44 +++++++++++ tests/format/jsx/comments/in-tags.js | 43 ----------- 5 files changed, 128 insertions(+), 46 deletions(-) create mode 100644 tests/format/jsx/comments/in-attributes.js diff --git a/src/language-js/print/jsx.js b/src/language-js/print/jsx.js index fe18414a95cf..db4a6849705b 100644 --- a/src/language-js/print/jsx.js +++ b/src/language-js/print/jsx.js @@ -3,6 +3,7 @@ const { printComments, printDanglingComments, + printCommentsSeparately, } = require("../../main/comments.js"); const { builders: { @@ -490,7 +491,11 @@ function printJsxAttribute(path, options, print) { options.jsxSingleQuote ? "'" : '"' ); final = final.replace(regex, escaped); - res = [quote, final, quote]; + const { leading, trailing } = path.call( + () => printCommentsSeparately(path, options), + "value" + ); + res = [leading, quote, final, quote, trailing]; } else { res = print("value"); } @@ -766,7 +771,7 @@ function printJsx(path, options, print) { return printJsxEmptyExpression(path, options /*, print*/); case "JSXText": /* istanbul ignore next */ - throw new Error("JSXTest should be handled by JSXElement"); + throw new Error("JSXText should be handled by JSXElement"); default: /* istanbul ignore next */ throw new Error(`Unknown JSX node type: ${JSON.stringify(node.type)}.`); diff --git a/tests/config/format-test.js b/tests/config/format-test.js index cbcdd244f769..3a084702fb25 100644 --- a/tests/config/format-test.js +++ b/tests/config/format-test.js @@ -42,7 +42,7 @@ const unstableTests = new Map( "js/for/continue-and-break-comment-without-blocks.js", "typescript/satisfies-operators/comments-unstable.ts", ["js/identifier/parentheses/let.js", (options) => options.semi === false], - "jsx/comments/in-tags.js", + "jsx/comments/in-attributes.js", ].map((fixture) => { const [file, isUnstable = () => true] = Array.isArray(fixture) ? fixture diff --git a/tests/format/jsx/comments/__snapshots__/jsfmt.spec.js.snap b/tests/format/jsx/comments/__snapshots__/jsfmt.spec.js.snap index 498158e46ded..c042a5aaa88c 100644 --- a/tests/format/jsx/comments/__snapshots__/jsfmt.spec.js.snap +++ b/tests/format/jsx/comments/__snapshots__/jsfmt.spec.js.snap @@ -27,6 +27,82 @@ const render = (items) => ( ================================================================================ `; +exports[`in-attributes.js - {"bracketSameLine":true} format 1`] = ` +====================================options===================================== +bracketSameLine: true +parsers: ["flow", "babel", "typescript"] +printWidth: 80 + | printWidth +=====================================input====================================== + + +
+ {foo} +
; + +
+ {foo} +
; + +
+ {foo} +
; + +
+ {foo} +
; + +
+ {foo} +
; + +
+ {foo} +
; + +=====================================output===================================== +
{foo}
; + +
+ {foo} +
; + +
{foo}
; + +
{foo}
; + +
+ {foo} +
; + +
// comment + {foo} +
; + +================================================================================ +`; + exports[`in-end-tag.js - {"bracketSameLine":true} [typescript] format 1`] = ` "Identifier expected. (2:6) 1 | /* =========== before slash =========== */ diff --git a/tests/format/jsx/comments/in-attributes.js b/tests/format/jsx/comments/in-attributes.js new file mode 100644 index 000000000000..795a2be4fa47 --- /dev/null +++ b/tests/format/jsx/comments/in-attributes.js @@ -0,0 +1,44 @@ + + +
+ {foo} +
; + +
+ {foo} +
; + +
+ {foo} +
; + +
+ {foo} +
; + +
+ {foo} +
; + +
+ {foo} +
; diff --git a/tests/format/jsx/comments/in-tags.js b/tests/format/jsx/comments/in-tags.js index 5bb78d6096e7..e06914134ef6 100644 --- a/tests/format/jsx/comments/in-tags.js +++ b/tests/format/jsx/comments/in-tags.js @@ -26,46 +26,3 @@
; - -
- {foo} -
; - -
- {foo} -
; - -
- {foo} -
; - -
- {foo} -
; - -
- {foo} -
; - -
- {foo} -
; From dfe76f41235edbdf620c6ebcaf2230374dbd323c Mon Sep 17 00:00:00 2001 From: fisker Cheung Date: Thu, 29 Dec 2022 19:13:41 +0800 Subject: [PATCH 3/5] Simplify tests --- .../comments/__snapshots__/jsfmt.spec.js.snap | 44 +++++-------------- tests/format/jsx/comments/in-attributes.js | 26 +++-------- 2 files changed, 18 insertions(+), 52 deletions(-) diff --git a/tests/format/jsx/comments/__snapshots__/jsfmt.spec.js.snap b/tests/format/jsx/comments/__snapshots__/jsfmt.spec.js.snap index c042a5aaa88c..6a7ba0281317 100644 --- a/tests/format/jsx/comments/__snapshots__/jsfmt.spec.js.snap +++ b/tests/format/jsx/comments/__snapshots__/jsfmt.spec.js.snap @@ -34,71 +34,51 @@ parsers: ["flow", "babel", "typescript"] printWidth: 80 | printWidth =====================================input====================================== - -
- {foo} -
; +>;
- {foo} -
; +>;
- {foo} -
; +>;
- {foo} -
; +>;
- {foo} -
; +>;
- {foo} -
; +>; =====================================output===================================== -
{foo}
; +
;
- {foo} -
; +"foo">; -
{foo}
; +
; -
{foo}
; +
;
- {foo} -
; +"foo">; -
// comment - {foo} -
; +
; // comment ================================================================================ `; diff --git a/tests/format/jsx/comments/in-attributes.js b/tests/format/jsx/comments/in-attributes.js index 795a2be4fa47..4b3565c387e8 100644 --- a/tests/format/jsx/comments/in-attributes.js +++ b/tests/format/jsx/comments/in-attributes.js @@ -1,44 +1,30 @@ - -
- {foo} -
; +>;
- {foo} -
; +>;
- {foo} -
; +>;
- {foo} -
; +>;
- {foo} -
; +>;
- {foo} -
; +>; From 7d861068290159b73a11400d0991789a635741a5 Mon Sep 17 00:00:00 2001 From: fisker Cheung Date: Thu, 29 Dec 2022 19:16:24 +0800 Subject: [PATCH 4/5] Add changelog --- changelog_unreleased/javascript/14082.md | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) create mode 100644 changelog_unreleased/javascript/14082.md diff --git a/changelog_unreleased/javascript/14082.md b/changelog_unreleased/javascript/14082.md new file mode 100644 index 000000000000..c2674375250f --- /dev/null +++ b/changelog_unreleased/javascript/14082.md @@ -0,0 +1,17 @@ +#### Fix formatting for comments inside JSX attribute (#auto-accessors with by @fisker) + + +```jsx +// Input +function MyFunctionComponent() { + +} + +// Prettier stable +Error: Comment "old" was not printed. Please report this error! + +// Prettier main +function MyFunctionComponent() { + ; +} +``` From 4095a94a5d38dcf04a34e0721994bba880daba93 Mon Sep 17 00:00:00 2001 From: fisker Cheung Date: Thu, 29 Dec 2022 19:28:51 +0800 Subject: [PATCH 5/5] Fix changelog --- changelog_unreleased/javascript/14082.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/changelog_unreleased/javascript/14082.md b/changelog_unreleased/javascript/14082.md index c2674375250f..e56693154c5a 100644 --- a/changelog_unreleased/javascript/14082.md +++ b/changelog_unreleased/javascript/14082.md @@ -1,4 +1,4 @@ -#### Fix formatting for comments inside JSX attribute (#auto-accessors with by @fisker) +#### Fix formatting for comments inside JSX attribute (#14082 with by @fisker) ```jsx