diff --git a/rules/sort-modules.ts b/rules/sort-modules.ts index 6db048b82..eed01837c 100644 --- a/rules/sort-modules.ts +++ b/rules/sort-modules.ts @@ -2,6 +2,7 @@ import type { TSESTree } from '@typescript-eslint/types' import type { TSESLint } from '@typescript-eslint/utils' import { AST_NODE_TYPES } from '@typescript-eslint/utils' +import { isSortable } from 'utils/is-sortable' import type { SortModulesOptions, @@ -192,14 +193,17 @@ export default createEslintRule({ }) return { - Program: program => - analyzeModule({ - eslintDisabledLines, - sourceCode, - options, - program, - context, - }), + Program: program => { + if (isSortable(program.body)) { + return analyzeModule({ + eslintDisabledLines, + sourceCode, + options, + program, + context, + }) + } + }, } }, defaultOptions: [defaultOptions], diff --git a/utils/is-sortable.ts b/utils/is-sortable.ts index e64f33230..5dbce2dcb 100644 --- a/utils/is-sortable.ts +++ b/utils/is-sortable.ts @@ -1 +1,2 @@ -export let isSortable = (array: unknown[]): boolean => array.length > 1 +export let isSortable = (node: unknown): boolean => + Array.isArray(node) && node.length > 1