最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Eslint rule to ensure minimum variable name length, but not on JSON? - Stack Overflow

programmeradmin0浏览0评论

Our code has bee 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:

  1. minimum of two characters, since id would be an acceptable variable
  2. allow i and j, since they are monly used in 'for' loops as indexes
  3. allow single letter properties on json, to allow point = { x: 2, y: 2 }

So far the best we have e 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?

Our code has bee 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:

  1. minimum of two characters, since id would be an acceptable variable
  2. allow i and j, since they are monly used in 'for' loops as indexes
  3. allow single letter properties on json, to allow point = { x: 2, y: 2 }

So far the best we have e 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?

Share Improve this question asked Aug 25, 2020 at 20:02 Andre MAndre M 7,5989 gold badges64 silver badges107 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 8

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" }]
}
发布评论

评论列表(0)

  1. 暂无评论