【问题标题】:@typescript-eslint/naming-convention: How to mix error and warn rules?@typescript-eslint/naming-convention:如何混合错误和警告规则?
【发布时间】:2021-04-04 06:27:42
【问题描述】:

我正在尝试为我的项目设置命名约定。

我在snake_case 中有一些变量,我希望 ESLint 警告我,例如:

const { order_id } = req.params;

我删除了typescript-eslint/camelcase,因为它已被弃用,并尝试设置naming-convention,并为布尔值添加了一个新的error 规则。

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

如何为snake_case 变量添加警告?

【问题讨论】:

    标签: typescript eslint


    【解决方案1】:

    如果您希望 ESLint 警告您有关不在 camelCase 中的变量名称,它很简单:

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

    VS Code 中显示的相应警告:

    【讨论】:

    • 但这是否适用于我对 Boolean 的当前规则?
    • 不,我的意思是,我想应用这两个规则。一种具有error 严重性,另一种具有warn
    • 我对@9​​87654322@的理解是你可以编写多个规则并将它们分成严重级别。但由于它声明使用选项“类型”“将要求您使用类型信息进行 lint”,我相信只有在变量中明确写入类型时它才会起作用。
    • 有谁知道“linting with type information”是什么意思?我无法让 OP 的布尔示例工作。
    猜你喜欢
    • 2022-12-16
    • 1970-01-01
    • 2021-12-26
    • 1970-01-01
    • 2022-11-10
    • 2023-02-07
    • 2021-01-15
    • 2021-06-02
    • 1970-01-01
    相关资源
    最近更新 更多