Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating different eslint rules for local development

Here's a motivating example: I am developing some code and want to figure out what's going wrong, so I have

function foo() {
    console.log("Look its 2016 and I'm still printf debugging");
}

Except... our build process runs esLint as part of the build system and by-design prevents even running the rest of the build pipeline if esLint fails. error Unexpected console statement no-console

What I effectively want is to set up a dev environment where certain rules are turned off (or converted to warnings), and then a production environment with the strict rules turned on. And I want to be able to easily toggle that locally so I can verify my code works before submitting it to a CI server.

I can't find any relevant code snippets to make this happen, which makes me sad. My build environment is just npm scripts (just using the esLint CLI + package.json), but I would be happy to port a solution from another build environment.

Right now, I'm left with either // eslint-disable-line or locally modifying an .eslintrc file and praying I never accidentally check that in by accident. There must be a better way.

like image 848
AnilRedshift Avatar asked Jun 18 '16 02:06

AnilRedshift


People also ask

How many ESLint rules are there?

Keep in mind that we have over 200 rules, and that is daunting both for end users and the ESLint team (who has to maintain them).

How do I disable a specific ESLint rule?

If you want to disable an ESLint rule in a file or on a specific line, you can add a comment. On a single line: const message = 'foo'; console. log(message); // eslint-disable-line no-console // eslint-disable-next-line no-console console.

What does 2 mean in ESLint?

I defines the severity of a rule. Severity should be one of the following: 0 = off, 1 = warning, 2 = error (you passed "3"). Documentation: https://eslint.org/docs/user-guide/configuring/rules.


2 Answers

Branch your rules by NODE_ENV

You can use .js version of eslint config (eslintrc.js) and branch your rules based on NODE_ENV variable.

In eslintrc.js:

rules: {
 
  'no-console':
    process.env.NODE_ENV === 'production'
      ? 'error'
      : 'warn'

}

Scripts in package.json:

"scripts": {
  "lint": "eslint",
  "lint:prod": "NODE_ENV=production eslint"
}

You will use one version of the rule and your CI the other (assuming NODE_ENV on your CI is set to production).

like image 66
exmaxx Avatar answered Sep 16 '22 13:09

exmaxx


From a related thread on github: https://github.com/eslint/eslint/issues/6460#issuecomment-226967834

It looks like what I am going to do is make a dev.eslintrc or similar and have that extend the main .eslintrc. And then I can use command line args to switch between the two rulesets as needed.

like image 45
AnilRedshift Avatar answered Sep 17 '22 13:09

AnilRedshift