Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I have a project-specific custom code deprecation message using eslint?

Tags:

I have a node.js project that checks itself for code consistency according to rules specified in .eslintrc using gulp and gulp-eslint.

Now, I would like to have it throw custom deprecation warnings when it encounters a certain require:

const someModule = require('myDeprecatedModule');
// Warning: myDeprecatedModule is deprecated. Use myNewModule in stead.

Is this possible in a simple way that will be picked up by IDE's too?

  • Using .eslint
  • No custom plugin to be published and installed using npm
  • Local code only that can be pushed to the repository, nothing global
  • No custom code in node_modules
like image 506
Redsandro Avatar asked Jan 26 '17 13:01

Redsandro


People also ask

How do I create an ESLint report?

You have to create a JavaScript file which you will run instead of ESLint and import/require ESLint in that file. By importing ESLint as a package instead of running it directly you can then run ESLint using Node's CLIEngine on your files to be linted and store the output in a variable.

What does ESLint check for?

ESLint is a tool for identifying and reporting on patterns found in ECMAScript/JavaScript code, with the goal of making code more consistent and avoiding bugs.


1 Answers

The rule no-restricted-modules does exactly this: it disallows requiring certain modules.

The names of the deprecated modules must be coded in the configuration. So in order to disallow the deprecated myDeprecatedModule you would add this setting to your .eslintrc file under the "rules" section.

"no-restricted-modules": ["error", "myDeprecatedModule"]

I don't think it's possible to customize the error message though. That would be possible with a custom plugin.

like image 182
GOTO 0 Avatar answered Sep 25 '22 10:09

GOTO 0