Skip to content

Commit 663e016

Browse files
authored
feat(react): react compiler preset (#784)
1 parent 2618575 commit 663e016

File tree

3 files changed

+37
-5
lines changed

3 files changed

+37
-5
lines changed

src/configs/react.ts

Lines changed: 31 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/* eslint-disable perfectionist/sort-objects */
2-
import type { OptionsFiles, OptionsOverrides, OptionsTypeScriptParserOptions, OptionsTypeScriptWithTypes, TypedFlatConfigItem } from '../types'
2+
import type { OptionsFiles, OptionsReact, OptionsTypeScriptParserOptions, OptionsTypeScriptWithTypes, TypedFlatConfigItem } from '../types'
33

44
import { isPackageExists } from 'local-pkg'
55
import { GLOB_ASTRO_TS, GLOB_MARKDOWN, GLOB_SRC, GLOB_TS, GLOB_TSX } from '../globs'
@@ -25,9 +25,12 @@ const ReactRouterPackages = [
2525
const NextJsPackages = [
2626
'next',
2727
]
28+
const ReactCompilerPackages = [
29+
'babel-plugin-react-compiler',
30+
]
2831

2932
export async function react(
30-
options: OptionsTypeScriptParserOptions & OptionsTypeScriptWithTypes & OptionsOverrides & OptionsFiles = {},
33+
options: OptionsTypeScriptParserOptions & OptionsTypeScriptWithTypes & OptionsReact & OptionsFiles = {},
3134
): Promise<TypedFlatConfigItem[]> {
3235
const {
3336
files = [GLOB_SRC],
@@ -38,6 +41,7 @@ export async function react(
3841
],
3942
overrides = {},
4043
tsconfigPath,
44+
reactCompiler = ReactCompilerPackages.some(i => isPackageExists(i)),
4145
} = options
4246

4347
await ensurePackages([
@@ -153,8 +157,31 @@ export async function react(
153157
'react-dom/no-use-form-state': 'error',
154158
'react-dom/no-void-elements-with-children': 'error',
155159

156-
// recommended rules eslint-plugin-react-hooks https://github.com/facebook/react/tree/main/packages/eslint-plugin-react-hooks/src/rules
157-
...pluginReactHooks.configs.recommended.rules,
160+
// recommended rules eslint-plugin-react-hooks https://github.com/facebook/react/blob/main/packages/eslint-plugin-react-hooks/README.md
161+
// Core hooks rules
162+
'react-hooks/rules-of-hooks': 'error',
163+
'react-hooks/exhaustive-deps': 'warn',
164+
165+
// React Compiler rules
166+
...(reactCompiler
167+
? {
168+
'react-hooks/config': 'error',
169+
'react-hooks/error-boundaries': 'error',
170+
'react-hooks/component-hook-factories': 'error',
171+
'react-hooks/gating': 'error',
172+
'react-hooks/globals': 'error',
173+
'react-hooks/immutability': 'error',
174+
'react-hooks/preserve-manual-memoization': 'error',
175+
'react-hooks/purity': 'error',
176+
'react-hooks/refs': 'error',
177+
'react-hooks/set-state-in-effect': 'error',
178+
'react-hooks/set-state-in-render': 'error',
179+
'react-hooks/static-components': 'error',
180+
'react-hooks/unsupported-syntax': 'warn',
181+
'react-hooks/use-memo': 'error',
182+
'react-hooks/incompatible-library': 'warn',
183+
}
184+
: {}),
158185

159186
// recommended rules from eslint-plugin-react-hooks-extra https://eslint-react.xyz/docs/rules/overview#hooks-extra-rules
160187
'react-hooks-extra/no-direct-set-state-in-use-effect': 'warn',

src/factory.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -229,6 +229,7 @@ export function antfu(
229229
if (enableReact) {
230230
configs.push(react({
231231
...typescriptOptions,
232+
...resolveSubOptions(options, 'react'),
232233
overrides: getOverrides(options, 'react'),
233234
tsconfigPath,
234235
}))

src/types.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -270,6 +270,10 @@ export interface OptionsUnoCSS extends OptionsOverrides {
270270
strict?: boolean
271271
}
272272

273+
export interface OptionsReact extends OptionsOverrides {
274+
reactCompiler?: boolean
275+
}
276+
273277
export interface OptionsConfig extends OptionsComponentExts, OptionsProjectType {
274278
/**
275279
* Enable gitignore support.
@@ -422,7 +426,7 @@ export interface OptionsConfig extends OptionsComponentExts, OptionsProjectType
422426
*
423427
* @default false
424428
*/
425-
react?: boolean | OptionsOverrides
429+
react?: boolean | OptionsReact
426430

427431
/**
428432
* Enable nextjs rules.

0 commit comments

Comments
 (0)