Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 10 additions & 8 deletions packages/react-doctor/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -119,14 +119,15 @@ If both exist, `react-doctor.config.json` takes precedence.

### Config options

| Key | Type | Default | Description |
| -------------- | ------------------- | ------- | ----------------------------------------------------------------------------------------------------------------------------------- |
| `ignore.rules` | `string[]` | `[]` | Rules to suppress, using the `plugin/rule` format shown in diagnostic output (e.g. `react/no-danger`, `knip/exports`, `knip/types`) |
| `ignore.files` | `string[]` | `[]` | File paths to exclude, supports glob patterns (`src/generated/**`, `**/*.test.tsx`) |
| `lint` | `boolean` | `true` | Enable/disable lint checks (same as `--no-lint`) |
| `deadCode` | `boolean` | `true` | Enable/disable dead code detection (same as `--no-dead-code`) |
| `verbose` | `boolean` | `false` | Show file details per rule (same as `--verbose`) |
| `diff` | `boolean \| string` | — | Force diff mode (`true`) or pin a base branch (`"main"`). Set to `false` to disable auto-detection. |
| Key | Type | Default | Description |
| --------------- | ------------------------------------------------- | ----------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `ignore.rules` | `string[]` | `[]` | Rules to suppress, using the `plugin/rule` format shown in diagnostic output (e.g. `react/no-danger`, `knip/exports`, `knip/types`) |
| `ignore.files` | `string[]` | `[]` | File paths to exclude, supports glob patterns (`src/generated/**`, `**/*.test.tsx`) |
| `lint` | `boolean` | `true` | Enable/disable lint checks (same as `--no-lint`) |
| `deadCode` | `boolean` | `true` | Enable/disable dead code detection (same as `--no-dead-code`) |
| `verbose` | `boolean` | `false` | Show file details per rule (same as `--verbose`) |
| `diff` | `boolean \| string` | — | Force diff mode (`true`) or pin a base branch (`"main"`). Set to `false` to disable auto-detection. |
| `accessibility` | `"minimal" \| "recommended" \| "strict" \| false` | `"minimal"` | Accessibility checking level. `minimal`: 15 high-impact rules, `recommended`: 31 rules (jsx-a11y/recommended), `strict`: 33 rules as errors. Set to `false` to disable. |

CLI flags always override config values.

