|
| 1 | +import { defineRule, type ESTree } from "@oxlint/plugins"; |
| 2 | +import * as Option from "effect/Option"; |
| 3 | + |
| 4 | +import { getPropertyName, unwrapExpression } from "../utils.ts"; |
| 5 | + |
| 6 | +const BROWSER_TEST_FILE = /\.browser\.test\.[cm]?[jt]sx?$/u; |
| 7 | +const LIFECYCLE_HOOKS = new Set(["afterAll", "afterEach", "beforeAll", "beforeEach"]); |
| 8 | +const REACT_ACT_EXPORTS = new Set(["act", "unstable_act"]); |
| 9 | +const ACT_ENVIRONMENT = "IS_REACT_ACT_ENVIRONMENT"; |
| 10 | +const HELPER = "Render with `await using screen = await render(...)` from apps/web/test/render.tsx"; |
| 11 | + |
| 12 | +const MESSAGES = { |
| 13 | + reactDomClient: `${HELPER}; it owns the React root and unmounts it on dispose.`, |
| 14 | + reactAct: `${HELPER} and import \`act\` from there; it enables React's act environment only while act runs.`, |
| 15 | + actEnvironment: `${HELPER}; its act() sets ${ACT_ENVIRONMENT} for exactly as long as it runs.`, |
| 16 | + lifecycleHook: |
| 17 | + "Browser tests acquire what they need with `using`/`await using` instead of lifecycle hooks, so nothing outlives the test that created it.", |
| 18 | +}; |
| 19 | + |
| 20 | +const stringValue = (node: ESTree.Node | null | undefined) => |
| 21 | + node?.type === "Literal" && typeof node.value === "string" ? node.value : undefined; |
| 22 | + |
| 23 | +const namesActEnvironment = (node: unknown) => { |
| 24 | + const target = unwrapExpression(node); |
| 25 | + if (Option.isNone(target)) return false; |
| 26 | + if (target.value.type === "Identifier") return target.value.name === ACT_ENVIRONMENT; |
| 27 | + if (target.value.type !== "MemberExpression") return false; |
| 28 | + const property = target.value.computed |
| 29 | + ? Option.fromNullishOr(stringValue(target.value.property)) |
| 30 | + : getPropertyName(target.value.property); |
| 31 | + return Option.getOrUndefined(property) === ACT_ENVIRONMENT; |
| 32 | +}; |
| 33 | + |
| 34 | +export default defineRule({ |
| 35 | + meta: { |
| 36 | + type: "problem", |
| 37 | + docs: { |
| 38 | + description: |
| 39 | + "Require web component tests to render through the disposable test helper instead of managing React roots, act, or lifecycle hooks by hand.", |
| 40 | + }, |
| 41 | + }, |
| 42 | + create(context) { |
| 43 | + const isBrowserTest = BROWSER_TEST_FILE.test(context.filename); |
| 44 | + |
| 45 | + return { |
| 46 | + ImportDeclaration(node) { |
| 47 | + if (node.source.value === "react-dom/client") { |
| 48 | + context.report({ node: node.source, message: MESSAGES.reactDomClient }); |
| 49 | + return; |
| 50 | + } |
| 51 | + if (node.source.value !== "react") return; |
| 52 | + for (const specifier of node.specifiers) { |
| 53 | + if (specifier.type !== "ImportSpecifier") continue; |
| 54 | + const imported = |
| 55 | + specifier.imported.type === "Identifier" |
| 56 | + ? specifier.imported.name |
| 57 | + : specifier.imported.value; |
| 58 | + if (REACT_ACT_EXPORTS.has(imported)) { |
| 59 | + context.report({ node: specifier, message: MESSAGES.reactAct }); |
| 60 | + } |
| 61 | + } |
| 62 | + }, |
| 63 | + AssignmentExpression(node) { |
| 64 | + if (namesActEnvironment(node.left)) { |
| 65 | + context.report({ node: node.left, message: MESSAGES.actEnvironment }); |
| 66 | + } |
| 67 | + }, |
| 68 | + CallExpression(node) { |
| 69 | + const callee = unwrapExpression(node.callee); |
| 70 | + if (Option.isNone(callee)) return; |
| 71 | + if (callee.value.type === "MemberExpression") { |
| 72 | + // vi.stubGlobal("IS_REACT_ACT_ENVIRONMENT", true) leaves the flag on for the whole test. |
| 73 | + const method = getPropertyName(callee.value.property); |
| 74 | + if ( |
| 75 | + Option.getOrUndefined(method) === "stubGlobal" && |
| 76 | + stringValue(node.arguments[0] as ESTree.Node | undefined) === ACT_ENVIRONMENT |
| 77 | + ) { |
| 78 | + context.report({ node, message: MESSAGES.actEnvironment }); |
| 79 | + } |
| 80 | + return; |
| 81 | + } |
| 82 | + if ( |
| 83 | + isBrowserTest && |
| 84 | + callee.value.type === "Identifier" && |
| 85 | + LIFECYCLE_HOOKS.has(callee.value.name) |
| 86 | + ) { |
| 87 | + context.report({ node: node.callee, message: MESSAGES.lifecycleHook }); |
| 88 | + } |
| 89 | + }, |
| 90 | + }; |
| 91 | + }, |
| 92 | +}); |
0 commit comments