diff --git a/packages/eslint-plugin-react-hooks/src/index.ts b/packages/eslint-plugin-react-hooks/src/index.ts index 924299d89894..3b26d1bb25c6 100644 --- a/packages/eslint-plugin-react-hooks/src/index.ts +++ b/packages/eslint-plugin-react-hooks/src/index.ts @@ -58,10 +58,20 @@ const recommendedLatestRuleConfigs: Linter.RulesRecord = { const plugins = ['react-hooks']; type ReactHooksFlatConfig = { - plugins: {react: any}; + plugins: {'react-hooks': typeof plugin}; rules: Linter.RulesRecord; }; +type FlatConfigs = { + recommended: ReactHooksFlatConfig; + 'recommended-latest': ReactHooksFlatConfig; +}; + +// We need to declare the type explicitly because `flat` is populated after +// `plugin` is defined (due to the circular reference: plugin.configs.flat +// needs to reference plugin itself). +let flatConfigs: FlatConfigs; + const configs = { recommended: { plugins, @@ -71,9 +81,8 @@ const configs = { plugins, rules: recommendedLatestRuleConfigs, }, - flat: {} as { - recommended: ReactHooksFlatConfig; - 'recommended-latest': ReactHooksFlatConfig; + get flat(): FlatConfigs { + return flatConfigs; }, }; @@ -86,7 +95,8 @@ const plugin = { configs, }; -Object.assign(configs.flat, { +// Initialize flat configs after plugin is defined to allow self-reference +flatConfigs = { 'recommended-latest': { plugins: {'react-hooks': plugin}, rules: configs['recommended-latest'].rules, @@ -95,6 +105,6 @@ Object.assign(configs.flat, { plugins: {'react-hooks': plugin}, rules: configs.recommended.rules, }, -}); +}; export default plugin;