@typescript-eslint/naming-convention: How to mix error and warn rules?

Viewed 23016

I am trying to set up naming conventions for my project.

I have some variables in snake_case that I would like ESLint to warn me about such as:

const { order_id } = req.params;

I removed typescript-eslint/camelcase as it is deprecated and trying to set up naming-convention and added a new error rule for boolean.

 '@typescript-eslint/naming-convention': [
          'error',
          {
            selector: 'variable',
            types: ['boolean'],
            format: ['PascalCase'],
            prefix: ['is', 'should', 'has', 'can', 'did', 'will'],
          },
        ],

How can I add a warning for snake_case variables?

2 Answers

If you want ESLint to warn you about variable names which are not in camelCase it is as simple as:

"@typescript-eslint/naming-convention": [
  "warn",
  {
    selector: "variable",
    format: ["camelCase"]
  },
],

Respective warning shown in VS Code:

enter image description here

I tried to get a similar setup working with mixing warn and error level configurations (e.g. an error-level rule that class names are PascalCased and a warn-level rule that variable names are camelCase). I tried writing two rules in the eslint file, one at warn and one at error, but found that the second rule in the eslint file was the only one that was respected, whichever it was.

I ended up setting everything to warn, which was not my ideal solution but is better than nothing.

Related