Skip to content

Commit

Permalink
fix(eslint-plugin): [sort-type-constituents, sort-type-union-intersec…
Browse files Browse the repository at this point in the history
…tion-members] handle some required parentheses cases in the fixer (#6118)
  • Loading branch information
cherryblossom000 committed Dec 2, 2022
1 parent e777f5e commit 5d49d5d
Show file tree
Hide file tree
Showing 5 changed files with 115 additions and 20 deletions.
17 changes: 8 additions & 9 deletions packages/eslint-plugin/src/rules/sort-type-constituents.ts
Expand Up @@ -2,7 +2,7 @@ import type { TSESLint, TSESTree } from '@typescript-eslint/utils';
import { AST_NODE_TYPES } from '@typescript-eslint/utils';

import * as util from '../util';
import { getEnumNames } from '../util';
import { getEnumNames, typeNodeRequiresParentheses } from '../util';

enum Group {
conditional = 'conditional',
Expand Down Expand Up @@ -96,13 +96,6 @@ function getGroup(node: TSESTree.TypeNode): Group {
}
}

function requiresParentheses(node: TSESTree.TypeNode): boolean {
return (
node.type === AST_NODE_TYPES.TSFunctionType ||
node.type === AST_NODE_TYPES.TSConstructorType
);
}

export type Options = [
{
checkIntersections?: boolean;
Expand Down Expand Up @@ -226,7 +219,13 @@ export default util.createRule<Options, MessageIds>({

const fix: TSESLint.ReportFixFunction = fixer => {
const sorted = expectedOrder
.map(t => (requiresParentheses(t.node) ? `(${t.text})` : t.text))
.map(t =>
typeNodeRequiresParentheses(t.node, t.text) ||
(node.type === AST_NODE_TYPES.TSIntersectionType &&
t.node.type === AST_NODE_TYPES.TSUnionType)
? `(${t.text})`
: t.text,
)
.join(
node.type === AST_NODE_TYPES.TSIntersectionType ? ' & ' : ' | ',
);
Expand Down
Expand Up @@ -2,7 +2,7 @@ import type { TSESLint, TSESTree } from '@typescript-eslint/utils';
import { AST_NODE_TYPES } from '@typescript-eslint/utils';

import * as util from '../util';
import { getEnumNames } from '../util';
import { getEnumNames, typeNodeRequiresParentheses } from '../util';

enum Group {
conditional = 'conditional',
Expand Down Expand Up @@ -96,13 +96,6 @@ function getGroup(node: TSESTree.TypeNode): Group {
}
}

function requiresParentheses(node: TSESTree.TypeNode): boolean {
return (
node.type === AST_NODE_TYPES.TSFunctionType ||
node.type === AST_NODE_TYPES.TSConstructorType
);
}

export type Options = [
{
checkIntersections?: boolean;
Expand Down Expand Up @@ -228,7 +221,13 @@ export default util.createRule<Options, MessageIds>({

const fix: TSESLint.ReportFixFunction = fixer => {
const sorted = expectedOrder
.map(t => (requiresParentheses(t.node) ? `(${t.text})` : t.text))
.map(t =>
typeNodeRequiresParentheses(t.node, t.text) ||
(node.type === AST_NODE_TYPES.TSIntersectionType &&
t.node.type === AST_NODE_TYPES.TSUnionType)
? `(${t.text})`
: t.text,
)
.join(
node.type === AST_NODE_TYPES.TSIntersectionType ? ' & ' : ' | ',
);
Expand Down
13 changes: 13 additions & 0 deletions packages/eslint-plugin/src/util/misc.ts
Expand Up @@ -203,6 +203,18 @@ function findLastIndex<T>(
return -1;
}

function typeNodeRequiresParentheses(
node: TSESTree.TypeNode,
text: string,
): boolean {
return (
node.type === AST_NODE_TYPES.TSFunctionType ||
node.type === AST_NODE_TYPES.TSConstructorType ||
(node.type === AST_NODE_TYPES.TSUnionType && text.startsWith('|')) ||
(node.type === AST_NODE_TYPES.TSIntersectionType && text.startsWith('&'))
);
}

export {
arrayGroupByToMap,
arraysAreEqual,
Expand All @@ -216,6 +228,7 @@ export {
isDefinitionFile,
MemberNameType,
RequireKeys,
typeNodeRequiresParentheses,
upperCaseFirst,
findLastIndex,
};
Expand Up @@ -287,6 +287,32 @@ type T =
},
],
},
{
code: `type T = (| A) ${operator} B;`,
output: `type T = B ${operator} (| A);`,
errors: [
{
messageId: 'notSortedNamed',
data: {
type,
name: 'T',
},
},
],
},
{
code: `type T = (& A) ${operator} B;`,
output: `type T = B ${operator} (& A);`,
errors: [
{
messageId: 'notSortedNamed',
data: {
type,
name: 'T',
},
},
],
},
];
};

Expand Down Expand Up @@ -334,5 +360,21 @@ type T = 1 | string | {} | A;
],
},
],
invalid: [...invalid('|'), ...invalid('&')],
invalid: [
...invalid('|'),
...invalid('&'),
{
code: 'type T = (B | C) & A;',
output: `type T = A & (B | C);`,
errors: [
{
messageId: 'notSortedNamed',
data: {
type: 'Intersection',
name: 'T',
},
},
],
},
],
});
Expand Up @@ -287,6 +287,32 @@ type T =
},
],
},
{
code: `type T = (| A) ${operator} B;`,
output: `type T = B ${operator} (| A);`,
errors: [
{
messageId: 'notSortedNamed',
data: {
type,
name: 'T',
},
},
],
},
{
code: `type T = (& A) ${operator} B;`,
output: `type T = B ${operator} (& A);`,
errors: [
{
messageId: 'notSortedNamed',
data: {
type,
name: 'T',
},
},
],
},
];
};

Expand Down Expand Up @@ -334,5 +360,21 @@ type T = 1 | string | {} | A;
],
},
],
invalid: [...invalid('|'), ...invalid('&')],
invalid: [
...invalid('|'),
...invalid('&'),
{
code: 'type T = (B | C) & A;',
output: `type T = A & (B | C);`,
errors: [
{
messageId: 'notSortedNamed',
data: {
type: 'Intersection',
name: 'T',
},
},
],
},
],
});

0 comments on commit 5d49d5d

Please sign in to comment.