sindresorhus/eslint-plugin-unicorn

Rule proposal: `no-improper-camelcase`

Chiusa

#427 aperta il 29 ott 2019

 (3 commenti) (4 reazioni) (0 assegnatari)JavaScript (468 fork)user submission
help wantednew rule

Metriche repository

Star
 (5022 stelle)
Metriche merge PR
 (Merge medio 4h 30m) (26 PR mergiate in 30 g)

Descrizione

This rule would fail when the constituent part of closed-form compound words are capitalised.

Fail

function unSubscribe() {}
let passWord;
let isInViewPort;

Pass

function unsubscribe() {}
let password;
let isInViewport;

Problem statement

Compound words are formed by joining together two or more other words. Closed-form compound words do that without a hyphen/space. They are words in their own right, and should be treated as such when applying programming casing conventions.

Time and again I see these words incorrectly treated as separate words for the purpose of camelCase. This causes the following problems:

  • confusion and bugs when code using one form interacts with code using the other form
  • spread of incorrect form throughout codebase in the name of consistency
  • personal aggrevation

Examples

Standalone: callBack, dataBase, fileName, lookUp, offLine, onLine, overRide, passWord, payLoad, placeHolder, preView, setUp, unSubscribe, userName, viewPort, weekEnd

Combined: isInViewPort, showPreView, isOnLine

Some of these are arguably OK, others are definitely not.

Existing solutions

The ESLint rule id-blacklist can be configured to disallow a list of identifiers. It has some limitations (by design) which make it less than ideal for this problem:

  1. Only works on complete names, i.e. would not detect isInViewPort
  2. ESLint rule options configured in a shareable config cannot be extended/merged by an extending config; the entire options object must be copied into the extending config.

Guida contributor