Skip to content

Commit

Permalink
fix(eslint-plugin): [sort-type-constituents] correctly fix constituen…
Browse files Browse the repository at this point in the history
…ts with leading `|` or `&`

Fixes #6117
  • Loading branch information
cherryblossom000 committed Nov 28, 2022
1 parent becd1f8 commit 03f26ba
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 8 deletions.
16 changes: 12 additions & 4 deletions packages/eslint-plugin/src/rules/sort-type-constituents.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,10 +96,18 @@ function getGroup(node: TSESTree.TypeNode): Group {
}
}

function requiresParentheses(node: TSESTree.TypeNode): boolean {
interface Constituent {
group: number;
node: TSESTree.TypeNode;
text: string;
}

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

Expand Down Expand Up @@ -183,7 +191,7 @@ export default util.createRule<Options, MessageIds>({
function checkSorting(
node: TSESTree.TSIntersectionType | TSESTree.TSUnionType,
): void {
const sourceOrder = node.types.map(type => {
const sourceOrder = node.types.map((type): Constituent => {
const group = groupOrder?.indexOf(getGroup(type)) ?? -1;
return {
group: group === -1 ? Number.MAX_SAFE_INTEGER : group,
Expand Down Expand Up @@ -226,7 +234,7 @@ 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 => (requiresParentheses(t) ? `(${t.text})` : t.text))
.join(
node.type === AST_NODE_TYPES.TSIntersectionType ? ' & ' : ' | ',
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,10 +96,18 @@ function getGroup(node: TSESTree.TypeNode): Group {
}
}

function requiresParentheses(node: TSESTree.TypeNode): boolean {
interface Constituent {
group: number;
node: TSESTree.TypeNode;
text: string;
}

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

Expand Down Expand Up @@ -185,7 +193,7 @@ export default util.createRule<Options, MessageIds>({
function checkSorting(
node: TSESTree.TSIntersectionType | TSESTree.TSUnionType,
): void {
const sourceOrder = node.types.map(type => {
const sourceOrder = node.types.map((type): Constituent => {
const group = groupOrder?.indexOf(getGroup(type)) ?? -1;
return {
group: group === -1 ? Number.MAX_SAFE_INTEGER : group,
Expand Down Expand Up @@ -228,7 +236,7 @@ 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 => (requiresParentheses(t) ? `(${t.text})` : t.text))
.join(
node.type === AST_NODE_TYPES.TSIntersectionType ? ' & ' : ' | ',
);
Expand Down
26 changes: 26 additions & 0 deletions packages/eslint-plugin/tests/rules/sort-type-constituents.test.ts
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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

0 comments on commit 03f26ba

Please sign in to comment.