From 5bf7f7fe48aee61a676dfbe829c2a5e9e44cd552 Mon Sep 17 00:00:00 2001 From: Sviatoslav Zaytsev Date: Tue, 31 Jan 2023 19:37:28 +0300 Subject: [PATCH] fix(eslint-plugin): [sort-type-constituents] fixed behavior change (#6384) fix(sort-type-constituents): Fixed behavior change when sorting TSConditionalType (#6339) --- packages/eslint-plugin/src/util/misc.ts | 1 + .../tests/rules/sort-type-constituents.test.ts | 14 ++++++++++++++ 2 files changed, 15 insertions(+) diff --git a/packages/eslint-plugin/src/util/misc.ts b/packages/eslint-plugin/src/util/misc.ts index fa9c5ccf528..8362736bd62 100644 --- a/packages/eslint-plugin/src/util/misc.ts +++ b/packages/eslint-plugin/src/util/misc.ts @@ -210,6 +210,7 @@ function typeNodeRequiresParentheses( return ( node.type === AST_NODE_TYPES.TSFunctionType || node.type === AST_NODE_TYPES.TSConstructorType || + node.type === AST_NODE_TYPES.TSConditionalType || (node.type === AST_NODE_TYPES.TSUnionType && text.startsWith('|')) || (node.type === AST_NODE_TYPES.TSIntersectionType && text.startsWith('&')) ); diff --git a/packages/eslint-plugin/tests/rules/sort-type-constituents.test.ts b/packages/eslint-plugin/tests/rules/sort-type-constituents.test.ts index 1aa6f8a6c9a..42f9ab8153a 100644 --- a/packages/eslint-plugin/tests/rules/sort-type-constituents.test.ts +++ b/packages/eslint-plugin/tests/rules/sort-type-constituents.test.ts @@ -359,6 +359,7 @@ type T = 1 | string | {} | A; }, ], }, + "type A = string | (T extends number ? 'hi' : 'there');", ], invalid: [ ...invalid('|'), @@ -376,5 +377,18 @@ type T = 1 | string | {} | A; }, ], }, + { + output: "type A = string | (T extends number ? 'hi' : 'there');", + code: "type A = (T extends number ? 'hi' : 'there') | string;", + errors: [ + { + messageId: 'notSortedNamed', + data: { + type: 'Union', + name: 'A', + }, + }, + ], + }, ], });