Skip to content

Commit 9040276

Browse files
committed
[compiler] Script to produce markdown of lint rule docs
1 parent a85ec04 commit 9040276

File tree

3 files changed

+69
-16
lines changed

3 files changed

+69
-16
lines changed

compiler/package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,8 @@
1919
"test": "yarn workspaces run test",
2020
"snap": "yarn workspace babel-plugin-react-compiler run snap",
2121
"snap:build": "yarn workspace snap run build",
22-
"npm:publish": "node scripts/release/publish"
22+
"npm:publish": "node scripts/release/publish",
23+
"eslint-docs": "yarn workspace babel-plugin-react-compiler build && node scripts/build-eslint-docs.js"
2324
},
2425
"dependencies": {
2526
"fs-extra": "^4.0.2",

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

Lines changed: 35 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,11 @@
77

88
import * as t from '@babel/types';
99
import {codeFrameColumns} from '@babel/code-frame';
10-
import type {SourceLocation} from './HIR';
10+
import {GeneratedSource, type SourceLocation} from './HIR';
1111
import {Err, Ok, Result} from './Utils/Result';
1212
import {assertExhaustive} from './Utils/utils';
13+
import {getReanimatedModuleType} from './HIR/Globals';
14+
import invariant from 'invariant';
1315

1416
export enum ErrorSeverity {
1517
/**
@@ -628,15 +630,26 @@ export type LintRule = {
628630
recommended: boolean;
629631
};
630632

633+
const RULE_NAME_PATTERN = /^[a-z]+(-[a-z]+)*$/;
634+
631635
export function getRuleForCategory(category: ErrorCategory): LintRule {
636+
const rule = getRuleForCategoryImpl(category);
637+
invariant(
638+
RULE_NAME_PATTERN.test(rule.name),
639+
`Invalid rule name, got '${rule.name}' but rules must match ${RULE_NAME_PATTERN.toString()}`,
640+
);
641+
return rule;
642+
}
643+
644+
function getRuleForCategoryImpl(category: ErrorCategory): LintRule {
632645
switch (category) {
633646
case ErrorCategory.AutomaticEffectDependencies: {
634647
return {
635648
category,
636649
name: 'automatic-effect-dependencies',
637650
description:
638651
'Verifies that automatic effect dependencies are compiled if opted-in',
639-
recommended: true,
652+
recommended: false,
640653
};
641654
}
642655
case ErrorCategory.CapitalizedCalls: {
@@ -652,7 +665,7 @@ export function getRuleForCategory(category: ErrorCategory): LintRule {
652665
return {
653666
category,
654667
name: 'config',
655-
description: 'Validates the configuration',
668+
description: 'Validates the compiler configuration options',
656669
recommended: true,
657670
};
658671
}
@@ -678,7 +691,7 @@ export function getRuleForCategory(category: ErrorCategory): LintRule {
678691
category,
679692
name: 'set-state-in-effect',
680693
description:
681-
'Validates against calling setState synchronously in an effect',
694+
'Validates against calling setState synchronously in an effect, which can lead to re-renders that degrade performance',
682695
recommended: true,
683696
};
684697
}
@@ -687,7 +700,7 @@ export function getRuleForCategory(category: ErrorCategory): LintRule {
687700
category,
688701
name: 'error-boundaries',
689702
description:
690-
'Validates usage of error boundaries instead of try/catch for errors in JSX',
703+
'Validates usage of error boundaries instead of try/catch for errors in child components',
691704
recommended: true,
692705
};
693706
}
@@ -711,7 +724,8 @@ export function getRuleForCategory(category: ErrorCategory): LintRule {
711724
return {
712725
category,
713726
name: 'gating',
714-
description: 'Validates configuration of gating mode',
727+
description:
728+
'Validates configuration of [gating mode](https://react.dev/reference/react-compiler/gating)',
715729
recommended: true,
716730
};
717731
}
@@ -720,7 +734,8 @@ export function getRuleForCategory(category: ErrorCategory): LintRule {
720734
category,
721735
name: 'globals',
722736
description:
723-
'Validates against assignment/mutation of globals during render',
737+
'Validates against assignment/mutation of globals during render, part of ensuring that ' +
738+
'[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)',
724739
recommended: true,
725740
};
726741
}
@@ -742,7 +757,7 @@ export function getRuleForCategory(category: ErrorCategory): LintRule {
742757
category,
743758
name: 'immutability',
744759
description:
745-
'Validates that immutable values (props, state, etc) are not mutated',
760+
'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)',
746761
recommended: true,
747762
};
748763
}
@@ -759,7 +774,9 @@ export function getRuleForCategory(category: ErrorCategory): LintRule {
759774
category,
760775
name: 'preserve-manual-memoization',
761776
description:
762-
'Validates that existing manual memoized is preserved by the compiler',
777+
'Validates that existing manual memoized is preserved by the compiler. ' +
778+
'React Compiler will only compile components and hooks if its inference ' +
779+
'[matches or exceeds the existing manual memoization](https://react.dev/learn/react-compiler/introduction#what-should-i-do-about-usememo-usecallback-and-reactmemo)',
763780
recommended: true,
764781
};
765782
}
@@ -768,7 +785,7 @@ export function getRuleForCategory(category: ErrorCategory): LintRule {
768785
category,
769786
name: 'purity',
770787
description:
771-
'Validates that the component/hook is pure, and does not call known-impure functions',
788+
'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',
772789
recommended: true,
773790
};
774791
}
@@ -777,15 +794,16 @@ export function getRuleForCategory(category: ErrorCategory): LintRule {
777794
category,
778795
name: 'refs',
779796
description:
780-
'Validates correct usage of refs, not reading/writing during render',
797+
'Validates correct usage of refs, not reading/writing during render. See the "pitfalls" section in [`useRef()` usage](https://react.dev/reference/react/useRef#usage)',
781798
recommended: true,
782799
};
783800
}
784801
case ErrorCategory.RenderSetState: {
785802
return {
786803
category,
787804
name: 'set-state-in-render',
788-
description: 'Validates against setting state during render',
805+
description:
806+
'Validates against setting state during render, which can trigger additional renders and potential infinite render loops',
789807
recommended: true,
790808
};
791809
}
@@ -794,7 +812,7 @@ export function getRuleForCategory(category: ErrorCategory): LintRule {
794812
category,
795813
name: 'static-components',
796814
description:
797-
'Validates that components are static, not recreated every render',
815+
'Validates that components are static, not recreated every render. Components that are recreated dynamically can reset state and trigger excessive re-rendering',
798816
recommended: true,
799817
};
800818
}
@@ -826,15 +844,17 @@ export function getRuleForCategory(category: ErrorCategory): LintRule {
826844
return {
827845
category,
828846
name: 'unsupported-syntax',
829-
description: 'Validates against syntax that we do not plan to support',
847+
description:
848+
'Validates against syntax that we do not plan to support in React Compiler',
830849
recommended: true,
831850
};
832851
}
833852
case ErrorCategory.UseMemo: {
834853
return {
835854
category,
836855
name: 'use-memo',
837-
description: 'Validates usage of the useMemo() hook',
856+
description:
857+
'Validates usage of the useMemo() hook against common mistakes. See [`useMemo()` docs](https://react.dev/reference/react/useMemo) for more information.',
838858
recommended: true,
839859
};
840860
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
const ReactCompiler = require('../packages/babel-plugin-react-compiler/dist');
2+
3+
const combinedRules = [
4+
{
5+
name: 'rules-of-hooks',
6+
recommended: true,
7+
description:
8+
'Validates that components and hooks follow the [Rules of Hooks](https://react.dev/reference/rules/rules-of-hooks)',
9+
},
10+
{
11+
name: 'exhaustive-deps',
12+
recommended: true,
13+
description:
14+
'Validates that hooks which accept dependency arrays (`useMemo()`, `useCallback()`, `useEffect()`, etc) ' +
15+
'list all referenced variables in their dependency array. Referencing a value without including it in the ' +
16+
'dependency array can lead to stale UI or callbacks.',
17+
},
18+
...ReactCompiler.LintRules,
19+
];
20+
21+
const printed = combinedRules
22+
.filter(rule => rule.recommended)
23+
.map(rule => {
24+
return `
25+
## \`react-hooks/${rule.name}\`
26+
27+
${rule.description}
28+
`.trim();
29+
})
30+
.join('\n\n');
31+
32+
console.log(printed);

0 commit comments

Comments
 (0)