react/compiler/packages/eslint-plugin-react-compiler/__tests__/shared-utils.ts
lauren 60d9b9740d
[compiler] Derive ErrorSeverity from ErrorCategory (#34401)
With #34176 we now have granular lint rules created for each compiler
ErrorCategory. However, we had remnants of our old error severities
still in use which makes reporting errors quite clunky. Previously you
would need to specify both a category and severity which often ended up
being the same.

This PR moves severity definition into our rules which are generated
from our categories. For now I decided to defer "upgrading" categories
from a simple string to a sum type since we are only using severities to
map errors to eslint severity.

---
[//]: # (BEGIN SAPLING FOOTER)
Stack created with [Sapling](https://sapling-scm.com). Best reviewed
with [ReviewStack](https://reviewstack.dev/facebook/react/pull/34401).
* #34409
* #34404
* #34403
* #34402
* __->__ #34401
2025-09-06 12:41:29 -04:00

77 lines
2.2 KiB
TypeScript

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';
/**
* A string template tag that removes padding from the left side of multi-line strings
* @param {Array} strings array of code strings (only one expected)
*/
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({
// @ts-ignore[2353] - outdated types
parser: require.resolve('hermes-eslint'),
parserOptions: {
ecmaVersion: 2015,
sourceType: 'module',
enableExperimentalComponentSyntax: true,
},
});
eslintTester.run(name, rule, tests);
}
/**
* Aggregates all recommended rules from the plugin.
*/
export const TestRecommendedRules: Rule.RuleModule = {
meta: {
type: 'problem',
docs: {
description: 'Disallow capitalized function calls',
category: 'Possible Errors',
recommended: true,
},
// validation is done at runtime with zod
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', () => {});