Skip to content

Commit

Permalink
feat: improve function detection
Browse files Browse the repository at this point in the history
  • Loading branch information
hugop95 authored Dec 4, 2024
1 parent 550c6c4 commit ac1e7c4
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 11 deletions.
7 changes: 2 additions & 5 deletions rules/sort-object-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import { singleCustomGroupJsonSchema } from './sort-object-types.types'
import { isNodeEslintDisabled } from '../utils/is-node-eslint-disabled'
import { allModifiers, allSelectors } from './sort-object-types.types'
import { hasPartitionComment } from '../utils/is-partition-comment'
import { isNodeFunctionType } from '../utils/is-node-function-type'
import { sortNodesByGroups } from '../utils/sort-nodes-by-groups'
import { getCommentsBefore } from '../utils/get-comments-before'
import { makeNewlinesFixes } from '../utils/make-newlines-fixes'
Expand Down Expand Up @@ -256,11 +257,7 @@ export let sortObjectTypeElements = <MessageIds extends string>({
selectors.push('index-signature')
}

if (
typeElement.type === 'TSMethodSignature' ||
(typeElement.type === 'TSPropertySignature' &&
typeElement.typeAnnotation?.typeAnnotation.type === 'TSFunctionType')
) {
if (isNodeFunctionType(typeElement)) {
selectors.push('method')
}

Expand Down
6 changes: 3 additions & 3 deletions test/sort-interfaces.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1789,7 +1789,7 @@ describe(ruleName, () => {
code: dedent`
interface Interface {
b(): void
c: () => void
c: (((v: false) => 'false') | ((v: true) => 'true')) & ((v: any) => any)
a: string
d: string
}
Expand Down Expand Up @@ -2773,7 +2773,7 @@ describe(ruleName, () => {
code: dedent`
interface Interface {
b(): void
c: () => void
c: (((v: false) => 'false') | ((v: true) => 'true')) & ((v: any) => any)
a: string
d: string
}
Expand Down Expand Up @@ -3758,7 +3758,7 @@ describe(ruleName, () => {
{
code: dedent`
interface Interface {
c: () => void
c: (((v: false) => 'false') | ((v: true) => 'true')) & ((v: any) => any)
b(): void
a: string
d: string
Expand Down
6 changes: 3 additions & 3 deletions test/sort-object-types.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1613,7 +1613,7 @@ describe(ruleName, () => {
code: dedent`
type Type = {
b(): void
c: () => void
c: (((v: false) => 'false') | ((v: true) => 'true')) & ((v: any) => any)
a: string
d: string
}
Expand Down Expand Up @@ -2395,7 +2395,7 @@ describe(ruleName, () => {
code: dedent`
type Type = {
b(): void
c: () => void
c: (((v: false) => 'false') | ((v: true) => 'true')) & ((v: any) => any)
a: string
d: string
}
Expand Down Expand Up @@ -2977,7 +2977,7 @@ describe(ruleName, () => {
{
code: dedent`
type Type = {
c: () => void
c: (((v: false) => 'false') | ((v: true) => 'true')) & ((v: any) => any)
b(): void
a: string
d: string
Expand Down
14 changes: 14 additions & 0 deletions utils/is-node-function-type.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import type { TSESTree } from '@typescript-eslint/types'

export let isNodeFunctionType = (node: TSESTree.Node): boolean => {
if (node.type === 'TSMethodSignature' || node.type === 'TSFunctionType') {
return true
}
if (node.type === 'TSUnionType' || node.type === 'TSIntersectionType') {
return node.types.every(isNodeFunctionType)
}
if (node.type === 'TSPropertySignature' && node.typeAnnotation) {
return isNodeFunctionType(node.typeAnnotation.typeAnnotation)
}
return false
}

0 comments on commit ac1e7c4

Please sign in to comment.