Skip to content
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
13 changes: 13 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
6 changes: 6 additions & 0 deletions docs/rules/compat.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
5 changes: 4 additions & 1 deletion src/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
Expand Down
1 change: 1 addition & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ export interface Context extends Rule.RuleContext {
browsers?: Array<string>;
polyfills?: Array<string>;
lintAllEsApis?: boolean;
ignoreConditionalChecks?: boolean;
browserslistOpts?: BrowsersListOpts;
};
}
Expand Down
16 changes: 16 additions & 0 deletions test/e2e.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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"] },
Expand Down
Loading