Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(eslint-plugin): [sort-type-constituents, sort-type-union-intersection-members] handle some required parentheses cases in the fixer #6118

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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',
},
},
],
},
],
});