Break nested ternaries#4120
Conversation
|
I think I like this change. Perhaps we should try it out on some real project first. I can do that. |
|
Yeah, I was waiting already on some more approvals (for consensus) and will try on some codebases at work and some OS projects. |
|
@vjeux do you have an opinion? Are you concerned about impact on the facebook codebase? |
|
I thought this was the current behavior. I definitely implemented this feature in the past, I guess that was for a test and I didn't actually push it. I'm onboard with it. Would be great to run it on some real code first though as mentioned in the comments to see the real impact it has. |
|
Here are the diffs I got on 51K source lines of JS: const rangeStartIndex =
$rangeStart.length > 0
? $allCells.index($rangeStart)
- : date < this.startDate ? $allCells.length : 0;
+ : date < this.startDate
+ ? $allCells.length
+ : 0; >
{submitState.state === constants.SUBMIT_STATES.SUBMITTING
? 'Saving…'
- : dirty ? 'Save your changes' : '(Nothing to save)'}
+ : dirty
+ ? 'Save your changes'
+ : '(Nothing to save)'}
</button> function sortRegions(selectedRegionId, regionA, regionB) {
return regionA.id === selectedRegionId
? 1
- : regionB.id === selectedRegionId ? -1 : 0;
+ : regionB.id === selectedRegionId
+ ? -1
+ : 0;
} const sign = line.y
- ? point.x < targetPoint.x ? 1 : -1
- : point.y < targetPoint.y ? 1 : -1;
+ ? point.x < targetPoint.x
+ ? 1
+ : -1
+ : point.y < targetPoint.y
+ ? 1
+ : -1;
return movePointAlongLine(line, point, distance * sign);
} const previewPositionId =
selectedPositionIds.length > 0
? selectedPositionIds[0]
- : selectableIds.length > 0 ? selectableIds[0] : null;
+ : selectableIds.length > 0
+ ? selectableIds[0]
+ : null; onClick={
isUnselectedAndEnabled
? this.onUnselectedControlsClick.bind(this)
- : enabled ? null : onDisabledClick
+ : enabled
+ ? null
+ : onDisabledClick
}
> const widthAdjustment = mainConstants.isInternetExplorer
? 1
- : mainConstants.isEdge ? 0.5 : 0;
+ : mainConstants.isEdge
+ ? 0.5
+ : 0; textAnchor={
date.getTime() === xStart.getTime()
? "start"
- : date.getTime() === xEnd.getTime() ? "end" : "middle"
+ : date.getTime() === xEnd.getTime()
+ ? "end"
+ : "middle"
} const finalWords =
bestWords.length > 1
? bestWords
- : preferredWords.length > 0 ? preferredWords : words;
+ : preferredWords.length > 0
+ ? preferredWords
+ : words; subText={
latestRound
? maybeDate(latestUser.added_on)
- : shimmer ? "" : null
+ : shimmer
+ ? ""
+ : null
} >
{selectedAccount.type === "existing"
- ? success ? "Account linked" : "Link existing Account"
+ ? success
+ ? "Account linked"
+ : "Link existing Account"
: success
? "Account created and linked"
: "Create and link Account"} compareByNumber(
({ dateCreated, closeDate }) =>
dateCreated != null && closeDate != null
- ? closeDate === ONGOING_ENTRY_DATE ? 0 : 1
+ ? closeDate === ONGOING_ENTRY_DATE
+ ? 0
+ : 1
: 2,
), |
|
The only one that I think looks better without the change is: const sign = line.y
- ? point.x < targetPoint.x ? 1 : -1
- : point.y < targetPoint.y ? 1 : -1;
+ ? point.x < targetPoint.x
+ ? 1
+ : -1
+ : point.y < targetPoint.y
+ ? 1
+ : -1;
return movePointAlongLine(line, point, distance * sign);
}But I think all the other diffs are an improvement, so it's fine. |
|
I agree. |
|
I finally ran on some repos, the most relevant changes I found on the react repo (there were more, only showing the ones that are "different"): diff --git a/packages/react-dom/src/events/SyntheticWheelEvent.js b/packages/react-dom/src/events/SyntheticWheelEvent.js
index b4c49f4..16435fe 100644
--- a/packages/react-dom/src/events/SyntheticWheelEvent.js
+++ b/packages/react-dom/src/events/SyntheticWheelEvent.js
@@ -16,7 +16,9 @@ const SyntheticWheelEvent = SyntheticMouseEvent.extend({
return 'deltaX' in event
? event.deltaX
: // Fallback to `wheelDeltaX` for Webkit and normalize (right is positive).
- 'wheelDeltaX' in event ? -event.wheelDeltaX : 0;
+ 'wheelDeltaX' in event
+ ? -event.wheelDeltaX
+ : 0;
},
deltaY(event) {
return 'deltaY' in event
@@ -25,7 +27,9 @@ const SyntheticWheelEvent = SyntheticMouseEvent.extend({
'wheelDeltaY' in event
? -event.wheelDeltaY
: // Fallback to `wheelDelta` for IE<9 and normalize (down is positive).
- 'wheelDelta' in event ? -event.wheelDelta : 0;
+ 'wheelDelta' in event
+ ? -event.wheelDelta
+ : 0;
},
deltaZ: null,
diff --git a/packages/shared/describeComponentFrame.js b/packages/shared/describeComponentFrame.js
index 3494c31..5c619e6 100644
--- a/packages/shared/describeComponentFrame.js
+++ b/packages/shared/describeComponentFrame.js
@@ -21,6 +21,8 @@ export default function(
':' +
source.lineNumber +
')'
- : ownerName ? ' (created by ' + ownerName + ')' : '')
+ : ownerName
+ ? ' (created by ' + ownerName + ')'
+ : '')
);
} |
|
Those are basically this change: Prettier pr-4120 Input: return (
(a || b) &&
(cond
? alternate
: //comment
consequent ? is : nested)
);Output: return (
(a || b) &&
(cond
? alternate
: //comment
consequent
? is
: nested)
);Thoughts? |
|
I'm calling it |
|
Boom! |
We'll reuse the same logic from JSX mode to only group at the outermost ternary, forcing all ternaries in a chain to break if any of them break.
Closes #3018