From 59951253a4445b04a196b6bad5b7a2f9a3ec0feb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tobias=20H=C3=B8egh?= Date: Tue, 13 Jan 2026 09:05:37 +0100 Subject: [PATCH] feat: add ignoreConditionalChecks setting Allow reporting inside feature-detection conditionals. Document the new setting. --- README.md | 13 +++++++++++++ docs/rules/compat.md | 6 ++++++ src/helpers.ts | 5 ++++- src/types.ts | 1 + test/e2e.spec.ts | 16 ++++++++++++++++ 5 files changed, 40 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 33ca6b28..f1db05a1 100644 --- a/README.md +++ b/README.md @@ -94,6 +94,19 @@ This plugin also supports linting the compatibility of ES APIs in addition to We } ``` +## Conditional Checks + +By default, feature detection like `if (fetch) { ... }` does not trigger a +report. To lint these conditionals anyway, set the following: + +```jsonc +{ + "settings": { + "ignoreConditionalChecks": true, + }, +} +``` + ## Configuring for Different Environments Browserslist allows specifying [different browser queries for multiple environments](https://github.com/browserslist/browserslist#configuring-for-different-environments). By default, this plugin targets the `production` browserslist environment. To change this default, set the `settings.browserslistOpts.env` property in your eslint config: diff --git a/docs/rules/compat.md b/docs/rules/compat.md index 51b0b43f..d53e14b8 100644 --- a/docs/rules/compat.md +++ b/docs/rules/compat.md @@ -15,3 +15,9 @@ fetch("https://exmaple.com"); // Using default browser targets fetch("https://exmaple.com"); ``` + +## Conditional Checks + +By default, feature detection like `if (fetch) { ... }` does not trigger a +report. Set `settings.ignoreConditionalChecks` to `true` to lint these +conditionals. diff --git a/src/helpers.ts b/src/helpers.ts index 3546c331..85ee2942 100644 --- a/src/helpers.ts +++ b/src/helpers.ts @@ -50,7 +50,10 @@ function checkNotInsideIfStatementAndReport( sourceCode: SourceCode, node: ESLintNode ) { - if (!isInsideIfStatement(node, sourceCode, context)) { + if ( + context.settings?.ignoreConditionalChecks === true || + !isInsideIfStatement(node, sourceCode, context) + ) { handleFailingRule(failingRule, node); } } diff --git a/src/types.ts b/src/types.ts index 08a16fe8..d3353fc8 100644 --- a/src/types.ts +++ b/src/types.ts @@ -79,6 +79,7 @@ export interface Context extends Rule.RuleContext { browsers?: Array; polyfills?: Array; lintAllEsApis?: boolean; + ignoreConditionalChecks?: boolean; browserslistOpts?: BrowsersListOpts; }; } diff --git a/test/e2e.spec.ts b/test/e2e.spec.ts index f89576a6..eb4e80e5 100644 --- a/test/e2e.spec.ts +++ b/test/e2e.spec.ts @@ -276,6 +276,22 @@ ruleTester.run("compat", rule, { }, ], invalid: [ + { + code: ` + if (fetch) { + fetch() + } + `, + settings: { + browsers: ["ExplorerMobile 10"], + ignoreConditionalChecks: true, + }, + errors: [ + { + message: "fetch is not supported in IE Mobile 10", + }, + ], + }, { code: "window?.fetch?.('example.com')", settings: { browsers: ["ie 9"] },