1 | # eslint-plugin-deprecation
|
2 |
|
3 | [![Test Workflow](https://github.com/gund/eslint-plugin-deprecation/actions/workflows/test.yml/badge.svg)](https://github.com/gund/eslint-plugin-deprecation/actions/workflows/test.yml)
|
4 | [![Release Workflow](https://github.com/gund/eslint-plugin-deprecation/actions/workflows/release.yml/badge.svg)](https://github.com/gund/eslint-plugin-deprecation/actions/workflows/release.yml)
|
5 | [![Maintainability](https://api.codeclimate.com/v1/badges/bfd9c6e327a267130e50/maintainability)](https://codeclimate.com/github/gund/eslint-plugin-deprecation/maintainability)
|
6 | [![Npm](https://img.shields.io/npm/v/eslint-plugin-deprecation.svg)](https://www.npmjs.com/package/eslint-plugin-deprecation)
|
7 | [![Npm Downloads](https://img.shields.io/npm/dt/eslint-plugin-deprecation.svg)](https://www.npmjs.com/package/eslint-plugin-deprecation)
|
8 | ![Size](https://badgen.net/bundlephobia/minzip/eslint-plugin-deprecation)
|
9 | [![License](https://img.shields.io/npm/l/eslint-plugin-deprecation.svg?maxAge=2592000)](https://github.com/gund/eslint-plugin-deprecation/blob/master/LICENSE)
|
10 | [![semantic-release](https://img.shields.io/badge/%20%20%F0%9F%93%A6%F0%9F%9A%80-semantic--release-e10079.svg)](https://github.com/semantic-release/semantic-release)
|
11 |
|
12 | > An [ESLint](https://eslint.org/) plugin with rules reporting usage of deprecated code
|
13 |
|
14 | ## Prerequisites
|
15 |
|
16 | If you already use [TypeScript](https://www.typescriptlang.org/) and one or more rules from the [`typescript-eslint`](https://typescript-eslint.io/) 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.)
|
17 |
|
18 | Otherwise, in order for you to use this plugin, you must also install the following dependencies:
|
19 |
|
20 | - `typescript`
|
21 | - `@typescript-eslint/parser`
|
22 |
|
23 | For example, if you use the `npm` package manager, then you would run the following command in the root of your project:
|
24 |
|
25 | ```sh
|
26 | npm install --save-dev typescript @typescript-eslint/parser
|
27 | ```
|
28 |
|
29 | Next, you must configure ESLint to parse TypeScript and include type information:
|
30 |
|
31 | ```jsonc
|
32 | {
|
33 | "parser": "@typescript-eslint/parser",
|
34 | "parserOptions": {
|
35 | "ecmaVersion": 2020,
|
36 | "sourceType": "module",
|
37 | "project": "./tsconfig.json" // <-- Point to your project's "tsconfig.json" or create a new one.
|
38 | }
|
39 | }
|
40 | ```
|
41 |
|
42 | ## Install
|
43 |
|
44 | For example, if you use the `npm` package manager, then you would run the following command in the root of your project:
|
45 |
|
46 | ```sh
|
47 | npm install --save-dev eslint-plugin-deprecation
|
48 | ```
|
49 |
|
50 | ## Setup
|
51 |
|
52 | ### Using the `recommended` Config
|
53 |
|
54 | The easiest way to use this plugin is to extend from the `recommended` config, like this:
|
55 |
|
56 | ```jsonc
|
57 | {
|
58 | "extends": [
|
59 | "plugin:deprecation/recommended",
|
60 | ],
|
61 | }
|
62 | ```
|
63 |
|
64 | The `recommended` config will enable the plugin and enable the `deprecation/deprecation` rule with a value of `error`.
|
65 |
|
66 | ### Manually Enable the Plugin and Rule
|
67 |
|
68 | If you don't want to use the `recommended` config for some reason, you can accomplish the same thing by specifying the following config:
|
69 |
|
70 | ```jsonc
|
71 | {
|
72 | "plugins": [
|
73 | "deprecation",
|
74 | ],
|
75 |
|
76 | "rules": {
|
77 | "deprecation/deprecation": "error",
|
78 | },
|
79 | }
|
80 | ```
|
81 |
|
82 | ## Rules
|
83 |
|
84 | ### Disallow usage of deprecated APIs (`deprecation/deprecation`)
|
85 |
|
86 | Reports usage of any code marked with a [`@deprecated` JSDoc tag](https://jsdoc.app/tags-deprecated.html).
|
87 |
|
88 | For example, this includes browser APIs, Node.js APIs, library APIs and any other code that is marked with this tag.
|
89 |
|
90 | #### Rule Details
|
91 |
|
92 | Examples of **incorrect** code for this rule:
|
93 |
|
94 | ```ts
|
95 | import { parse } from 'node:url';
|
96 | import cheerio from 'cheerio';
|
97 |
|
98 | // Node.js API
|
99 | const url = parse('/foo'); // ❌ 'parse' is deprecated. Use the WHATWG URL API instead. eslint(deprecation/deprecation)
|
100 |
|
101 | // Browser API
|
102 | console.log(event?.bubbles); // ❌ 'event' is deprecated. [MDN Reference](https://developer.mozilla.org/docs/Web/API/Window/event) eslint(deprecation/deprecation)
|
103 |
|
104 | // Deprecated library API
|
105 | cheerio('<h2 class="title">Hello world</h2>'); // ❌ 'cheerio' is deprecated. Use the function returned by `load` instead. eslint(deprecation/deprecation)
|
106 | ```
|
107 |
|
108 | Examples of **correct** code for this rule:
|
109 |
|
110 | ```ts
|
111 | import { load } from 'cheerio';
|
112 | import { ChangeEvent } from 'react';
|
113 |
|
114 | // Node.js API
|
115 | const url2 = new URL('/foo', 'http://www.example.com'); // ✅ Modern Node.js API, uses `new URL()`
|
116 |
|
117 | // Browser API
|
118 | function onClick(event: ChangeEvent<HTMLInputElement>) {
|
119 | console.log(event.bubbles); // ✅ Modern browser API, does not use global
|
120 | }
|
121 |
|
122 | // Library API
|
123 | load('<h2 class="title">Hello world</h2>'); // ✅ Allowed library API, uses named `load` import
|
124 | ```
|
125 |
|
126 | ## Credits
|
127 |
|
128 | This rule was originally ported from the [SonarJS repository](https://github.com/SonarSource/SonarJS).
|