Skip to content

Commit

Permalink
chore: create typeNodeRequiresParentheses utility
Browse files Browse the repository at this point in the history
  • Loading branch information
cherryblossom000 committed Nov 28, 2022
1 parent 03f26ba commit 9caa9eb
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 36 deletions.
25 changes: 7 additions & 18 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,21 +96,6 @@ function getGroup(node: TSESTree.TypeNode): Group {
}
}

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.TSUnionType && text.startsWith('|')) ||
(node.type === AST_NODE_TYPES.TSIntersectionType && text.startsWith('&'))
);
}

export type Options = [
{
checkIntersections?: boolean;
Expand Down Expand Up @@ -191,7 +176,7 @@ export default util.createRule<Options, MessageIds>({
function checkSorting(
node: TSESTree.TSIntersectionType | TSESTree.TSUnionType,
): void {
const sourceOrder = node.types.map((type): Constituent => {
const sourceOrder = node.types.map(type => {
const group = groupOrder?.indexOf(getGroup(type)) ?? -1;
return {
group: group === -1 ? Number.MAX_SAFE_INTEGER : group,
Expand Down Expand Up @@ -234,7 +219,11 @@ export default util.createRule<Options, MessageIds>({

const fix: TSESLint.ReportFixFunction = fixer => {
const sorted = expectedOrder
.map(t => (requiresParentheses(t) ? `(${t.text})` : t.text))
.map(t =>
typeNodeRequiresParentheses(t.node, t.text)
? `(${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,21 +96,6 @@ function getGroup(node: TSESTree.TypeNode): Group {
}
}

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.TSUnionType && text.startsWith('|')) ||
(node.type === AST_NODE_TYPES.TSIntersectionType && text.startsWith('&'))
);
}

export type Options = [
{
checkIntersections?: boolean;
Expand Down Expand Up @@ -193,7 +178,7 @@ export default util.createRule<Options, MessageIds>({
function checkSorting(
node: TSESTree.TSIntersectionType | TSESTree.TSUnionType,
): void {
const sourceOrder = node.types.map((type): Constituent => {
const sourceOrder = node.types.map(type => {
const group = groupOrder?.indexOf(getGroup(type)) ?? -1;
return {
group: group === -1 ? Number.MAX_SAFE_INTEGER : group,
Expand Down Expand Up @@ -236,7 +221,11 @@ export default util.createRule<Options, MessageIds>({

const fix: TSESLint.ReportFixFunction = fixer => {
const sorted = expectedOrder
.map(t => (requiresParentheses(t) ? `(${t.text})` : t.text))
.map(t =>
typeNodeRequiresParentheses(t.node, t.text)
? `(${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 @@ -180,6 +180,18 @@ function formatWordList(words: string[]): string {
return [words.slice(0, -1).join(', '), words.slice(-1)[0]].join(' and ');
}

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 @@ -193,5 +205,6 @@ export {
isDefinitionFile,
MemberNameType,
RequireKeys,
typeNodeRequiresParentheses,
upperCaseFirst,
};

0 comments on commit 9caa9eb

Please sign in to comment.