From 4e8f911a2914830ff0c8ac1cfeb71e53429a45a5 Mon Sep 17 00:00:00 2001 From: Lucas Duailibe Date: Mon, 10 Jun 2019 09:30:54 -0300 Subject: [PATCH] Print comment following a JSX generic (#6209) --- CHANGELOG.unreleased.md | 33 ++++++++++ package.json | 2 +- src/language-js/printer-estree.js | 5 +- .../__snapshots__/jsfmt.spec.js.snap | 62 +++++++++++++++++++ .../typescript_comments/after_jsx_generic.ts | 25 ++++++++ yarn.lock | 7 ++- 6 files changed, 129 insertions(+), 5 deletions(-) create mode 100644 tests/typescript_comments/after_jsx_generic.ts diff --git a/CHANGELOG.unreleased.md b/CHANGELOG.unreleased.md index 538a5fec..39590f47 100644 --- a/CHANGELOG.unreleased.md +++ b/CHANGELOG.unreleased.md @@ -43,3 +43,36 @@ const link = http://example.com; ``` --> + +#### TypeScript: Print comment following a JSX element with generic ([#6209] by [@duailibe]) + +Previous versions would not print this comment, this has been fixed in this version. + + +```ts +// Input +const comp = ( + + // This comment goes missing + value={4} + > + Test + +); + +// Output (Prettier stable) +const comp = value={4}>Test; + +// Output (Prettier master) +const comp = ( + + // This comment goes missing + value={4} + > + Test + +); +``` + +[#6209]: https://github.com/prettier/prettier/pull/6209 +[@duailibe]: https://github.com/duailibe diff --git a/package.json b/package.json index b8646dbe..7c588c3d 100644 --- a/package.json +++ b/package.json @@ -19,7 +19,7 @@ "@babel/parser": "7.2.0", "@glimmer/syntax": "0.38.4", "@iarna/toml": "2.2.3", - "@typescript-eslint/typescript-estree": "1.6.0", + "@typescript-eslint/typescript-estree": "1.10.2", "angular-estree-parser": "1.1.5", "angular-html-parser": "1.2.0", "camelcase": "4.1.0", diff --git a/src/language-js/printer-estree.js b/src/language-js/printer-estree.js index b56a59f9..3de46222 100644 --- a/src/language-js/printer-estree.js +++ b/src/language-js/printer-estree.js @@ -2154,7 +2154,10 @@ function printPathNoParens(path, options, print, args) { const n = path.getValue(); const nameHasComments = - n.name && n.name.comments && n.name.comments.length > 0; + (n.name && n.name.comments && n.name.comments.length > 0) || + (n.typeParameters && + n.typeParameters.comments && + n.typeParameters.comments.length > 0); // Don't break self-closing elements with no attributes and no comments if (n.selfClosing && !n.attributes.length && !nameHasComments) { diff --git a/tests/typescript_comments/__snapshots__/jsfmt.spec.js.snap b/tests/typescript_comments/__snapshots__/jsfmt.spec.js.snap index 7740e074..48f6cd13 100644 --- a/tests/typescript_comments/__snapshots__/jsfmt.spec.js.snap +++ b/tests/typescript_comments/__snapshots__/jsfmt.spec.js.snap @@ -28,6 +28,68 @@ abstract class AbstractRule { ================================================================================ `; +exports[`after_jsx_generic.ts 1`] = ` +====================================options===================================== +parsers: ["typescript"] +printWidth: 80 + | printWidth +=====================================input====================================== +let comp = ( + <> + /* comment1 */> + foo /* comment2 */> + /* comment3 */ bar> + foo /* comment4 */ bar> + + + // comment5 + > + + foo + // comment6 + > + + // comment7 + foo + > + + foo + // comment8 + bar + > + +); + +=====================================output===================================== +let comp = ( + <> + /* comment1 */> + foo /* comment2 */> + /* comment3 */ bar> + foo /* comment4 */ bar> + + + // comment5 + > + + foo + // comment6 + > + + // comment7 + foo + > + + foo + // comment8 + bar + > + +); + +================================================================================ +`; + exports[`jsx.ts 1`] = ` ====================================options===================================== parsers: ["typescript"] diff --git a/tests/typescript_comments/after_jsx_generic.ts b/tests/typescript_comments/after_jsx_generic.ts new file mode 100644 index 00000000..a3825662 --- /dev/null +++ b/tests/typescript_comments/after_jsx_generic.ts @@ -0,0 +1,25 @@ +let comp = ( + <> + /* comment1 */> + foo /* comment2 */> + /* comment3 */ bar> + foo /* comment4 */ bar> + + + // comment5 + > + + foo + // comment6 + > + + // comment7 + foo + > + + foo + // comment8 + bar + > + +); diff --git a/yarn.lock b/yarn.lock index 4f386f41..589a5125 100644 --- a/yarn.lock +++ b/yarn.lock @@ -782,9 +782,10 @@ version "5.5.0" resolved "https://registry.yarnpkg.com/@types/semver/-/semver-5.5.0.tgz#146c2a29ee7d3bae4bf2fcb274636e264c813c45" -"@typescript-eslint/typescript-estree@1.6.0": - version "1.6.0" - resolved "https://registry.yarnpkg.com/@typescript-eslint/typescript-estree/-/typescript-estree-1.6.0.tgz#6cf43a07fee08b8eb52e4513b428c8cdc9751ef0" +"@typescript-eslint/typescript-estree@1.10.2": + version "1.10.2" + resolved "https://registry.yarnpkg.com/@typescript-eslint/typescript-estree/-/typescript-estree-1.10.2.tgz#8403585dd74b6cfb6f78aa98b6958de158b5897b" + integrity sha512-Kutjz0i69qraOsWeI8ETqYJ07tRLvD9URmdrMoF10bG8y8ucLmPtSxROvVejWvlJUGl2et/plnMiKRDW+rhEhw== dependencies: lodash.unescape "4.0.1" semver "5.5.0"