【问题标题】:How to set ESLint rule to identify functions are either camelcase or not?如何设置 ESLint 规则来识别函数是否是驼峰式?
【发布时间】:2017-09-05 03:46:18
【问题描述】:

如果我检查 ESLint 文档,有一个完美的插件可用于 camel case properties,而同样的事情我试图识别函数是否是驼峰式。

index.js

var first_name;
var lastName;
function getFirstName(a,b){
    return firstName;
}

.eslintrc

module.exports = {
    "rules": {
         "camelcase": [2, {"properties": "always"}] 
    }
}

如果我运行 eslint index.js,我将收到类似这样的正确 lint 错误

  2:5  error  Identifier 'first_name' is not in camel case  camelcase

✖ 1 problem (1 error, 0 warnings)

同样,我也想为函数实现这一点。在这里,getfirstname 不是正确的驼峰式。我需要得到一个 lint 错误,所以我将规则更改为

 module.exports = {
        "rules": {
             **"camelcase": [2, {"functions": "always"}]**  
        }
    }

如果我设置了上述内容,我不会收到错误消息。我应该怎么做才能使用 eslint 模块验证函数的 linting?请提出另一种方法来识别这种棉绒。

【问题讨论】:

    标签: javascript error-handling eslint lint


    【解决方案1】:

    在上面的问题中,我也希望实现这个功能,这里 getfirstname 不是正确的骆驼案例,我需要得到 lint 错误

    您将无法自动检测到像 getfirstname 这样不适合骆驼案例的事物。如果 linter 看到下划线,它可以确定它不是驼峰式大小写,但在这里它看起来就像一个单词,知道它不是这样并不那么聪明。

    如果它这样做了,那么它将不得不拒绝像 XMLHttpRequestsetTimeoutfs.realpath 这样的东西。

    【讨论】:

      猜你喜欢
      • 2015-05-11
      • 1970-01-01
      • 2015-09-03
      • 2021-11-19
      • 1970-01-01
      • 2020-07-21
      • 2012-05-14
      • 2020-06-05
      • 2020-07-22
      相关资源
      最近更新 更多