Skip to content

Commit

Permalink
feat(no-conditional-statements): add option ignoreCodePattern for i…
Browse files Browse the repository at this point in the history
…gnoring if conditions (#909)
  • Loading branch information
RebeccaStevens committed Dec 19, 2024
1 parent f5a4916 commit d3cf5d8
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 5 deletions.
9 changes: 9 additions & 0 deletions docs/rules/no-conditional-statements.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ This rule accepts an options object of the following type:
```ts
type Options = {
allowReturningBranches: boolean | "ifExhaustive";
ignoreCodePattern?: ReadonlyArray<string> | string;
};
```

Expand Down Expand Up @@ -124,3 +125,11 @@ const x = (() => {

Note: Currently this option is not useable with the [no-else-return](https://eslint.org/docs/rules/no-else-return) rule;
`else` statements must contain a return statement.

### `ignoreCodePattern`

This option takes a RegExp string or an array of RegExp strings.
It allows for the ability to ignore violations based on the test condition of the `if`
statement.

Note: This option has no effect on `switch` statements.
19 changes: 14 additions & 5 deletions src/rules/no-conditional-statements.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import type { TSESTree } from "@typescript-eslint/utils";
import type { JSONSchema4 } from "@typescript-eslint/utils/json-schema";
import type { JSONSchema4, JSONSchema4ObjectSchema } from "@typescript-eslint/utils/json-schema";
import type { RuleContext } from "@typescript-eslint/utils/ts-eslint";
import { deepmerge } from "deepmerge-ts";
import type { Type } from "typescript";

import tsApiUtils from "#/conditional-imports/ts-api-utils";
import { type IgnoreCodePatternOption, ignoreCodePatternOptionSchema, shouldIgnorePattern } from "#/options";
import { ruleNameScope } from "#/utils/misc";
import { type NamedCreateRuleCustomMeta, type Rule, type RuleResult, createRule, getTypeOfNode } from "#/utils/rule";
import {
Expand Down Expand Up @@ -32,7 +34,7 @@ export const fullName: `${typeof ruleNameScope}/${typeof name}` = `${ruleNameSco
* The options this rule can take.
*/
type Options = [
{
IgnoreCodePatternOption & {
allowReturningBranches: boolean | "ifExhaustive";
},
];
Expand All @@ -43,7 +45,7 @@ type Options = [
const schema: JSONSchema4[] = [
{
type: "object",
properties: {
properties: deepmerge(ignoreCodePatternOptionSchema, {
allowReturningBranches: {
oneOf: [
{
Expand All @@ -55,7 +57,7 @@ const schema: JSONSchema4[] = [
},
],
},
},
} satisfies JSONSchema4ObjectSchema["properties"]),
additionalProperties: false,
},
];
Expand Down Expand Up @@ -273,7 +275,14 @@ function checkIfStatement(
context: Readonly<RuleContext<keyof typeof errorMessages, Options>>,
options: Readonly<Options>,
): RuleResult<keyof typeof errorMessages, Options> {
const [{ allowReturningBranches }] = options;
const [{ allowReturningBranches, ignoreCodePattern }] = options;

if (shouldIgnorePattern(node.test, context, undefined, undefined, ignoreCodePattern)) {
return {
context,
descriptors: [],
};
}

return {
context,
Expand Down
13 changes: 13 additions & 0 deletions tests/rules/no-conditional-statements.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,19 @@ describe(name, () => {
});
});
});

describe("ignoreCodePattern", () => {
it("ignores matching conditionals", () => {
valid({
code: dedent`
if (import.meta.vitest) {
const { it, expect } = import.meta.vitest;
}
`,
options: [{ ignoreCodePattern: ["import\\.meta\\.vitest"] }],
});
});
});
});
});

Expand Down

0 comments on commit d3cf5d8

Please sign in to comment.