From 09f147decd17cc2c95286d19ba0d6801a4b9a4e9 Mon Sep 17 00:00:00 2001 From: Christopher Chedeau Date: Wed, 7 Jun 2017 12:57:03 -0700 Subject: [PATCH] Do not throw for unprinted comments inside of JSX (#2036) Fixes #1990 --- src/comments.js | 7 ++++++ .../__snapshots__/jsfmt.spec.js.snap | 23 +++++++++++++++++++ tests/typescript_comments/jsx.ts | 7 ++++++ 3 files changed, 37 insertions(+) create mode 100644 tests/typescript_comments/jsx.ts diff --git a/src/comments.js b/src/comments.js index 638821068..515ed12d3 100644 --- a/src/comments.js +++ b/src/comments.js @@ -369,6 +369,13 @@ function addCommentHelper(node, comment) { const comments = node.comments || (node.comments = []); comments.push(comment); comment.printed = false; + + // For some reason, TypeScript parses `// x` inside of JSXText as a comment + // We already "print" it via the raw text, we don't need to re-print it as a + // comment + if (node.type === "JSXText") { + comment.printed = true; + } } function addLeadingComment(node, comment) { diff --git a/tests/typescript_comments/__snapshots__/jsfmt.spec.js.snap b/tests/typescript_comments/__snapshots__/jsfmt.spec.js.snap index 537599567..d4bfd9635 100644 --- a/tests/typescript_comments/__snapshots__/jsfmt.spec.js.snap +++ b/tests/typescript_comments/__snapshots__/jsfmt.spec.js.snap @@ -21,6 +21,29 @@ abstract class AbstractRule { `; +exports[`jsx.ts 1`] = ` +var example1 =
+ https://test +
; + +var example2 =
+ /*test*/ +
; +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +var example1 = ( +
+ https://test +
+); + +var example2 = ( +
+ /*test*/ +
+); + +`; + exports[`types.ts 1`] = ` (() => { // swallow error and fallback to using directory as path diff --git a/tests/typescript_comments/jsx.ts b/tests/typescript_comments/jsx.ts new file mode 100644 index 000000000..da5adae25 --- /dev/null +++ b/tests/typescript_comments/jsx.ts @@ -0,0 +1,7 @@ +var example1 =
+ https://test +
; + +var example2 =
+ /*test*/ +
;