react-hooks-lint.test.mjs 4.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. import assert from "node:assert/strict";
  2. import { execFile } from "node:child_process";
  3. import { mkdtemp, mkdir, readFile, rm, writeFile } from "node:fs/promises";
  4. import { tmpdir } from "node:os";
  5. import { dirname, join, resolve } from "node:path";
  6. import { promisify } from "node:util";
  7. import test from "node:test";
  8. const exec = promisify(execFile);
  9. const root = resolve(import.meta.dirname, "../..");
  10. const dependenciesRule = "eslint-plugin-react-hooks(exhaustive-deps)";
  11. const hooksRule = "eslint-plugin-react-hooks(rules-of-hooks)";
  12. async function lint(source, file = "packages/ui/src/Example.tsx", packageDirectory = ".") {
  13. const cwd = await mkdtemp(join(tmpdir(), "zcode-hooks-lint-"));
  14. try {
  15. await writeFile(join(cwd, ".oxlintrc.json"), await readFile(join(root, ".oxlintrc.json")));
  16. await mkdir(dirname(join(cwd, file)), { recursive: true });
  17. await writeFile(join(cwd, file), source);
  18. const target = packageDirectory === "." ? file : file.slice(packageDirectory.length + 1);
  19. const result = await exec(
  20. process.execPath,
  21. [join(root, "node_modules/oxlint/bin/oxlint"), "--format", "json", target],
  22. { cwd: join(cwd, packageDirectory) },
  23. ).catch((error) => {
  24. if (error.code !== 1 || !error.stdout) throw error;
  25. return error;
  26. });
  27. return { code: result.code ?? 0, diagnostics: JSON.parse(result.stdout).diagnostics };
  28. } finally {
  29. await rm(cwd, { recursive: true, force: true });
  30. }
  31. }
  32. test("missing and unnecessary dependencies are warnings from root and package lint", async () => {
  33. const source = `import { useCallback } from "react";
  34. export function useMissing(value: number) { return useCallback(() => value, []); }
  35. export function useExtra(value: number) { return useCallback(() => 1, [value]); }`;
  36. for (const directory of [".", "packages/ui"]) {
  37. const result = await lint(source, "packages/ui/src/Example.tsx", directory);
  38. const diagnostics = result.diagnostics.filter((item) => item.code === dependenciesRule);
  39. assert.equal(diagnostics.length, 2);
  40. assert.ok(diagnostics.some((item) => item.message.includes("missing dependency")));
  41. assert.ok(diagnostics.some((item) => item.message.includes("unnecessary dependency")));
  42. assert.ok(diagnostics.every((item) => item.severity === "warning"));
  43. assert.equal(result.code, 0);
  44. }
  45. });
  46. test("conditional Hooks block new files and remain visible in an admitted legacy file", async () => {
  47. const source = `import { useState } from "react";
  48. export function Example({ enabled }: { enabled: boolean }) {
  49. if (enabled) return useState(0)[0];
  50. return null;
  51. }`;
  52. const regular = await lint(source);
  53. assert.equal(regular.code, 1);
  54. assert.equal(regular.diagnostics.find((item) => item.code === hooksRule)?.severity, "error");
  55. const legacy = await lint(source, "packages/ui/src/hooks/useZCodeAgentService.ts");
  56. assert.equal(legacy.code, 0);
  57. assert.equal(legacy.diagnostics.find((item) => item.code === hooksRule)?.severity, "warning");
  58. });
  59. test("precise callbacks and whole-object dependencies keep the native rule semantics", async () => {
  60. const result = await lint(`import { useCallback } from "react";
  61. type Props = { onOpen: () => void; unrelated: number };
  62. declare function open(props: Props): void;
  63. export function usePrecise({ onOpen }: Props) { return useCallback(() => onOpen(), [onOpen]); }
  64. export function useBroad(props: Props) { return useCallback(() => open(props), [props]); }`);
  65. assert.deepEqual(
  66. result.diagnostics.filter((item) => item.code === dependenciesRule),
  67. [],
  68. );
  69. });
  70. test("adding Hooks checks preserves existing plugins without enabling other React defaults", async () => {
  71. const result = await lint(`export const values = [...[1, 2]];
  72. export function List({ items }: { items: string[] }) { return items.map(item => <div>{item}</div>); }`);
  73. assert.ok(
  74. result.diagnostics.some((item) => item.code === "eslint-plugin-unicorn(no-useless-spread)"),
  75. );
  76. assert.ok(!result.diagnostics.some((item) => item.code?.includes("jsx-key")));
  77. });