Skip to content

Commit

Permalink
Add browserlist config as eslint rule config (#200)
Browse files Browse the repository at this point in the history
* Add browserlist config as eslint rule config

* fix linter rules
  • Loading branch information
terenc3 authored and amilajack committed Mar 22, 2019
1 parent a52598c commit 7db4820
Show file tree
Hide file tree
Showing 5 changed files with 110 additions and 3 deletions.
11 changes: 10 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ Alternatively, you can use the `recommended` configuration which will do this fo

## Targeting Browsers

`eslint-plugin-compat` uses the browserslist configuration in `package.json`. If no configuration is found, browserslist [defaults to](https://github.com/browserslist/browserslist#queries) `> 0.5%, last 2 versions, Firefox ESR, not dead`.
`eslint-plugin-compat` uses the browserslist configuration in `package.json` or the rule configuration in `.eslintrc.*`. If no configuration is found, browserslist [defaults to](https://github.com/browserslist/browserslist#queries) `> 0.5%, last 2 versions, Firefox ESR, not dead`.

See [browserslist/browserslist](https://github.com/browserslist/browserslist) for configuration. Here's some examples:

Expand All @@ -59,6 +59,15 @@ See [browserslist/browserslist](https://github.com/browserslist/browserslist) fo
}
```

```js
// Rule configuration (.eslintrc.json)
{
// ...
"rules": [
"compat/compat": ["error", "defaults, not ie < 9"],
]

This comment has been minimized.

Copy link
@eligeske

eligeske Apr 4, 2019

malformatted array

}

```js
// Use development and production configurations (package.json)
{
Expand Down
2 changes: 1 addition & 1 deletion src/Versioning.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ type TargetListItem = {
export default function DetermineTargetsFromConfig(
config?: BrowserListConfig
): Array<string> {
if (Array.isArray(config)) {
if (Array.isArray(config) || typeof config === 'string') {
return browserslist(config);
}

Expand Down
4 changes: 3 additions & 1 deletion src/rules/compat.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,9 @@ export default {
// Determine lowest targets from browserslist config, which reads user's
// package.json config section. Use config from eslintrc for testing purposes
const browserslistConfig: BrowserListConfig =
context.settings.browsers || context.settings.targets;
context.settings.browsers ||
context.settings.targets ||
context.options[0];

const browserslistTargets = Versioning(
DetermineTargetsFromConfig(browserslistConfig)
Expand Down
6 changes: 6 additions & 0 deletions test/Versioning.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,4 +41,10 @@ describe('Versioning', () => {
];
expect(Versioning(versions)).toMatchSnapshot();
});

it('should support string config in rule option', () => {
const config = DetermineTargetsFromConfig('defaults, not ie < 9');
const result = Versioning(config);
expect(result).toMatchSnapshot();
});
});
90 changes: 90 additions & 0 deletions test/__snapshots__/Versioning.spec.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -234,3 +234,93 @@ Object {
},
}
`;

exports[`Versioning should support string config in rule option 1`] = `
Array [
Object {
"parsedVersion": 4,
"target": "samsung",
"version": "4",
},
Object {
"parsedVersion": 11.1,
"target": "safari",
"version": "11.1",
},
Object {
"parsedVersion": 57,
"target": "opera",
"version": "57",
},
Object {
"parsedVersion": 46,
"target": "op_mob",
"version": "46",
},
Object {
"parsedVersion": 0,
"target": "op_mini",
"version": "all",
},
Object {
"parsedVersion": 11.3,
"target": "ios_saf",
"version": "11.3-11.4",
},
Object {
"parsedVersion": 11,
"target": "ie_mob",
"version": "11",
},
Object {
"parsedVersion": 11,
"target": "ie",
"version": "11",
},
Object {
"parsedVersion": 60,
"target": "firefox",
"version": "60",
},
Object {
"parsedVersion": 17,
"target": "edge",
"version": "17",
},
Object {
"parsedVersion": 71,
"target": "chrome",
"version": "71",
},
Object {
"parsedVersion": 7.12,
"target": "baidu",
"version": "7.12",
},
Object {
"parsedVersion": 4.4,
"target": "android",
"version": "4.4.3-4.4.4",
},
Object {
"parsedVersion": 11.8,
"target": "and_uc",
"version": "11.8",
},
Object {
"parsedVersion": 1.2,
"target": "and_qq",
"version": "1.2",
},
Object {
"parsedVersion": 64,
"target": "and_ff",
"version": "64",
},
Object {
"parsedVersion": 71,
"target": "and_chr",
"version": "71",
},
]
`;

0 comments on commit 7db4820

Please sign in to comment.