From e1da90fc414a3c9c16f52db4a5bd81bd4f9532a4 Mon Sep 17 00:00:00 2001 From: Chris Brody Date: Fri, 29 Jan 2021 22:32:22 -0500 Subject: [PATCH] Fix: nested indenting for offsetTernaryExpressions: true (fixes #13971) (#13972) * reproduce extra nested ternary indentation with a test * add a similar test with proper indenting of nested ternary * apply condition to avoid extra indenting with offsetTernaryExpressions Co-authored-by: Adam Stankiewicz Co-authored-by: Adam Stankiewicz --- lib/rules/indent.js | 1 + tests/lib/rules/indent.js | 20 ++++++++++++++++++++ 2 files changed, 21 insertions(+) diff --git a/lib/rules/indent.js b/lib/rules/indent.js index 1c0dccc5c98..8f4079d31f9 100644 --- a/lib/rules/indent.js +++ b/lib/rules/indent.js @@ -1178,6 +1178,7 @@ module.exports = { offsets.setDesiredOffset(colonToken, firstToken, 1); offsets.setDesiredOffset(firstConsequentToken, firstToken, + firstConsequentToken.type === "Punctuator" && options.offsetTernaryExpressions ? 2 : 1); /* diff --git a/tests/lib/rules/indent.js b/tests/lib/rules/indent.js index ad35ad09c70..e7ad6074c49 100644 --- a/tests/lib/rules/indent.js +++ b/tests/lib/rules/indent.js @@ -2204,6 +2204,26 @@ ruleTester.run("indent", rule, { `, options: [4, { offsetTernaryExpressions: true }] }, + { + code: unIndent` + condition1 + ? condition2 + ? Promise.resolve(1) + : Promise.resolve(2) + : Promise.resolve(3) + `, + options: [2, { offsetTernaryExpressions: true }] + }, + { + code: unIndent` + condition1 + ? Promise.resolve(1) + : condition2 + ? Promise.resolve(2) + : Promise.resolve(3) + `, + options: [2, { offsetTernaryExpressions: true }] + }, { code: unIndent` condition