From c0aaa0fa523a0cd4cf112fa5e53efe35a62c454b Mon Sep 17 00:00:00 2001 From: Lucas Azzola Date: Mon, 23 Oct 2017 17:13:12 +1100 Subject: [PATCH] Don't print JSX bracket on same line when it has trailing comments (#3088) --- src/printer.js | 12 ++++++++++-- tests/comments/__snapshots__/jsfmt.spec.js.snap | 13 +++++++++++++ tests/comments/jsx.js | 7 +++++++ .../__snapshots__/jsfmt.spec.js.snap | 16 ++++++++++++++++ tests/comments_jsx_same_line/jsfmt.spec.js | 1 + tests/comments_jsx_same_line/jsx_same_line.js | 5 +++++ 6 files changed, 52 insertions(+), 2 deletions(-) create mode 100644 tests/comments_jsx_same_line/__snapshots__/jsfmt.spec.js.snap create mode 100644 tests/comments_jsx_same_line/jsfmt.spec.js create mode 100644 tests/comments_jsx_same_line/jsx_same_line.js diff --git a/src/printer.js b/src/printer.js index 336cec09c..d57947a64 100644 --- a/src/printer.js +++ b/src/printer.js @@ -1774,6 +1774,14 @@ function genericPrintNoParens(path, options, print, args) { ); } + const bracketSameLine = + options.jsxBracketSameLine && + !( + n.name && + ((n.name.trailingComments && n.name.trailingComments.length) || + (n.name.comments && n.name.comments.length)) + ); + return group( concat([ "<", @@ -1784,9 +1792,9 @@ function genericPrintNoParens(path, options, print, args) { path.map(attr => concat([line, print(attr)]), "attributes") ) ), - n.selfClosing ? line : options.jsxBracketSameLine ? ">" : softline + n.selfClosing ? line : bracketSameLine ? ">" : softline ]), - n.selfClosing ? "/>" : options.jsxBracketSameLine ? "" : ">" + n.selfClosing ? "/>" : bracketSameLine ? "" : ">" ]) ); } diff --git a/tests/comments/__snapshots__/jsfmt.spec.js.snap b/tests/comments/__snapshots__/jsfmt.spec.js.snap index b00a46528..0f462d487 100644 --- a/tests/comments/__snapshots__/jsfmt.spec.js.snap +++ b/tests/comments/__snapshots__/jsfmt.spec.js.snap @@ -838,6 +838,13 @@ onClick={() => {}}> ; +
+ {foo} +
; + + {} @@ -916,6 +923,12 @@ onClick={() => {}}> onClick={() => {}} />; +
+ {foo} +
; + {} diff --git a/tests/comments/jsx.js b/tests/comments/jsx.js index f135ccc1b..bff243611 100644 --- a/tests/comments/jsx.js +++ b/tests/comments/jsx.js @@ -89,6 +89,13 @@ onClick={() => {}}> ; +
+ {foo} +
; + + {} diff --git a/tests/comments_jsx_same_line/__snapshots__/jsfmt.spec.js.snap b/tests/comments_jsx_same_line/__snapshots__/jsfmt.spec.js.snap new file mode 100644 index 000000000..a6bd40911 --- /dev/null +++ b/tests/comments_jsx_same_line/__snapshots__/jsfmt.spec.js.snap @@ -0,0 +1,16 @@ +// Jest Snapshot v1, https://goo.gl/fbAQLP + +exports[`jsx_same_line.js 1`] = ` +
+ {foo} +
; +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +
+ {foo} +
; + +`; diff --git a/tests/comments_jsx_same_line/jsfmt.spec.js b/tests/comments_jsx_same_line/jsfmt.spec.js new file mode 100644 index 000000000..d0c9c5f94 --- /dev/null +++ b/tests/comments_jsx_same_line/jsfmt.spec.js @@ -0,0 +1 @@ +run_spec(__dirname, { jsxBracketSameLine: true }, ["babylon", "typescript"]); diff --git a/tests/comments_jsx_same_line/jsx_same_line.js b/tests/comments_jsx_same_line/jsx_same_line.js new file mode 100644 index 000000000..ed1fb27e1 --- /dev/null +++ b/tests/comments_jsx_same_line/jsx_same_line.js @@ -0,0 +1,5 @@ +
+ {foo} +
;