Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(no-conditional-statements): add option ignoreCodePattern for ignoring if conditions #909

Merged
merged 1 commit into from
Dec 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading