How to set ESLint rule to identify functions are either camelcase or not?

怎甘沉沦 提交于 2019-12-25 09:06:05

问题


If I check for ESLint docs there is a perfect plugin available for camel case properties, whereas the same thing I'm trying to identify functions if they are either camelcase or not.

index.js

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

.eslintrc

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

if I ran eslint index.js, I will be getting a proper lint error like this

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

✖ 1 problem (1 error, 0 warnings)

Similarly, I want to achieve this for functions too. Here, getfirstname is not in proper camelcase. I need to get a lint error, so I have changed the rule to

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

if I set the above, I'm not getting the error. what should I do to validate linting for functions using the eslint module? please suggest another way to identify this linting.


回答1:


In the above problem similarly i want this to achieve for functions too,here getfirstname is not proper camel case for this i need to get lint error

You will not be able to detect automatically things like getfirstname that is not a proper camel case. The linter can be sure that it's not camel case if it sees an underscore but here it just looks like one word and it's not that intelligent to know that it isn't.

If it did then it would have to reject things like XMLHttpRequest, setTimeout and fs.realpath.



来源:https://stackoverflow.com/questions/43317406/how-to-set-eslint-rule-to-identify-functions-are-either-camelcase-or-not

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!