Skip to content

Commit

Permalink
remove & forbid wrong act calls
Browse files Browse the repository at this point in the history
  • Loading branch information
phryneas committed Nov 28, 2024
1 parent bcf52df commit 852e33e
Show file tree
Hide file tree
Showing 12 changed files with 354 additions and 237 deletions.
3 changes: 2 additions & 1 deletion .eslintrc
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,8 @@
"testing-library/prefer-user-event": "error",
"testing-library/no-wait-for-multiple-assertions": "off",
"local-rules/require-using-disposable": "error",
"local-rules/require-disable-act-environment": "error"
"local-rules/require-disable-act-environment": "error",
"local-rules/forbid-act-in-disabled-act-environment": "error"
}
}
],
Expand Down
56 changes: 56 additions & 0 deletions eslint-local-rules/forbid-act-in-disabled-act-environment.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import { rule } from "./forbid-act-in-disabled-act-environment";
import { ruleTester } from "./testSetup";

ruleTester.run("forbid-act-in-disabled-act-environment", rule, {
valid: [
`
() => {
using _disabledAct = disableActEnvironment();
}
() => {
act(() => {})
}
`,
`
() => {
using _disabledAct = disableActEnvironment();
}
() => {
actAsync(() => {})
}
`,
],
invalid: [
`
() => {
using _disabledAct = disableActEnvironment();
act(() => {})
}
`,
`
() => {
using _disabledAct = disableActEnvironment();
actAsync(() => {})
}
`,
`
() => {
using _disabledAct = disableActEnvironment();
() => {
act(() => {})
}
}
`,
`
() => {
using _disabledAct = disableActEnvironment();
() => {
actAsync(() => {})
}
}
`,
].map((code) => ({
code,
errors: [{ messageId: "forbiddenActInNonActEnvironment" }],
})),
});
63 changes: 63 additions & 0 deletions eslint-local-rules/forbid-act-in-disabled-act-environment.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
import { ESLintUtils } from "@typescript-eslint/utils";

export const rule = ESLintUtils.RuleCreator.withoutDocs({
create(context) {
let depth = 1;
let disabledDepth: number | false = false;

function EnterFn() {
depth++;
}
function ExitFn() {
depth--;
if (disabledDepth !== false && disabledDepth > depth) {
disabledDepth = false;
}
}

return {
CallExpression(node) {
const directCallee =
node.callee.type === "Identifier" ? node.callee
: node.callee.type === "MemberExpression" ? node.callee.property
: null;

if (
directCallee?.type === "Identifier" &&
directCallee.name === "disableActEnvironment"
) {
if (disabledDepth === false) {
disabledDepth = depth;
}
}

if (
directCallee?.type === "Identifier" &&
(directCallee.name === "act" || directCallee.name === "actAsync")
) {
if (disabledDepth !== false) {
context.report({
messageId: "forbiddenActInNonActEnvironment",
node: node,
});
}
}
},
ArrowFunctionExpression: EnterFn,
FunctionExpression: EnterFn,
FunctionDeclaration: EnterFn,
"ArrowFunctionExpression:exit": ExitFn,
"FunctionExpression:exit": ExitFn,
"FunctionDeclaration:exit": ExitFn,
};
},
meta: {
messages: {
forbiddenActInNonActEnvironment:
"`act` should not be called in a `disableActEnvironment`.",
},
type: "problem",
schema: [],
},
defaultOptions: [],
});
2 changes: 2 additions & 0 deletions eslint-local-rules/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,6 @@ module.exports = {
"require-using-disposable": require("./require-using-disposable").rule,
"require-disable-act-environment":
require("./require-disable-act-environment").rule,
"forbid-act-in-disabled-act-environment":
require("./forbid-act-in-disabled-act-environment").rule,
};
9 changes: 3 additions & 6 deletions src/react/hooks/__tests__/useFragment.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2002,12 +2002,9 @@ describe("has the same timing as `useQuery`", () => {
expect(withinDOM().queryAllByText(/Item #2/).length).toBe(2);
}

act(
() =>
void cache.evict({
id: cache.identify(item2),
})
);
cache.evict({
id: cache.identify(item2),
});

{
const { withinDOM } = await takeRender();
Expand Down
Loading

0 comments on commit 852e33e

Please sign in to comment.