From 21c6d4f50199f4589d17be277f4d928e2d30534b Mon Sep 17 00:00:00 2001 From: Lucas Duailibe Date: Wed, 1 Nov 2017 13:56:04 -0200 Subject: [PATCH] Prevent inlining JSXAttribute if it has comments (#3132) --- src/printer.js | 28 +++++++----- tests/jsx/__snapshots__/jsfmt.spec.js.snap | 53 ++++++++++++++++++++++ tests/jsx/attr-comments.js | 24 ++++++++++ 3 files changed, 94 insertions(+), 11 deletions(-) create mode 100644 tests/jsx/attr-comments.js diff --git a/src/printer.js b/src/printer.js index fc8eb44d..5ab5b641 100644 --- a/src/printer.js +++ b/src/printer.js @@ -1717,18 +1717,24 @@ function genericPrintNoParens(path, options, print, args) { case "JSXExpressionContainer": { const parent = path.getParentNode(0); + const preventInline = + parent.type === "JSXAttribute" && + n.expression.comments && + n.expression.comments.length > 0; + const shouldInline = - n.expression.type === "ArrayExpression" || - n.expression.type === "ObjectExpression" || - n.expression.type === "ArrowFunctionExpression" || - n.expression.type === "CallExpression" || - n.expression.type === "FunctionExpression" || - n.expression.type === "JSXEmptyExpression" || - n.expression.type === "TemplateLiteral" || - n.expression.type === "TaggedTemplateExpression" || - (parent.type === "JSXElement" && - (n.expression.type === "ConditionalExpression" || - isBinaryish(n.expression))); + !preventInline && + (n.expression.type === "ArrayExpression" || + n.expression.type === "ObjectExpression" || + n.expression.type === "ArrowFunctionExpression" || + n.expression.type === "CallExpression" || + n.expression.type === "FunctionExpression" || + n.expression.type === "JSXEmptyExpression" || + n.expression.type === "TemplateLiteral" || + n.expression.type === "TaggedTemplateExpression" || + (parent.type === "JSXElement" && + (n.expression.type === "ConditionalExpression" || + isBinaryish(n.expression)))); if (shouldInline) { const printExpression = diff --git a/tests/jsx/__snapshots__/jsfmt.spec.js.snap b/tests/jsx/__snapshots__/jsfmt.spec.js.snap index 96fcb599..7a6deb57 100644 --- a/tests/jsx/__snapshots__/jsfmt.spec.js.snap +++ b/tests/jsx/__snapshots__/jsfmt.spec.js.snap @@ -1,5 +1,58 @@ // Jest Snapshot v1, https://goo.gl/fbAQLP +exports[`attr-comments.js 1`] = ` + fn(arg) + } + propArray={ + // comment + [el1, el2] + } + propObj={ + // comment + { key: val } + } + propTemplate={ + // comment + \`text\` + } +/>; +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + fn(arg) + } + propArray={ + // comment + [el1, el2] + } + propObj={ + // comment + { key: val } + } + propTemplate={ + // comment + \`text\` + } +/>; + +`; + exports[`conditional-expression.js 1`] = ` // There are two ways to print ConditionalExpressions: "normal mode" and // "JSX mode". This is normal mode (when breaking): diff --git a/tests/jsx/attr-comments.js b/tests/jsx/attr-comments.js new file mode 100644 index 00000000..1fc74d0a --- /dev/null +++ b/tests/jsx/attr-comments.js @@ -0,0 +1,24 @@ + fn(arg) + } + propArray={ + // comment + [el1, el2] + } + propObj={ + // comment + { key: val } + } + propTemplate={ + // comment + `text` + } +/>;