Fix inconsistent indentation for ternary in call expressions (#10266)

* Refactor with do while

* return false when ternary in CallExpression is not callee

* Update changelog

* For computed member property

* Fix

* Simplify

* Minor tweak

Co-authored-by: fisker Cheung <lionkay@gmail.com>
This commit is contained in:
Sosuke Suzukiandfisker Cheung authored and GitHub committed 2021-02-12 08:11:24 +09:00
1 parent bc30016500
commit 7d84c0a5a4
5 files changed
+553 -44

No files matched your search

+1 -1
View File
@@ -1,4 +1,4 @@
#### Consistent indentations for conditional operators (#10187 by @sosukesuzuki)
#### Consistent indentations for conditional operators (#10187, #10266 by @sosukesuzuki)
<!-- prettier-ignore -->
```ts
+30 -32
View File
@@ -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;
}
/**
-11
View File
@@ -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,
@@ -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
];
================================================================================
`;
+58
View File
@@ -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
];