Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 16 additions & 6 deletions packages/eslint-plugin-react-hooks/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -71,9 +81,8 @@ const configs = {
plugins,
rules: recommendedLatestRuleConfigs,
},
flat: {} as {
recommended: ReactHooksFlatConfig;
'recommended-latest': ReactHooksFlatConfig;
get flat(): FlatConfigs {
return flatConfigs;
},
};

Expand All @@ -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,
Expand All @@ -95,6 +105,6 @@ Object.assign(configs.flat, {
plugins: {'react-hooks': plugin},
rules: configs.recommended.rules,
},
});
};

export default plugin;