main
ts 76 lines 2.15 KB
Raw
1 import {RuleTester as ESLintTester, Rule} from 'eslint';
2 import {type ErrorCategory} from 'babel-plugin-react-compiler/src/CompilerError';
3 import escape from 'regexp.escape';
4 import {configs} from '../src/index';
5 import {allRules} from '../src/rules/ReactCompilerRule';
6
7 /**
8 * A string template tag that removes padding from the left side of multi-line strings
9 * @param {Array} strings array of code strings (only one expected)
10 */
11 export function normalizeIndent(strings: TemplateStringsArray): string {
12 const codeLines = strings[0].split('\n');
13 const leftPadding = codeLines[1].match(/\s+/)![0];
14 return codeLines.map(line => line.slice(leftPadding.length)).join('\n');
15 }
16
17 export type CompilerTestCases = {
18 valid: ESLintTester.ValidTestCase[];
19 invalid: ESLintTester.InvalidTestCase[];
20 };
21
22 export function makeTestCaseError(reason: string): ESLintTester.TestCaseError {
23 return {
24 message: new RegExp(escape(reason)),
25 };
26 }
27
28 export function testRule(
29 name: string,
30 rule: Rule.RuleModule,
31 tests: {
32 valid: ESLintTester.ValidTestCase[];
33 invalid: ESLintTester.InvalidTestCase[];
34 },
35 ): void {
36 const eslintTester = new ESLintTester({
37 // @ts-ignore[2353] - outdated types
38 parser: require.resolve('hermes-eslint'),
39 parserOptions: {
40 ecmaVersion: 2015,
41 sourceType: 'module',
42 enableExperimentalComponentSyntax: true,
43 },
44 });
45
46 eslintTester.run(name, rule, tests);
47 }
48
49 /**
50 * Aggregates all recommended rules from the plugin.
51 */
52 export const TestRecommendedRules: Rule.RuleModule = {
53 meta: {
54 type: 'problem',
55 docs: {
56 description: 'Disallow capitalized function calls',
57 category: 'Possible Errors',
58 recommended: true,
59 },
60 // validation is done at runtime with zod
61 schema: [{type: 'object', additionalProperties: true}],
62 },
63 create(context) {
64 for (const ruleConfig of Object.values(
65 configs.recommended.plugins['react-compiler'].rules,
66 )) {
67 const listener = ruleConfig.rule.create(context);
68 if (Object.entries(listener).length !== 0) {
69 throw new Error('TODO: handle rules that return listeners to eslint');
70 }
71 }
72 return {};
73 },
74 };
75
76 test('no test', () => {});