Expand All @@ -150,6 +151,7 @@ The `diagnose` function accepts an optional second argument:
const result = await diagnose(".", {
lint: true, // run lint checks (default: true)
deadCode: true, // run dead code detection (default: true)
accessibility: "recommended", // "minimal" | "recommended" | "strict" | false (default: "minimal")
});
```

Expand Down
21 changes: 19 additions & 2 deletions packages/react-doctor/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,35 @@
import path from "node:path";
import { performance } from "node:perf_hooks";
import type { Diagnostic, DiffInfo, ProjectInfo, ReactDoctorConfig, ScoreResult } from "./types.js";
import type {
AccessibilityPreset,
Diagnostic,
DiffInfo,
ProjectInfo,
ReactDoctorConfig,
ScoreResult,
} from "./types.js";
import { calculateScore } from "./utils/calculate-score.js";
import { combineDiagnostics, computeJsxIncludePaths } from "./utils/combine-diagnostics.js";
import { discoverProject } from "./utils/discover-project.js";
import { loadConfig } from "./utils/load-config.js";
import { runKnip } from "./utils/run-knip.js";
import { runOxlint } from "./utils/run-oxlint.js";

export type { Diagnostic, DiffInfo, ProjectInfo, ReactDoctorConfig, ScoreResult };
export type {
AccessibilityPreset,
Diagnostic,
DiffInfo,
ProjectInfo,
ReactDoctorConfig,
ScoreResult,
};
export { getDiffInfo, filterSourceFiles } from "./utils/get-diff-files.js";

export interface DiagnoseOptions {
lint?: boolean;
deadCode?: boolean;
includePaths?: string[];
accessibility?: AccessibilityPreset | false;
}

export interface DiagnoseResult {
Expand All @@ -38,6 +53,7 @@ export const diagnose = async (

const effectiveLint = options.lint ?? userConfig?.lint ?? true;
const effectiveDeadCode = options.deadCode ?? userConfig?.deadCode ?? true;
const effectiveAccessibility = options.accessibility ?? userConfig?.accessibility ?? "minimal";

if (!projectInfo.reactVersion) {
throw new Error("No React dependency found in package.json");
Expand All @@ -54,6 +70,7 @@ export const diagnose = async (
projectInfo.framework,
projectInfo.hasReactCompiler,
jsxIncludePaths,
effectiveAccessibility,
).catch((error: unknown) => {
console.error("Lint failed:", error);
return emptyDiagnostics;
Expand Down
88 changes: 71 additions & 17 deletions packages/react-doctor/src/oxlint-config.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,70 @@
import { createRequire } from "node:module";
import type { Framework } from "./types.js";
import type { AccessibilityPreset, Framework } from "./types.js";

const esmRequire = createRequire(import.meta.url);

// Minimal preset: The original curated set of high-impact rules
const A11Y_RULES_MINIMAL: Record<string, string> = {
"jsx-a11y/alt-text": "error",
"jsx-a11y/anchor-is-valid": "warn",
"jsx-a11y/click-events-have-key-events": "warn",
"jsx-a11y/no-static-element-interactions": "warn",
"jsx-a11y/no-noninteractive-element-interactions": "warn",
"jsx-a11y/role-has-required-aria-props": "error",
"jsx-a11y/no-autofocus": "warn",
"jsx-a11y/heading-has-content": "warn",
"jsx-a11y/html-has-lang": "warn",
"jsx-a11y/no-redundant-roles": "warn",
"jsx-a11y/scope": "warn",
"jsx-a11y/tabindex-no-positive": "warn",
"jsx-a11y/label-has-associated-control": "warn",
"jsx-a11y/no-distracting-elements": "error",
"jsx-a11y/iframe-has-title": "warn",
};

// Recommended preset: All jsx-a11y recommended rules
const A11Y_RULES_RECOMMENDED: Record<string, string> = {
...A11Y_RULES_MINIMAL,
"jsx-a11y/anchor-has-content": "warn",
"jsx-a11y/aria-activedescendant-has-tabindex": "warn",
"jsx-a11y/aria-props": "warn",
"jsx-a11y/aria-proptypes": "warn",
"jsx-a11y/aria-role": "warn",
"jsx-a11y/aria-unsupported-elements": "warn",
"jsx-a11y/autocomplete-valid": "warn",
"jsx-a11y/img-redundant-alt": "warn",
"jsx-a11y/interactive-supports-focus": "warn",
"jsx-a11y/media-has-caption": "warn",
"jsx-a11y/mouse-events-have-key-events": "warn",
"jsx-a11y/no-access-key": "warn",
"jsx-a11y/no-interactive-element-to-noninteractive-role": "warn",
"jsx-a11y/no-noninteractive-element-to-interactive-role": "warn",
"jsx-a11y/no-noninteractive-tabindex": "warn",
"jsx-a11y/role-supports-aria-props": "warn",
};

// Strict preset: All recommended rules plus strict-only rules, with errors instead of warnings
const A11Y_RULES_STRICT: Record<string, string> = {
...Object.fromEntries(Object.entries(A11Y_RULES_RECOMMENDED).map(([rule]) => [rule, "error"])),
"jsx-a11y/anchor-ambiguous-text": "error",
"jsx-a11y/control-has-associated-label": "error",
};

const getAccessibilityRules = (preset: AccessibilityPreset | false): Record<string, string> => {
switch (preset) {
case false:
return {};
case "minimal":
return A11Y_RULES_MINIMAL;
case "recommended":
return A11Y_RULES_RECOMMENDED;
case "strict":
return A11Y_RULES_STRICT;
default:
return A11Y_RULES_MINIMAL;
}
};

const NEXTJS_RULES: Record<string, string> = {
"react-doctor/nextjs-no-img-element": "warn",
"react-doctor/nextjs-async-client-component": "error",
Expand Down Expand Up @@ -56,12 +118,14 @@ interface OxlintConfigOptions {
pluginPath: string;
framework: Framework;
hasReactCompiler: boolean;
accessibilityPreset: AccessibilityPreset | false;
}

export const createOxlintConfig = ({
pluginPath,
framework,
hasReactCompiler,
accessibilityPreset,
}: OxlintConfigOptions) => ({
categories: {
correctness: "off",
Expand All @@ -72,7 +136,11 @@ export const createOxlintConfig = ({
style: "off",
nursery: "off",
},
plugins: ["react", "jsx-a11y", ...(hasReactCompiler ? [] : ["react-perf"])],
plugins: [
"react",
...(accessibilityPreset !== false ? ["jsx-a11y"] : []),
...(hasReactCompiler ? [] : ["react-perf"]),
],
jsPlugins: [
...(hasReactCompiler
? [{ name: "react-hooks-js", specifier: esmRequire.resolve("eslint-plugin-react-hooks") }]
Expand All @@ -93,21 +161,7 @@ export const createOxlintConfig = ({
"react/require-render-return": "error",
"react/no-unknown-property": "warn",

"jsx-a11y/alt-text": "error",
"jsx-a11y/anchor-is-valid": "warn",
"jsx-a11y/click-events-have-key-events": "warn",
"jsx-a11y/no-static-element-interactions": "warn",
"jsx-a11y/no-noninteractive-element-interactions": "warn",
"jsx-a11y/role-has-required-aria-props": "error",
"jsx-a11y/no-autofocus": "warn",
"jsx-a11y/heading-has-content": "warn",
"jsx-a11y/html-has-lang": "warn",
"jsx-a11y/no-redundant-roles": "warn",
"jsx-a11y/scope": "warn",
"jsx-a11y/tabindex-no-positive": "warn",
"jsx-a11y/label-has-associated-control": "warn",
"jsx-a11y/no-distracting-elements": "error",
"jsx-a11y/iframe-has-title": "warn",
...getAccessibilityRules(accessibilityPreset),

...(hasReactCompiler ? REACT_COMPILER_RULES : {}),

Expand Down
2 changes: 2 additions & 0 deletions packages/react-doctor/src/scan.ts
Original file line number Diff line number Diff line change
Expand Up @@ -463,6 +463,7 @@ export const scan = async (
const options = mergeScanOptions(inputOptions, userConfig);
const { includePaths } = options;
const isDiffMode = includePaths.length > 0;
const effectiveAccessibility = userConfig?.accessibility ?? "minimal";

if (!projectInfo.reactVersion) {
throw new Error("No React dependency found in package.json");
Expand Down Expand Up @@ -490,6 +491,7 @@ export const scan = async (
projectInfo.framework,
projectInfo.hasReactCompiler,
jsxIncludePaths,
effectiveAccessibility,
resolvedNodeBinaryPath,
);
lintSpinner?.succeed("Running lint checks.");
Expand Down
3 changes: 3 additions & 0 deletions packages/react-doctor/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -167,11 +167,14 @@ export interface ReactDoctorIgnoreConfig {
files?: string[];
}

export type AccessibilityPreset = "minimal" | "recommended" | "strict";

export interface ReactDoctorConfig {
ignore?: ReactDoctorIgnoreConfig;
lint?: boolean;
deadCode?: boolean;
verbose?: boolean;
diff?: boolean | string;
failOn?: FailOnLevel;
accessibility?: AccessibilityPreset | false;
}
21 changes: 18 additions & 3 deletions packages/react-doctor/src/utils/run-oxlint.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,13 @@ import {
SPAWN_ARGS_MAX_LENGTH_CHARS,
} from "../constants.js";
import { createOxlintConfig } from "../oxlint-config.js";
import type { CleanedDiagnostic, Diagnostic, Framework, OxlintOutput } from "../types.js";
import type {
AccessibilityPreset,
CleanedDiagnostic,
Diagnostic,
Framework,
OxlintOutput,
} from "../types.js";
import { neutralizeDisableDirectives } from "./neutralize-disable-directives.js";

const esmRequire = createRequire(import.meta.url);
Expand Down Expand Up @@ -251,7 +257,10 @@ const cleanDiagnosticMessage = (
return { message: REACT_COMPILER_MESSAGE, help: rawMessage || help };
}
const cleaned = message.replace(FILEPATH_WITH_LOCATION_PATTERN, "").trim();
return { message: cleaned || message, help: help || RULE_HELP_MAP[rule] || "" };
return {
message: cleaned || message,
help: help || RULE_HELP_MAP[rule] || "",
};
};

const parseRuleCode = (code: string): { plugin: string; rule: string } => {
Expand Down Expand Up @@ -379,6 +388,7 @@ export const runOxlint = async (
framework: Framework,
hasReactCompiler: boolean,
includePaths?: string[],
accessibilityPreset: AccessibilityPreset | false = "minimal",
nodeBinaryPath: string = process.execPath,
): Promise<Diagnostic[]> => {
if (includePaths !== undefined && includePaths.length === 0) {
Expand All @@ -387,7 +397,12 @@ export const runOxlint = async (

const configPath = path.join(os.tmpdir(), `react-doctor-oxlintrc-${process.pid}.json`);
const pluginPath = resolvePluginPath();
const config = createOxlintConfig({ pluginPath, framework, hasReactCompiler });
const config = createOxlintConfig({
pluginPath,
framework,
hasReactCompiler,
accessibilityPreset,
});
const restoreDisableDirectives = neutralizeDisableDirectives(rootDirectory);

try {
Expand Down
Loading