Skip to content

Commit

Permalink
feat: Enable setting Browserslist JS API options (#587)
Browse files Browse the repository at this point in the history
In your ESLint config, you can now add `settings.browserslistOpts` to configure which settings to pass to the [Browserslist JS API](https://github.com/browserslist/browserslist#js-api)
  • Loading branch information
frehner authored Aug 27, 2023
1 parent 62bfb53 commit ed1b34f
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 3 deletions.
6 changes: 4 additions & 2 deletions src/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
Target,
HandleFailingRule,
Context,
BrowsersListOpts,
} from "./types";
import { TargetNameMappings } from "./constants";

Expand Down Expand Up @@ -180,9 +181,10 @@ export function reverseTargetMappings<K extends string, V extends string>(
*/
export function determineTargetsFromConfig(
configPath: string,
config?: BrowserListConfig
config?: BrowserListConfig,
browserslistOptsFromConfig?: BrowsersListOpts
): Array<string> {
const browserslistOpts = { path: configPath };
const browserslistOpts = { path: configPath, ...browserslistOptsFromConfig };

const eslintTargets = (() => {
// Get targets from eslint settings
Expand Down
19 changes: 18 additions & 1 deletion src/rules/compat.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import {
BrowserListConfig,
HandleFailingRule,
Context,
BrowsersListOpts,
} from "../types";
import { nodes } from "../providers";

Expand Down Expand Up @@ -156,13 +157,29 @@ export default {
context.settings?.targets ||
context.options[0];

if (
!context.settings?.browserslistOpts &&
// @ts-expect-error Checking for accidental misspellings
context.settings.browsersListOpts
) {
console.error(
'Please ensure you spell `browserslistOpts` with a lowercase "l"!'
);
}
const browserslistOpts: BrowsersListOpts | undefined =
context.settings?.browserslistOpts;

const lintAllEsApis: boolean =
context.settings?.lintAllEsApis === true ||
// Attempt to infer polyfilling of ES APIs from babel config
(!context.settings?.polyfills?.includes("es:all") &&
!isUsingTranspiler(context));
const browserslistTargets = parseBrowsersListVersion(
determineTargetsFromConfig(context.getFilename(), browserslistConfig)
determineTargetsFromConfig(
context.getFilename(),
browserslistConfig,
browserslistOpts
)
);

// Stringify to support memoization; browserslistConfig is always an array of new objects.
Expand Down
5 changes: 5 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { APIKind } from "ast-metadata-inferer/lib/types";
import { Rule } from "eslint";
import { TargetNameMappings } from "./constants";
import type { Options as DefaultBrowsersListOpts } from "browserslist";

export type BrowserListConfig =
| string
Expand Down Expand Up @@ -66,5 +67,9 @@ export interface Context extends Rule.RuleContext {
browsers?: Array<string>;
polyfills?: Array<string>;
lintAllEsApis?: boolean;
browserslistOpts?: BrowsersListOpts;
};
}

export interface BrowsersListOpts
extends Exclude<DefaultBrowsersListOpts, "path"> {}

0 comments on commit ed1b34f

Please sign in to comment.