import {RuleTester as ESLintTester, Rule} from 'eslint';
import {type ErrorCategory} from 'babel-plugin-react-compiler/src/CompilerError';
import escape from 'regexp.escape';
import {configs} from '../src/index';
import {allRules} from '../src/rules/ReactCompilerRule';
export function normalizeIndent(strings: TemplateStringsArray): string {
const codeLines = strings[0].split('\n');
const leftPadding = codeLines[1].match(/\s+/)![0];
return codeLines.map(line => line.slice(leftPadding.length)).join('\n');
}
export type CompilerTestCases = {
valid: ESLintTester.ValidTestCase[];
invalid: ESLintTester.InvalidTestCase[];
};
export function makeTestCaseError(reason: string): ESLintTester.TestCaseError {
return {
message: new RegExp(escape(reason)),
};
}
export function testRule(
name: string,
rule: Rule.RuleModule,
tests: {
valid: ESLintTester.ValidTestCase[];
invalid: ESLintTester.InvalidTestCase[];
},
): void {
const eslintTester = new ESLintTester({
parser: require.resolve('hermes-eslint'),
parserOptions: {
ecmaVersion: 2015,
sourceType: 'module',
enableExperimentalComponentSyntax: true,
},
});
eslintTester.run(name, rule, tests);
}
export const TestRecommendedRules: Rule.RuleModule = {
meta: {
type: 'problem',
docs: {
description: 'Disallow capitalized function calls',
category: 'Possible Errors',
recommended: true,
},
schema: [{type: 'object', additionalProperties: true}],
},
create(context) {
for (const ruleConfig of Object.values(
configs.recommended.plugins['react-compiler'].rules,
)) {
const listener = ruleConfig.rule.create(context);
if (Object.entries(listener).length !== 0) {
throw new Error('TODO: handle rules that return listeners to eslint');
}
}
return {};
},
};
test('no test', () => {});