Skip to content

Commit d3cf5d8

Browse files
feat(no-conditional-statements): add option ignoreCodePattern for ignoring if conditions (#909)
1 parent f5a4916 commit d3cf5d8

File tree

3 files changed

+36
-5
lines changed

3 files changed

+36
-5
lines changed

docs/rules/no-conditional-statements.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ This rule accepts an options object of the following type:
6565
```ts
6666
type Options = {
6767
allowReturningBranches: boolean | "ifExhaustive";
68+
ignoreCodePattern?: ReadonlyArray<string> | string;
6869
};
6970
```
7071

@@ -124,3 +125,11 @@ const x = (() => {
124125

125126
Note: Currently this option is not useable with the [no-else-return](https://eslint.org/docs/rules/no-else-return) rule;
126127
`else` statements must contain a return statement.
128+
129+
### `ignoreCodePattern`
130+
131+
This option takes a RegExp string or an array of RegExp strings.
132+
It allows for the ability to ignore violations based on the test condition of the `if`
133+
statement.
134+
135+
Note: This option has no effect on `switch` statements.

src/rules/no-conditional-statements.ts

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
import type { TSESTree } from "@typescript-eslint/utils";
2-
import type { JSONSchema4 } from "@typescript-eslint/utils/json-schema";
2+
import type { JSONSchema4, JSONSchema4ObjectSchema } from "@typescript-eslint/utils/json-schema";
33
import type { RuleContext } from "@typescript-eslint/utils/ts-eslint";
4+
import { deepmerge } from "deepmerge-ts";
45
import type { Type } from "typescript";
56

67
import tsApiUtils from "#/conditional-imports/ts-api-utils";
8+
import { type IgnoreCodePatternOption, ignoreCodePatternOptionSchema, shouldIgnorePattern } from "#/options";
79
import { ruleNameScope } from "#/utils/misc";
810
import { type NamedCreateRuleCustomMeta, type Rule, type RuleResult, createRule, getTypeOfNode } from "#/utils/rule";
911
import {
@@ -32,7 +34,7 @@ export const fullName: `${typeof ruleNameScope}/${typeof name}` = `${ruleNameSco
3234
* The options this rule can take.
3335
*/
3436
type Options = [
35-
{
37+
IgnoreCodePatternOption & {
3638
allowReturningBranches: boolean | "ifExhaustive";
3739
},
3840
];
@@ -43,7 +45,7 @@ type Options = [
4345
const schema: JSONSchema4[] = [
4446
{
4547
type: "object",
46-
properties: {
48+
properties: deepmerge(ignoreCodePatternOptionSchema, {
4749
allowReturningBranches: {
4850
oneOf: [
4951
{
@@ -55,7 +57,7 @@ const schema: JSONSchema4[] = [
5557
},
5658
],
5759
},
58-
},
60+
} satisfies JSONSchema4ObjectSchema["properties"]),
5961
additionalProperties: false,
6062
},
6163
];
@@ -273,7 +275,14 @@ function checkIfStatement(
273275
context: Readonly<RuleContext<keyof typeof errorMessages, Options>>,
274276
options: Readonly<Options>,
275277
): RuleResult<keyof typeof errorMessages, Options> {
276-
const [{ allowReturningBranches }] = options;
278+
const [{ allowReturningBranches, ignoreCodePattern }] = options;
279+
280+
if (shouldIgnorePattern(node.test, context, undefined, undefined, ignoreCodePattern)) {
281+
return {
282+
context,
283+
descriptors: [],
284+
};
285+
}
277286

278287
return {
279288
context,

tests/rules/no-conditional-statements.test.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,19 @@ describe(name, () => {
132132
});
133133
});
134134
});
135+
136+
describe("ignoreCodePattern", () => {
137+
it("ignores matching conditionals", () => {
138+
valid({
139+
code: dedent`
140+
if (import.meta.vitest) {
141+
const { it, expect } = import.meta.vitest;
142+
}
143+
`,
144+
options: [{ ignoreCodePattern: ["import\\.meta\\.vitest"] }],
145+
});
146+
});
147+
});
135148
});
136149
});
137150

0 commit comments

Comments
 (0)