Skip to content

Commit 5995125

Browse files
committed
feat: add ignoreConditionalChecks setting
Allow reporting inside feature-detection conditionals. Document the new setting.
1 parent 2b62f0e commit 5995125

File tree

5 files changed

+40
-1
lines changed

5 files changed

+40
-1
lines changed

README.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,19 @@ This plugin also supports linting the compatibility of ES APIs in addition to We
9494
}
9595
```
9696

97+
## Conditional Checks
98+
99+
By default, feature detection like `if (fetch) { ... }` does not trigger a
100+
report. To lint these conditionals anyway, set the following:
101+
102+
```jsonc
103+
{
104+
"settings": {
105+
"ignoreConditionalChecks": true,
106+
},
107+
}
108+
```
109+
97110
## Configuring for Different Environments
98111

99112
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:

docs/rules/compat.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,3 +15,9 @@ fetch("https://exmaple.com");
1515
// Using default browser targets
1616
fetch("https://exmaple.com");
1717
```
18+
19+
## Conditional Checks
20+
21+
By default, feature detection like `if (fetch) { ... }` does not trigger a
22+
report. Set `settings.ignoreConditionalChecks` to `true` to lint these
23+
conditionals.

src/helpers.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,10 @@ function checkNotInsideIfStatementAndReport(
5050
sourceCode: SourceCode,
5151
node: ESLintNode
5252
) {
53-
if (!isInsideIfStatement(node, sourceCode, context)) {
53+
if (
54+
context.settings?.ignoreConditionalChecks === true ||
55+
!isInsideIfStatement(node, sourceCode, context)
56+
) {
5457
handleFailingRule(failingRule, node);
5558
}
5659
}

src/types.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ export interface Context extends Rule.RuleContext {
7979
browsers?: Array<string>;
8080
polyfills?: Array<string>;
8181
lintAllEsApis?: boolean;
82+
ignoreConditionalChecks?: boolean;
8283
browserslistOpts?: BrowsersListOpts;
8384
};
8485
}

test/e2e.spec.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -276,6 +276,22 @@ ruleTester.run("compat", rule, {
276276
},
277277
],
278278
invalid: [
279+
{
280+
code: `
281+
if (fetch) {
282+
fetch()
283+
}
284+
`,
285+
settings: {
286+
browsers: ["ExplorerMobile 10"],
287+
ignoreConditionalChecks: true,
288+
},
289+
errors: [
290+
{
291+
message: "fetch is not supported in IE Mobile 10",
292+
},
293+
],
294+
},
279295
{
280296
code: "window?.fetch?.('example.com')",
281297
settings: { browsers: ["ie 9"] },

0 commit comments

Comments
 (0)