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

Add browserlist config as eslint rule config #200

Merged
merged 2 commits into from
Mar 22, 2019
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
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"],
]
}

```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",
},
]
`;