From bb232d48a34027b3b0f4f2fd1e1ae3ab978c7c52 Mon Sep 17 00:00:00 2001 From: Christopher Chedeau Date: Fri, 5 May 2017 21:44:50 -0700 Subject: [PATCH] Inline chained conditionals inside of jsx attribute (#1519) Fixes #1515 --- src/printer.js | 3 ++ .../__snapshots__/jsfmt.spec.js.snap | 31 +++++++++++++++++++ tests/binary-expressions/jsx_parent.js | 13 ++++++++ tests/jsx/__snapshots__/jsfmt.spec.js.snap | 4 +-- 4 files changed, 49 insertions(+), 2 deletions(-) create mode 100644 tests/binary-expressions/jsx_parent.js diff --git a/src/printer.js b/src/printer.js index b924eff8..d15644ce 100644 --- a/src/printer.js +++ b/src/printer.js @@ -196,6 +196,7 @@ function genericPrintNoParens(path, options, print, args) { case "BinaryExpression": case "LogicalExpression": { const parent = path.getParentNode(); + const parentParent = path.getParentNode(1); const isInsideParenthesis = n !== parent.body && (parent.type === "IfStatement" || @@ -230,6 +231,8 @@ function genericPrintNoParens(path, options, print, args) { parent.type === "VariableDeclarator" || shouldInlineLogicalExpression(n) || parent.type === "ReturnStatement" || + (parent.type === "JSXExpressionContainer" && + parentParent.type === "JSXAttribute") || (n === parent.body && parent.type === "ArrowFunctionExpression") || (n !== parent.body && parent.type === "ForStatement") ) { diff --git a/tests/binary-expressions/__snapshots__/jsfmt.spec.js.snap b/tests/binary-expressions/__snapshots__/jsfmt.spec.js.snap index 8d9f16e5..f3067ee1 100644 --- a/tests/binary-expressions/__snapshots__/jsfmt.spec.js.snap +++ b/tests/binary-expressions/__snapshots__/jsfmt.spec.js.snap @@ -120,6 +120,37 @@ this.steps = steps || [ `; +exports[`jsx_parent.js 1`] = ` +
; + +
+ {!isJellyfishEnabled && + diffUpdateMessageInput != null && + this.state.isUpdateMessageEmpty} +
; +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +
; + +
+ {!isJellyfishEnabled && + diffUpdateMessageInput != null && + this.state.isUpdateMessageEmpty} +
; + +`; + exports[`short-right.js 1`] = ` this._cumulativeHeights && Math.abs( diff --git a/tests/binary-expressions/jsx_parent.js b/tests/binary-expressions/jsx_parent.js new file mode 100644 index 00000000..0339b514 --- /dev/null +++ b/tests/binary-expressions/jsx_parent.js @@ -0,0 +1,13 @@ +
; + +
+ {!isJellyfishEnabled && + diffUpdateMessageInput != null && + this.state.isUpdateMessageEmpty} +
; diff --git a/tests/jsx/__snapshots__/jsfmt.spec.js.snap b/tests/jsx/__snapshots__/jsfmt.spec.js.snap index 1b58cc4f..55566cf8 100644 --- a/tests/jsx/__snapshots__/jsfmt.spec.js.snap +++ b/tests/jsx/__snapshots__/jsfmt.spec.js.snap @@ -118,12 +118,12 @@ exports[`expression.js 1`] = ` key={option} imageSource={ this.props.veryBigItemImageSourceFunc && - this.props.veryBigItemImageSourceFunc(option) + this.props.veryBigItemImageSourceFunc(option) } imageSize={this.props.veryBigItemImageSize} imageView={ this.props.veryBigItemImageViewFunc && - this.props.veryBigItemImageViewFunc(option) + this.props.veryBigItemImageViewFunc(option) } heading={this.props.displayTextFunc(option)} value={option}