From c4977dfd27f528333950509c0f500c43230ae1b3 Mon Sep 17 00:00:00 2001 From: "Azat S." Date: Fri, 19 Jul 2024 23:50:29 +0300 Subject: [PATCH] feat!: remove nullable last option in sort-union-types --- docs/content/rules/sort-union-types.mdx | 6 -- rules/sort-union-types.ts | 41 +-------- test/sort-union-types.test.ts | 108 ------------------------ 3 files changed, 1 insertion(+), 154 deletions(-) diff --git a/docs/content/rules/sort-union-types.mdx b/docs/content/rules/sort-union-types.mdx index e76cbc95..f7837f27 100644 --- a/docs/content/rules/sort-union-types.mdx +++ b/docs/content/rules/sort-union-types.mdx @@ -122,12 +122,6 @@ Controls whether sorting should be case-sensitive or not. - `true` — Ignore case when sorting alphabetically or naturally (e.g., “A” and “a” are the same). - `false` — Consider case when sorting (e.g., “A” comes before “a”). -### nullableLast - -(default: `false`) - -When `true` requires the types `null` and `undefined` to be last. - ## Usage , @@ -50,10 +49,6 @@ export default createEslintRule({ type: 'boolean', default: true, }, - nullableLast: { - type: 'boolean', - default: false, - }, }, additionalProperties: false, }, @@ -73,7 +68,6 @@ export default createEslintRule({ TSUnionType: node => { let options = complete(context.options.at(0), { type: 'alphabetical', - nullableLast: false, ignoreCase: true, order: 'asc', } as const) @@ -81,10 +75,6 @@ export default createEslintRule({ let sourceCode = getSourceCode(context) let nodes: SortingNode[] = node.types.map(type => ({ - group: - type.type === 'TSNullKeyword' || type.type === 'TSUndefinedKeyword' - ? 'nullable' - : 'unknown', name: sourceCode.text.slice(...type.range), size: rangeToDiff(type.range), node: type, @@ -93,14 +83,6 @@ export default createEslintRule({ pairwise(nodes, (left, right) => { let compareValue = isPositive(compare(left, right, options)) - if (options.nullableLast) { - if (left.group === 'nullable' && right.group === 'unknown') { - compareValue = true - } else if (left.group === 'unknown' && right.group === 'nullable') { - compareValue = false - } - } - if (compareValue) { context.report({ messageId: 'unexpectedUnionTypesOrder', @@ -110,28 +92,7 @@ export default createEslintRule({ }, node: right.node, fix: fixer => { - let sortedNodes: SortingNode[] = [] - - if (options.nullableLast) { - let nullable: SortingNode[] = [] - - let nonNullable = nodes.filter(currentNode => { - if (currentNode.group === 'nullable') { - nullable.push(currentNode) - return false - } - - return true - }) - - sortedNodes = [ - ...sortNodes(nonNullable, options), - ...sortNodes(nullable, options), - ] - } else { - sortedNodes = sortNodes(nodes, options) - } - + let sortedNodes: SortingNode[] = sortNodes(nodes, options) return makeFixes(fixer, nodes, sortedNodes, sourceCode) }, }) diff --git a/test/sort-union-types.test.ts b/test/sort-union-types.test.ts index eb2ab6bf..3541bb10 100644 --- a/test/sort-union-types.test.ts +++ b/test/sort-union-types.test.ts @@ -264,42 +264,6 @@ describe(RULE_NAME, () => { }, ], }) - - ruleTester.run(`${RULE_NAME}: can put nullable types to the end`, rule, { - valid: [], - invalid: [ - { - code: dedent` - type Type = 'null' | null | 'a' | undefined | unknown - `, - output: dedent` - type Type = 'a' | 'null' | unknown | null | undefined - `, - options: [ - { - ...options, - nullableLast: true, - }, - ], - errors: [ - { - messageId: 'unexpectedUnionTypesOrder', - data: { - left: 'null', - right: "'a'", - }, - }, - { - messageId: 'unexpectedUnionTypesOrder', - data: { - left: 'undefined', - right: 'unknown', - }, - }, - ], - }, - ], - }) }) describe(`${RULE_NAME}: sorting by natural order`, () => { @@ -543,42 +507,6 @@ describe(RULE_NAME, () => { }, ], }) - - ruleTester.run(`${RULE_NAME}: can put nullable types to the end`, rule, { - valid: [], - invalid: [ - { - code: dedent` - type Type = 'null' | null | 'a' | undefined | unknown - `, - output: dedent` - type Type = 'a' | 'null' | unknown | null | undefined - `, - options: [ - { - ...options, - nullableLast: true, - }, - ], - errors: [ - { - messageId: 'unexpectedUnionTypesOrder', - data: { - left: 'null', - right: "'a'", - }, - }, - { - messageId: 'unexpectedUnionTypesOrder', - data: { - left: 'undefined', - right: 'unknown', - }, - }, - ], - }, - ], - }) }) describe(`${RULE_NAME}: sorting by line length`, () => { @@ -814,42 +742,6 @@ describe(RULE_NAME, () => { }, ], }) - - ruleTester.run(`${RULE_NAME}: can put nullable types to the end`, rule, { - valid: [], - invalid: [ - { - code: dedent` - type Type = 'null' | null | 'a' | undefined | unknown - `, - output: dedent` - type Type = unknown | 'null' | 'a' | undefined | null - `, - options: [ - { - ...options, - nullableLast: true, - }, - ], - errors: [ - { - messageId: 'unexpectedUnionTypesOrder', - data: { - left: 'null', - right: "'a'", - }, - }, - { - messageId: 'unexpectedUnionTypesOrder', - data: { - left: 'undefined', - right: 'unknown', - }, - }, - ], - }, - ], - }) }) describe(`${RULE_NAME}: misc`, () => {