Skip to content

Commit

Permalink
fix(sort-modules): handle deprecated eslint enum attributes
Browse files Browse the repository at this point in the history
  • Loading branch information
hugop95 authored Nov 19, 2024
1 parent db22e8b commit 979ece8
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 9 deletions.
5 changes: 2 additions & 3 deletions rules/sort-enums.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import { hasPartitionComment } from '../utils/is-partition-comment'
import { getCommentsBefore } from '../utils/get-comments-before'
import { createEslintRule } from '../utils/create-eslint-rule'
import { getLinesBetween } from '../utils/get-lines-between'
import { getEnumMembers } from '../utils/get-enum-members'
import { getSourceCode } from '../utils/get-source-code'
import { toSingleLine } from '../utils/to-single-line'
import { rangeToDiff } from '../utils/range-to-diff'
Expand Down Expand Up @@ -63,9 +64,7 @@ let defaultOptions: Required<Options[0]> = {
export default createEslintRule<Options, MESSAGE_ID>({
create: context => ({
TSEnumDeclaration: node => {
/* v8 ignore next 2 */
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
let members = node.body.members ?? node.members ?? []
let members = getEnumMembers(node)
if (
!isSortable(members) ||
!members.every(({ initializer }) => initializer)
Expand Down
9 changes: 3 additions & 6 deletions rules/sort-modules.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ import { getCommentsBefore } from '../utils/get-comments-before'
import { createEslintRule } from '../utils/create-eslint-rule'
import { getLinesBetween } from '../utils/get-lines-between'
import { getGroupNumber } from '../utils/get-group-number'
import { getEnumMembers } from '../utils/get-enum-members'
import { getSourceCode } from '../utils/get-source-code'
import { toSingleLine } from '../utils/to-single-line'
import { rangeToDiff } from '../utils/range-to-diff'
Expand Down Expand Up @@ -288,7 +289,7 @@ let analyzeModule = ({
;({ name } = nodeToParse.id)
dependencies = [
...dependencies,
...extractDependencies(nodeToParse.body),
...getEnumMembers(nodeToParse).flatMap(extractDependencies),
]
break
case AST_NODE_TYPES.ClassDeclaration:
Expand Down Expand Up @@ -483,7 +484,7 @@ let analyzeModule = ({
}

let extractDependencies = (
expression: TSESTree.TSEnumBody | TSESTree.ClassBody,
expression: TSESTree.TSEnumMember | TSESTree.ClassBody,
): string[] => {
let dependencies: string[] = []

Expand Down Expand Up @@ -569,10 +570,6 @@ let extractDependencies = (
checkNode(nodeValue.right)
}

if ('members' in nodeValue) {
traverseNode(nodeValue.members)
}

if ('initializer' in nodeValue && nodeValue.initializer) {
checkNode(nodeValue.initializer)
}
Expand Down
7 changes: 7 additions & 0 deletions utils/get-enum-members.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import type { TSESTree } from '@typescript-eslint/types'

export let getEnumMembers = (
value: TSESTree.TSEnumDeclaration,
/* v8 ignore next 2 */
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition -- Handle deprecated property
): TSESTree.TSEnumMember[] => value.body?.members ?? value.members

0 comments on commit 979ece8

Please sign in to comment.