Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

eslint-plugin-compat

amilajack2.5mMIT6.0.2TypeScript support: included

Lint browser compatibility of API used

eslint, browser, support, api, lint, caniuse, kangax

readme

eslint-plugin-compat

Test Financial Contributors on Open Collective NPM version npm Backers on Open Collective Sponsors on Open Collective

Lint the browser compatibility of your code

demo of plugin usage

Setup

1. Install

npm install eslint-plugin-compat

2. Update ESLint Config

New Config (eslint.config.mjs)

import compat from "eslint-plugin-compat";

export default [compat.configs["flat/recommended"]];

Legacy Config (.eslintrc.json)

{
  "plugins": ["compat"],
  "extends": ["plugin:compat/recommended"],
  "env": {
    "browser": true,
  },
  // ...
}

3. Configure Target Browsers

Browser targets are configured using browserslist. You can configure browser targets in your package.json:

package.json

{
  // ...
  "browserslist": ["defaults"],
}

If no configuration is found, browserslist defaults to "> 0.5%, last 2 versions, Firefox ESR, not dead".

See browserslist/browserslist for more details.

Adding Polyfills

Add polyfills to the settings section of your eslint config. Append the name of the object and the property if one exists. Here are some examples:

{
  // ...
  "settings": {
    "polyfills": [
      // Example of marking entire API and all methods and properties as polyfilled
      "Promise",
      // Example of marking specific method of an API as polyfilled
      "WebAssembly.compile",
      // Example of API with no property (i.e. a function)
      "fetch",
      // Example of instance method, must add `.prototype.`
      "Array.prototype.push",
    ],
  },
}

Linting ES APIs (Experimental)

This plugin also supports linting the compatibility of ES APIs in addition to Web APIs. This is an experimental feature and is disabled by default. To enable this feature, add the following to your eslint config:

{
  // ...
  "settings": {
    "lintAllEsApis": true,
  },
}

Configuring for Different Environments

Browserslist allows specifying different browser queries for multiple environments. By default, this plugin targets the production browserslist environment. To change this default, set the settings.browserslistOpts.env property in your eslint config:

Example:

A browserslist with multiple environments:

  "browserslist": {
    "production": [
      "> 1%",
      "not dead"
    ],
    "modern": [
      "last 1 chrome version",
      "last 1 firefox version"
    ]
  }

Target modern browserslist environment:

{
  "settings": {
    "browserslistOpts": {
      "env": "modern",
    },
  },
}

Demo

For a minimal demo, see amilajack/eslint-plugin-compat-demo

Inspiration

Toolchains for native platforms, like iOS and Android, have had API linting from the start. It's about time that the web had similar tooling.

This project was inspired by a two hour conversation I had with someone on the experience of web development and if it is terrible or not. The premise they argued was that x browser doesn't support y feature while z browser does. Eventually, I agreed with him on this and made this plugin to save web developers from having to memorize the browser compatibility of WebAPIs.

Contributors

Code Contributors

This project exists thanks to all the people who contribute. [Contribute].

Financial Contributors

Become a financial contributor and help us sustain our community. [Contribute]

Individuals

Organizations

Support this project with your organization. Your logo will show up here with a link to your website. [Contribute]

changelog

v3.8.0

Added

  • Support for feature detection of APIs (#327)
  • Implement expected behavior when defining targets in eslintrc and browserslist (this might be deprecated in the future)

Internal

  • Migrated from Flow to Typescript
  • Created performance benchmarks of popular repositories
  • Create E2E linting tests
  • Internal refactors

v3.7.0

Updates

  • Allow ESLint peerDependency version 7.0.0

Fixed

  • Fixed many bugs reporting incorrect linter errors

v3.6.0

Fixed

  • Update dependencies
  • Remove fixable and add meta.type (305)

v3.5.1

Fixed

  • Support Safari TP as a target (#285)

v3.5.0

Fixed

  • Allow targets not caniuse db (#280
  • Added missing browser mapping and default fallback (#272)
  • Support Node >=8 (#281)

Added

  • Bump all deps to latest semver

v3.4.0

Added

  • Add schema to support browserlist as a second paramenter in eslintrc (#265)
  • Bumped all dependencies to latest semver

v3.3.0

Performance

v3.2.0

Added

  • Support for eslint@6

v3.1.2

Fixed

  • Remove npm restriction from engines

v3.1.1

Fixed

  • Resolving browserslist config correctly despite being called from a cwd that is not the root of the project. (#217)

v3.1.0

Added

  • Support detecting locally defined polyfills (#207) bb3be6e

v3.0.2

Fixed

  • Handle entire API polyfill case (#190) e784b3d

v3.0.1

Fixed

  • Bug when returning unsupported when mdn compat data has null record

v3.0.0

Added

Deprecated

  • Using caniuse id's for polyfills is no longer supported

v2.7.0

Added

  • Object.values() support

v2.6.1

Fixed

  • Removed console.log statement

v2.4.0

Updated

  • Updated all deps to latest semver

    Fixed

  • Fixed recommendation config

v2.3.0

Updated

  • Updated browserslist

v2.2.0

Updated

  • Bumped all dependencies to latest semver

v2.1.0

Added

  • Promise support

v2.0.1

Fixed

  • Corrected incorrect babel exports config that prevented plugin from being loaded

v2.0.0

Updated

  • Bumped all dependencies to latest semver

Infra

  • Removed boilerplate from .eslintrc
  • Run CI against node 8
  • Removed flow-typed definitions
  • Updated tests to reflect dependency changes

v1.0.4

Fixed

  • Required peerDependency of eslint>=4.0.0

v1.0.3

Updated

  • Bumped all dependencies to latest semver

v1.0.2

Added

  • Range implementation