-
Notifications
You must be signed in to change notification settings - Fork 2.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
12 changed files
with
354 additions
and
237 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
56 changes: 56 additions & 0 deletions
56
eslint-local-rules/forbid-act-in-disabled-act-environment.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
63
eslint-local-rules/forbid-act-in-disabled-act-environment.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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: [], | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.