What is eslint-plugin-deprecation?
The eslint-plugin-deprecation package is an ESLint plugin that helps developers identify and manage deprecated code in their JavaScript projects. It allows you to mark certain functions, methods, or properties as deprecated and provides warnings or errors when they are used.
What are eslint-plugin-deprecation's main functionalities?
Detect Deprecated Functions
This feature allows you to specify deprecated functions and provide a custom message. When the deprecated function is used, ESLint will warn the developer.
module.exports = {
rules: {
'deprecation/deprecation': [
'warn',
{
'methods': {
'myDeprecatedFunction': 'This function is deprecated. Use newFunction instead.'
}
}
]
}
};
Detect Deprecated Properties
This feature allows you to mark object properties as deprecated. When the deprecated property is accessed, ESLint will issue a warning.
module.exports = {
rules: {
'deprecation/deprecation': [
'warn',
{
'properties': {
'myObject.oldProperty': 'This property is deprecated. Use newProperty instead.'
}
}
]
}
};
Custom Deprecation Messages
This feature allows you to provide custom deprecation messages for both methods and properties, giving developers clear guidance on what to use instead.
module.exports = {
rules: {
'deprecation/deprecation': [
'warn',
{
'methods': {
'oldFunction': 'oldFunction is deprecated. Please use newFunction.'
},
'properties': {
'oldProperty': 'oldProperty is deprecated. Please use newProperty.'
}
}
]
}
};
Other packages similar to eslint-plugin-deprecation
eslint-plugin-deprecate
eslint-plugin-deprecate is an ESLint plugin that allows you to mark functions, methods, and properties as deprecated. It provides warnings when deprecated code is used. This package is similar to eslint-plugin-deprecation but may have different syntax and configuration options.
eslint-plugin-deprecation
ESLint rule that reports usage of deprecated code
Prerequisites
This plugin only works with @typescript-eslint/parser
.
Which means that you should install dev deps:
@typescript-eslint/parser
typescript
Then configure your .eslintrc
like this:
{
"parser": "@typescript-eslint/parser",
"parserOptions": {
"ecmaVersion": 2020,
"sourceType": "module",
"project": "./tsconfig.json" // <-- Point to your project's tsconfig.json or create new one
},
"settings": {
"import/parsers": {
"@typescript-eslint/parser": [".ts", ".tsx"]
},
"import/resolver": {
"typescript": {
"alwaysTryTypes": true,
"directory": "./tsconfig.json" // <-- Same tsconfig.json must be referenced
}
}
}
}
Install
Install the plugin
npm i -D eslint-plugin-deprecation
Setup
Now add deprecation plugin and rule to your .eslintrc
:
{
"plugins": ["deprecation", ...],
"rules": {
"deprecation/deprecation": "warn", // or "error" to have stricter rule
...
}
}
Now eslint will report all deprecated code that you use!
NOTE: This rule was ported from https://github.com/SonarSource/SonarJS repository.