diff --git a/rules/sort-modules.ts b/rules/sort-modules.ts index 6db048b8..eed01837 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 e64f3323..5dbce2dc 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