ESLint rule that reports usage of deprecated code
npm install eslint-plugin-deprecationtypescript-eslint plugin, then eslint-plugin-deprecation will work out of the box without any additional dependencies or special configuration specified in this section. (This is because @typescript-eslint/plugin automatically contains @typescript-eslint/parser and your ESLint should already be configured with the parserOptions to work properly with TypeScript.)
typescript
@typescript-eslint/parser
npm package manager, then you would run the following command in the root of your project:
sh
npm install --save-dev typescript @typescript-eslint/parser
`
Next, you must configure ESLint to parse TypeScript and include type information:
`jsonc
{
"parser": "@typescript-eslint/parser",
"parserOptions": {
"ecmaVersion": 2020,
"sourceType": "module",
"project": "./tsconfig.json" // <-- Point to your project's "tsconfig.json" or create a new one.
}
}
`
Install
For example, if you use the npm package manager, then you would run the following command in the root of your project:
`sh
npm install --save-dev eslint-plugin-deprecation
`
Setup
$3
The easiest way to use this plugin is to extend from the recommended config, like this:
`jsonc
{
"extends": [
"plugin:deprecation/recommended",
],
}
`
The recommended config will enable the plugin and enable the deprecation/deprecation rule with a value of error.
$3
If you don't want to use the recommended config for some reason, you can accomplish the same thing by specifying the following config:
`jsonc
{
"plugins": [
"deprecation",
],
"rules": {
"deprecation/deprecation": "error",
},
}
`
Rules
$3
Reports usage of any code marked with a @deprecated JSDoc tag.
For example, this includes browser APIs, Node.js APIs, library APIs and any other code that is marked with this tag.
#### Rule Details
Examples of incorrect code for this rule:
`ts
import { parse } from 'node:url';
import cheerio from 'cheerio';
// Node.js API
const url = parse('/foo'); // ❌ 'parse' is deprecated. Use the WHATWG URL API instead. eslint(deprecation/deprecation)
// Browser API
console.log(event?.bubbles); // ❌ 'event' is deprecated. MDN Reference eslint(deprecation/deprecation)
// Deprecated library API
cheerio('Hello world
'); // ❌ 'cheerio' is deprecated. Use the function returned by load instead. eslint(deprecation/deprecation)
`
Examples of correct code for this rule:
`ts
import { load } from 'cheerio';
import { ChangeEvent } from 'react';
// Node.js API
const url2 = new URL('/foo', 'http://www.example.com'); // ✅ Modern Node.js API, uses new URL()
// Browser API
function onClick(event: ChangeEvent) {
console.log(event.bubbles); // ✅ Modern browser API, does not use global
}
// Library API
load('Hello world
'); // ✅ Allowed library API, uses named load import
``