Skip to content

Commit

Permalink
feat!: remove nullable last option in sort-union-types
Browse files Browse the repository at this point in the history
  • Loading branch information
azat-io committed Jul 22, 2024
1 parent 208a9db commit c4977df
Show file tree
Hide file tree
Showing 3 changed files with 1 addition and 154 deletions.
6 changes: 0 additions & 6 deletions docs/content/rules/sort-union-types.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -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

<sub>(default: `false`)</sub>

When `true` requires the types `null` and `undefined` to be last.

## Usage

<CodeTabs
Expand Down
41 changes: 1 addition & 40 deletions rules/sort-union-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ type MESSAGE_ID = 'unexpectedUnionTypesOrder'
type Options = [
Partial<{
type: 'alphabetical' | 'line-length' | 'natural'
nullableLast: boolean
order: 'desc' | 'asc'
ignoreCase: boolean
}>,
Expand Down Expand Up @@ -50,10 +49,6 @@ export default createEslintRule<Options, MESSAGE_ID>({
type: 'boolean',
default: true,
},
nullableLast: {
type: 'boolean',
default: false,
},
},
additionalProperties: false,
},
Expand All @@ -73,18 +68,13 @@ export default createEslintRule<Options, MESSAGE_ID>({
TSUnionType: node => {
let options = complete(context.options.at(0), {
type: 'alphabetical',
nullableLast: false,
ignoreCase: true,
order: 'asc',
} as const)

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,
Expand All @@ -93,14 +83,6 @@ export default createEslintRule<Options, MESSAGE_ID>({
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',
Expand All @@ -110,28 +92,7 @@ export default createEslintRule<Options, MESSAGE_ID>({
},
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)
},
})
Expand Down
108 changes: 0 additions & 108 deletions test/sort-union-types.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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`, () => {
Expand Down Expand Up @@ -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`, () => {
Expand Down Expand Up @@ -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`, () => {
Expand Down

2 comments on commit c4977df

@Sparticuz
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Was this removed in favor of groups?

@azat-io
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.