How to set ESLint rule to identify functions are e

2019-09-13 20:04发布

问题:

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.