diff --git a/changelog_unreleased/javascript/10187.md b/changelog_unreleased/javascript/10187.md index 30714a50e..86db16462 100644 --- a/changelog_unreleased/javascript/10187.md +++ b/changelog_unreleased/javascript/10187.md @@ -1,4 +1,4 @@ -#### Consistent indentations for conditional operators (#10187 by @sosukesuzuki) +#### Consistent indentations for conditional operators (#10187, #10266 by @sosukesuzuki) ```ts diff --git a/src/language-js/print/ternary.js b/src/language-js/print/ternary.js index cfad3acd8..c4d5e792e 100644 --- a/src/language-js/print/ternary.js +++ b/src/language-js/print/ternary.js @@ -6,7 +6,6 @@ const { isJsxNode, isBlockComment, getComments, - isChainElement, isCallExpression, isMemberExpression, } = require("../utils"); @@ -174,40 +173,39 @@ function shouldExtraIndentForConditionalExpression(path) { return false; } - let ancestorCount = 1; - let ancestor = path.getParentNode(ancestorCount); - if (!ancestor) { + let parent; + let child = node; + for (let ancestorCount = 0; !parent; ancestorCount++) { + const node = path.getParentNode(ancestorCount); + + if ( + (isCallExpression(node) && node.callee === child) || + (isMemberExpression(node) && node.object === child) || + (node.type === "TSNonNullExpression" && node.expression === child) + ) { + child = node; + continue; + } + + // Reached chain root + + if ( + (node.type === "NewExpression" && node.callee === child) || + (node.type === "TSAsExpression" && node.expression === child) + ) { + parent = path.getParentNode(ancestorCount + 1); + child = node; + } else { + parent = node; + } + } + + // Do not add indent to direct `ConditionalExpression` + if (child === node) { return false; } - while (isChainElement(ancestor)) { - ancestor = path.getParentNode(++ancestorCount); - } - const checkAncestor = (node) => { - const name = ancestorNameMap.get(ancestor.type); - return ancestor[name] === node; - }; - const parent = path.getParentNode(); - const name = path.getName(); - - if ( - ((name === "callee" && parent.type === "NewExpression") || - (name === "expression" && parent.type === "TSAsExpression")) && - checkAncestor(parent) - ) { - return true; - } - - if ( - ((name === "callee" && isCallExpression(parent)) || - (name === "object" && isMemberExpression(parent)) || - (name === "expression" && parent.type === "TSNonNullExpression")) && - checkAncestor(path.getParentNode(ancestorCount - 1)) - ) { - return true; - } - - return false; + return parent[ancestorNameMap.get(parent.type)] === child; } /** diff --git a/src/language-js/utils.js b/src/language-js/utils.js index 15ce8dd97..c4d288cce 100644 --- a/src/language-js/utils.js +++ b/src/language-js/utils.js @@ -1327,16 +1327,6 @@ function getComments(node, flags, fn) { const isNextLineEmpty = (node, { originalText }) => isNextLineEmptyAfterIndex(originalText, locEnd(node)); -function isChainElement(node) { - return ( - node.type === "MemberExpression" || - node.type === "OptionalMemberExpression" || - node.type === "CallExpression" || - node.type === "OptionalCallExpression" || - node.type === "TSNonNullExpression" - ); -} - module.exports = { getFunctionParameters, iterateFunctionParametersPath, @@ -1357,7 +1347,6 @@ module.exports = { identity, isBinaryish, isBlockComment, - isChainElement, isLineComment, isPrettierIgnoreComment, isCallExpression, diff --git a/tests/js/ternaries/__snapshots__/jsfmt.spec.js.snap b/tests/js/ternaries/__snapshots__/jsfmt.spec.js.snap index 27a6f6055..64a356b85 100644 --- a/tests/js/ternaries/__snapshots__/jsfmt.spec.js.snap +++ b/tests/js/ternaries/__snapshots__/jsfmt.spec.js.snap @@ -1850,6 +1850,64 @@ const decorated = (arg, ignoreRequestError) => { ).foo(); }; +bifornCringerMoshedPerplexSawder = fn( + (glimseGlyphsHazardNoopsTieTie === 0 + ? averredBathersBoxroomBuggyNurl + : anodyneCondosMalateOverateRetinol + ).prop +); + +fn( + (glimseGlyphsHazardNoopsTieTie === 0 + ? averredBathersBoxroomBuggyNurl + : anodyneCondosMalateOverateRetinol + ).prop +) + +bifornCringerMoshedPerplexSawder = fn?.( + (glimseGlyphsHazardNoopsTieTie === 0 + ? averredBathersBoxroomBuggyNurl + : anodyneCondosMalateOverateRetinol + ).prop +); + +fn?.( + (glimseGlyphsHazardNoopsTieTie === 0 + ? averredBathersBoxroomBuggyNurl + : anodyneCondosMalateOverateRetinol + ).prop +) + +bifornCringerMoshedPerplexSawder = + fn[ + (glimseGlyphsHazardNoopsTieTie === 0 + ? averredBathersBoxroomBuggyNurl + : anodyneCondosMalateOverateRetinol + ).prop + ]; + +fn[ + (glimseGlyphsHazardNoopsTieTie === 0 + ? averredBathersBoxroomBuggyNurl + : anodyneCondosMalateOverateRetinol + ).prop +]; + +bifornCringerMoshedPerplexSawder = + fn?.[ + (glimseGlyphsHazardNoopsTieTie === 0 + ? averredBathersBoxroomBuggyNurl + : anodyneCondosMalateOverateRetinol + ).prop + ]; + +fn?.[ + (glimseGlyphsHazardNoopsTieTie === 0 + ? averredBathersBoxroomBuggyNurl + : anodyneCondosMalateOverateRetinol + ).prop +]; + =====================================output===================================== foo7 = ( coooooooooooooooooooooooooooooooooooooooooooooooooooond @@ -2140,6 +2198,64 @@ const decorated = (arg, ignoreRequestError) => { ).foo(); }; +bifornCringerMoshedPerplexSawder = fn( + (glimseGlyphsHazardNoopsTieTie === 0 + ? averredBathersBoxroomBuggyNurl + : anodyneCondosMalateOverateRetinol + ).prop +); + +fn( + (glimseGlyphsHazardNoopsTieTie === 0 + ? averredBathersBoxroomBuggyNurl + : anodyneCondosMalateOverateRetinol + ).prop +); + +bifornCringerMoshedPerplexSawder = fn?.( + (glimseGlyphsHazardNoopsTieTie === 0 + ? averredBathersBoxroomBuggyNurl + : anodyneCondosMalateOverateRetinol + ).prop +); + +fn?.( + (glimseGlyphsHazardNoopsTieTie === 0 + ? averredBathersBoxroomBuggyNurl + : anodyneCondosMalateOverateRetinol + ).prop +); + +bifornCringerMoshedPerplexSawder = + fn[ + (glimseGlyphsHazardNoopsTieTie === 0 + ? averredBathersBoxroomBuggyNurl + : anodyneCondosMalateOverateRetinol + ).prop + ]; + +fn[ + (glimseGlyphsHazardNoopsTieTie === 0 + ? averredBathersBoxroomBuggyNurl + : anodyneCondosMalateOverateRetinol + ).prop +]; + +bifornCringerMoshedPerplexSawder = + fn?.[ + (glimseGlyphsHazardNoopsTieTie === 0 + ? averredBathersBoxroomBuggyNurl + : anodyneCondosMalateOverateRetinol + ).prop + ]; + +fn?.[ + (glimseGlyphsHazardNoopsTieTie === 0 + ? averredBathersBoxroomBuggyNurl + : anodyneCondosMalateOverateRetinol + ).prop +]; + ================================================================================ `; @@ -2386,6 +2502,64 @@ const decorated = (arg, ignoreRequestError) => { ).foo(); }; +bifornCringerMoshedPerplexSawder = fn( + (glimseGlyphsHazardNoopsTieTie === 0 + ? averredBathersBoxroomBuggyNurl + : anodyneCondosMalateOverateRetinol + ).prop +); + +fn( + (glimseGlyphsHazardNoopsTieTie === 0 + ? averredBathersBoxroomBuggyNurl + : anodyneCondosMalateOverateRetinol + ).prop +) + +bifornCringerMoshedPerplexSawder = fn?.( + (glimseGlyphsHazardNoopsTieTie === 0 + ? averredBathersBoxroomBuggyNurl + : anodyneCondosMalateOverateRetinol + ).prop +); + +fn?.( + (glimseGlyphsHazardNoopsTieTie === 0 + ? averredBathersBoxroomBuggyNurl + : anodyneCondosMalateOverateRetinol + ).prop +) + +bifornCringerMoshedPerplexSawder = + fn[ + (glimseGlyphsHazardNoopsTieTie === 0 + ? averredBathersBoxroomBuggyNurl + : anodyneCondosMalateOverateRetinol + ).prop + ]; + +fn[ + (glimseGlyphsHazardNoopsTieTie === 0 + ? averredBathersBoxroomBuggyNurl + : anodyneCondosMalateOverateRetinol + ).prop +]; + +bifornCringerMoshedPerplexSawder = + fn?.[ + (glimseGlyphsHazardNoopsTieTie === 0 + ? averredBathersBoxroomBuggyNurl + : anodyneCondosMalateOverateRetinol + ).prop + ]; + +fn?.[ + (glimseGlyphsHazardNoopsTieTie === 0 + ? averredBathersBoxroomBuggyNurl + : anodyneCondosMalateOverateRetinol + ).prop +]; + =====================================output===================================== foo7 = ( coooooooooooooooooooooooooooooooooooooooooooooooooooond @@ -2676,6 +2850,64 @@ const decorated = (arg, ignoreRequestError) => { ).foo(); }; +bifornCringerMoshedPerplexSawder = fn( + (glimseGlyphsHazardNoopsTieTie === 0 + ? averredBathersBoxroomBuggyNurl + : anodyneCondosMalateOverateRetinol + ).prop +); + +fn( + (glimseGlyphsHazardNoopsTieTie === 0 + ? averredBathersBoxroomBuggyNurl + : anodyneCondosMalateOverateRetinol + ).prop +); + +bifornCringerMoshedPerplexSawder = fn?.( + (glimseGlyphsHazardNoopsTieTie === 0 + ? averredBathersBoxroomBuggyNurl + : anodyneCondosMalateOverateRetinol + ).prop +); + +fn?.( + (glimseGlyphsHazardNoopsTieTie === 0 + ? averredBathersBoxroomBuggyNurl + : anodyneCondosMalateOverateRetinol + ).prop +); + +bifornCringerMoshedPerplexSawder = + fn[ + (glimseGlyphsHazardNoopsTieTie === 0 + ? averredBathersBoxroomBuggyNurl + : anodyneCondosMalateOverateRetinol + ).prop + ]; + +fn[ + (glimseGlyphsHazardNoopsTieTie === 0 + ? averredBathersBoxroomBuggyNurl + : anodyneCondosMalateOverateRetinol + ).prop +]; + +bifornCringerMoshedPerplexSawder = + fn?.[ + (glimseGlyphsHazardNoopsTieTie === 0 + ? averredBathersBoxroomBuggyNurl + : anodyneCondosMalateOverateRetinol + ).prop + ]; + +fn?.[ + (glimseGlyphsHazardNoopsTieTie === 0 + ? averredBathersBoxroomBuggyNurl + : anodyneCondosMalateOverateRetinol + ).prop +]; + ================================================================================ `; @@ -2921,6 +3153,64 @@ const decorated = (arg, ignoreRequestError) => { ).foo(); }; +bifornCringerMoshedPerplexSawder = fn( + (glimseGlyphsHazardNoopsTieTie === 0 + ? averredBathersBoxroomBuggyNurl + : anodyneCondosMalateOverateRetinol + ).prop +); + +fn( + (glimseGlyphsHazardNoopsTieTie === 0 + ? averredBathersBoxroomBuggyNurl + : anodyneCondosMalateOverateRetinol + ).prop +) + +bifornCringerMoshedPerplexSawder = fn?.( + (glimseGlyphsHazardNoopsTieTie === 0 + ? averredBathersBoxroomBuggyNurl + : anodyneCondosMalateOverateRetinol + ).prop +); + +fn?.( + (glimseGlyphsHazardNoopsTieTie === 0 + ? averredBathersBoxroomBuggyNurl + : anodyneCondosMalateOverateRetinol + ).prop +) + +bifornCringerMoshedPerplexSawder = + fn[ + (glimseGlyphsHazardNoopsTieTie === 0 + ? averredBathersBoxroomBuggyNurl + : anodyneCondosMalateOverateRetinol + ).prop + ]; + +fn[ + (glimseGlyphsHazardNoopsTieTie === 0 + ? averredBathersBoxroomBuggyNurl + : anodyneCondosMalateOverateRetinol + ).prop +]; + +bifornCringerMoshedPerplexSawder = + fn?.[ + (glimseGlyphsHazardNoopsTieTie === 0 + ? averredBathersBoxroomBuggyNurl + : anodyneCondosMalateOverateRetinol + ).prop + ]; + +fn?.[ + (glimseGlyphsHazardNoopsTieTie === 0 + ? averredBathersBoxroomBuggyNurl + : anodyneCondosMalateOverateRetinol + ).prop +]; + =====================================output===================================== foo7 = ( coooooooooooooooooooooooooooooooooooooooooooooooooooond @@ -3211,6 +3501,64 @@ const decorated = (arg, ignoreRequestError) => { ).foo(); }; +bifornCringerMoshedPerplexSawder = fn( + (glimseGlyphsHazardNoopsTieTie === 0 + ? averredBathersBoxroomBuggyNurl + : anodyneCondosMalateOverateRetinol + ).prop +); + +fn( + (glimseGlyphsHazardNoopsTieTie === 0 + ? averredBathersBoxroomBuggyNurl + : anodyneCondosMalateOverateRetinol + ).prop +); + +bifornCringerMoshedPerplexSawder = fn?.( + (glimseGlyphsHazardNoopsTieTie === 0 + ? averredBathersBoxroomBuggyNurl + : anodyneCondosMalateOverateRetinol + ).prop +); + +fn?.( + (glimseGlyphsHazardNoopsTieTie === 0 + ? averredBathersBoxroomBuggyNurl + : anodyneCondosMalateOverateRetinol + ).prop +); + +bifornCringerMoshedPerplexSawder = + fn[ + (glimseGlyphsHazardNoopsTieTie === 0 + ? averredBathersBoxroomBuggyNurl + : anodyneCondosMalateOverateRetinol + ).prop + ]; + +fn[ + (glimseGlyphsHazardNoopsTieTie === 0 + ? averredBathersBoxroomBuggyNurl + : anodyneCondosMalateOverateRetinol + ).prop +]; + +bifornCringerMoshedPerplexSawder = + fn?.[ + (glimseGlyphsHazardNoopsTieTie === 0 + ? averredBathersBoxroomBuggyNurl + : anodyneCondosMalateOverateRetinol + ).prop + ]; + +fn?.[ + (glimseGlyphsHazardNoopsTieTie === 0 + ? averredBathersBoxroomBuggyNurl + : anodyneCondosMalateOverateRetinol + ).prop +]; + ================================================================================ `; @@ -3455,6 +3803,64 @@ const decorated = (arg, ignoreRequestError) => { ).foo(); }; +bifornCringerMoshedPerplexSawder = fn( + (glimseGlyphsHazardNoopsTieTie === 0 + ? averredBathersBoxroomBuggyNurl + : anodyneCondosMalateOverateRetinol + ).prop +); + +fn( + (glimseGlyphsHazardNoopsTieTie === 0 + ? averredBathersBoxroomBuggyNurl + : anodyneCondosMalateOverateRetinol + ).prop +) + +bifornCringerMoshedPerplexSawder = fn?.( + (glimseGlyphsHazardNoopsTieTie === 0 + ? averredBathersBoxroomBuggyNurl + : anodyneCondosMalateOverateRetinol + ).prop +); + +fn?.( + (glimseGlyphsHazardNoopsTieTie === 0 + ? averredBathersBoxroomBuggyNurl + : anodyneCondosMalateOverateRetinol + ).prop +) + +bifornCringerMoshedPerplexSawder = + fn[ + (glimseGlyphsHazardNoopsTieTie === 0 + ? averredBathersBoxroomBuggyNurl + : anodyneCondosMalateOverateRetinol + ).prop + ]; + +fn[ + (glimseGlyphsHazardNoopsTieTie === 0 + ? averredBathersBoxroomBuggyNurl + : anodyneCondosMalateOverateRetinol + ).prop +]; + +bifornCringerMoshedPerplexSawder = + fn?.[ + (glimseGlyphsHazardNoopsTieTie === 0 + ? averredBathersBoxroomBuggyNurl + : anodyneCondosMalateOverateRetinol + ).prop + ]; + +fn?.[ + (glimseGlyphsHazardNoopsTieTie === 0 + ? averredBathersBoxroomBuggyNurl + : anodyneCondosMalateOverateRetinol + ).prop +]; + =====================================output===================================== foo7 = ( coooooooooooooooooooooooooooooooooooooooooooooooooooond @@ -3745,6 +4151,64 @@ const decorated = (arg, ignoreRequestError) => { ).foo(); }; +bifornCringerMoshedPerplexSawder = fn( + (glimseGlyphsHazardNoopsTieTie === 0 + ? averredBathersBoxroomBuggyNurl + : anodyneCondosMalateOverateRetinol + ).prop +); + +fn( + (glimseGlyphsHazardNoopsTieTie === 0 + ? averredBathersBoxroomBuggyNurl + : anodyneCondosMalateOverateRetinol + ).prop +); + +bifornCringerMoshedPerplexSawder = fn?.( + (glimseGlyphsHazardNoopsTieTie === 0 + ? averredBathersBoxroomBuggyNurl + : anodyneCondosMalateOverateRetinol + ).prop +); + +fn?.( + (glimseGlyphsHazardNoopsTieTie === 0 + ? averredBathersBoxroomBuggyNurl + : anodyneCondosMalateOverateRetinol + ).prop +); + +bifornCringerMoshedPerplexSawder = + fn[ + (glimseGlyphsHazardNoopsTieTie === 0 + ? averredBathersBoxroomBuggyNurl + : anodyneCondosMalateOverateRetinol + ).prop + ]; + +fn[ + (glimseGlyphsHazardNoopsTieTie === 0 + ? averredBathersBoxroomBuggyNurl + : anodyneCondosMalateOverateRetinol + ).prop +]; + +bifornCringerMoshedPerplexSawder = + fn?.[ + (glimseGlyphsHazardNoopsTieTie === 0 + ? averredBathersBoxroomBuggyNurl + : anodyneCondosMalateOverateRetinol + ).prop + ]; + +fn?.[ + (glimseGlyphsHazardNoopsTieTie === 0 + ? averredBathersBoxroomBuggyNurl + : anodyneCondosMalateOverateRetinol + ).prop +]; + ================================================================================ `; diff --git a/tests/js/ternaries/indent-after-paren.js b/tests/js/ternaries/indent-after-paren.js index 35ebca828..f4c0bdbf9 100644 --- a/tests/js/ternaries/indent-after-paren.js +++ b/tests/js/ternaries/indent-after-paren.js @@ -232,3 +232,61 @@ const decorated = (arg, ignoreRequestError) => { : handleAsyncOperations(arg, ignoreRequestError) ).foo(); }; + +bifornCringerMoshedPerplexSawder = fn( + (glimseGlyphsHazardNoopsTieTie === 0 + ? averredBathersBoxroomBuggyNurl + : anodyneCondosMalateOverateRetinol + ).prop +); + +fn( + (glimseGlyphsHazardNoopsTieTie === 0 + ? averredBathersBoxroomBuggyNurl + : anodyneCondosMalateOverateRetinol + ).prop +) + +bifornCringerMoshedPerplexSawder = fn?.( + (glimseGlyphsHazardNoopsTieTie === 0 + ? averredBathersBoxroomBuggyNurl + : anodyneCondosMalateOverateRetinol + ).prop +); + +fn?.( + (glimseGlyphsHazardNoopsTieTie === 0 + ? averredBathersBoxroomBuggyNurl + : anodyneCondosMalateOverateRetinol + ).prop +) + +bifornCringerMoshedPerplexSawder = + fn[ + (glimseGlyphsHazardNoopsTieTie === 0 + ? averredBathersBoxroomBuggyNurl + : anodyneCondosMalateOverateRetinol + ).prop + ]; + +fn[ + (glimseGlyphsHazardNoopsTieTie === 0 + ? averredBathersBoxroomBuggyNurl + : anodyneCondosMalateOverateRetinol + ).prop +]; + +bifornCringerMoshedPerplexSawder = + fn?.[ + (glimseGlyphsHazardNoopsTieTie === 0 + ? averredBathersBoxroomBuggyNurl + : anodyneCondosMalateOverateRetinol + ).prop + ]; + +fn?.[ + (glimseGlyphsHazardNoopsTieTie === 0 + ? averredBathersBoxroomBuggyNurl + : anodyneCondosMalateOverateRetinol + ).prop +];