sindresorhus/eslint-plugin-unicorn

Rule proposal: `better-boolean-var-name`

Closed

#2,441 opened on Sep 1, 2024

 (7 comments) (1 reaction) (0 assignees)JavaScript (468 forks)user submission
help wantednew ruletypes

Repository metrics

Stars
 (5,022 stars)
PR merge metrics
 (Avg merge 1d 16h) (399 merged PRs in 30d)

Description

Description

There are many irregular variable declarations in some Boolean variable declarations.

For example

const showingModal = false

This value is obviously a Boolean value, but it cannot be seen from the variable.

A better variable name can improve code readability

Boolean variables should start with 'is', 'was', 'has', 'can', 'should', etc.

Fail

const showingModal = false

const greatThen = a > b

const completed = progress === 100

const setProperty = key in object

const stringType = typeof input === 'string'

// ...

Pass

const isShowingModal = false

const isGreatThen = a > b

const isCompleted = progress === 100

const hasSetProperty = key in object

const isStringType = typeof input === 'string'

Proposed rule name

better-boolean-var-declaration

Additional Info

This rule has been implemented in my company project, it can't autofix, but it can provide suggestions. I think it is useful

If this is acceptable, I'd be happy to PR

https://github.com/user-attachments/assets/c6bbccea-5efc-4fc5-8beb-3fb2fb0e1bd9

Contributor guide