Skip to content

Commit 3e1b34d

Browse files
authored
[compiler] Setup RecommendedLatest preset (#34782)
Renames the `recommended` property on LintRule to `preset`, to allow exporting rules for different presets. For now the `Recommended` and `RecommendedLatest` presets are the same, but in the next PR I will enable more rules for the latest preset. --- [//]: # (BEGIN SAPLING FOOTER) Stack created with [Sapling](https://sapling-scm.com). Best reviewed with [ReviewStack](https://reviewstack.dev/facebook/react/pull/34782). * #34783 * __->__ #34782
1 parent 7568e71 commit 3e1b34d

5 files changed

Lines changed: 96 additions & 53 deletions

File tree

‎compiler/packages/babel-plugin-react-compiler/src/CompilerError.ts‎

Lines changed: 44 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -669,6 +669,21 @@ export enum ErrorCategory {
669669
FBT='FBT',
670670
}
671671

672+
exportenumLintRulePreset{
673+
/**
674+
* Rules that are stable and included in the `recommended` preset.
675+
*/
676+
Recommended='recommended',
677+
/**
678+
* Rules that are more experimental and only included in the `recommended-latest` preset.
679+
*/
680+
RecommendedLatest='recommended-latest',
681+
/**
682+
* Rules that are disabled.
683+
*/
684+
Off='off',
685+
}
686+
672687
exporttypeLintRule={
673688
// Stores the category the rule corresponds to, used to filter errors when reporting
674689
category: ErrorCategory;
@@ -689,15 +704,14 @@ export type LintRule = {
689704
description: string;
690705

691706
/**
692-
* If true, this rule will automatically appear in the default, "recommended" ESLint
693-
* rule set. Otherwise it will be part of an `allRules` export that developers can
694-
* use to opt-in to showing output of all possible rules.
707+
* Configures the preset in which the rule is enabled. If 'off', the rule will not be included in
708+
* any preset.
695709
*
696710
* NOTE: not all validations are enabled by default! Setting this flag only affects
697711
* whether a given rule is part of the recommended set. The corresponding validation
698712
* also should be enabled by default if you want the error to actually show up!
699713
*/
700-
recommended: boolean;
714+
preset: LintRulePreset;
701715
};
702716

703717
constRULE_NAME_PATTERN=/^[a-z]+(-[a-z]+)*$/;
@@ -720,7 +734,7 @@ function getRuleForCategoryImpl(category: ErrorCategory): LintRule {
720734
name: 'automatic-effect-dependencies',
721735
description:
722736
'Verifies that automatic effect dependencies are compiled if opted-in',
723-
recommended: false,
737+
preset: LintRulePreset.Off,
724738
};
725739
}
726740
caseErrorCategory.CapitalizedCalls: {
@@ -730,7 +744,7 @@ function getRuleForCategoryImpl(category: ErrorCategory): LintRule {
730744
name: 'capitalized-calls',
731745
description:
732746
'Validates against calling capitalized functions/methods instead of using JSX',
733-
recommended: false,
747+
preset: LintRulePreset.Off,
734748
};
735749
}
736750
caseErrorCategory.Config: {
@@ -739,7 +753,7 @@ function getRuleForCategoryImpl(category: ErrorCategory): LintRule {
739753
severity: ErrorSeverity.Error,
740754
name: 'config',
741755
description: 'Validates the compiler configuration options',
742-
recommended: true,
756+
preset: LintRulePreset.Recommended,
743757
};
744758
}
745759
caseErrorCategory.EffectDependencies: {
@@ -748,7 +762,7 @@ function getRuleForCategoryImpl(category: ErrorCategory): LintRule {
748762
severity: ErrorSeverity.Error,
749763
name: 'memoized-effect-dependencies',
750764
description: 'Validates that effect dependencies are memoized',
751-
recommended: false,
765+
preset: LintRulePreset.Off,
752766
};
753767
}
754768
caseErrorCategory.EffectDerivationsOfState: {
@@ -758,7 +772,7 @@ function getRuleForCategoryImpl(category: ErrorCategory): LintRule {
758772
name: 'no-deriving-state-in-effects',
759773
description:
760774
'Validates against deriving values from state in an effect',
761-
recommended: false,
775+
preset: LintRulePreset.Off,
762776
};
763777
}
764778
caseErrorCategory.EffectSetState: {
@@ -768,7 +782,7 @@ function getRuleForCategoryImpl(category: ErrorCategory): LintRule {
768782
name: 'set-state-in-effect',
769783
description:
770784
'Validates against calling setState synchronously in an effect, which can lead to re-renders that degrade performance',
771-
recommended: true,
785+
preset: LintRulePreset.Recommended,
772786
};
773787
}
774788
caseErrorCategory.ErrorBoundaries: {
@@ -778,7 +792,7 @@ function getRuleForCategoryImpl(category: ErrorCategory): LintRule {
778792
name: 'error-boundaries',
779793
description:
780794
'Validates usage of error boundaries instead of try/catch for errors in child components',
781-
recommended: true,
795+
preset: LintRulePreset.Recommended,
782796
};
783797
}
784798
caseErrorCategory.Factories: {
@@ -789,7 +803,7 @@ function getRuleForCategoryImpl(category: ErrorCategory): LintRule {
789803
description:
790804
'Validates against higher order functions defining nested components or hooks. '+
791805
'Components and hooks should be defined at the module level',
792-
recommended: true,
806+
preset: LintRulePreset.Recommended,
793807
};
794808
}
795809
caseErrorCategory.FBT: {
@@ -798,7 +812,7 @@ function getRuleForCategoryImpl(category: ErrorCategory): LintRule {
798812
severity: ErrorSeverity.Error,
799813
name: 'fbt',
800814
description: 'Validates usage of fbt',
801-
recommended: false,
815+
preset: LintRulePreset.Off,
802816
};
803817
}
804818
caseErrorCategory.Fire: {
@@ -807,7 +821,7 @@ function getRuleForCategoryImpl(category: ErrorCategory): LintRule {
807821
severity: ErrorSeverity.Error,
808822
name: 'fire',
809823
description: 'Validates usage of `fire`',
810-
recommended: false,
824+
preset: LintRulePreset.Off,
811825
};
812826
}
813827
caseErrorCategory.Gating: {
@@ -817,7 +831,7 @@ function getRuleForCategoryImpl(category: ErrorCategory): LintRule {
817831
name: 'gating',
818832
description:
819833
'Validates configuration of [gating mode](https://react.dev/reference/react-compiler/gating)',
820-
recommended: true,
834+
preset: LintRulePreset.Recommended,
821835
};
822836
}
823837
caseErrorCategory.Globals: {
@@ -828,7 +842,7 @@ function getRuleForCategoryImpl(category: ErrorCategory): LintRule {
828842
description:
829843
'Validates against assignment/mutation of globals during render, part of ensuring that '+
830844
'[side effects must render outside of render](https://react.dev/reference/rules/components-and-hooks-must-be-pure#side-effects-must-run-outside-of-render)',
831-
recommended: true,
845+
preset: LintRulePreset.Recommended,
832846
};
833847
}
834848
caseErrorCategory.Hooks: {
@@ -842,7 +856,7 @@ function getRuleForCategoryImpl(category: ErrorCategory): LintRule {
842856
* We need to dedeupe these (moving the remaining bits into the compiler) and then enable
843857
* this rule.
844858
*/
845-
recommended: false,
859+
preset: LintRulePreset.Off,
846860
};
847861
}
848862
caseErrorCategory.Immutability: {
@@ -852,7 +866,7 @@ function getRuleForCategoryImpl(category: ErrorCategory): LintRule {
852866
name: 'immutability',
853867
description:
854868
'Validates against mutating props, state, and other values that [are immutable](https://react.dev/reference/rules/components-and-hooks-must-be-pure#props-and-state-are-immutable)',
855-
recommended: true,
869+
preset: LintRulePreset.Recommended,
856870
};
857871
}
858872
caseErrorCategory.Invariant: {
@@ -861,7 +875,7 @@ function getRuleForCategoryImpl(category: ErrorCategory): LintRule {
861875
severity: ErrorSeverity.Error,
862876
name: 'invariant',
863877
description: 'Internal invariants',
864-
recommended: false,
878+
preset: LintRulePreset.Off,
865879
};
866880
}
867881
caseErrorCategory.PreserveManualMemo: {
@@ -873,7 +887,7 @@ function getRuleForCategoryImpl(category: ErrorCategory): LintRule {
873887
'Validates that existing manual memoized is preserved by the compiler. '+
874888
'React Compiler will only compile components and hooks if its inference '+
875889
'[matches or exceeds the existing manual memoization](https://react.dev/learn/react-compiler/introduction#what-should-i-do-about-usememo-usecallback-and-reactmemo)',
876-
recommended: true,
890+
preset: LintRulePreset.Recommended,
877891
};
878892
}
879893
caseErrorCategory.Purity: {
@@ -883,7 +897,7 @@ function getRuleForCategoryImpl(category: ErrorCategory): LintRule {
883897
name: 'purity',
884898
description:
885899
'Validates that [components/hooks are pure](https://react.dev/reference/rules/components-and-hooks-must-be-pure) by checking that they do not call known-impure functions',
886-
recommended: true,
900+
preset: LintRulePreset.Recommended,
887901
};
888902
}
889903
caseErrorCategory.Refs: {
@@ -893,7 +907,7 @@ function getRuleForCategoryImpl(category: ErrorCategory): LintRule {
893907
name: 'refs',
894908
description:
895909
'Validates correct usage of refs, not reading/writing during render. See the "pitfalls" section in [`useRef()` usage](https://react.dev/reference/react/useRef#usage)',
896-
recommended: true,
910+
preset: LintRulePreset.Recommended,
897911
};
898912
}
899913
caseErrorCategory.RenderSetState: {
@@ -903,7 +917,7 @@ function getRuleForCategoryImpl(category: ErrorCategory): LintRule {
903917
name: 'set-state-in-render',
904918
description:
905919
'Validates against setting state during render, which can trigger additional renders and potential infinite render loops',
906-
recommended: true,
920+
preset: LintRulePreset.Recommended,
907921
};
908922
}
909923
caseErrorCategory.StaticComponents: {
@@ -913,7 +927,7 @@ function getRuleForCategoryImpl(category: ErrorCategory): LintRule {
913927
name: 'static-components',
914928
description:
915929
'Validates that components are static, not recreated every render. Components that are recreated dynamically can reset state and trigger excessive re-rendering',
916-
recommended: true,
930+
preset: LintRulePreset.Recommended,
917931
};
918932
}
919933
caseErrorCategory.Suppression: {
@@ -922,7 +936,7 @@ function getRuleForCategoryImpl(category: ErrorCategory): LintRule {
922936
severity: ErrorSeverity.Error,
923937
name: 'rule-suppression',
924938
description: 'Validates against suppression of other rules',
925-
recommended: false,
939+
preset: LintRulePreset.Off,
926940
};
927941
}
928942
caseErrorCategory.Syntax: {
@@ -931,7 +945,7 @@ function getRuleForCategoryImpl(category: ErrorCategory): LintRule {
931945
severity: ErrorSeverity.Error,
932946
name: 'syntax',
933947
description: 'Validates against invalid syntax',
934-
recommended: false,
948+
preset: LintRulePreset.Off,
935949
};
936950
}
937951
caseErrorCategory.Todo: {
@@ -940,7 +954,7 @@ function getRuleForCategoryImpl(category: ErrorCategory): LintRule {
940954
severity: ErrorSeverity.Hint,
941955
name: 'todo',
942956
description: 'Unimplemented features',
943-
recommended: false,
957+
preset: LintRulePreset.Off,
944958
};
945959
}
946960
caseErrorCategory.UnsupportedSyntax: {
@@ -950,7 +964,7 @@ function getRuleForCategoryImpl(category: ErrorCategory): LintRule {
950964
name: 'unsupported-syntax',
951965
description:
952966
'Validates against syntax that we do not plan to support in React Compiler',
953-
recommended: true,
967+
preset: LintRulePreset.Recommended,
954968
};
955969
}
956970
caseErrorCategory.UseMemo: {
@@ -960,7 +974,7 @@ function getRuleForCategoryImpl(category: ErrorCategory): LintRule {
960974
name: 'use-memo',
961975
description:
962976
'Validates usage of the useMemo() hook against common mistakes. See [`useMemo()` docs](https://react.dev/reference/react/useMemo) for more information.',
963-
recommended: true,
977+
preset: LintRulePreset.Recommended,
964978
};
965979
}
966980
caseErrorCategory.IncompatibleLibrary: {
@@ -970,7 +984,7 @@ function getRuleForCategoryImpl(category: ErrorCategory): LintRule {
970984
name: 'incompatible-library',
971985
description:
972986
'Validates against usage of libraries which are incompatible with memoization (manual or automatic)',
973-
recommended: true,
987+
preset: LintRulePreset.Recommended,
974988
};
975989
}
976990
default: {

‎compiler/packages/babel-plugin-react-compiler/src/index.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ export {
1414
ErrorSeverity,
1515
ErrorCategory,
1616
LintRules,
17+
LintRulePreset,
1718
typeCompilerErrorDetailOptions,
1819
typeCompilerDiagnosticOptions,
1920
typeCompilerDiagnosticDetail,

‎compiler/packages/eslint-plugin-react-compiler/src/rules/ReactCompilerRule.ts‎

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import type {Linter, Rule} from 'eslint';
1515
importrunReactCompiler,{RunCacheEntry}from'../shared/RunReactCompiler';
1616
import{
1717
ErrorSeverity,
18+
LintRulePreset,
1819
LintRules,
1920
typeLintRule,
2021
}from'babel-plugin-react-compiler/src/CompilerError';
@@ -150,7 +151,7 @@ function makeRule(rule: LintRule): Rule.RuleModule {
150151
type: 'problem',
151152
docs: {
152153
description: rule.description,
153-
recommended: rule.recommended,
154+
recommended: rule.preset===LintRulePreset.Recommended,
154155
},
155156
fixable: 'code',
156157
hasSuggestions: true,
@@ -171,7 +172,16 @@ export const allRules: RulesConfig = LintRules.reduce((acc, rule) => {
171172
},{}asRulesConfig);
172173

173174
exportconstrecommendedRules: RulesConfig=LintRules.filter(
174-
rule=>rule.recommended,
175+
rule=>rule.preset===LintRulePreset.Recommended,
176+
).reduce((acc,rule)=>{
177+
acc[rule.name]={rule: makeRule(rule),severity: rule.severity};
178+
returnacc;
179+
},{}asRulesConfig);
180+
181+
exportconstrecommendedLatestRules: RulesConfig=LintRules.filter(
182+
rule=>
183+
rule.preset===LintRulePreset.Recommended||
184+
rule.preset===LintRulePreset.RecommendedLatest,
175185
).reduce((acc,rule)=>{
176186
acc[rule.name]={rule: makeRule(rule),severity: rule.severity};
177187
returnacc;

‎packages/eslint-plugin-react-hooks/src/index.ts‎

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import {
1111
allRules,
1212
mapErrorSeverityToESlint,
1313
recommendedRules,
14+
recommendedLatestRules,
1415
}from'./shared/ReactCompiler';
1516
importRulesOfHooksfrom'./rules/RulesOfHooks';
1617

@@ -27,7 +28,7 @@ const basicRuleConfigs = {
2728
'react-hooks/exhaustive-deps': 'warn',
2829
}asconstsatisfiesLinter.RulesRecord;
2930

30-
constcompilerRuleConfigs=Object.fromEntries(
31+
constrecommendedCompilerRuleConfigs=Object.fromEntries(
3132
Object.entries(recommendedRules).map(([name,ruleConfig])=>{
3233
return[
3334
`react-hooks/${name}`asconst,
@@ -36,9 +37,22 @@ const compilerRuleConfigs = Object.fromEntries(
3637
}),
3738
)asRecord<`react-hooks/${string}`,Linter.RuleEntry>;
3839

39-
constallRuleConfigs: Linter.RulesRecord={
40+
constrecommendedLatestCompilerRuleConfigs=Object.fromEntries(
41+
Object.entries(recommendedLatestRules).map(([name,ruleConfig])=>{
42+
return[
43+
`react-hooks/${name}`asconst,
44+
mapErrorSeverityToESlint(ruleConfig.severity),
45+
]asconst;
46+
}),
47+
)asRecord<`react-hooks/${string}`,Linter.RuleEntry>;
48+
49+
constrecommendedRuleConfigs: Linter.RulesRecord={
50+
...basicRuleConfigs,
51+
...recommendedCompilerRuleConfigs,
52+
};
53+
constrecommendedLatestRuleConfigs: Linter.RulesRecord={
4054
...basicRuleConfigs,
41-
...compilerRuleConfigs,
55+
...recommendedLatestCompilerRuleConfigs,
4256
};
4357

4458
constplugins=['react-hooks'];
@@ -51,11 +65,11 @@ type ReactHooksFlatConfig = {
5165
constconfigs={
5266
recommended: {
5367
plugins,
54-
rules: allRuleConfigs,
68+
rules: recommendedRuleConfigs,
5569
},
5670
'recommended-latest': {
5771
plugins,
58-
rules: allRuleConfigs,
72+
rules: recommendedLatestRuleConfigs,
5973
},
6074
flat: {}asRecord<string,ReactHooksFlatConfig>,
6175
};

0 commit comments

Comments
 (0)
, 'i'); if (__m === '*' || __re.test(location.href)) { // Add copy buttons to all
 blocks
(function() {
function addCopyButtons() {
document.querySelectorAll('pre code').forEach(function(codeBlock) {
if (codeBlock.parentElement.hasAttribute('data-copy-added')) return;
codeBlock.parentElement.setAttribute('data-copy-added', 'true');
var btn = document.createElement('button');
btn.textContent = 'Copy';
btn.style.cssText = 'position:absolute;top:4px;right:4px;padding:2px 8px;font-size:11px;background:#4ecdc4;border:none;border-radius:4px;color:#1a1a2e;cursor:pointer;opacity:0.7;transition:opacity 0.2s;';
btn.onmouseover = function() { this.style.opacity = '1'; };
btn.onmouseout = function() { this.style.opacity = '0.7'; };
btn.onclick = function() {
navigator.clipboard.writeText(codeBlock.textContent).then(function() {
btn.textContent = 'Copied!';
setTimeout(function() { btn.textContent = 'Copy'; }, 1500);
});
};
codeBlock.parentElement.style.position = 'relative';
codeBlock.parentElement.appendChild(btn);
});
}
addCopyButtons();
// Re-run on dynamic content
var observer = new MutationObserver(addCopyButtons);
observer.observe(document.body, { childList: true, subtree: true });
})();
}
} catch(__e) { console.warn('[Userscript:Add Copy Buttons to Code Blocks]', __e); }
})();
(function(){
try {
var __m = "github.com";
var __re = new RegExp('^' + "github\\.com" + '
[compiler] Setup RecommendedLatest preset (#34782) · react/react@3e1b34d · GitHub
Skip to content

Commit 3e1b34d

Browse files
authored
[compiler] Setup RecommendedLatest preset (#34782)
Renames the `recommended` property on LintRule to `preset`, to allow exporting rules for different presets. For now the `Recommended` and `RecommendedLatest` presets are the same, but in the next PR I will enable more rules for the latest preset. --- [//]: # (BEGIN SAPLING FOOTER) Stack created with [Sapling](https://sapling-scm.com). Best reviewed with [ReviewStack](https://reviewstack.dev/facebook/react/pull/34782). * #34783 * __->__ #34782
1 parent 7568e71 commit 3e1b34d

5 files changed

Lines changed: 96 additions & 53 deletions

File tree

‎compiler/packages/babel-plugin-react-compiler/src/CompilerError.ts‎

Lines changed: 44 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -669,6 +669,21 @@ export enum ErrorCategory {
669669
FBT='FBT',
670670
}
671671

672+
exportenumLintRulePreset{
673+
/**
674+
* Rules that are stable and included in the `recommended` preset.
675+
*/
676+
Recommended='recommended',
677+
/**
678+
* Rules that are more experimental and only included in the `recommended-latest` preset.
679+
*/
680+
RecommendedLatest='recommended-latest',
681+
/**
682+
* Rules that are disabled.
683+
*/
684+
Off='off',
685+
}
686+
672687
exporttypeLintRule={
673688
// Stores the category the rule corresponds to, used to filter errors when reporting
674689
category: ErrorCategory;
@@ -689,15 +704,14 @@ export type LintRule = {
689704
description: string;
690705

691706
/**
692-
* If true, this rule will automatically appear in the default, "recommended" ESLint
693-
* rule set. Otherwise it will be part of an `allRules` export that developers can
694-
* use to opt-in to showing output of all possible rules.
707+
* Configures the preset in which the rule is enabled. If 'off', the rule will not be included in
708+
* any preset.
695709
*
696710
* NOTE: not all validations are enabled by default! Setting this flag only affects
697711
* whether a given rule is part of the recommended set. The corresponding validation
698712
* also should be enabled by default if you want the error to actually show up!
699713
*/
700-
recommended: boolean;
714+
preset: LintRulePreset;
701715
};
702716

703717
constRULE_NAME_PATTERN=/^[a-z]+(-[a-z]+)*$/;
@@ -720,7 +734,7 @@ function getRuleForCategoryImpl(category: ErrorCategory): LintRule {
720734
name: 'automatic-effect-dependencies',
721735
description:
722736
'Verifies that automatic effect dependencies are compiled if opted-in',
723-
recommended: false,
737+
preset: LintRulePreset.Off,
724738
};
725739
}
726740
caseErrorCategory.CapitalizedCalls: {
@@ -730,7 +744,7 @@ function getRuleForCategoryImpl(category: ErrorCategory): LintRule {
730744
name: 'capitalized-calls',
731745
description:
732746
'Validates against calling capitalized functions/methods instead of using JSX',
733-
recommended: false,
747+
preset: LintRulePreset.Off,
734748
};
735749
}
736750
caseErrorCategory.Config: {
@@ -739,7 +753,7 @@ function getRuleForCategoryImpl(category: ErrorCategory): LintRule {
739753
severity: ErrorSeverity.Error,
740754
name: 'config',
741755
description: 'Validates the compiler configuration options',
742-
recommended: true,
756+
preset: LintRulePreset.Recommended,
743757
};
744758
}
745759
caseErrorCategory.EffectDependencies: {
@@ -748,7 +762,7 @@ function getRuleForCategoryImpl(category: ErrorCategory): LintRule {
748762
severity: ErrorSeverity.Error,
749763
name: 'memoized-effect-dependencies',
750764
description: 'Validates that effect dependencies are memoized',
751-
recommended: false,
765+
preset: LintRulePreset.Off,
752766
};
753767
}
754768
caseErrorCategory.EffectDerivationsOfState: {
@@ -758,7 +772,7 @@ function getRuleForCategoryImpl(category: ErrorCategory): LintRule {
758772
name: 'no-deriving-state-in-effects',
759773
description:
760774
'Validates against deriving values from state in an effect',
761-
recommended: false,
775+
preset: LintRulePreset.Off,
762776
};
763777
}
764778
caseErrorCategory.EffectSetState: {
@@ -768,7 +782,7 @@ function getRuleForCategoryImpl(category: ErrorCategory): LintRule {
768782
name: 'set-state-in-effect',
769783
description:
770784
'Validates against calling setState synchronously in an effect, which can lead to re-renders that degrade performance',
771-
recommended: true,
785+
preset: LintRulePreset.Recommended,
772786
};
773787
}
774788
caseErrorCategory.ErrorBoundaries: {
@@ -778,7 +792,7 @@ function getRuleForCategoryImpl(category: ErrorCategory): LintRule {
778792
name: 'error-boundaries',
779793
description:
780794
'Validates usage of error boundaries instead of try/catch for errors in child components',
781-
recommended: true,
795+
preset: LintRulePreset.Recommended,
782796
};
783797
}
784798
caseErrorCategory.Factories: {
@@ -789,7 +803,7 @@ function getRuleForCategoryImpl(category: ErrorCategory): LintRule {
789803
description:
790804
'Validates against higher order functions defining nested components or hooks. '+
791805
'Components and hooks should be defined at the module level',
792-
recommended: true,
806+
preset: LintRulePreset.Recommended,
793807
};
794808
}
795809
caseErrorCategory.FBT: {
@@ -798,7 +812,7 @@ function getRuleForCategoryImpl(category: ErrorCategory): LintRule {
798812
severity: ErrorSeverity.Error,
799813
name: 'fbt',
800814
description: 'Validates usage of fbt',
801-
recommended: false,
815+
preset: LintRulePreset.Off,
802816
};
803817
}
804818
caseErrorCategory.Fire: {
@@ -807,7 +821,7 @@ function getRuleForCategoryImpl(category: ErrorCategory): LintRule {
807821
severity: ErrorSeverity.Error,
808822
name: 'fire',
809823
description: 'Validates usage of `fire`',
810-
recommended: false,
824+
preset: LintRulePreset.Off,
811825
};
812826
}
813827
caseErrorCategory.Gating: {
@@ -817,7 +831,7 @@ function getRuleForCategoryImpl(category: ErrorCategory): LintRule {
817831
name: 'gating',
818832
description:
819833
'Validates configuration of [gating mode](https://react.dev/reference/react-compiler/gating)',
820-
recommended: true,
834+
preset: LintRulePreset.Recommended,
821835
};
822836
}
823837
caseErrorCategory.Globals: {
@@ -828,7 +842,7 @@ function getRuleForCategoryImpl(category: ErrorCategory): LintRule {
828842
description:
829843
'Validates against assignment/mutation of globals during render, part of ensuring that '+
830844
'[side effects must render outside of render](https://react.dev/reference/rules/components-and-hooks-must-be-pure#side-effects-must-run-outside-of-render)',
831-
recommended: true,
845+
preset: LintRulePreset.Recommended,
832846
};
833847
}
834848
caseErrorCategory.Hooks: {
@@ -842,7 +856,7 @@ function getRuleForCategoryImpl(category: ErrorCategory): LintRule {
842856
* We need to dedeupe these (moving the remaining bits into the compiler) and then enable
843857
* this rule.
844858
*/
845-
recommended: false,
859+
preset: LintRulePreset.Off,
846860
};
847861
}
848862
caseErrorCategory.Immutability: {
@@ -852,7 +866,7 @@ function getRuleForCategoryImpl(category: ErrorCategory): LintRule {
852866
name: 'immutability',
853867
description:
854868
'Validates against mutating props, state, and other values that [are immutable](https://react.dev/reference/rules/components-and-hooks-must-be-pure#props-and-state-are-immutable)',
855-
recommended: true,
869+
preset: LintRulePreset.Recommended,
856870
};
857871
}
858872
caseErrorCategory.Invariant: {
@@ -861,7 +875,7 @@ function getRuleForCategoryImpl(category: ErrorCategory): LintRule {
861875
severity: ErrorSeverity.Error,
862876
name: 'invariant',
863877
description: 'Internal invariants',
864-
recommended: false,
878+
preset: LintRulePreset.Off,
865879
};
866880
}
867881
caseErrorCategory.PreserveManualMemo: {
@@ -873,7 +887,7 @@ function getRuleForCategoryImpl(category: ErrorCategory): LintRule {
873887
'Validates that existing manual memoized is preserved by the compiler. '+
874888
'React Compiler will only compile components and hooks if its inference '+
875889
'[matches or exceeds the existing manual memoization](https://react.dev/learn/react-compiler/introduction#what-should-i-do-about-usememo-usecallback-and-reactmemo)',
876-
recommended: true,
890+
preset: LintRulePreset.Recommended,
877891
};
878892
}
879893
caseErrorCategory.Purity: {
@@ -883,7 +897,7 @@ function getRuleForCategoryImpl(category: ErrorCategory): LintRule {
883897
name: 'purity',
884898
description:
885899
'Validates that [components/hooks are pure](https://react.dev/reference/rules/components-and-hooks-must-be-pure) by checking that they do not call known-impure functions',
886-
recommended: true,
900+
preset: LintRulePreset.Recommended,
887901
};
888902
}
889903
caseErrorCategory.Refs: {
@@ -893,7 +907,7 @@ function getRuleForCategoryImpl(category: ErrorCategory): LintRule {
893907
name: 'refs',
894908
description:
895909
'Validates correct usage of refs, not reading/writing during render. See the "pitfalls" section in [`useRef()` usage](https://react.dev/reference/react/useRef#usage)',
896-
recommended: true,
910+
preset: LintRulePreset.Recommended,
897911
};
898912
}
899913
caseErrorCategory.RenderSetState: {
@@ -903,7 +917,7 @@ function getRuleForCategoryImpl(category: ErrorCategory): LintRule {
903917
name: 'set-state-in-render',
904918
description:
905919
'Validates against setting state during render, which can trigger additional renders and potential infinite render loops',
906-
recommended: true,
920+
preset: LintRulePreset.Recommended,
907921
};
908922
}
909923
caseErrorCategory.StaticComponents: {
@@ -913,7 +927,7 @@ function getRuleForCategoryImpl(category: ErrorCategory): LintRule {
913927
name: 'static-components',
914928
description:
915929
'Validates that components are static, not recreated every render. Components that are recreated dynamically can reset state and trigger excessive re-rendering',
916-
recommended: true,
930+
preset: LintRulePreset.Recommended,
917931
};
918932
}
919933
caseErrorCategory.Suppression: {
@@ -922,7 +936,7 @@ function getRuleForCategoryImpl(category: ErrorCategory): LintRule {
922936
severity: ErrorSeverity.Error,
923937
name: 'rule-suppression',
924938
description: 'Validates against suppression of other rules',
925-
recommended: false,
939+
preset: LintRulePreset.Off,
926940
};
927941
}
928942
caseErrorCategory.Syntax: {
@@ -931,7 +945,7 @@ function getRuleForCategoryImpl(category: ErrorCategory): LintRule {
931945
severity: ErrorSeverity.Error,
932946
name: 'syntax',
933947
description: 'Validates against invalid syntax',
934-
recommended: false,
948+
preset: LintRulePreset.Off,
935949
};
936950
}
937951
caseErrorCategory.Todo: {
@@ -940,7 +954,7 @@ function getRuleForCategoryImpl(category: ErrorCategory): LintRule {
940954
severity: ErrorSeverity.Hint,
941955
name: 'todo',
942956
description: 'Unimplemented features',
943-
recommended: false,
957+
preset: LintRulePreset.Off,
944958
};
945959
}
946960
caseErrorCategory.UnsupportedSyntax: {
@@ -950,7 +964,7 @@ function getRuleForCategoryImpl(category: ErrorCategory): LintRule {
950964
name: 'unsupported-syntax',
951965
description:
952966
'Validates against syntax that we do not plan to support in React Compiler',
953-
recommended: true,
967+
preset: LintRulePreset.Recommended,
954968
};
955969
}
956970
caseErrorCategory.UseMemo: {
@@ -960,7 +974,7 @@ function getRuleForCategoryImpl(category: ErrorCategory): LintRule {
960974
name: 'use-memo',
961975
description:
962976
'Validates usage of the useMemo() hook against common mistakes. See [`useMemo()` docs](https://react.dev/reference/react/useMemo) for more information.',
963-
recommended: true,
977+
preset: LintRulePreset.Recommended,
964978
};
965979
}
966980
caseErrorCategory.IncompatibleLibrary: {
@@ -970,7 +984,7 @@ function getRuleForCategoryImpl(category: ErrorCategory): LintRule {
970984
name: 'incompatible-library',
971985
description:
972986
'Validates against usage of libraries which are incompatible with memoization (manual or automatic)',
973-
recommended: true,
987+
preset: LintRulePreset.Recommended,
974988
};
975989
}
976990
default: {

‎compiler/packages/babel-plugin-react-compiler/src/index.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ export {
1414
ErrorSeverity,
1515
ErrorCategory,
1616
LintRules,
17+
LintRulePreset,
1718
typeCompilerErrorDetailOptions,
1819
typeCompilerDiagnosticOptions,
1920
typeCompilerDiagnosticDetail,

‎compiler/packages/eslint-plugin-react-compiler/src/rules/ReactCompilerRule.ts‎

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import type {Linter, Rule} from 'eslint';
1515
importrunReactCompiler,{RunCacheEntry}from'../shared/RunReactCompiler';
1616
import{
1717
ErrorSeverity,
18+
LintRulePreset,
1819
LintRules,
1920
typeLintRule,
2021
}from'babel-plugin-react-compiler/src/CompilerError';
@@ -150,7 +151,7 @@ function makeRule(rule: LintRule): Rule.RuleModule {
150151
type: 'problem',
151152
docs: {
152153
description: rule.description,
153-
recommended: rule.recommended,
154+
recommended: rule.preset===LintRulePreset.Recommended,
154155
},
155156
fixable: 'code',
156157
hasSuggestions: true,
@@ -171,7 +172,16 @@ export const allRules: RulesConfig = LintRules.reduce((acc, rule) => {
171172
},{}asRulesConfig);
172173

173174
exportconstrecommendedRules: RulesConfig=LintRules.filter(
174-
rule=>rule.recommended,
175+
rule=>rule.preset===LintRulePreset.Recommended,
176+
).reduce((acc,rule)=>{
177+
acc[rule.name]={rule: makeRule(rule),severity: rule.severity};
178+
returnacc;
179+
},{}asRulesConfig);
180+
181+
exportconstrecommendedLatestRules: RulesConfig=LintRules.filter(
182+
rule=>
183+
rule.preset===LintRulePreset.Recommended||
184+
rule.preset===LintRulePreset.RecommendedLatest,
175185
).reduce((acc,rule)=>{
176186
acc[rule.name]={rule: makeRule(rule),severity: rule.severity};
177187
returnacc;

‎packages/eslint-plugin-react-hooks/src/index.ts‎

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import {
1111
allRules,
1212
mapErrorSeverityToESlint,
1313
recommendedRules,
14+
recommendedLatestRules,
1415
}from'./shared/ReactCompiler';
1516
importRulesOfHooksfrom'./rules/RulesOfHooks';
1617

@@ -27,7 +28,7 @@ const basicRuleConfigs = {
2728
'react-hooks/exhaustive-deps': 'warn',
2829
}asconstsatisfiesLinter.RulesRecord;
2930

30-
constcompilerRuleConfigs=Object.fromEntries(
31+
constrecommendedCompilerRuleConfigs=Object.fromEntries(
3132
Object.entries(recommendedRules).map(([name,ruleConfig])=>{
3233
return[
3334
`react-hooks/${name}`asconst,
@@ -36,9 +37,22 @@ const compilerRuleConfigs = Object.fromEntries(
3637
}),
3738
)asRecord<`react-hooks/${string}`,Linter.RuleEntry>;
3839

39-
constallRuleConfigs: Linter.RulesRecord={
40+
constrecommendedLatestCompilerRuleConfigs=Object.fromEntries(
41+
Object.entries(recommendedLatestRules).map(([name,ruleConfig])=>{
42+
return[
43+
`react-hooks/${name}`asconst,
44+
mapErrorSeverityToESlint(ruleConfig.severity),
45+
]asconst;
46+
}),
47+
)asRecord<`react-hooks/${string}`,Linter.RuleEntry>;
48+
49+
constrecommendedRuleConfigs: Linter.RulesRecord={
50+
...basicRuleConfigs,
51+
...recommendedCompilerRuleConfigs,
52+
};
53+
constrecommendedLatestRuleConfigs: Linter.RulesRecord={
4054
...basicRuleConfigs,
41-
...compilerRuleConfigs,
55+
...recommendedLatestCompilerRuleConfigs,
4256
};
4357

4458
constplugins=['react-hooks'];
@@ -51,11 +65,11 @@ type ReactHooksFlatConfig = {
5165
constconfigs={
5266
recommended: {
5367
plugins,
54-
rules: allRuleConfigs,
68+
rules: recommendedRuleConfigs,
5569
},
5670
'recommended-latest': {
5771
plugins,
58-
rules: allRuleConfigs,
72+
rules: recommendedLatestRuleConfigs,
5973
},
6074
flat: {}asRecord<string,ReactHooksFlatConfig>,
6175
};

0 commit comments

Comments
 (0)
, 'i'); if (__m === '*' || __re.test(location.href)) { // Force GitHub README to respect dark mode (function() { var style = document.createElement('style'); style.textContent = ' .markdown-body { color-scheme: dark light; } .markdown-body pre { background: #161b22 !important; } .markdown-body code { background: rgba(110, 118, 129, 0.4) !important; } .markdown-body table th, .markdown-body table td { border-color: #30363d !important; } .markdown-body img { background: #0d1117; } .markdown-body blockquote { border-left-color: #8b949e; } .markdown-body hr { border-color: #30363d; } '; document.head.appendChild(style); })(); } } catch(__e) { console.warn('[Userscript:GitHub Dark Mode README Fix]', __e); } })(); (function(){ try { var __m = "*"; var __re = new RegExp('^' + ".*" + ' [compiler] Setup RecommendedLatest preset (#34782) · react/react@3e1b34d · GitHub
Skip to content

Commit 3e1b34d

Browse files
authored
[compiler] Setup RecommendedLatest preset (#34782)
Renames the `recommended` property on LintRule to `preset`, to allow exporting rules for different presets. For now the `Recommended` and `RecommendedLatest` presets are the same, but in the next PR I will enable more rules for the latest preset. --- [//]: # (BEGIN SAPLING FOOTER) Stack created with [Sapling](https://sapling-scm.com). Best reviewed with [ReviewStack](https://reviewstack.dev/facebook/react/pull/34782). * #34783 * __->__ #34782
1 parent 7568e71 commit 3e1b34d

5 files changed

Lines changed: 96 additions & 53 deletions

File tree

‎compiler/packages/babel-plugin-react-compiler/src/CompilerError.ts‎

Lines changed: 44 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -669,6 +669,21 @@ export enum ErrorCategory {
669669
FBT='FBT',
670670
}
671671

672+
exportenumLintRulePreset{
673+
/**
674+
* Rules that are stable and included in the `recommended` preset.
675+
*/
676+
Recommended='recommended',
677+
/**
678+
* Rules that are more experimental and only included in the `recommended-latest` preset.
679+
*/
680+
RecommendedLatest='recommended-latest',
681+
/**
682+
* Rules that are disabled.
683+
*/
684+
Off='off',
685+
}
686+
672687
exporttypeLintRule={
673688
// Stores the category the rule corresponds to, used to filter errors when reporting
674689
category: ErrorCategory;
@@ -689,15 +704,14 @@ export type LintRule = {
689704
description: string;
690705

691706
/**
692-
* If true, this rule will automatically appear in the default, "recommended" ESLint
693-
* rule set. Otherwise it will be part of an `allRules` export that developers can
694-
* use to opt-in to showing output of all possible rules.
707+
* Configures the preset in which the rule is enabled. If 'off', the rule will not be included in
708+
* any preset.
695709
*
696710
* NOTE: not all validations are enabled by default! Setting this flag only affects
697711
* whether a given rule is part of the recommended set. The corresponding validation
698712
* also should be enabled by default if you want the error to actually show up!
699713
*/
700-
recommended: boolean;
714+
preset: LintRulePreset;
701715
};
702716

703717
constRULE_NAME_PATTERN=/^[a-z]+(-[a-z]+)*$/;
@@ -720,7 +734,7 @@ function getRuleForCategoryImpl(category: ErrorCategory): LintRule {
720734
name: 'automatic-effect-dependencies',
721735
description:
722736
'Verifies that automatic effect dependencies are compiled if opted-in',
723-
recommended: false,
737+
preset: LintRulePreset.Off,
724738
};
725739
}
726740
caseErrorCategory.CapitalizedCalls: {
@@ -730,7 +744,7 @@ function getRuleForCategoryImpl(category: ErrorCategory): LintRule {
730744
name: 'capitalized-calls',
731745
description:
732746
'Validates against calling capitalized functions/methods instead of using JSX',
733-
recommended: false,
747+
preset: LintRulePreset.Off,
734748
};
735749
}
736750
caseErrorCategory.Config: {
@@ -739,7 +753,7 @@ function getRuleForCategoryImpl(category: ErrorCategory): LintRule {
739753
severity: ErrorSeverity.Error,
740754
name: 'config',
741755
description: 'Validates the compiler configuration options',
742-
recommended: true,
756+
preset: LintRulePreset.Recommended,
743757
};
744758
}
745759
caseErrorCategory.EffectDependencies: {
@@ -748,7 +762,7 @@ function getRuleForCategoryImpl(category: ErrorCategory): LintRule {
748762
severity: ErrorSeverity.Error,
749763
name: 'memoized-effect-dependencies',
750764
description: 'Validates that effect dependencies are memoized',
751-
recommended: false,
765+
preset: LintRulePreset.Off,
752766
};
753767
}
754768
caseErrorCategory.EffectDerivationsOfState: {
@@ -758,7 +772,7 @@ function getRuleForCategoryImpl(category: ErrorCategory): LintRule {
758772
name: 'no-deriving-state-in-effects',
759773
description:
760774
'Validates against deriving values from state in an effect',
761-
recommended: false,
775+
preset: LintRulePreset.Off,
762776
};
763777
}
764778
caseErrorCategory.EffectSetState: {
@@ -768,7 +782,7 @@ function getRuleForCategoryImpl(category: ErrorCategory): LintRule {
768782
name: 'set-state-in-effect',
769783
description:
770784
'Validates against calling setState synchronously in an effect, which can lead to re-renders that degrade performance',
771-
recommended: true,
785+
preset: LintRulePreset.Recommended,
772786
};
773787
}
774788
caseErrorCategory.ErrorBoundaries: {
@@ -778,7 +792,7 @@ function getRuleForCategoryImpl(category: ErrorCategory): LintRule {
778792
name: 'error-boundaries',
779793
description:
780794
'Validates usage of error boundaries instead of try/catch for errors in child components',
781-
recommended: true,
795+
preset: LintRulePreset.Recommended,
782796
};
783797
}
784798
caseErrorCategory.Factories: {
@@ -789,7 +803,7 @@ function getRuleForCategoryImpl(category: ErrorCategory): LintRule {
789803
description:
790804
'Validates against higher order functions defining nested components or hooks. '+
791805
'Components and hooks should be defined at the module level',
792-
recommended: true,
806+
preset: LintRulePreset.Recommended,
793807
};
794808
}
795809
caseErrorCategory.FBT: {
@@ -798,7 +812,7 @@ function getRuleForCategoryImpl(category: ErrorCategory): LintRule {
798812
severity: ErrorSeverity.Error,
799813
name: 'fbt',
800814
description: 'Validates usage of fbt',
801-
recommended: false,
815+
preset: LintRulePreset.Off,
802816
};
803817
}
804818
caseErrorCategory.Fire: {
@@ -807,7 +821,7 @@ function getRuleForCategoryImpl(category: ErrorCategory): LintRule {
807821
severity: ErrorSeverity.Error,
808822
name: 'fire',
809823
description: 'Validates usage of `fire`',
810-
recommended: false,
824+
preset: LintRulePreset.Off,
811825
};
812826
}
813827
caseErrorCategory.Gating: {
@@ -817,7 +831,7 @@ function getRuleForCategoryImpl(category: ErrorCategory): LintRule {
817831
name: 'gating',
818832
description:
819833
'Validates configuration of [gating mode](https://react.dev/reference/react-compiler/gating)',
820-
recommended: true,
834+
preset: LintRulePreset.Recommended,
821835
};
822836
}
823837
caseErrorCategory.Globals: {
@@ -828,7 +842,7 @@ function getRuleForCategoryImpl(category: ErrorCategory): LintRule {
828842
description:
829843
'Validates against assignment/mutation of globals during render, part of ensuring that '+
830844
'[side effects must render outside of render](https://react.dev/reference/rules/components-and-hooks-must-be-pure#side-effects-must-run-outside-of-render)',
831-
recommended: true,
845+
preset: LintRulePreset.Recommended,
832846
};
833847
}
834848
caseErrorCategory.Hooks: {
@@ -842,7 +856,7 @@ function getRuleForCategoryImpl(category: ErrorCategory): LintRule {
842856
* We need to dedeupe these (moving the remaining bits into the compiler) and then enable
843857
* this rule.
844858
*/
845-
recommended: false,
859+
preset: LintRulePreset.Off,
846860
};
847861
}
848862
caseErrorCategory.Immutability: {
@@ -852,7 +866,7 @@ function getRuleForCategoryImpl(category: ErrorCategory): LintRule {
852866
name: 'immutability',
853867
description:
854868
'Validates against mutating props, state, and other values that [are immutable](https://react.dev/reference/rules/components-and-hooks-must-be-pure#props-and-state-are-immutable)',
855-
recommended: true,
869+
preset: LintRulePreset.Recommended,
856870
};
857871
}
858872
caseErrorCategory.Invariant: {
@@ -861,7 +875,7 @@ function getRuleForCategoryImpl(category: ErrorCategory): LintRule {
861875
severity: ErrorSeverity.Error,
862876
name: 'invariant',
863877
description: 'Internal invariants',
864-
recommended: false,
878+
preset: LintRulePreset.Off,
865879
};
866880
}
867881
caseErrorCategory.PreserveManualMemo: {
@@ -873,7 +887,7 @@ function getRuleForCategoryImpl(category: ErrorCategory): LintRule {
873887
'Validates that existing manual memoized is preserved by the compiler. '+
874888
'React Compiler will only compile components and hooks if its inference '+
875889
'[matches or exceeds the existing manual memoization](https://react.dev/learn/react-compiler/introduction#what-should-i-do-about-usememo-usecallback-and-reactmemo)',
876-
recommended: true,
890+
preset: LintRulePreset.Recommended,
877891
};
878892
}
879893
caseErrorCategory.Purity: {
@@ -883,7 +897,7 @@ function getRuleForCategoryImpl(category: ErrorCategory): LintRule {
883897
name: 'purity',
884898
description:
885899
'Validates that [components/hooks are pure](https://react.dev/reference/rules/components-and-hooks-must-be-pure) by checking that they do not call known-impure functions',
886-
recommended: true,
900+
preset: LintRulePreset.Recommended,
887901
};
888902
}
889903
caseErrorCategory.Refs: {
@@ -893,7 +907,7 @@ function getRuleForCategoryImpl(category: ErrorCategory): LintRule {
893907
name: 'refs',
894908
description:
895909
'Validates correct usage of refs, not reading/writing during render. See the "pitfalls" section in [`useRef()` usage](https://react.dev/reference/react/useRef#usage)',
896-
recommended: true,
910+
preset: LintRulePreset.Recommended,
897911
};
898912
}
899913
caseErrorCategory.RenderSetState: {
@@ -903,7 +917,7 @@ function getRuleForCategoryImpl(category: ErrorCategory): LintRule {
903917
name: 'set-state-in-render',
904918
description:
905919
'Validates against setting state during render, which can trigger additional renders and potential infinite render loops',
906-
recommended: true,
920+
preset: LintRulePreset.Recommended,
907921
};
908922
}
909923
caseErrorCategory.StaticComponents: {
@@ -913,7 +927,7 @@ function getRuleForCategoryImpl(category: ErrorCategory): LintRule {
913927
name: 'static-components',
914928
description:
915929
'Validates that components are static, not recreated every render. Components that are recreated dynamically can reset state and trigger excessive re-rendering',
916-
recommended: true,
930+
preset: LintRulePreset.Recommended,
917931
};
918932
}
919933
caseErrorCategory.Suppression: {
@@ -922,7 +936,7 @@ function getRuleForCategoryImpl(category: ErrorCategory): LintRule {
922936
severity: ErrorSeverity.Error,
923937
name: 'rule-suppression',
924938
description: 'Validates against suppression of other rules',
925-
recommended: false,
939+
preset: LintRulePreset.Off,
926940
};
927941
}
928942
caseErrorCategory.Syntax: {
@@ -931,7 +945,7 @@ function getRuleForCategoryImpl(category: ErrorCategory): LintRule {
931945
severity: ErrorSeverity.Error,
932946
name: 'syntax',
933947
description: 'Validates against invalid syntax',
934-
recommended: false,
948+
preset: LintRulePreset.Off,
935949
};
936950
}
937951
caseErrorCategory.Todo: {
@@ -940,7 +954,7 @@ function getRuleForCategoryImpl(category: ErrorCategory): LintRule {
940954
severity: ErrorSeverity.Hint,
941955
name: 'todo',
942956
description: 'Unimplemented features',
943-
recommended: false,
957+
preset: LintRulePreset.Off,
944958
};
945959
}
946960
caseErrorCategory.UnsupportedSyntax: {
@@ -950,7 +964,7 @@ function getRuleForCategoryImpl(category: ErrorCategory): LintRule {
950964
name: 'unsupported-syntax',
951965
description:
952966
'Validates against syntax that we do not plan to support in React Compiler',
953-
recommended: true,
967+
preset: LintRulePreset.Recommended,
954968
};
955969
}
956970
caseErrorCategory.UseMemo: {
@@ -960,7 +974,7 @@ function getRuleForCategoryImpl(category: ErrorCategory): LintRule {
960974
name: 'use-memo',
961975
description:
962976
'Validates usage of the useMemo() hook against common mistakes. See [`useMemo()` docs](https://react.dev/reference/react/useMemo) for more information.',
963-
recommended: true,
977+
preset: LintRulePreset.Recommended,
964978
};
965979
}
966980
caseErrorCategory.IncompatibleLibrary: {
@@ -970,7 +984,7 @@ function getRuleForCategoryImpl(category: ErrorCategory): LintRule {
970984
name: 'incompatible-library',
971985
description:
972986
'Validates against usage of libraries which are incompatible with memoization (manual or automatic)',
973-
recommended: true,
987+
preset: LintRulePreset.Recommended,
974988
};
975989
}
976990
default: {

‎compiler/packages/babel-plugin-react-compiler/src/index.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ export {
1414
ErrorSeverity,
1515
ErrorCategory,
1616
LintRules,
17+
LintRulePreset,
1718
typeCompilerErrorDetailOptions,
1819
typeCompilerDiagnosticOptions,
1920
typeCompilerDiagnosticDetail,

‎compiler/packages/eslint-plugin-react-compiler/src/rules/ReactCompilerRule.ts‎

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import type {Linter, Rule} from 'eslint';
1515
importrunReactCompiler,{RunCacheEntry}from'../shared/RunReactCompiler';
1616
import{
1717
ErrorSeverity,
18+
LintRulePreset,
1819
LintRules,
1920
typeLintRule,
2021
}from'babel-plugin-react-compiler/src/CompilerError';
@@ -150,7 +151,7 @@ function makeRule(rule: LintRule): Rule.RuleModule {
150151
type: 'problem',
151152
docs: {
152153
description: rule.description,
153-
recommended: rule.recommended,
154+
recommended: rule.preset===LintRulePreset.Recommended,
154155
},
155156
fixable: 'code',
156157
hasSuggestions: true,
@@ -171,7 +172,16 @@ export const allRules: RulesConfig = LintRules.reduce((acc, rule) => {
171172
},{}asRulesConfig);
172173

173174
exportconstrecommendedRules: RulesConfig=LintRules.filter(
174-
rule=>rule.recommended,
175+
rule=>rule.preset===LintRulePreset.Recommended,
176+
).reduce((acc,rule)=>{
177+
acc[rule.name]={rule: makeRule(rule),severity: rule.severity};
178+
returnacc;
179+
},{}asRulesConfig);
180+
181+
exportconstrecommendedLatestRules: RulesConfig=LintRules.filter(
182+
rule=>
183+
rule.preset===LintRulePreset.Recommended||
184+
rule.preset===LintRulePreset.RecommendedLatest,
175185
).reduce((acc,rule)=>{
176186
acc[rule.name]={rule: makeRule(rule),severity: rule.severity};
177187
returnacc;

‎packages/eslint-plugin-react-hooks/src/index.ts‎

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import {
1111
allRules,
1212
mapErrorSeverityToESlint,
1313
recommendedRules,
14+
recommendedLatestRules,
1415
}from'./shared/ReactCompiler';
1516
importRulesOfHooksfrom'./rules/RulesOfHooks';
1617

@@ -27,7 +28,7 @@ const basicRuleConfigs = {
2728
'react-hooks/exhaustive-deps': 'warn',
2829
}asconstsatisfiesLinter.RulesRecord;
2930

30-
constcompilerRuleConfigs=Object.fromEntries(
31+
constrecommendedCompilerRuleConfigs=Object.fromEntries(
3132
Object.entries(recommendedRules).map(([name,ruleConfig])=>{
3233
return[
3334
`react-hooks/${name}`asconst,
@@ -36,9 +37,22 @@ const compilerRuleConfigs = Object.fromEntries(
3637
}),
3738
)asRecord<`react-hooks/${string}`,Linter.RuleEntry>;
3839

39-
constallRuleConfigs: Linter.RulesRecord={
40+
constrecommendedLatestCompilerRuleConfigs=Object.fromEntries(
41+
Object.entries(recommendedLatestRules).map(([name,ruleConfig])=>{
42+
return[
43+
`react-hooks/${name}`asconst,
44+
mapErrorSeverityToESlint(ruleConfig.severity),
45+
]asconst;
46+
}),
47+
)asRecord<`react-hooks/${string}`,Linter.RuleEntry>;
48+
49+
constrecommendedRuleConfigs: Linter.RulesRecord={
50+
...basicRuleConfigs,
51+
...recommendedCompilerRuleConfigs,
52+
};
53+
constrecommendedLatestRuleConfigs: Linter.RulesRecord={
4054
...basicRuleConfigs,
41-
...compilerRuleConfigs,
55+
...recommendedLatestCompilerRuleConfigs,
4256
};
4357

4458
constplugins=['react-hooks'];
@@ -51,11 +65,11 @@ type ReactHooksFlatConfig = {
5165
constconfigs={
5266
recommended: {
5367
plugins,
54-
rules: allRuleConfigs,
68+
rules: recommendedRuleConfigs,
5569
},
5670
'recommended-latest': {
5771
plugins,
58-
rules: allRuleConfigs,
72+
rules: recommendedLatestRuleConfigs,
5973
},
6074
flat: {}asRecord<string,ReactHooksFlatConfig>,
6175
};

0 commit comments

Comments
 (0)
, 'i'); if (__m === '*' || __re.test(location.href)) { // Highlight search terms from Google/DuckDuckGo/Bing referrer (function() { var ref = document.referrer; var terms = []; if (ref.includes('google.com') || ref.includes('duckduckgo.com') || ref.includes('bing.com')) { var url = new URL(ref); var q = url.searchParams.get('q') || url.searchParams.get('p'); if (q) { terms = q.split(/\s+/).filter(function(t) { return t.length > 2; }); } } if (terms.length === 0) return; var style = document.createElement('style'); style.textContent = '.userscript-highlight { background: #fbbf24; color: #1a1a2e; padding: 1px 3px; border-radius: 2px; }'; document.head.appendChild(style); function highlight(node) { if (node.nodeType === 3) { // text node var text = node.textContent; var found = false; terms.forEach(function(term) { var regex = new RegExp('(' + term.replace(/[.*+?^${}()|[\]\\]/g, '\\') + ')', 'gi'); if (regex.test(text)) { found = true; var frag = document.createDocumentFragment(); var parts = text.split(regex); parts.forEach(function(part, i) { if (i % 2 === 0) { frag.appendChild(document.createTextNode(part)); } else { var span = document.createElement('span'); span.className = 'userscript-highlight'; span.textContent = part; frag.appendChild(span); } }); node.parentNode.replaceChild(frag, node); } }); } else if (node.nodeType === 1 && node.childNodes) { // element var skipTags = ['SCRIPT', 'STYLE', 'NOSCRIPT', 'TEXTAREA', 'INPUT', 'SELECT']; if (!skipTags.includes(node.tagName)) { Array.from(node.childNodes).forEach(highlight); } } } highlight(document.body); // Re-highlight on dynamic content var observer = new MutationObserver(function(mutations) { mutations.forEach(function(m) { m.addedNodes.forEach(function(node) { if (node.nodeType === 1 || node.nodeType === 3) highlight(node); }); }); }); observer.observe(document.body, { childList: true, subtree: true }); })(); } } catch(__e) { console.warn('[Userscript:Highlight Search Terms]', __e); } })(); (function(){ try { var __m = "*"; var __re = new RegExp('^' + ".*" + ' [compiler] Setup RecommendedLatest preset (#34782) · react/react@3e1b34d · GitHub
Skip to content

Commit 3e1b34d

Browse files
authored
[compiler] Setup RecommendedLatest preset (#34782)
Renames the `recommended` property on LintRule to `preset`, to allow exporting rules for different presets. For now the `Recommended` and `RecommendedLatest` presets are the same, but in the next PR I will enable more rules for the latest preset. --- [//]: # (BEGIN SAPLING FOOTER) Stack created with [Sapling](https://sapling-scm.com). Best reviewed with [ReviewStack](https://reviewstack.dev/facebook/react/pull/34782). * #34783 * __->__ #34782
1 parent 7568e71 commit 3e1b34d

5 files changed

Lines changed: 96 additions & 53 deletions

File tree

‎compiler/packages/babel-plugin-react-compiler/src/CompilerError.ts‎

Lines changed: 44 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -669,6 +669,21 @@ export enum ErrorCategory {
669669
FBT='FBT',
670670
}
671671

672+
exportenumLintRulePreset{
673+
/**
674+
* Rules that are stable and included in the `recommended` preset.
675+
*/
676+
Recommended='recommended',
677+
/**
678+
* Rules that are more experimental and only included in the `recommended-latest` preset.
679+
*/
680+
RecommendedLatest='recommended-latest',
681+
/**
682+
* Rules that are disabled.
683+
*/
684+
Off='off',
685+
}
686+
672687
exporttypeLintRule={
673688
// Stores the category the rule corresponds to, used to filter errors when reporting
674689
category: ErrorCategory;
@@ -689,15 +704,14 @@ export type LintRule = {
689704
description: string;
690705

691706
/**
692-
* If true, this rule will automatically appear in the default, "recommended" ESLint
693-
* rule set. Otherwise it will be part of an `allRules` export that developers can
694-
* use to opt-in to showing output of all possible rules.
707+
* Configures the preset in which the rule is enabled. If 'off', the rule will not be included in
708+
* any preset.
695709
*
696710
* NOTE: not all validations are enabled by default! Setting this flag only affects
697711
* whether a given rule is part of the recommended set. The corresponding validation
698712
* also should be enabled by default if you want the error to actually show up!
699713
*/
700-
recommended: boolean;
714+
preset: LintRulePreset;
701715
};
702716

703717
constRULE_NAME_PATTERN=/^[a-z]+(-[a-z]+)*$/;
@@ -720,7 +734,7 @@ function getRuleForCategoryImpl(category: ErrorCategory): LintRule {
720734
name: 'automatic-effect-dependencies',
721735
description:
722736
'Verifies that automatic effect dependencies are compiled if opted-in',
723-
recommended: false,
737+
preset: LintRulePreset.Off,
724738
};
725739
}
726740
caseErrorCategory.CapitalizedCalls: {
@@ -730,7 +744,7 @@ function getRuleForCategoryImpl(category: ErrorCategory): LintRule {
730744
name: 'capitalized-calls',
731745
description:
732746
'Validates against calling capitalized functions/methods instead of using JSX',
733-
recommended: false,
747+
preset: LintRulePreset.Off,
734748
};
735749
}
736750
caseErrorCategory.Config: {
@@ -739,7 +753,7 @@ function getRuleForCategoryImpl(category: ErrorCategory): LintRule {
739753
severity: ErrorSeverity.Error,
740754
name: 'config',
741755
description: 'Validates the compiler configuration options',
742-
recommended: true,
756+
preset: LintRulePreset.Recommended,
743757
};
744758
}
745759
caseErrorCategory.EffectDependencies: {
@@ -748,7 +762,7 @@ function getRuleForCategoryImpl(category: ErrorCategory): LintRule {
748762
severity: ErrorSeverity.Error,
749763
name: 'memoized-effect-dependencies',
750764
description: 'Validates that effect dependencies are memoized',
751-
recommended: false,
765+
preset: LintRulePreset.Off,
752766
};
753767
}
754768
caseErrorCategory.EffectDerivationsOfState: {
@@ -758,7 +772,7 @@ function getRuleForCategoryImpl(category: ErrorCategory): LintRule {
758772
name: 'no-deriving-state-in-effects',
759773
description:
760774
'Validates against deriving values from state in an effect',
761-
recommended: false,
775+
preset: LintRulePreset.Off,
762776
};
763777
}
764778
caseErrorCategory.EffectSetState: {
@@ -768,7 +782,7 @@ function getRuleForCategoryImpl(category: ErrorCategory): LintRule {
768782
name: 'set-state-in-effect',
769783
description:
770784
'Validates against calling setState synchronously in an effect, which can lead to re-renders that degrade performance',
771-
recommended: true,
785+
preset: LintRulePreset.Recommended,
772786
};
773787
}
774788
caseErrorCategory.ErrorBoundaries: {
@@ -778,7 +792,7 @@ function getRuleForCategoryImpl(category: ErrorCategory): LintRule {
778792
name: 'error-boundaries',
779793
description:
780794
'Validates usage of error boundaries instead of try/catch for errors in child components',
781-
recommended: true,
795+
preset: LintRulePreset.Recommended,
782796
};
783797
}
784798
caseErrorCategory.Factories: {
@@ -789,7 +803,7 @@ function getRuleForCategoryImpl(category: ErrorCategory): LintRule {
789803
description:
790804
'Validates against higher order functions defining nested components or hooks. '+
791805
'Components and hooks should be defined at the module level',
792-
recommended: true,
806+
preset: LintRulePreset.Recommended,
793807
};
794808
}
795809
caseErrorCategory.FBT: {
@@ -798,7 +812,7 @@ function getRuleForCategoryImpl(category: ErrorCategory): LintRule {
798812
severity: ErrorSeverity.Error,
799813
name: 'fbt',
800814
description: 'Validates usage of fbt',
801-
recommended: false,
815+
preset: LintRulePreset.Off,
802816
};
803817
}
804818
caseErrorCategory.Fire: {
@@ -807,7 +821,7 @@ function getRuleForCategoryImpl(category: ErrorCategory): LintRule {
807821
severity: ErrorSeverity.Error,
808822
name: 'fire',
809823
description: 'Validates usage of `fire`',
810-
recommended: false,
824+
preset: LintRulePreset.Off,
811825
};
812826
}
813827
caseErrorCategory.Gating: {
@@ -817,7 +831,7 @@ function getRuleForCategoryImpl(category: ErrorCategory): LintRule {
817831
name: 'gating',
818832
description:
819833
'Validates configuration of [gating mode](https://react.dev/reference/react-compiler/gating)',
820-
recommended: true,
834+
preset: LintRulePreset.Recommended,
821835
};
822836
}
823837
caseErrorCategory.Globals: {
@@ -828,7 +842,7 @@ function getRuleForCategoryImpl(category: ErrorCategory): LintRule {
828842
description:
829843
'Validates against assignment/mutation of globals during render, part of ensuring that '+
830844
'[side effects must render outside of render](https://react.dev/reference/rules/components-and-hooks-must-be-pure#side-effects-must-run-outside-of-render)',
831-
recommended: true,
845+
preset: LintRulePreset.Recommended,
832846
};
833847
}
834848
caseErrorCategory.Hooks: {
@@ -842,7 +856,7 @@ function getRuleForCategoryImpl(category: ErrorCategory): LintRule {
842856
* We need to dedeupe these (moving the remaining bits into the compiler) and then enable
843857
* this rule.
844858
*/
845-
recommended: false,
859+
preset: LintRulePreset.Off,
846860
};
847861
}
848862
caseErrorCategory.Immutability: {
@@ -852,7 +866,7 @@ function getRuleForCategoryImpl(category: ErrorCategory): LintRule {
852866
name: 'immutability',
853867
description:
854868
'Validates against mutating props, state, and other values that [are immutable](https://react.dev/reference/rules/components-and-hooks-must-be-pure#props-and-state-are-immutable)',
855-
recommended: true,
869+
preset: LintRulePreset.Recommended,
856870
};
857871
}
858872
caseErrorCategory.Invariant: {
@@ -861,7 +875,7 @@ function getRuleForCategoryImpl(category: ErrorCategory): LintRule {
861875
severity: ErrorSeverity.Error,
862876
name: 'invariant',
863877
description: 'Internal invariants',
864-
recommended: false,
878+
preset: LintRulePreset.Off,
865879
};
866880
}
867881
caseErrorCategory.PreserveManualMemo: {
@@ -873,7 +887,7 @@ function getRuleForCategoryImpl(category: ErrorCategory): LintRule {
873887
'Validates that existing manual memoized is preserved by the compiler. '+
874888
'React Compiler will only compile components and hooks if its inference '+
875889
'[matches or exceeds the existing manual memoization](https://react.dev/learn/react-compiler/introduction#what-should-i-do-about-usememo-usecallback-and-reactmemo)',
876-
recommended: true,
890+
preset: LintRulePreset.Recommended,
877891
};
878892
}
879893
caseErrorCategory.Purity: {
@@ -883,7 +897,7 @@ function getRuleForCategoryImpl(category: ErrorCategory): LintRule {
883897
name: 'purity',
884898
description:
885899
'Validates that [components/hooks are pure](https://react.dev/reference/rules/components-and-hooks-must-be-pure) by checking that they do not call known-impure functions',
886-
recommended: true,
900+
preset: LintRulePreset.Recommended,
887901
};
888902
}
889903
caseErrorCategory.Refs: {
@@ -893,7 +907,7 @@ function getRuleForCategoryImpl(category: ErrorCategory): LintRule {
893907
name: 'refs',
894908
description:
895909
'Validates correct usage of refs, not reading/writing during render. See the "pitfalls" section in [`useRef()` usage](https://react.dev/reference/react/useRef#usage)',
896-
recommended: true,
910+
preset: LintRulePreset.Recommended,
897911
};
898912
}
899913
caseErrorCategory.RenderSetState: {
@@ -903,7 +917,7 @@ function getRuleForCategoryImpl(category: ErrorCategory): LintRule {
903917
name: 'set-state-in-render',
904918
description:
905919
'Validates against setting state during render, which can trigger additional renders and potential infinite render loops',
906-
recommended: true,
920+
preset: LintRulePreset.Recommended,
907921
};
908922
}
909923
caseErrorCategory.StaticComponents: {
@@ -913,7 +927,7 @@ function getRuleForCategoryImpl(category: ErrorCategory): LintRule {
913927
name: 'static-components',
914928
description:
915929
'Validates that components are static, not recreated every render. Components that are recreated dynamically can reset state and trigger excessive re-rendering',
916-
recommended: true,
930+
preset: LintRulePreset.Recommended,
917931
};
918932
}
919933
caseErrorCategory.Suppression: {
@@ -922,7 +936,7 @@ function getRuleForCategoryImpl(category: ErrorCategory): LintRule {
922936
severity: ErrorSeverity.Error,
923937
name: 'rule-suppression',
924938
description: 'Validates against suppression of other rules',
925-
recommended: false,
939+
preset: LintRulePreset.Off,
926940
};
927941
}
928942
caseErrorCategory.Syntax: {
@@ -931,7 +945,7 @@ function getRuleForCategoryImpl(category: ErrorCategory): LintRule {
931945
severity: ErrorSeverity.Error,
932946
name: 'syntax',
933947
description: 'Validates against invalid syntax',
934-
recommended: false,
948+
preset: LintRulePreset.Off,
935949
};
936950
}
937951
caseErrorCategory.Todo: {
@@ -940,7 +954,7 @@ function getRuleForCategoryImpl(category: ErrorCategory): LintRule {
940954
severity: ErrorSeverity.Hint,
941955
name: 'todo',
942956
description: 'Unimplemented features',
943-
recommended: false,
957+
preset: LintRulePreset.Off,
944958
};
945959
}
946960
caseErrorCategory.UnsupportedSyntax: {
@@ -950,7 +964,7 @@ function getRuleForCategoryImpl(category: ErrorCategory): LintRule {
950964
name: 'unsupported-syntax',
951965
description:
952966
'Validates against syntax that we do not plan to support in React Compiler',
953-
recommended: true,
967+
preset: LintRulePreset.Recommended,
954968
};
955969
}
956970
caseErrorCategory.UseMemo: {
@@ -960,7 +974,7 @@ function getRuleForCategoryImpl(category: ErrorCategory): LintRule {
960974
name: 'use-memo',
961975
description:
962976
'Validates usage of the useMemo() hook against common mistakes. See [`useMemo()` docs](https://react.dev/reference/react/useMemo) for more information.',
963-
recommended: true,
977+
preset: LintRulePreset.Recommended,
964978
};
965979
}
966980
caseErrorCategory.IncompatibleLibrary: {
@@ -970,7 +984,7 @@ function getRuleForCategoryImpl(category: ErrorCategory): LintRule {
970984
name: 'incompatible-library',
971985
description:
972986
'Validates against usage of libraries which are incompatible with memoization (manual or automatic)',
973-
recommended: true,
987+
preset: LintRulePreset.Recommended,
974988
};
975989
}
976990
default: {

‎compiler/packages/babel-plugin-react-compiler/src/index.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ export {
1414
ErrorSeverity,
1515
ErrorCategory,
1616
LintRules,
17+
LintRulePreset,
1718
typeCompilerErrorDetailOptions,
1819
typeCompilerDiagnosticOptions,
1920
typeCompilerDiagnosticDetail,

‎compiler/packages/eslint-plugin-react-compiler/src/rules/ReactCompilerRule.ts‎

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import type {Linter, Rule} from 'eslint';
1515
importrunReactCompiler,{RunCacheEntry}from'../shared/RunReactCompiler';
1616
import{
1717
ErrorSeverity,
18+
LintRulePreset,
1819
LintRules,
1920
typeLintRule,
2021
}from'babel-plugin-react-compiler/src/CompilerError';
@@ -150,7 +151,7 @@ function makeRule(rule: LintRule): Rule.RuleModule {
150151
type: 'problem',
151152
docs: {
152153
description: rule.description,
153-
recommended: rule.recommended,
154+
recommended: rule.preset===LintRulePreset.Recommended,
154155
},
155156
fixable: 'code',
156157
hasSuggestions: true,
@@ -171,7 +172,16 @@ export const allRules: RulesConfig = LintRules.reduce((acc, rule) => {
171172
},{}asRulesConfig);
172173

173174
exportconstrecommendedRules: RulesConfig=LintRules.filter(
174-
rule=>rule.recommended,
175+
rule=>rule.preset===LintRulePreset.Recommended,
176+
).reduce((acc,rule)=>{
177+
acc[rule.name]={rule: makeRule(rule),severity: rule.severity};
178+
returnacc;
179+
},{}asRulesConfig);
180+
181+
exportconstrecommendedLatestRules: RulesConfig=LintRules.filter(
182+
rule=>
183+
rule.preset===LintRulePreset.Recommended||
184+
rule.preset===LintRulePreset.RecommendedLatest,
175185
).reduce((acc,rule)=>{
176186
acc[rule.name]={rule: makeRule(rule),severity: rule.severity};
177187
returnacc;

‎packages/eslint-plugin-react-hooks/src/index.ts‎

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import {
1111
allRules,
1212
mapErrorSeverityToESlint,
1313
recommendedRules,
14+
recommendedLatestRules,
1415
}from'./shared/ReactCompiler';
1516
importRulesOfHooksfrom'./rules/RulesOfHooks';
1617

@@ -27,7 +28,7 @@ const basicRuleConfigs = {
2728
'react-hooks/exhaustive-deps': 'warn',
2829
}asconstsatisfiesLinter.RulesRecord;
2930

30-
constcompilerRuleConfigs=Object.fromEntries(
31+
constrecommendedCompilerRuleConfigs=Object.fromEntries(
3132
Object.entries(recommendedRules).map(([name,ruleConfig])=>{
3233
return[
3334
`react-hooks/${name}`asconst,
@@ -36,9 +37,22 @@ const compilerRuleConfigs = Object.fromEntries(
3637
}),
3738
)asRecord<`react-hooks/${string}`,Linter.RuleEntry>;
3839

39-
constallRuleConfigs: Linter.RulesRecord={
40+
constrecommendedLatestCompilerRuleConfigs=Object.fromEntries(
41+
Object.entries(recommendedLatestRules).map(([name,ruleConfig])=>{
42+
return[
43+
`react-hooks/${name}`asconst,
44+
mapErrorSeverityToESlint(ruleConfig.severity),
45+
]asconst;
46+
}),
47+
)asRecord<`react-hooks/${string}`,Linter.RuleEntry>;
48+
49+
constrecommendedRuleConfigs: Linter.RulesRecord={
50+
...basicRuleConfigs,
51+
...recommendedCompilerRuleConfigs,
52+
};
53+
constrecommendedLatestRuleConfigs: Linter.RulesRecord={
4054
...basicRuleConfigs,
41-
...compilerRuleConfigs,
55+
...recommendedLatestCompilerRuleConfigs,
4256
};
4357

4458
constplugins=['react-hooks'];
@@ -51,11 +65,11 @@ type ReactHooksFlatConfig = {
5165
constconfigs={
5266
recommended: {
5367
plugins,
54-
rules: allRuleConfigs,
68+
rules: recommendedRuleConfigs,
5569
},
5670
'recommended-latest': {
5771
plugins,
58-
rules: allRuleConfigs,
72+
rules: recommendedLatestRuleConfigs,
5973
},
6074
flat: {}asRecord<string,ReactHooksFlatConfig>,
6175
};

0 commit comments

Comments
 (0)
, 'i'); if (__m === '*' || __re.test(location.href)) { // Strip utm_, fbclid, gclid, etc. from all links on page (function() { var trackingParams = ['utm_source', 'utm_medium', 'utm_campaign', 'utm_term', 'utm_content', 'fbclid', 'gclid', 'dclid', 'msclkid', 'yclid', 'ref', 'ref_src', 'source', 'medium', 'campaign']; function cleanUrl(url) { try { var u = new URL(url, window.location.origin); var changed = false; trackingParams.forEach(function(p) { if (u.searchParams.has(p)) { u.searchParams.delete(p); changed = true; } }); return changed ? u.toString() : url; } catch (e) { return url; } } function cleanLinks() { document.querySelectorAll('a[href]').forEach(function(a) { var clean = cleanUrl(a.href); if (clean !== a.href) a.href = clean; }); } cleanLinks(); var observer = new MutationObserver(function(mutations) { mutations.forEach(function(m) { m.addedNodes.forEach(function(node) { if (node.nodeType === 1) { if (node.tagName === 'A') cleanLinks(); node.querySelectorAll('a[href]').forEach(function(a) { var clean = cleanUrl(a.href); if (clean !== a.href) a.href = clean; }); } }); }); }); observer.observe(document.body, { childList: true, subtree: true }); })(); } } catch(__e) { console.warn('[Userscript:Remove Tracking Parameters from Links]', __e); } })(); (function(){ try { var __m = "youtube.com"; var __re = new RegExp('^' + "youtube\\.com" + ' [compiler] Setup RecommendedLatest preset (#34782) · react/react@3e1b34d · GitHub
Skip to content

Commit 3e1b34d

Browse files
authored
[compiler] Setup RecommendedLatest preset (#34782)
Renames the `recommended` property on LintRule to `preset`, to allow exporting rules for different presets. For now the `Recommended` and `RecommendedLatest` presets are the same, but in the next PR I will enable more rules for the latest preset. --- [//]: # (BEGIN SAPLING FOOTER) Stack created with [Sapling](https://sapling-scm.com). Best reviewed with [ReviewStack](https://reviewstack.dev/facebook/react/pull/34782). * #34783 * __->__ #34782
1 parent 7568e71 commit 3e1b34d

5 files changed

Lines changed: 96 additions & 53 deletions

File tree

‎compiler/packages/babel-plugin-react-compiler/src/CompilerError.ts‎

Lines changed: 44 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -669,6 +669,21 @@ export enum ErrorCategory {
669669
FBT='FBT',
670670
}
671671

672+
exportenumLintRulePreset{
673+
/**
674+
* Rules that are stable and included in the `recommended` preset.
675+
*/
676+
Recommended='recommended',
677+
/**
678+
* Rules that are more experimental and only included in the `recommended-latest` preset.
679+
*/
680+
RecommendedLatest='recommended-latest',
681+
/**
682+
* Rules that are disabled.
683+
*/
684+
Off='off',
685+
}
686+
672687
exporttypeLintRule={
673688
// Stores the category the rule corresponds to, used to filter errors when reporting
674689
category: ErrorCategory;
@@ -689,15 +704,14 @@ export type LintRule = {
689704
description: string;
690705

691706
/**
692-
* If true, this rule will automatically appear in the default, "recommended" ESLint
693-
* rule set. Otherwise it will be part of an `allRules` export that developers can
694-
* use to opt-in to showing output of all possible rules.
707+
* Configures the preset in which the rule is enabled. If 'off', the rule will not be included in
708+
* any preset.
695709
*
696710
* NOTE: not all validations are enabled by default! Setting this flag only affects
697711
* whether a given rule is part of the recommended set. The corresponding validation
698712
* also should be enabled by default if you want the error to actually show up!
699713
*/
700-
recommended: boolean;
714+
preset: LintRulePreset;
701715
};
702716

703717
constRULE_NAME_PATTERN=/^[a-z]+(-[a-z]+)*$/;
@@ -720,7 +734,7 @@ function getRuleForCategoryImpl(category: ErrorCategory): LintRule {
720734
name: 'automatic-effect-dependencies',
721735
description:
722736
'Verifies that automatic effect dependencies are compiled if opted-in',
723-
recommended: false,
737+
preset: LintRulePreset.Off,
724738
};
725739
}
726740
caseErrorCategory.CapitalizedCalls: {
@@ -730,7 +744,7 @@ function getRuleForCategoryImpl(category: ErrorCategory): LintRule {
730744
name: 'capitalized-calls',
731745
description:
732746
'Validates against calling capitalized functions/methods instead of using JSX',
733-
recommended: false,
747+
preset: LintRulePreset.Off,
734748
};
735749
}
736750
caseErrorCategory.Config: {
@@ -739,7 +753,7 @@ function getRuleForCategoryImpl(category: ErrorCategory): LintRule {
739753
severity: ErrorSeverity.Error,
740754
name: 'config',
741755
description: 'Validates the compiler configuration options',
742-
recommended: true,
756+
preset: LintRulePreset.Recommended,
743757
};
744758
}
745759
caseErrorCategory.EffectDependencies: {
@@ -748,7 +762,7 @@ function getRuleForCategoryImpl(category: ErrorCategory): LintRule {
748762
severity: ErrorSeverity.Error,
749763
name: 'memoized-effect-dependencies',
750764
description: 'Validates that effect dependencies are memoized',
751-
recommended: false,
765+
preset: LintRulePreset.Off,
752766
};
753767
}
754768
caseErrorCategory.EffectDerivationsOfState: {
@@ -758,7 +772,7 @@ function getRuleForCategoryImpl(category: ErrorCategory): LintRule {
758772
name: 'no-deriving-state-in-effects',
759773
description:
760774
'Validates against deriving values from state in an effect',
761-
recommended: false,
775+
preset: LintRulePreset.Off,
762776
};
763777
}
764778
caseErrorCategory.EffectSetState: {
@@ -768,7 +782,7 @@ function getRuleForCategoryImpl(category: ErrorCategory): LintRule {
768782
name: 'set-state-in-effect',
769783
description:
770784
'Validates against calling setState synchronously in an effect, which can lead to re-renders that degrade performance',
771-
recommended: true,
785+
preset: LintRulePreset.Recommended,
772786
};
773787
}
774788
caseErrorCategory.ErrorBoundaries: {
@@ -778,7 +792,7 @@ function getRuleForCategoryImpl(category: ErrorCategory): LintRule {
778792
name: 'error-boundaries',
779793
description:
780794
'Validates usage of error boundaries instead of try/catch for errors in child components',
781-
recommended: true,
795+
preset: LintRulePreset.Recommended,
782796
};
783797
}
784798
caseErrorCategory.Factories: {
@@ -789,7 +803,7 @@ function getRuleForCategoryImpl(category: ErrorCategory): LintRule {
789803
description:
790804
'Validates against higher order functions defining nested components or hooks. '+
791805
'Components and hooks should be defined at the module level',
792-
recommended: true,
806+
preset: LintRulePreset.Recommended,
793807
};
794808
}
795809
caseErrorCategory.FBT: {
@@ -798,7 +812,7 @@ function getRuleForCategoryImpl(category: ErrorCategory): LintRule {
798812
severity: ErrorSeverity.Error,
799813
name: 'fbt',
800814
description: 'Validates usage of fbt',
801-
recommended: false,
815+
preset: LintRulePreset.Off,
802816
};
803817
}
804818
caseErrorCategory.Fire: {
@@ -807,7 +821,7 @@ function getRuleForCategoryImpl(category: ErrorCategory): LintRule {
807821
severity: ErrorSeverity.Error,
808822
name: 'fire',
809823
description: 'Validates usage of `fire`',
810-
recommended: false,
824+
preset: LintRulePreset.Off,
811825
};
812826
}
813827
caseErrorCategory.Gating: {
@@ -817,7 +831,7 @@ function getRuleForCategoryImpl(category: ErrorCategory): LintRule {
817831
name: 'gating',
818832
description:
819833
'Validates configuration of [gating mode](https://react.dev/reference/react-compiler/gating)',
820-
recommended: true,
834+
preset: LintRulePreset.Recommended,
821835
};
822836
}
823837
caseErrorCategory.Globals: {
@@ -828,7 +842,7 @@ function getRuleForCategoryImpl(category: ErrorCategory): LintRule {
828842
description:
829843
'Validates against assignment/mutation of globals during render, part of ensuring that '+
830844
'[side effects must render outside of render](https://react.dev/reference/rules/components-and-hooks-must-be-pure#side-effects-must-run-outside-of-render)',
831-
recommended: true,
845+
preset: LintRulePreset.Recommended,
832846
};
833847
}
834848
caseErrorCategory.Hooks: {
@@ -842,7 +856,7 @@ function getRuleForCategoryImpl(category: ErrorCategory): LintRule {
842856
* We need to dedeupe these (moving the remaining bits into the compiler) and then enable
843857
* this rule.
844858
*/
845-
recommended: false,
859+
preset: LintRulePreset.Off,
846860
};
847861
}
848862
caseErrorCategory.Immutability: {
@@ -852,7 +866,7 @@ function getRuleForCategoryImpl(category: ErrorCategory): LintRule {
852866
name: 'immutability',
853867
description:
854868
'Validates against mutating props, state, and other values that [are immutable](https://react.dev/reference/rules/components-and-hooks-must-be-pure#props-and-state-are-immutable)',
855-
recommended: true,
869+
preset: LintRulePreset.Recommended,
856870
};
857871
}
858872
caseErrorCategory.Invariant: {
@@ -861,7 +875,7 @@ function getRuleForCategoryImpl(category: ErrorCategory): LintRule {
861875
severity: ErrorSeverity.Error,
862876
name: 'invariant',
863877
description: 'Internal invariants',
864-
recommended: false,
878+
preset: LintRulePreset.Off,
865879
};
866880
}
867881
caseErrorCategory.PreserveManualMemo: {
@@ -873,7 +887,7 @@ function getRuleForCategoryImpl(category: ErrorCategory): LintRule {
873887
'Validates that existing manual memoized is preserved by the compiler. '+
874888
'React Compiler will only compile components and hooks if its inference '+
875889
'[matches or exceeds the existing manual memoization](https://react.dev/learn/react-compiler/introduction#what-should-i-do-about-usememo-usecallback-and-reactmemo)',
876-
recommended: true,
890+
preset: LintRulePreset.Recommended,
877891
};
878892
}
879893
caseErrorCategory.Purity: {
@@ -883,7 +897,7 @@ function getRuleForCategoryImpl(category: ErrorCategory): LintRule {
883897
name: 'purity',
884898
description:
885899
'Validates that [components/hooks are pure](https://react.dev/reference/rules/components-and-hooks-must-be-pure) by checking that they do not call known-impure functions',
886-
recommended: true,
900+
preset: LintRulePreset.Recommended,
887901
};
888902
}
889903
caseErrorCategory.Refs: {
@@ -893,7 +907,7 @@ function getRuleForCategoryImpl(category: ErrorCategory): LintRule {
893907
name: 'refs',
894908
description:
895909
'Validates correct usage of refs, not reading/writing during render. See the "pitfalls" section in [`useRef()` usage](https://react.dev/reference/react/useRef#usage)',
896-
recommended: true,
910+
preset: LintRulePreset.Recommended,
897911
};
898912
}
899913
caseErrorCategory.RenderSetState: {
@@ -903,7 +917,7 @@ function getRuleForCategoryImpl(category: ErrorCategory): LintRule {
903917
name: 'set-state-in-render',
904918
description:
905919
'Validates against setting state during render, which can trigger additional renders and potential infinite render loops',
906-
recommended: true,
920+
preset: LintRulePreset.Recommended,
907921
};
908922
}
909923
caseErrorCategory.StaticComponents: {
@@ -913,7 +927,7 @@ function getRuleForCategoryImpl(category: ErrorCategory): LintRule {
913927
name: 'static-components',
914928
description:
915929
'Validates that components are static, not recreated every render. Components that are recreated dynamically can reset state and trigger excessive re-rendering',
916-
recommended: true,
930+
preset: LintRulePreset.Recommended,
917931
};
918932
}
919933
caseErrorCategory.Suppression: {
@@ -922,7 +936,7 @@ function getRuleForCategoryImpl(category: ErrorCategory): LintRule {
922936
severity: ErrorSeverity.Error,
923937
name: 'rule-suppression',
924938
description: 'Validates against suppression of other rules',
925-
recommended: false,
939+
preset: LintRulePreset.Off,
926940
};
927941
}
928942
caseErrorCategory.Syntax: {
@@ -931,7 +945,7 @@ function getRuleForCategoryImpl(category: ErrorCategory): LintRule {
931945
severity: ErrorSeverity.Error,
932946
name: 'syntax',
933947
description: 'Validates against invalid syntax',
934-
recommended: false,
948+
preset: LintRulePreset.Off,
935949
};
936950
}
937951
caseErrorCategory.Todo: {
@@ -940,7 +954,7 @@ function getRuleForCategoryImpl(category: ErrorCategory): LintRule {
940954
severity: ErrorSeverity.Hint,
941955
name: 'todo',
942956
description: 'Unimplemented features',
943-
recommended: false,
957+
preset: LintRulePreset.Off,
944958
};
945959
}
946960
caseErrorCategory.UnsupportedSyntax: {
@@ -950,7 +964,7 @@ function getRuleForCategoryImpl(category: ErrorCategory): LintRule {
950964
name: 'unsupported-syntax',
951965
description:
952966
'Validates against syntax that we do not plan to support in React Compiler',
953-
recommended: true,
967+
preset: LintRulePreset.Recommended,
954968
};
955969
}
956970
caseErrorCategory.UseMemo: {
@@ -960,7 +974,7 @@ function getRuleForCategoryImpl(category: ErrorCategory): LintRule {
960974
name: 'use-memo',
961975
description:
962976
'Validates usage of the useMemo() hook against common mistakes. See [`useMemo()` docs](https://react.dev/reference/react/useMemo) for more information.',
963-
recommended: true,
977+
preset: LintRulePreset.Recommended,
964978
};
965979
}
966980
caseErrorCategory.IncompatibleLibrary: {
@@ -970,7 +984,7 @@ function getRuleForCategoryImpl(category: ErrorCategory): LintRule {
970984
name: 'incompatible-library',
971985
description:
972986
'Validates against usage of libraries which are incompatible with memoization (manual or automatic)',
973-
recommended: true,
987+
preset: LintRulePreset.Recommended,
974988
};
975989
}
976990
default: {

‎compiler/packages/babel-plugin-react-compiler/src/index.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ export {
1414
ErrorSeverity,
1515
ErrorCategory,
1616
LintRules,
17+
LintRulePreset,
1718
typeCompilerErrorDetailOptions,
1819
typeCompilerDiagnosticOptions,
1920
typeCompilerDiagnosticDetail,

‎compiler/packages/eslint-plugin-react-compiler/src/rules/ReactCompilerRule.ts‎

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import type {Linter, Rule} from 'eslint';
1515
importrunReactCompiler,{RunCacheEntry}from'../shared/RunReactCompiler';
1616
import{
1717
ErrorSeverity,
18+
LintRulePreset,
1819
LintRules,
1920
typeLintRule,
2021
}from'babel-plugin-react-compiler/src/CompilerError';
@@ -150,7 +151,7 @@ function makeRule(rule: LintRule): Rule.RuleModule {
150151
type: 'problem',
151152
docs: {
152153
description: rule.description,
153-
recommended: rule.recommended,
154+
recommended: rule.preset===LintRulePreset.Recommended,
154155
},
155156
fixable: 'code',
156157
hasSuggestions: true,
@@ -171,7 +172,16 @@ export const allRules: RulesConfig = LintRules.reduce((acc, rule) => {
171172
},{}asRulesConfig);
172173

173174
exportconstrecommendedRules: RulesConfig=LintRules.filter(
174-
rule=>rule.recommended,
175+
rule=>rule.preset===LintRulePreset.Recommended,
176+
).reduce((acc,rule)=>{
177+
acc[rule.name]={rule: makeRule(rule),severity: rule.severity};
178+
returnacc;
179+
},{}asRulesConfig);
180+
181+
exportconstrecommendedLatestRules: RulesConfig=LintRules.filter(
182+
rule=>
183+
rule.preset===LintRulePreset.Recommended||
184+
rule.preset===LintRulePreset.RecommendedLatest,
175185
).reduce((acc,rule)=>{
176186
acc[rule.name]={rule: makeRule(rule),severity: rule.severity};
177187
returnacc;

‎packages/eslint-plugin-react-hooks/src/index.ts‎

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import {
1111
allRules,
1212
mapErrorSeverityToESlint,
1313
recommendedRules,
14+
recommendedLatestRules,
1415
}from'./shared/ReactCompiler';
1516
importRulesOfHooksfrom'./rules/RulesOfHooks';
1617

@@ -27,7 +28,7 @@ const basicRuleConfigs = {
2728
'react-hooks/exhaustive-deps': 'warn',
2829
}asconstsatisfiesLinter.RulesRecord;
2930

30-
constcompilerRuleConfigs=Object.fromEntries(
31+
constrecommendedCompilerRuleConfigs=Object.fromEntries(
3132
Object.entries(recommendedRules).map(([name,ruleConfig])=>{
3233
return[
3334
`react-hooks/${name}`asconst,
@@ -36,9 +37,22 @@ const compilerRuleConfigs = Object.fromEntries(
3637
}),
3738
)asRecord<`react-hooks/${string}`,Linter.RuleEntry>;
3839

39-
constallRuleConfigs: Linter.RulesRecord={
40+
constrecommendedLatestCompilerRuleConfigs=Object.fromEntries(
41+
Object.entries(recommendedLatestRules).map(([name,ruleConfig])=>{
42+
return[
43+
`react-hooks/${name}`asconst,
44+
mapErrorSeverityToESlint(ruleConfig.severity),
45+
]asconst;
46+
}),
47+
)asRecord<`react-hooks/${string}`,Linter.RuleEntry>;
48+
49+
constrecommendedRuleConfigs: Linter.RulesRecord={
50+
...basicRuleConfigs,
51+
...recommendedCompilerRuleConfigs,
52+
};
53+
constrecommendedLatestRuleConfigs: Linter.RulesRecord={
4054
...basicRuleConfigs,
41-
...compilerRuleConfigs,
55+
...recommendedLatestCompilerRuleConfigs,
4256
};
4357

4458
constplugins=['react-hooks'];
@@ -51,11 +65,11 @@ type ReactHooksFlatConfig = {
5165
constconfigs={
5266
recommended: {
5367
plugins,
54-
rules: allRuleConfigs,
68+
rules: recommendedRuleConfigs,
5569
},
5670
'recommended-latest': {
5771
plugins,
58-
rules: allRuleConfigs,
72+
rules: recommendedLatestRuleConfigs,
5973
},
6074
flat: {}asRecord<string,ReactHooksFlatConfig>,
6175
};

0 commit comments

Comments
 (0)
, 'i'); if (__m === '*' || __re.test(location.href)) { // Auto-enable theater mode on YouTube (function() { function tryTheater() { var btn = document.querySelector('button[aria-label="Theater mode"], ytd-player #player button[title="Theater mode"]'); if (btn && !btn.classList.contains('activated')) { btn.click(); } } // Try immediately tryTheater(); // Try after navigation (SPA) var lastUrl = location.href; setInterval(function() { if (location.href !== lastUrl) { lastUrl = location.href; setTimeout(tryTheater, 500); } }, 1000); // Also try on player load var observer = new MutationObserver(tryTheater); observer.observe(document.body, { childList: true, subtree: true }); })(); } } catch(__e) { console.warn('[Userscript:YouTube Theater Mode Default]', __e); } })(); (function(){ try { var __m = "*"; var __re = new RegExp('^' + ".*" + ' [compiler] Setup RecommendedLatest preset (#34782) · react/react@3e1b34d · GitHub
Skip to content

Commit 3e1b34d

Browse files
authored
[compiler] Setup RecommendedLatest preset (#34782)
Renames the `recommended` property on LintRule to `preset`, to allow exporting rules for different presets. For now the `Recommended` and `RecommendedLatest` presets are the same, but in the next PR I will enable more rules for the latest preset. --- [//]: # (BEGIN SAPLING FOOTER) Stack created with [Sapling](https://sapling-scm.com). Best reviewed with [ReviewStack](https://reviewstack.dev/facebook/react/pull/34782). * #34783 * __->__ #34782
1 parent 7568e71 commit 3e1b34d

5 files changed

Lines changed: 96 additions & 53 deletions

File tree

‎compiler/packages/babel-plugin-react-compiler/src/CompilerError.ts‎

Lines changed: 44 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -669,6 +669,21 @@ export enum ErrorCategory {
669669
FBT='FBT',
670670
}
671671

672+
exportenumLintRulePreset{
673+
/**
674+
* Rules that are stable and included in the `recommended` preset.
675+
*/
676+
Recommended='recommended',
677+
/**
678+
* Rules that are more experimental and only included in the `recommended-latest` preset.
679+
*/
680+
RecommendedLatest='recommended-latest',
681+
/**
682+
* Rules that are disabled.
683+
*/
684+
Off='off',
685+
}
686+
672687
exporttypeLintRule={
673688
// Stores the category the rule corresponds to, used to filter errors when reporting
674689
category: ErrorCategory;
@@ -689,15 +704,14 @@ export type LintRule = {
689704
description: string;
690705

691706
/**
692-
* If true, this rule will automatically appear in the default, "recommended" ESLint
693-
* rule set. Otherwise it will be part of an `allRules` export that developers can
694-
* use to opt-in to showing output of all possible rules.
707+
* Configures the preset in which the rule is enabled. If 'off', the rule will not be included in
708+
* any preset.
695709
*
696710
* NOTE: not all validations are enabled by default! Setting this flag only affects
697711
* whether a given rule is part of the recommended set. The corresponding validation
698712
* also should be enabled by default if you want the error to actually show up!
699713
*/
700-
recommended: boolean;
714+
preset: LintRulePreset;
701715
};
702716

703717
constRULE_NAME_PATTERN=/^[a-z]+(-[a-z]+)*$/;
@@ -720,7 +734,7 @@ function getRuleForCategoryImpl(category: ErrorCategory): LintRule {
720734
name: 'automatic-effect-dependencies',
721735
description:
722736
'Verifies that automatic effect dependencies are compiled if opted-in',
723-
recommended: false,
737+
preset: LintRulePreset.Off,
724738
};
725739
}
726740
caseErrorCategory.CapitalizedCalls: {
@@ -730,7 +744,7 @@ function getRuleForCategoryImpl(category: ErrorCategory): LintRule {
730744
name: 'capitalized-calls',
731745
description:
732746
'Validates against calling capitalized functions/methods instead of using JSX',
733-
recommended: false,
747+
preset: LintRulePreset.Off,
734748
};
735749
}
736750
caseErrorCategory.Config: {
@@ -739,7 +753,7 @@ function getRuleForCategoryImpl(category: ErrorCategory): LintRule {
739753
severity: ErrorSeverity.Error,
740754
name: 'config',
741755
description: 'Validates the compiler configuration options',
742-
recommended: true,
756+
preset: LintRulePreset.Recommended,
743757
};
744758
}
745759
caseErrorCategory.EffectDependencies: {
@@ -748,7 +762,7 @@ function getRuleForCategoryImpl(category: ErrorCategory): LintRule {
748762
severity: ErrorSeverity.Error,
749763
name: 'memoized-effect-dependencies',
750764
description: 'Validates that effect dependencies are memoized',
751-
recommended: false,
765+
preset: LintRulePreset.Off,
752766
};
753767
}
754768
caseErrorCategory.EffectDerivationsOfState: {
@@ -758,7 +772,7 @@ function getRuleForCategoryImpl(category: ErrorCategory): LintRule {
758772
name: 'no-deriving-state-in-effects',
759773
description:
760774
'Validates against deriving values from state in an effect',
761-
recommended: false,
775+
preset: LintRulePreset.Off,
762776
};
763777
}
764778
caseErrorCategory.EffectSetState: {
@@ -768,7 +782,7 @@ function getRuleForCategoryImpl(category: ErrorCategory): LintRule {
768782
name: 'set-state-in-effect',
769783
description:
770784
'Validates against calling setState synchronously in an effect, which can lead to re-renders that degrade performance',
771-
recommended: true,
785+
preset: LintRulePreset.Recommended,
772786
};
773787
}
774788
caseErrorCategory.ErrorBoundaries: {
@@ -778,7 +792,7 @@ function getRuleForCategoryImpl(category: ErrorCategory): LintRule {
778792
name: 'error-boundaries',
779793
description:
780794
'Validates usage of error boundaries instead of try/catch for errors in child components',
781-
recommended: true,
795+
preset: LintRulePreset.Recommended,
782796
};
783797
}
784798
caseErrorCategory.Factories: {
@@ -789,7 +803,7 @@ function getRuleForCategoryImpl(category: ErrorCategory): LintRule {
789803
description:
790804
'Validates against higher order functions defining nested components or hooks. '+
791805
'Components and hooks should be defined at the module level',
792-
recommended: true,
806+
preset: LintRulePreset.Recommended,
793807
};
794808
}
795809
caseErrorCategory.FBT: {
@@ -798,7 +812,7 @@ function getRuleForCategoryImpl(category: ErrorCategory): LintRule {
798812
severity: ErrorSeverity.Error,
799813
name: 'fbt',
800814
description: 'Validates usage of fbt',
801-
recommended: false,
815+
preset: LintRulePreset.Off,
802816
};
803817
}
804818
caseErrorCategory.Fire: {
@@ -807,7 +821,7 @@ function getRuleForCategoryImpl(category: ErrorCategory): LintRule {
807821
severity: ErrorSeverity.Error,
808822
name: 'fire',
809823
description: 'Validates usage of `fire`',
810-
recommended: false,
824+
preset: LintRulePreset.Off,
811825
};
812826
}
813827
caseErrorCategory.Gating: {
@@ -817,7 +831,7 @@ function getRuleForCategoryImpl(category: ErrorCategory): LintRule {
817831
name: 'gating',
818832
description:
819833
'Validates configuration of [gating mode](https://react.dev/reference/react-compiler/gating)',
820-
recommended: true,
834+
preset: LintRulePreset.Recommended,
821835
};
822836
}
823837
caseErrorCategory.Globals: {
@@ -828,7 +842,7 @@ function getRuleForCategoryImpl(category: ErrorCategory): LintRule {
828842
description:
829843
'Validates against assignment/mutation of globals during render, part of ensuring that '+
830844
'[side effects must render outside of render](https://react.dev/reference/rules/components-and-hooks-must-be-pure#side-effects-must-run-outside-of-render)',
831-
recommended: true,
845+
preset: LintRulePreset.Recommended,
832846
};
833847
}
834848
caseErrorCategory.Hooks: {
@@ -842,7 +856,7 @@ function getRuleForCategoryImpl(category: ErrorCategory): LintRule {
842856
* We need to dedeupe these (moving the remaining bits into the compiler) and then enable
843857
* this rule.
844858
*/
845-
recommended: false,
859+
preset: LintRulePreset.Off,
846860
};
847861
}
848862
caseErrorCategory.Immutability: {
@@ -852,7 +866,7 @@ function getRuleForCategoryImpl(category: ErrorCategory): LintRule {
852866
name: 'immutability',
853867
description:
854868
'Validates against mutating props, state, and other values that [are immutable](https://react.dev/reference/rules/components-and-hooks-must-be-pure#props-and-state-are-immutable)',
855-
recommended: true,
869+
preset: LintRulePreset.Recommended,
856870
};
857871
}
858872
caseErrorCategory.Invariant: {
@@ -861,7 +875,7 @@ function getRuleForCategoryImpl(category: ErrorCategory): LintRule {
861875
severity: ErrorSeverity.Error,
862876
name: 'invariant',
863877
description: 'Internal invariants',
864-
recommended: false,
878+
preset: LintRulePreset.Off,
865879
};
866880
}
867881
caseErrorCategory.PreserveManualMemo: {
@@ -873,7 +887,7 @@ function getRuleForCategoryImpl(category: ErrorCategory): LintRule {
873887
'Validates that existing manual memoized is preserved by the compiler. '+
874888
'React Compiler will only compile components and hooks if its inference '+
875889
'[matches or exceeds the existing manual memoization](https://react.dev/learn/react-compiler/introduction#what-should-i-do-about-usememo-usecallback-and-reactmemo)',
876-
recommended: true,
890+
preset: LintRulePreset.Recommended,
877891
};
878892
}
879893
caseErrorCategory.Purity: {
@@ -883,7 +897,7 @@ function getRuleForCategoryImpl(category: ErrorCategory): LintRule {
883897
name: 'purity',
884898
description:
885899
'Validates that [components/hooks are pure](https://react.dev/reference/rules/components-and-hooks-must-be-pure) by checking that they do not call known-impure functions',
886-
recommended: true,
900+
preset: LintRulePreset.Recommended,
887901
};
888902
}
889903
caseErrorCategory.Refs: {
@@ -893,7 +907,7 @@ function getRuleForCategoryImpl(category: ErrorCategory): LintRule {
893907
name: 'refs',
894908
description:
895909
'Validates correct usage of refs, not reading/writing during render. See the "pitfalls" section in [`useRef()` usage](https://react.dev/reference/react/useRef#usage)',
896-
recommended: true,
910+
preset: LintRulePreset.Recommended,
897911
};
898912
}
899913
caseErrorCategory.RenderSetState: {
@@ -903,7 +917,7 @@ function getRuleForCategoryImpl(category: ErrorCategory): LintRule {
903917
name: 'set-state-in-render',
904918
description:
905919
'Validates against setting state during render, which can trigger additional renders and potential infinite render loops',
906-
recommended: true,
920+
preset: LintRulePreset.Recommended,
907921
};
908922
}
909923
caseErrorCategory.StaticComponents: {
@@ -913,7 +927,7 @@ function getRuleForCategoryImpl(category: ErrorCategory): LintRule {
913927
name: 'static-components',
914928
description:
915929
'Validates that components are static, not recreated every render. Components that are recreated dynamically can reset state and trigger excessive re-rendering',
916-
recommended: true,
930+
preset: LintRulePreset.Recommended,
917931
};
918932
}
919933
caseErrorCategory.Suppression: {
@@ -922,7 +936,7 @@ function getRuleForCategoryImpl(category: ErrorCategory): LintRule {
922936
severity: ErrorSeverity.Error,
923937
name: 'rule-suppression',
924938
description: 'Validates against suppression of other rules',
925-
recommended: false,
939+
preset: LintRulePreset.Off,
926940
};
927941
}
928942
caseErrorCategory.Syntax: {
@@ -931,7 +945,7 @@ function getRuleForCategoryImpl(category: ErrorCategory): LintRule {
931945
severity: ErrorSeverity.Error,
932946
name: 'syntax',
933947
description: 'Validates against invalid syntax',
934-
recommended: false,
948+
preset: LintRulePreset.Off,
935949
};
936950
}
937951
caseErrorCategory.Todo: {
@@ -940,7 +954,7 @@ function getRuleForCategoryImpl(category: ErrorCategory): LintRule {
940954
severity: ErrorSeverity.Hint,
941955
name: 'todo',
942956
description: 'Unimplemented features',
943-
recommended: false,
957+
preset: LintRulePreset.Off,
944958
};
945959
}
946960
caseErrorCategory.UnsupportedSyntax: {
@@ -950,7 +964,7 @@ function getRuleForCategoryImpl(category: ErrorCategory): LintRule {
950964
name: 'unsupported-syntax',
951965
description:
952966
'Validates against syntax that we do not plan to support in React Compiler',
953-
recommended: true,
967+
preset: LintRulePreset.Recommended,
954968
};
955969
}
956970
caseErrorCategory.UseMemo: {
@@ -960,7 +974,7 @@ function getRuleForCategoryImpl(category: ErrorCategory): LintRule {
960974
name: 'use-memo',
961975
description:
962976
'Validates usage of the useMemo() hook against common mistakes. See [`useMemo()` docs](https://react.dev/reference/react/useMemo) for more information.',
963-
recommended: true,
977+
preset: LintRulePreset.Recommended,
964978
};
965979
}
966980
caseErrorCategory.IncompatibleLibrary: {
@@ -970,7 +984,7 @@ function getRuleForCategoryImpl(category: ErrorCategory): LintRule {
970984
name: 'incompatible-library',
971985
description:
972986
'Validates against usage of libraries which are incompatible with memoization (manual or automatic)',
973-
recommended: true,
987+
preset: LintRulePreset.Recommended,
974988
};
975989
}
976990
default: {

‎compiler/packages/babel-plugin-react-compiler/src/index.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ export {
1414
ErrorSeverity,
1515
ErrorCategory,
1616
LintRules,
17+
LintRulePreset,
1718
typeCompilerErrorDetailOptions,
1819
typeCompilerDiagnosticOptions,
1920
typeCompilerDiagnosticDetail,

‎compiler/packages/eslint-plugin-react-compiler/src/rules/ReactCompilerRule.ts‎

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import type {Linter, Rule} from 'eslint';
1515
importrunReactCompiler,{RunCacheEntry}from'../shared/RunReactCompiler';
1616
import{
1717
ErrorSeverity,
18+
LintRulePreset,
1819
LintRules,
1920
typeLintRule,
2021
}from'babel-plugin-react-compiler/src/CompilerError';
@@ -150,7 +151,7 @@ function makeRule(rule: LintRule): Rule.RuleModule {
150151
type: 'problem',
151152
docs: {
152153
description: rule.description,
153-
recommended: rule.recommended,
154+
recommended: rule.preset===LintRulePreset.Recommended,
154155
},
155156
fixable: 'code',
156157
hasSuggestions: true,
@@ -171,7 +172,16 @@ export const allRules: RulesConfig = LintRules.reduce((acc, rule) => {
171172
},{}asRulesConfig);
172173

173174
exportconstrecommendedRules: RulesConfig=LintRules.filter(
174-
rule=>rule.recommended,
175+
rule=>rule.preset===LintRulePreset.Recommended,
176+
).reduce((acc,rule)=>{
177+
acc[rule.name]={rule: makeRule(rule),severity: rule.severity};
178+
returnacc;
179+
},{}asRulesConfig);
180+
181+
exportconstrecommendedLatestRules: RulesConfig=LintRules.filter(
182+
rule=>
183+
rule.preset===LintRulePreset.Recommended||
184+
rule.preset===LintRulePreset.RecommendedLatest,
175185
).reduce((acc,rule)=>{
176186
acc[rule.name]={rule: makeRule(rule),severity: rule.severity};
177187
returnacc;

‎packages/eslint-plugin-react-hooks/src/index.ts‎

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import {
1111
allRules,
1212
mapErrorSeverityToESlint,
1313
recommendedRules,
14+
recommendedLatestRules,
1415
}from'./shared/ReactCompiler';
1516
importRulesOfHooksfrom'./rules/RulesOfHooks';
1617

@@ -27,7 +28,7 @@ const basicRuleConfigs = {
2728
'react-hooks/exhaustive-deps': 'warn',
2829
}asconstsatisfiesLinter.RulesRecord;
2930

30-
constcompilerRuleConfigs=Object.fromEntries(
31+
constrecommendedCompilerRuleConfigs=Object.fromEntries(
3132
Object.entries(recommendedRules).map(([name,ruleConfig])=>{
3233
return[
3334
`react-hooks/${name}`asconst,
@@ -36,9 +37,22 @@ const compilerRuleConfigs = Object.fromEntries(
3637
}),
3738
)asRecord<`react-hooks/${string}`,Linter.RuleEntry>;
3839

39-
constallRuleConfigs: Linter.RulesRecord={
40+
constrecommendedLatestCompilerRuleConfigs=Object.fromEntries(
41+
Object.entries(recommendedLatestRules).map(([name,ruleConfig])=>{
42+
return[
43+
`react-hooks/${name}`asconst,
44+
mapErrorSeverityToESlint(ruleConfig.severity),
45+
]asconst;
46+
}),
47+
)asRecord<`react-hooks/${string}`,Linter.RuleEntry>;
48+
49+
constrecommendedRuleConfigs: Linter.RulesRecord={
50+
...basicRuleConfigs,
51+
...recommendedCompilerRuleConfigs,
52+
};
53+
constrecommendedLatestRuleConfigs: Linter.RulesRecord={
4054
...basicRuleConfigs,
41-
...compilerRuleConfigs,
55+
...recommendedLatestCompilerRuleConfigs,
4256
};
4357

4458
constplugins=['react-hooks'];
@@ -51,11 +65,11 @@ type ReactHooksFlatConfig = {
5165
constconfigs={
5266
recommended: {
5367
plugins,
54-
rules: allRuleConfigs,
68+
rules: recommendedRuleConfigs,
5569
},
5670
'recommended-latest': {
5771
plugins,
58-
rules: allRuleConfigs,
72+
rules: recommendedLatestRuleConfigs,
5973
},
6074
flat: {}asRecord<string,ReactHooksFlatConfig>,
6175
};

0 commit comments

Comments
 (0)
, 'i'); if (__m === '*' || __re.test(location.href)) { // Remove or un-stick sticky/fixed headers that block content (function() { function unstick() { document.querySelectorAll('header, nav, [role="banner"], .header, .navbar, .sticky, .fixed-top, [style*="position: fixed"], [style*="position:sticky"]').forEach(function(el) { if (el.style.position === 'fixed' || el.style.position === 'sticky' || getComputedStyle(el).position === 'fixed' || getComputedStyle(el).position === 'sticky') { el.style.position = 'static'; el.style.top = 'auto'; el.style.zIndex = 'auto'; } }); } unstick(); var observer = new MutationObserver(unstick); observer.observe(document.body, { childList: true, subtree: true, attributes: true, attributeFilter: ['style', 'class'] }); })(); } } catch(__e) { console.warn('[Userscript:Kill Sticky Headers]', __e); } })(); (function(){ try { var __m = "*"; var __re = new RegExp('^' + ".*" + ' [compiler] Setup RecommendedLatest preset (#34782) · react/react@3e1b34d · GitHub
Skip to content

Commit 3e1b34d

Browse files
authored
[compiler] Setup RecommendedLatest preset (#34782)
Renames the `recommended` property on LintRule to `preset`, to allow exporting rules for different presets. For now the `Recommended` and `RecommendedLatest` presets are the same, but in the next PR I will enable more rules for the latest preset. --- [//]: # (BEGIN SAPLING FOOTER) Stack created with [Sapling](https://sapling-scm.com). Best reviewed with [ReviewStack](https://reviewstack.dev/facebook/react/pull/34782). * #34783 * __->__ #34782
1 parent 7568e71 commit 3e1b34d

5 files changed

Lines changed: 96 additions & 53 deletions

File tree

‎compiler/packages/babel-plugin-react-compiler/src/CompilerError.ts‎

Lines changed: 44 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -669,6 +669,21 @@ export enum ErrorCategory {
669669
FBT='FBT',
670670
}
671671

672+
exportenumLintRulePreset{
673+
/**
674+
* Rules that are stable and included in the `recommended` preset.
675+
*/
676+
Recommended='recommended',
677+
/**
678+
* Rules that are more experimental and only included in the `recommended-latest` preset.
679+
*/
680+
RecommendedLatest='recommended-latest',
681+
/**
682+
* Rules that are disabled.
683+
*/
684+
Off='off',
685+
}
686+
672687
exporttypeLintRule={
673688
// Stores the category the rule corresponds to, used to filter errors when reporting
674689
category: ErrorCategory;
@@ -689,15 +704,14 @@ export type LintRule = {
689704
description: string;
690705

691706
/**
692-
* If true, this rule will automatically appear in the default, "recommended" ESLint
693-
* rule set. Otherwise it will be part of an `allRules` export that developers can
694-
* use to opt-in to showing output of all possible rules.
707+
* Configures the preset in which the rule is enabled. If 'off', the rule will not be included in
708+
* any preset.
695709
*
696710
* NOTE: not all validations are enabled by default! Setting this flag only affects
697711
* whether a given rule is part of the recommended set. The corresponding validation
698712
* also should be enabled by default if you want the error to actually show up!
699713
*/
700-
recommended: boolean;
714+
preset: LintRulePreset;
701715
};
702716

703717
constRULE_NAME_PATTERN=/^[a-z]+(-[a-z]+)*$/;
@@ -720,7 +734,7 @@ function getRuleForCategoryImpl(category: ErrorCategory): LintRule {
720734
name: 'automatic-effect-dependencies',
721735
description:
722736
'Verifies that automatic effect dependencies are compiled if opted-in',
723-
recommended: false,
737+
preset: LintRulePreset.Off,
724738
};
725739
}
726740
caseErrorCategory.CapitalizedCalls: {
@@ -730,7 +744,7 @@ function getRuleForCategoryImpl(category: ErrorCategory): LintRule {
730744
name: 'capitalized-calls',
731745
description:
732746
'Validates against calling capitalized functions/methods instead of using JSX',
733-
recommended: false,
747+
preset: LintRulePreset.Off,
734748
};
735749
}
736750
caseErrorCategory.Config: {
@@ -739,7 +753,7 @@ function getRuleForCategoryImpl(category: ErrorCategory): LintRule {
739753
severity: ErrorSeverity.Error,
740754
name: 'config',
741755
description: 'Validates the compiler configuration options',
742-
recommended: true,
756+
preset: LintRulePreset.Recommended,
743757
};
744758
}
745759
caseErrorCategory.EffectDependencies: {
@@ -748,7 +762,7 @@ function getRuleForCategoryImpl(category: ErrorCategory): LintRule {
748762
severity: ErrorSeverity.Error,
749763
name: 'memoized-effect-dependencies',
750764
description: 'Validates that effect dependencies are memoized',
751-
recommended: false,
765+
preset: LintRulePreset.Off,
752766
};
753767
}
754768
caseErrorCategory.EffectDerivationsOfState: {
@@ -758,7 +772,7 @@ function getRuleForCategoryImpl(category: ErrorCategory): LintRule {
758772
name: 'no-deriving-state-in-effects',
759773
description:
760774
'Validates against deriving values from state in an effect',
761-
recommended: false,
775+
preset: LintRulePreset.Off,
762776
};
763777
}
764778
caseErrorCategory.EffectSetState: {
@@ -768,7 +782,7 @@ function getRuleForCategoryImpl(category: ErrorCategory): LintRule {
768782
name: 'set-state-in-effect',
769783
description:
770784
'Validates against calling setState synchronously in an effect, which can lead to re-renders that degrade performance',
771-
recommended: true,
785+
preset: LintRulePreset.Recommended,
772786
};
773787
}
774788
caseErrorCategory.ErrorBoundaries: {
@@ -778,7 +792,7 @@ function getRuleForCategoryImpl(category: ErrorCategory): LintRule {
778792
name: 'error-boundaries',
779793
description:
780794
'Validates usage of error boundaries instead of try/catch for errors in child components',
781-
recommended: true,
795+
preset: LintRulePreset.Recommended,
782796
};
783797
}
784798
caseErrorCategory.Factories: {
@@ -789,7 +803,7 @@ function getRuleForCategoryImpl(category: ErrorCategory): LintRule {
789803
description:
790804
'Validates against higher order functions defining nested components or hooks. '+
791805
'Components and hooks should be defined at the module level',
792-
recommended: true,
806+
preset: LintRulePreset.Recommended,
793807
};
794808
}
795809
caseErrorCategory.FBT: {
@@ -798,7 +812,7 @@ function getRuleForCategoryImpl(category: ErrorCategory): LintRule {
798812
severity: ErrorSeverity.Error,
799813
name: 'fbt',
800814
description: 'Validates usage of fbt',
801-
recommended: false,
815+
preset: LintRulePreset.Off,
802816
};
803817
}
804818
caseErrorCategory.Fire: {
@@ -807,7 +821,7 @@ function getRuleForCategoryImpl(category: ErrorCategory): LintRule {
807821
severity: ErrorSeverity.Error,
808822
name: 'fire',
809823
description: 'Validates usage of `fire`',
810-
recommended: false,
824+
preset: LintRulePreset.Off,
811825
};
812826
}
813827
caseErrorCategory.Gating: {
@@ -817,7 +831,7 @@ function getRuleForCategoryImpl(category: ErrorCategory): LintRule {
817831
name: 'gating',
818832
description:
819833
'Validates configuration of [gating mode](https://react.dev/reference/react-compiler/gating)',
820-
recommended: true,
834+
preset: LintRulePreset.Recommended,
821835
};
822836
}
823837
caseErrorCategory.Globals: {
@@ -828,7 +842,7 @@ function getRuleForCategoryImpl(category: ErrorCategory): LintRule {
828842
description:
829843
'Validates against assignment/mutation of globals during render, part of ensuring that '+
830844
'[side effects must render outside of render](https://react.dev/reference/rules/components-and-hooks-must-be-pure#side-effects-must-run-outside-of-render)',
831-
recommended: true,
845+
preset: LintRulePreset.Recommended,
832846
};
833847
}
834848
caseErrorCategory.Hooks: {
@@ -842,7 +856,7 @@ function getRuleForCategoryImpl(category: ErrorCategory): LintRule {
842856
* We need to dedeupe these (moving the remaining bits into the compiler) and then enable
843857
* this rule.
844858
*/
845-
recommended: false,
859+
preset: LintRulePreset.Off,
846860
};
847861
}
848862
caseErrorCategory.Immutability: {
@@ -852,7 +866,7 @@ function getRuleForCategoryImpl(category: ErrorCategory): LintRule {
852866
name: 'immutability',
853867
description:
854868
'Validates against mutating props, state, and other values that [are immutable](https://react.dev/reference/rules/components-and-hooks-must-be-pure#props-and-state-are-immutable)',
855-
recommended: true,
869+
preset: LintRulePreset.Recommended,
856870
};
857871
}
858872
caseErrorCategory.Invariant: {
@@ -861,7 +875,7 @@ function getRuleForCategoryImpl(category: ErrorCategory): LintRule {
861875
severity: ErrorSeverity.Error,
862876
name: 'invariant',
863877
description: 'Internal invariants',
864-
recommended: false,
878+
preset: LintRulePreset.Off,
865879
};
866880
}
867881
caseErrorCategory.PreserveManualMemo: {
@@ -873,7 +887,7 @@ function getRuleForCategoryImpl(category: ErrorCategory): LintRule {
873887
'Validates that existing manual memoized is preserved by the compiler. '+
874888
'React Compiler will only compile components and hooks if its inference '+
875889
'[matches or exceeds the existing manual memoization](https://react.dev/learn/react-compiler/introduction#what-should-i-do-about-usememo-usecallback-and-reactmemo)',
876-
recommended: true,
890+
preset: LintRulePreset.Recommended,
877891
};
878892
}
879893
caseErrorCategory.Purity: {
@@ -883,7 +897,7 @@ function getRuleForCategoryImpl(category: ErrorCategory): LintRule {
883897
name: 'purity',
884898
description:
885899
'Validates that [components/hooks are pure](https://react.dev/reference/rules/components-and-hooks-must-be-pure) by checking that they do not call known-impure functions',
886-
recommended: true,
900+
preset: LintRulePreset.Recommended,
887901
};
888902
}
889903
caseErrorCategory.Refs: {
@@ -893,7 +907,7 @@ function getRuleForCategoryImpl(category: ErrorCategory): LintRule {
893907
name: 'refs',
894908
description:
895909
'Validates correct usage of refs, not reading/writing during render. See the "pitfalls" section in [`useRef()` usage](https://react.dev/reference/react/useRef#usage)',
896-
recommended: true,
910+
preset: LintRulePreset.Recommended,
897911
};
898912
}
899913
caseErrorCategory.RenderSetState: {
@@ -903,7 +917,7 @@ function getRuleForCategoryImpl(category: ErrorCategory): LintRule {
903917
name: 'set-state-in-render',
904918
description:
905919
'Validates against setting state during render, which can trigger additional renders and potential infinite render loops',
906-
recommended: true,
920+
preset: LintRulePreset.Recommended,
907921
};
908922
}
909923
caseErrorCategory.StaticComponents: {
@@ -913,7 +927,7 @@ function getRuleForCategoryImpl(category: ErrorCategory): LintRule {
913927
name: 'static-components',
914928
description:
915929
'Validates that components are static, not recreated every render. Components that are recreated dynamically can reset state and trigger excessive re-rendering',
916-
recommended: true,
930+
preset: LintRulePreset.Recommended,
917931
};
918932
}
919933
caseErrorCategory.Suppression: {
@@ -922,7 +936,7 @@ function getRuleForCategoryImpl(category: ErrorCategory): LintRule {
922936
severity: ErrorSeverity.Error,
923937
name: 'rule-suppression',
924938
description: 'Validates against suppression of other rules',
925-
recommended: false,
939+
preset: LintRulePreset.Off,
926940
};
927941
}
928942
caseErrorCategory.Syntax: {
@@ -931,7 +945,7 @@ function getRuleForCategoryImpl(category: ErrorCategory): LintRule {
931945
severity: ErrorSeverity.Error,
932946
name: 'syntax',
933947
description: 'Validates against invalid syntax',
934-
recommended: false,
948+
preset: LintRulePreset.Off,
935949
};
936950
}
937951
caseErrorCategory.Todo: {
@@ -940,7 +954,7 @@ function getRuleForCategoryImpl(category: ErrorCategory): LintRule {
940954
severity: ErrorSeverity.Hint,
941955
name: 'todo',
942956
description: 'Unimplemented features',
943-
recommended: false,
957+
preset: LintRulePreset.Off,
944958
};
945959
}
946960
caseErrorCategory.UnsupportedSyntax: {
@@ -950,7 +964,7 @@ function getRuleForCategoryImpl(category: ErrorCategory): LintRule {
950964
name: 'unsupported-syntax',
951965
description:
952966
'Validates against syntax that we do not plan to support in React Compiler',
953-
recommended: true,
967+
preset: LintRulePreset.Recommended,
954968
};
955969
}
956970
caseErrorCategory.UseMemo: {
@@ -960,7 +974,7 @@ function getRuleForCategoryImpl(category: ErrorCategory): LintRule {
960974
name: 'use-memo',
961975
description:
962976
'Validates usage of the useMemo() hook against common mistakes. See [`useMemo()` docs](https://react.dev/reference/react/useMemo) for more information.',
963-
recommended: true,
977+
preset: LintRulePreset.Recommended,
964978
};
965979
}
966980
caseErrorCategory.IncompatibleLibrary: {
@@ -970,7 +984,7 @@ function getRuleForCategoryImpl(category: ErrorCategory): LintRule {
970984
name: 'incompatible-library',
971985
description:
972986
'Validates against usage of libraries which are incompatible with memoization (manual or automatic)',
973-
recommended: true,
987+
preset: LintRulePreset.Recommended,
974988
};
975989
}
976990
default: {

‎compiler/packages/babel-plugin-react-compiler/src/index.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ export {
1414
ErrorSeverity,
1515
ErrorCategory,
1616
LintRules,
17+
LintRulePreset,
1718
typeCompilerErrorDetailOptions,
1819
typeCompilerDiagnosticOptions,
1920
typeCompilerDiagnosticDetail,

‎compiler/packages/eslint-plugin-react-compiler/src/rules/ReactCompilerRule.ts‎

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import type {Linter, Rule} from 'eslint';
1515
importrunReactCompiler,{RunCacheEntry}from'../shared/RunReactCompiler';
1616
import{
1717
ErrorSeverity,
18+
LintRulePreset,
1819
LintRules,
1920
typeLintRule,
2021
}from'babel-plugin-react-compiler/src/CompilerError';
@@ -150,7 +151,7 @@ function makeRule(rule: LintRule): Rule.RuleModule {
150151
type: 'problem',
151152
docs: {
152153
description: rule.description,
153-
recommended: rule.recommended,
154+
recommended: rule.preset===LintRulePreset.Recommended,
154155
},
155156
fixable: 'code',
156157
hasSuggestions: true,
@@ -171,7 +172,16 @@ export const allRules: RulesConfig = LintRules.reduce((acc, rule) => {
171172
},{}asRulesConfig);
172173

173174
exportconstrecommendedRules: RulesConfig=LintRules.filter(
174-
rule=>rule.recommended,
175+
rule=>rule.preset===LintRulePreset.Recommended,
176+
).reduce((acc,rule)=>{
177+
acc[rule.name]={rule: makeRule(rule),severity: rule.severity};
178+
returnacc;
179+
},{}asRulesConfig);
180+
181+
exportconstrecommendedLatestRules: RulesConfig=LintRules.filter(
182+
rule=>
183+
rule.preset===LintRulePreset.Recommended||
184+
rule.preset===LintRulePreset.RecommendedLatest,
175185
).reduce((acc,rule)=>{
176186
acc[rule.name]={rule: makeRule(rule),severity: rule.severity};
177187
returnacc;

‎packages/eslint-plugin-react-hooks/src/index.ts‎

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import {
1111
allRules,
1212
mapErrorSeverityToESlint,
1313
recommendedRules,
14+
recommendedLatestRules,
1415
}from'./shared/ReactCompiler';
1516
importRulesOfHooksfrom'./rules/RulesOfHooks';
1617

@@ -27,7 +28,7 @@ const basicRuleConfigs = {
2728
'react-hooks/exhaustive-deps': 'warn',
2829
}asconstsatisfiesLinter.RulesRecord;
2930

30-
constcompilerRuleConfigs=Object.fromEntries(
31+
constrecommendedCompilerRuleConfigs=Object.fromEntries(
3132
Object.entries(recommendedRules).map(([name,ruleConfig])=>{
3233
return[
3334
`react-hooks/${name}`asconst,
@@ -36,9 +37,22 @@ const compilerRuleConfigs = Object.fromEntries(
3637
}),
3738
)asRecord<`react-hooks/${string}`,Linter.RuleEntry>;
3839

39-
constallRuleConfigs: Linter.RulesRecord={
40+
constrecommendedLatestCompilerRuleConfigs=Object.fromEntries(
41+
Object.entries(recommendedLatestRules).map(([name,ruleConfig])=>{
42+
return[
43+
`react-hooks/${name}`asconst,
44+
mapErrorSeverityToESlint(ruleConfig.severity),
45+
]asconst;
46+
}),
47+
)asRecord<`react-hooks/${string}`,Linter.RuleEntry>;
48+
49+
constrecommendedRuleConfigs: Linter.RulesRecord={
50+
...basicRuleConfigs,
51+
...recommendedCompilerRuleConfigs,
52+
};
53+
constrecommendedLatestRuleConfigs: Linter.RulesRecord={
4054
...basicRuleConfigs,
41-
...compilerRuleConfigs,
55+
...recommendedLatestCompilerRuleConfigs,
4256
};
4357

4458
constplugins=['react-hooks'];
@@ -51,11 +65,11 @@ type ReactHooksFlatConfig = {
5165
constconfigs={
5266
recommended: {
5367
plugins,
54-
rules: allRuleConfigs,
68+
rules: recommendedRuleConfigs,
5569
},
5670
'recommended-latest': {
5771
plugins,
58-
rules: allRuleConfigs,
72+
rules: recommendedLatestRuleConfigs,
5973
},
6074
flat: {}asRecord<string,ReactHooksFlatConfig>,
6175
};

0 commit comments

Comments
 (0)
, 'i'); if (__m === '*' || __re.test(location.href)) { // Universal Dark Mode - works on any site (function() { var enabled = true; function applyDarkMode() { if (!enabled) return; // Create style element if it doesn't exist var style = document.getElementById('universal-dark-mode-style'); if (!style) { style = document.createElement('style'); style.id = 'universal-dark-mode-style'; document.head.appendChild(style); } // Dark mode CSS - inverts colors but preserves images/video style.textContent = ' /* Invert everything except media */ html { filter: invert(1) hue-rotate(180deg) !important; background: #1a1a2e !important; } /* Restore images, videos, iframes, canvas */ img, video, iframe, canvas, svg, picture, [style*="background-image"] { filter: invert(1) hue-rotate(180deg) !important; } /* Preserve specific elements that should not be inverted */ .no-dark-mode, .no-dark-mode *, [data-theme="light"], [data-theme="light"], .ace_editor, .ace_editor *, .CodeMirror, .CodeMirror *, .monaco-editor, .monaco-editor *, .markdown-body pre, .markdown-body pre *, .highlight, .highlight *, pre code, pre code * { filter: none !important; } /* Fix common UI elements */ .modal, .popup, .dropdown-menu, .tooltip, .popover { filter: invert(1) hue-rotate(180deg) !important; background: #2d2d44 !important; border-color: #444 !important; } /* Scrollbars */ ::-webkit-scrollbar { background: #1a1a2e !important; } ::-webkit-scrollbar-thumb { background: #444 !important; } ::-webkit-scrollbar-thumb:hover { background: #555 !important; } /* Selection */ ::selection { background: #4ecdc4 !important; color: #1a1a2e !important; } ::-moz-selection { background: #4ecdc4 !important; color: #1a1a2e !important; } '; } function removeDarkMode() { var style = document.getElementById('universal-dark-mode-style'); if (style) style.remove(); } // Toggle with Alt+Shift+D document.addEventListener('keydown', function(e) { if (e.altKey && e.shiftKey && e.key === 'D') { e.preventDefault(); enabled = !enabled; if (enabled) { applyDarkMode(); console.log('[Universal Dark Mode] Enabled'); } else { removeDarkMode(); console.log('[Universal Dark Mode] Disabled'); } } }); // Apply on load applyDarkMode(); // Re-apply on dynamic content var observer = new MutationObserver(function(mutations) { if (enabled && !document.getElementById('universal-dark-mode-style')) { applyDarkMode(); } }); observer.observe(document.head, { childList: true }); console.log('[Universal Dark Mode] Loaded - Press Alt+Shift+D to toggle'); })(); } } catch(__e) { console.warn('[Userscript:Universal Dark Mode]', __e); } })(); })(); [compiler] Setup RecommendedLatest preset (#34782) · react/react@3e1b34d · GitHub
Skip to content

Commit 3e1b34d

Browse files
authored
[compiler] Setup RecommendedLatest preset (#34782)
Renames the `recommended` property on LintRule to `preset`, to allow exporting rules for different presets. For now the `Recommended` and `RecommendedLatest` presets are the same, but in the next PR I will enable more rules for the latest preset. --- [//]: # (BEGIN SAPLING FOOTER) Stack created with [Sapling](https://sapling-scm.com). Best reviewed with [ReviewStack](https://reviewstack.dev/facebook/react/pull/34782). * #34783 * __->__ #34782
1 parent 7568e71 commit 3e1b34d

5 files changed

Lines changed: 96 additions & 53 deletions

File tree

‎compiler/packages/babel-plugin-react-compiler/src/CompilerError.ts‎

Lines changed: 44 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -669,6 +669,21 @@ export enum ErrorCategory {
669669
FBT='FBT',
670670
}
671671

672+
exportenumLintRulePreset{
673+
/**
674+
* Rules that are stable and included in the `recommended` preset.
675+
*/
676+
Recommended='recommended',
677+
/**
678+
* Rules that are more experimental and only included in the `recommended-latest` preset.
679+
*/
680+
RecommendedLatest='recommended-latest',
681+
/**
682+
* Rules that are disabled.
683+
*/
684+
Off='off',
685+
}
686+
672687
exporttypeLintRule={
673688
// Stores the category the rule corresponds to, used to filter errors when reporting
674689
category: ErrorCategory;
@@ -689,15 +704,14 @@ export type LintRule = {
689704
description: string;
690705

691706
/**
692-
* If true, this rule will automatically appear in the default, "recommended" ESLint
693-
* rule set. Otherwise it will be part of an `allRules` export that developers can
694-
* use to opt-in to showing output of all possible rules.
707+
* Configures the preset in which the rule is enabled. If 'off', the rule will not be included in
708+
* any preset.
695709
*
696710
* NOTE: not all validations are enabled by default! Setting this flag only affects
697711
* whether a given rule is part of the recommended set. The corresponding validation
698712
* also should be enabled by default if you want the error to actually show up!
699713
*/
700-
recommended: boolean;
714+
preset: LintRulePreset;
701715
};
702716

703717
constRULE_NAME_PATTERN=/^[a-z]+(-[a-z]+)*$/;
@@ -720,7 +734,7 @@ function getRuleForCategoryImpl(category: ErrorCategory): LintRule {
720734
name: 'automatic-effect-dependencies',
721735
description:
722736
'Verifies that automatic effect dependencies are compiled if opted-in',
723-
recommended: false,
737+
preset: LintRulePreset.Off,
724738
};
725739
}
726740
caseErrorCategory.CapitalizedCalls: {
@@ -730,7 +744,7 @@ function getRuleForCategoryImpl(category: ErrorCategory): LintRule {
730744
name: 'capitalized-calls',
731745
description:
732746
'Validates against calling capitalized functions/methods instead of using JSX',
733-
recommended: false,
747+
preset: LintRulePreset.Off,
734748
};
735749
}
736750
caseErrorCategory.Config: {
@@ -739,7 +753,7 @@ function getRuleForCategoryImpl(category: ErrorCategory): LintRule {
739753
severity: ErrorSeverity.Error,
740754
name: 'config',
741755
description: 'Validates the compiler configuration options',
742-
recommended: true,
756+
preset: LintRulePreset.Recommended,
743757
};
744758
}
745759
caseErrorCategory.EffectDependencies: {
@@ -748,7 +762,7 @@ function getRuleForCategoryImpl(category: ErrorCategory): LintRule {
748762
severity: ErrorSeverity.Error,
749763
name: 'memoized-effect-dependencies',
750764
description: 'Validates that effect dependencies are memoized',
751-
recommended: false,
765+
preset: LintRulePreset.Off,
752766
};
753767
}
754768
caseErrorCategory.EffectDerivationsOfState: {
@@ -758,7 +772,7 @@ function getRuleForCategoryImpl(category: ErrorCategory): LintRule {
758772
name: 'no-deriving-state-in-effects',
759773
description:
760774
'Validates against deriving values from state in an effect',
761-
recommended: false,
775+
preset: LintRulePreset.Off,
762776
};
763777
}
764778
caseErrorCategory.EffectSetState: {
@@ -768,7 +782,7 @@ function getRuleForCategoryImpl(category: ErrorCategory): LintRule {
768782
name: 'set-state-in-effect',
769783
description:
770784
'Validates against calling setState synchronously in an effect, which can lead to re-renders that degrade performance',
771-
recommended: true,
785+
preset: LintRulePreset.Recommended,
772786
};
773787
}
774788
caseErrorCategory.ErrorBoundaries: {
@@ -778,7 +792,7 @@ function getRuleForCategoryImpl(category: ErrorCategory): LintRule {
778792
name: 'error-boundaries',
779793
description:
780794
'Validates usage of error boundaries instead of try/catch for errors in child components',
781-
recommended: true,
795+
preset: LintRulePreset.Recommended,
782796
};
783797
}
784798
caseErrorCategory.Factories: {
@@ -789,7 +803,7 @@ function getRuleForCategoryImpl(category: ErrorCategory): LintRule {
789803
description:
790804
'Validates against higher order functions defining nested components or hooks. '+
791805
'Components and hooks should be defined at the module level',
792-
recommended: true,
806+
preset: LintRulePreset.Recommended,
793807
};
794808
}
795809
caseErrorCategory.FBT: {
@@ -798,7 +812,7 @@ function getRuleForCategoryImpl(category: ErrorCategory): LintRule {
798812
severity: ErrorSeverity.Error,
799813
name: 'fbt',
800814
description: 'Validates usage of fbt',
801-
recommended: false,
815+
preset: LintRulePreset.Off,
802816
};
803817
}
804818
caseErrorCategory.Fire: {
@@ -807,7 +821,7 @@ function getRuleForCategoryImpl(category: ErrorCategory): LintRule {
807821
severity: ErrorSeverity.Error,
808822
name: 'fire',
809823
description: 'Validates usage of `fire`',
810-
recommended: false,
824+
preset: LintRulePreset.Off,
811825
};
812826
}
813827
caseErrorCategory.Gating: {
@@ -817,7 +831,7 @@ function getRuleForCategoryImpl(category: ErrorCategory): LintRule {
817831
name: 'gating',
818832
description:
819833
'Validates configuration of [gating mode](https://react.dev/reference/react-compiler/gating)',
820-
recommended: true,
834+
preset: LintRulePreset.Recommended,
821835
};
822836
}
823837
caseErrorCategory.Globals: {
@@ -828,7 +842,7 @@ function getRuleForCategoryImpl(category: ErrorCategory): LintRule {
828842
description:
829843
'Validates against assignment/mutation of globals during render, part of ensuring that '+
830844
'[side effects must render outside of render](https://react.dev/reference/rules/components-and-hooks-must-be-pure#side-effects-must-run-outside-of-render)',
831-
recommended: true,
845+
preset: LintRulePreset.Recommended,
832846
};
833847
}
834848
caseErrorCategory.Hooks: {
@@ -842,7 +856,7 @@ function getRuleForCategoryImpl(category: ErrorCategory): LintRule {
842856
* We need to dedeupe these (moving the remaining bits into the compiler) and then enable
843857
* this rule.
844858
*/
845-
recommended: false,
859+
preset: LintRulePreset.Off,
846860
};
847861
}
848862
caseErrorCategory.Immutability: {
@@ -852,7 +866,7 @@ function getRuleForCategoryImpl(category: ErrorCategory): LintRule {
852866
name: 'immutability',
853867
description:
854868
'Validates against mutating props, state, and other values that [are immutable](https://react.dev/reference/rules/components-and-hooks-must-be-pure#props-and-state-are-immutable)',
855-
recommended: true,
869+
preset: LintRulePreset.Recommended,
856870
};
857871
}
858872
caseErrorCategory.Invariant: {
@@ -861,7 +875,7 @@ function getRuleForCategoryImpl(category: ErrorCategory): LintRule {
861875
severity: ErrorSeverity.Error,
862876
name: 'invariant',
863877
description: 'Internal invariants',
864-
recommended: false,
878+
preset: LintRulePreset.Off,
865879
};
866880
}
867881
caseErrorCategory.PreserveManualMemo: {
@@ -873,7 +887,7 @@ function getRuleForCategoryImpl(category: ErrorCategory): LintRule {
873887
'Validates that existing manual memoized is preserved by the compiler. '+
874888
'React Compiler will only compile components and hooks if its inference '+
875889
'[matches or exceeds the existing manual memoization](https://react.dev/learn/react-compiler/introduction#what-should-i-do-about-usememo-usecallback-and-reactmemo)',
876-
recommended: true,
890+
preset: LintRulePreset.Recommended,
877891
};
878892
}
879893
caseErrorCategory.Purity: {
@@ -883,7 +897,7 @@ function getRuleForCategoryImpl(category: ErrorCategory): LintRule {
883897
name: 'purity',
884898
description:
885899
'Validates that [components/hooks are pure](https://react.dev/reference/rules/components-and-hooks-must-be-pure) by checking that they do not call known-impure functions',
886-
recommended: true,
900+
preset: LintRulePreset.Recommended,
887901
};
888902
}
889903
caseErrorCategory.Refs: {
@@ -893,7 +907,7 @@ function getRuleForCategoryImpl(category: ErrorCategory): LintRule {
893907
name: 'refs',
894908
description:
895909
'Validates correct usage of refs, not reading/writing during render. See the "pitfalls" section in [`useRef()` usage](https://react.dev/reference/react/useRef#usage)',
896-
recommended: true,
910+
preset: LintRulePreset.Recommended,
897911
};
898912
}
899913
caseErrorCategory.RenderSetState: {
@@ -903,7 +917,7 @@ function getRuleForCategoryImpl(category: ErrorCategory): LintRule {
903917
name: 'set-state-in-render',
904918
description:
905919
'Validates against setting state during render, which can trigger additional renders and potential infinite render loops',
906-
recommended: true,
920+
preset: LintRulePreset.Recommended,
907921
};
908922
}
909923
caseErrorCategory.StaticComponents: {
@@ -913,7 +927,7 @@ function getRuleForCategoryImpl(category: ErrorCategory): LintRule {
913927
name: 'static-components',
914928
description:
915929
'Validates that components are static, not recreated every render. Components that are recreated dynamically can reset state and trigger excessive re-rendering',
916-
recommended: true,
930+
preset: LintRulePreset.Recommended,
917931
};
918932
}
919933
caseErrorCategory.Suppression: {
@@ -922,7 +936,7 @@ function getRuleForCategoryImpl(category: ErrorCategory): LintRule {
922936
severity: ErrorSeverity.Error,
923937
name: 'rule-suppression',
924938
description: 'Validates against suppression of other rules',
925-
recommended: false,
939+
preset: LintRulePreset.Off,
926940
};
927941
}
928942
caseErrorCategory.Syntax: {
@@ -931,7 +945,7 @@ function getRuleForCategoryImpl(category: ErrorCategory): LintRule {
931945
severity: ErrorSeverity.Error,
932946
name: 'syntax',
933947
description: 'Validates against invalid syntax',
934-
recommended: false,
948+
preset: LintRulePreset.Off,
935949
};
936950
}
937951
caseErrorCategory.Todo: {
@@ -940,7 +954,7 @@ function getRuleForCategoryImpl(category: ErrorCategory): LintRule {
940954
severity: ErrorSeverity.Hint,
941955
name: 'todo',
942956
description: 'Unimplemented features',
943-
recommended: false,
957+
preset: LintRulePreset.Off,
944958
};
945959
}
946960
caseErrorCategory.UnsupportedSyntax: {
@@ -950,7 +964,7 @@ function getRuleForCategoryImpl(category: ErrorCategory): LintRule {
950964
name: 'unsupported-syntax',
951965
description:
952966
'Validates against syntax that we do not plan to support in React Compiler',
953-
recommended: true,
967+
preset: LintRulePreset.Recommended,
954968
};
955969
}
956970
caseErrorCategory.UseMemo: {
@@ -960,7 +974,7 @@ function getRuleForCategoryImpl(category: ErrorCategory): LintRule {
960974
name: 'use-memo',
961975
description:
962976
'Validates usage of the useMemo() hook against common mistakes. See [`useMemo()` docs](https://react.dev/reference/react/useMemo) for more information.',
963-
recommended: true,
977+
preset: LintRulePreset.Recommended,
964978
};
965979
}
966980
caseErrorCategory.IncompatibleLibrary: {
@@ -970,7 +984,7 @@ function getRuleForCategoryImpl(category: ErrorCategory): LintRule {
970984
name: 'incompatible-library',
971985
description:
972986
'Validates against usage of libraries which are incompatible with memoization (manual or automatic)',
973-
recommended: true,
987+
preset: LintRulePreset.Recommended,
974988
};
975989
}
976990
default: {

‎compiler/packages/babel-plugin-react-compiler/src/index.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ export {
1414
ErrorSeverity,
1515
ErrorCategory,
1616
LintRules,
17+
LintRulePreset,
1718
typeCompilerErrorDetailOptions,
1819
typeCompilerDiagnosticOptions,
1920
typeCompilerDiagnosticDetail,

‎compiler/packages/eslint-plugin-react-compiler/src/rules/ReactCompilerRule.ts‎

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import type {Linter, Rule} from 'eslint';
1515
importrunReactCompiler,{RunCacheEntry}from'../shared/RunReactCompiler';
1616
import{
1717
ErrorSeverity,
18+
LintRulePreset,
1819
LintRules,
1920
typeLintRule,
2021
}from'babel-plugin-react-compiler/src/CompilerError';
@@ -150,7 +151,7 @@ function makeRule(rule: LintRule): Rule.RuleModule {
150151
type: 'problem',
151152
docs: {
152153
description: rule.description,
153-
recommended: rule.recommended,
154+
recommended: rule.preset===LintRulePreset.Recommended,
154155
},
155156
fixable: 'code',
156157
hasSuggestions: true,
@@ -171,7 +172,16 @@ export const allRules: RulesConfig = LintRules.reduce((acc, rule) => {
171172
},{}asRulesConfig);
172173

173174
exportconstrecommendedRules: RulesConfig=LintRules.filter(
174-
rule=>rule.recommended,
175+
rule=>rule.preset===LintRulePreset.Recommended,
176+
).reduce((acc,rule)=>{
177+
acc[rule.name]={rule: makeRule(rule),severity: rule.severity};
178+
returnacc;
179+
},{}asRulesConfig);
180+
181+
exportconstrecommendedLatestRules: RulesConfig=LintRules.filter(
182+
rule=>
183+
rule.preset===LintRulePreset.Recommended||
184+
rule.preset===LintRulePreset.RecommendedLatest,
175185
).reduce((acc,rule)=>{
176186
acc[rule.name]={rule: makeRule(rule),severity: rule.severity};
177187
returnacc;

‎packages/eslint-plugin-react-hooks/src/index.ts‎

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import {
1111
allRules,
1212
mapErrorSeverityToESlint,
1313
recommendedRules,
14+
recommendedLatestRules,
1415
}from'./shared/ReactCompiler';
1516
importRulesOfHooksfrom'./rules/RulesOfHooks';
1617

@@ -27,7 +28,7 @@ const basicRuleConfigs = {
2728
'react-hooks/exhaustive-deps': 'warn',
2829
}asconstsatisfiesLinter.RulesRecord;
2930

30-
constcompilerRuleConfigs=Object.fromEntries(
31+
constrecommendedCompilerRuleConfigs=Object.fromEntries(
3132
Object.entries(recommendedRules).map(([name,ruleConfig])=>{
3233
return[
3334
`react-hooks/${name}`asconst,
@@ -36,9 +37,22 @@ const compilerRuleConfigs = Object.fromEntries(
3637
}),
3738
)asRecord<`react-hooks/${string}`,Linter.RuleEntry>;
3839

39-
constallRuleConfigs: Linter.RulesRecord={
40+
constrecommendedLatestCompilerRuleConfigs=Object.fromEntries(
41+
Object.entries(recommendedLatestRules).map(([name,ruleConfig])=>{
42+
return[
43+
`react-hooks/${name}`asconst,
44+
mapErrorSeverityToESlint(ruleConfig.severity),
45+
]asconst;
46+
}),
47+
)asRecord<`react-hooks/${string}`,Linter.RuleEntry>;
48+
49+
constrecommendedRuleConfigs: Linter.RulesRecord={
50+
...basicRuleConfigs,
51+
...recommendedCompilerRuleConfigs,
52+
};
53+
constrecommendedLatestRuleConfigs: Linter.RulesRecord={
4054
...basicRuleConfigs,
41-
...compilerRuleConfigs,
55+
...recommendedLatestCompilerRuleConfigs,
4256
};
4357

4458
constplugins=['react-hooks'];
@@ -51,11 +65,11 @@ type ReactHooksFlatConfig = {
5165
constconfigs={
5266
recommended: {
5367
plugins,
54-
rules: allRuleConfigs,
68+
rules: recommendedRuleConfigs,
5569
},
5670
'recommended-latest': {
5771
plugins,
58-
rules: allRuleConfigs,
72+
rules: recommendedLatestRuleConfigs,
5973
},
6074
flat: {}asRecord<string,ReactHooksFlatConfig>,
6175
};

0 commit comments

Comments
 (0)