简体   繁体   中英

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

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?

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:

在此处输入图像描述

I tried to get a similar setup working with mixing warn and error level configurations (eg 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM