Consistent indentations for conditional operators#10187
Consistent indentations for conditional operators#10187thorn0 merged 35 commits intoprettier:mainfrom
Conversation
Co-authored-by: fisker Cheung <[email protected]>
|
What about binary expressions? I don't think this change is intended for this PR. Prettier pr-10187 Output: bifornCringerMoshedPerplexSawder =
askTrovenaBeenaDependsRowans +
((
glimseGlyphsHazardNoopsTieTie === 0
? averredBathersBoxroomBuggyNurl
: anodyneCondosMalateOverateRetinol
) as AnnularCooeedSplicesWalksWayWay);
bifornCringerMoshedPerplexSawder =
askTrovenaBeenaDependsRowans +
((
glimseGlyphsHazardNoopsTieTie === 0 &&
kochabCooieGameOnOboleUnweave === Math.PI
? averredBathersBoxroomBuggyNurl
: anodyneCondosMalateOverateRetinol
) as AnnularCooeedSplicesWalksWayWay);Prettier 2.2.1 Output: bifornCringerMoshedPerplexSawder =
askTrovenaBeenaDependsRowans +
((glimseGlyphsHazardNoopsTieTie === 0
? averredBathersBoxroomBuggyNurl
: anodyneCondosMalateOverateRetinol) as AnnularCooeedSplicesWalksWayWay);
bifornCringerMoshedPerplexSawder =
askTrovenaBeenaDependsRowans +
((glimseGlyphsHazardNoopsTieTie === 0 &&
kochabCooieGameOnOboleUnweave === Math.PI
? averredBathersBoxroomBuggyNurl
: anodyneCondosMalateOverateRetinol) as AnnularCooeedSplicesWalksWayWay); |
src/language-js/print/ternary.js
Outdated
| ) { | ||
| let memberChainingRootNodeName = parentName; | ||
| let count = 0; | ||
| while (memberChainingRootNodeName === "object") { |
There was a problem hiding this comment.
Member chains also include calls:
Prettier pr-10187
Playground link
--parser typescriptInput:
bifornCringerMoshedPerplexSawder = (
glimseGlyphsHazardNoopsTieTie === 0 &&
kochabCooieGameOnOboleUnweave === Math.PI
? averredBathersBoxroomBuggyNurl
: anodyneCondosMalateOverateRetinol
).annularCooeedSplicesWalksWayWay.annularCooeedSplicesWalksWayWay(
annularCooeedSplicesWalksWayWay
).annularCooeedSplicesWalksWayWay();Output:
bifornCringerMoshedPerplexSawder = (glimseGlyphsHazardNoopsTieTie === 0 &&
kochabCooieGameOnOboleUnweave === Math.PI
? averredBathersBoxroomBuggyNurl
: anodyneCondosMalateOverateRetinol
).annularCooeedSplicesWalksWayWay
.annularCooeedSplicesWalksWayWay(annularCooeedSplicesWalksWayWay)
.annularCooeedSplicesWalksWayWay();as.|
Also member chains might include |
src/language-js/print/ternary.js
Outdated
| return printed; | ||
| } | ||
|
|
||
| const rightSideNodeNamesSet = new Set(["right", "init", "argument"]); |
There was a problem hiding this comment.
If I didn't misunderstand this, we are checking NodeA is NodeB's .right or .init, but not checking NodeB's type. I don't like this part, this may lead to unexpect cases.
For example argument, I guess you mean to check argument of ReturnStatement/YieldExpression etc, let's list them.
src/language-js/print/ternary.js
Outdated
| * )(arguments); | ||
| */ | ||
| if ( | ||
| parent.type === "CallExpression" && |
There was a problem hiding this comment.
OptionalCallExpression too?
Prettier pr-10187
Playground link
--parser babelInput:
function foo22() {
return (
coooooooooooooooooooooooooooooooooooooooooooooooooooond
? baaaaaaaaaaaaaaaaaaaaar
: baaaaaaaaaaaaaaaaaaaaaz
)(Fooooooooooo.Fooooooooooo);
return (
coooooooooooooooooooooooooooooooooooooooooooooooooooond
? baaaaaaaaaaaaaaaaaaaaar
: baaaaaaaaaaaaaaaaaaaaaz
)?.(Fooooooooooo.Fooooooooooo);
}Output:
function foo22() {
return (
coooooooooooooooooooooooooooooooooooooooooooooooooooond
? baaaaaaaaaaaaaaaaaaaaar
: baaaaaaaaaaaaaaaaaaaaaz
)(Fooooooooooo.Fooooooooooo);
return (coooooooooooooooooooooooooooooooooooooooooooooooooooond
? baaaaaaaaaaaaaaaaaaaaar
: baaaaaaaaaaaaaaaaaaaaaz)?.(Fooooooooooo.Fooooooooooo);
}
src/language-js/print/ternary.js
Outdated
| if ( | ||
| (parent.type === "CallExpression" || | ||
| parent.type === "OptionalCallExpression") && | ||
| checkAncestor(parent) && |
There was a problem hiding this comment.
Can't you reuse ancestorCount here instead of climbing up the path one more time in getChainRoot?
Also what about the case where parent.type === "TSNonNullExpression"?
There was a problem hiding this comment.
I meant to write this under parent.type === "MemberExpression" || parent.type === "OptionalMemberExpression". But maybe you should do the chain root check here too? After all, the search for ancestor above considers calls chain elements.
There was a problem hiding this comment.
I was able to get rid of the getChainRoot function by reusing the ancestorCount, thanks!
src/language-js/print/ternary.js
Outdated
| return true; | ||
| } | ||
|
|
||
| const chainRoot = path.getParentNode(ancestorCount - 1); |
There was a problem hiding this comment.
The rest part in this function seems just CallExpression.callee, MemberExpression.object, and TSNonNullExpression.object? Code like this should work?
if (
isChainElement(parent) &&
checkAncestor(chainRoot) &&
(
(isCallExpression() && name === "callee") ||
(isMemberExpression() && name === "object") ||
(parent.type === 'TSNonNullExpression' && name === "object") ||
)
)Note: name === "object" was not checking, we should add tests for them.
|
@thorn0 Do you think this fixes your old issue #4976? Your comment in |
src/language-js/print/ternary.js
Outdated
|
|
||
| const chainRoot = path.getParentNode(ancestorCount - 1); | ||
| if ( | ||
| isChainElement(parent) && |
There was a problem hiding this comment.
isChainElement check is not needed anymore, following codition already ensure it.
There was a problem hiding this comment.
And please tweak the conditions order a little bit. check name first and check parent first.
src/language-js/print/ternary.js
Outdated
| * : second | ||
| * ) as SomeType; | ||
| */ | ||
| if (parent.type === "TSAsExpression" && checkAncestor(parent)) { |
There was a problem hiding this comment.
Let's merge TSAsExpression and NewExpression too,
if (
(
(name === "callee" && parent.type === "NewExpression") ||
(name === "expression" && parent.type === "TSAsExpression")
) &&
checkAncestor(parent)
)Once again, name === "expression" not checked, TSAsExpression has two children, seems typeAnnotation can't be ConditionalExpression, but let's add it to be safer.
src/language-js/print/ternary.js
Outdated
| const chainRoot = path.getParentNode(ancestorCount - 1); | ||
| if ( | ||
| isChainElement(parent) && | ||
| checkAncestor(chainRoot) && | ||
| ((isCallExpression(parent) && name === "callee") || | ||
| (isMemberExpression(parent) && name === "object") || | ||
| (parent.type === "TSNonNullExpression" && name === "expression")) | ||
| ((name === "callee" && isCallExpression(parent)) || | ||
| (name === "object" && isMemberExpression(parent)) || | ||
| (name === "expression" && parent.type === "TSNonNullExpression")) | ||
| ) { | ||
| return true; | ||
| } |
There was a problem hiding this comment.
| const chainRoot = path.getParentNode(ancestorCount - 1); | |
| if ( | |
| isChainElement(parent) && | |
| checkAncestor(chainRoot) && | |
| ((isCallExpression(parent) && name === "callee") || | |
| (isMemberExpression(parent) && name === "object") || | |
| (parent.type === "TSNonNullExpression" && name === "expression")) | |
| ((name === "callee" && isCallExpression(parent)) || | |
| (name === "object" && isMemberExpression(parent)) || | |
| (name === "expression" && parent.type === "TSNonNullExpression")) | |
| ) { | |
| return true; | |
| } | |
| if ( | |
| ((name === "callee" && isCallExpression(parent)) || | |
| (name === "object" && isMemberExpression(parent)) || | |
| (name === "expression" && parent.type === "TSNonNullExpression")) && | |
| checkAncestor(path.getParentNode(ancestorCount - 1)) | |
| ) { | |
| return true; | |
| } |
This reduces one getParentNode() call.
|
Can we merge this? |
|
Oops. This inconsistency doesn't seem to be intended. Prettier pr-10187 --parser babelInput: bifornCringerMoshedPerplexSawder = fn((
glimseGlyphsHazardNoopsTieTie === 0
? averredBathersBoxroomBuggyNurl
: anodyneCondosMalateOverateRetinol
).prop);
fn((
glimseGlyphsHazardNoopsTieTie === 0
? averredBathersBoxroomBuggyNurl
: anodyneCondosMalateOverateRetinol
).prop);Output: bifornCringerMoshedPerplexSawder = fn(
(
glimseGlyphsHazardNoopsTieTie === 0
? averredBathersBoxroomBuggyNurl
: anodyneCondosMalateOverateRetinol
).prop
);
fn(
(glimseGlyphsHazardNoopsTieTie === 0
? averredBathersBoxroomBuggyNurl
: anodyneCondosMalateOverateRetinol
).prop
); |
|
Ooops. I didn't notice, I'll create new issue for this. |
Description
Fixes #8840
Fixes #4976
Checklist
changelog_unreleased/*/XXXX.mdfile followingchangelog_unreleased/TEMPLATE.md.✨Try the playground for this PR✨