'Eslint rule to ensure minimum variable name length, but not on JSON?
Our code has become a bit of a maintenance nightmare due to previous developers being liberal with single letter variables and little documentation. The latter we could deal with if the variables names were meaningful and self-descriptive. For this reason we are trying to set up eslint to avoid this going forward.
Our requirements:
- minimum of two characters, since
id
would be an acceptable variable - allow
i
andj
, since they are commonly used in 'for' loops as indexes - allow single letter properties on json, to allow
point = { x: 2, y: 2 }
So far the best we have come up with is:
"id-length": [2, { "exceptions": ["i", "j"] }]
This covers points 1 and 2, but fails for point 3. Quoting single letter json attributes does not work for us to the quote-props
rule being present and that we would rather keep.
Can anyone suggest an eslint configuration that would allow to support all three requirements?
Solution 1:[1]
ESLint allows the rule to not be applied on object properties with "properties": never
. It's not enabled by default, so you have to specify it deliberately.
"rules": {
"id-length": [ 2, { "exceptions": ["i", "j"], "properties": "never" }]
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Kostas Minaidis |