useConsistentBooleanName (JavaScript)
Summary
Section titled “Summary”- Diagnostic Category:
lint/nursery/useConsistentBooleanName - This rule doesn’t have a fix.
- The default severity of this rule is information.
- Sources:
- Inspired from
unicorn/consistent-boolean-name
- Inspired from
How to configure
Section titled “How to configure”{ "linter": { "rules": { "nursery": { "useConsistentBooleanName": "error" } } }}Description
Section titled “Description”Require boolean names to start with a prefix such as is or has, and reserve those prefixes for booleans.
A name like isReady or hasAccess tells the reader that the value is
either true or false. When a boolean has a name like ready, readers
have to look at the code to learn what kind of value it holds. When a
name like hasName holds a string, it misleads readers.
This rule checks variables, function parameters, and function names. It reports a name that holds a boolean, or a function that returns one, when the name doesn’t start with a boolean prefix. It also reports a name that starts with a boolean prefix when its value isn’t a boolean.
The rule decides whether a value is a boolean from what the code shows:
type annotations, literals like true, comparisons like a === b,
negations like !value, calls like Boolean(value) or
Array.isArray(value), and the values that a function returns. When the
rule can’t tell whether a value is a boolean, it doesn’t report the name.
A prefix counts only when it’s a separate word in the name. isReady,
is_ready, IS_READY, and _isReady all start with the prefix is,
but island doesn’t. For React hooks, the rule checks the part of the
name after use, so useIsReady is a boolean name.
The default prefixes are are, can, did, does, has, have,
is, requires, should, was, were, and will.
Destructured variables, imports, class names, and catch parameters
aren’t checked. Object properties, class properties, and methods
aren’t checked either.
Examples
Section titled “Examples”Invalid
Section titled “Invalid”const completed = true;code-block.js:1:7 lint/nursery/useConsistentBooleanName ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
ℹ completed holds a boolean, but its name doesn't start with a boolean prefix.
> 1 │ const completed = true;
│ ^^^^^^^^^
2 │
ℹ A boolean prefix tells readers that the value is either true or false.
ℹ Rename it so that its name starts with one of these prefixes: are, can, did, does, has, have, is, requires, should, was, were, will.
ℹ This rule belongs to the nursery group, which means it is not yet stable and may change in the future. Visit https://biomejs.dev/linter/#nursery for more information.
const completed = progress === 100;code-block.js:1:7 lint/nursery/useConsistentBooleanName ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
ℹ completed holds a boolean, but its name doesn't start with a boolean prefix.
> 1 │ const completed = progress === 100;
│ ^^^^^^^^^
2 │
ℹ A boolean prefix tells readers that the value is either true or false.
ℹ Rename it so that its name starts with one of these prefixes: are, can, did, does, has, have, is, requires, should, was, were, will.
ℹ This rule belongs to the nursery group, which means it is not yet stable and may change in the future. Visit https://biomejs.dev/linter/#nursery for more information.
const hasName = "Sindre";code-block.js:1:7 lint/nursery/useConsistentBooleanName ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
ℹ hasName starts with the boolean prefix has, but doesn't hold a boolean.
> 1 │ const hasName = "Sindre";
│ ^^^^^^^
2 │
ℹ Readers expect a name that starts with has to be either true or false.
ℹ Rename it so that it doesn't start with has.
ℹ This rule belongs to the nursery group, which means it is not yet stable and may change in the future. Visit https://biomejs.dev/linter/#nursery for more information.
function hasTitle() { return "Unicorn";}code-block.js:1:10 lint/nursery/useConsistentBooleanName ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
ℹ hasTitle starts with the boolean prefix has, but doesn't return a boolean.
> 1 │ function hasTitle() {
│ ^^^^^^^^
2 │ return "Unicorn";
3 │ }
ℹ Readers expect a name that starts with has to be either true or false.
ℹ Rename it so that it doesn't start with has.
ℹ This rule belongs to the nursery group, which means it is not yet stable and may change in the future. Visit https://biomejs.dev/linter/#nursery for more information.
function completed() { return true;}code-block.js:1:10 lint/nursery/useConsistentBooleanName ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
ℹ completed returns a boolean, but its name doesn't start with a boolean prefix.
> 1 │ function completed() {
│ ^^^^^^^^^
2 │ return true;
3 │ }
ℹ A boolean prefix tells readers that the value is either true or false.
ℹ Rename it so that its name starts with one of these prefixes: are, can, did, does, has, have, is, requires, should, was, were, will.
ℹ This rule belongs to the nursery group, which means it is not yet stable and may change in the future. Visit https://biomejs.dev/linter/#nursery for more information.
function download(showProgress: boolean) {}code-block.ts:1:19 lint/nursery/useConsistentBooleanName ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
ℹ showProgress holds a boolean, but its name doesn't start with a boolean prefix.
> 1 │ function download(showProgress: boolean) {}
│ ^^^^^^^^^^^^
2 │
ℹ A boolean prefix tells readers that the value is either true or false.
ℹ Rename it so that its name starts with one of these prefixes: are, can, did, does, has, have, is, requires, should, was, were, will.
ℹ This rule belongs to the nursery group, which means it is not yet stable and may change in the future. Visit https://biomejs.dev/linter/#nursery for more information.
const isCompleted = true;const hasCompleted = progress === 100;const name = "Sindre";function getTitle() { return "Unicorn";}function isCompleted() { return true;}function download(shouldShowProgress: boolean) {}const task = { completed: progress === 100 };Options
Section titled “Options”prefixes
Section titled “prefixes”Default: {}
Prefixes to add to the defaults, or to turn off. Set a prefix to true
to accept it on boolean names and to require a boolean value for names
that start with it. Set a prefix to false to turn it off: the rule no
longer accepts it on boolean names, and no longer requires a boolean
value for names that start with it.
{ "linter": { "rules": { "nursery": { "useConsistentBooleanName": { "level": "on", "options": { "prefixes": { "needs": true, "did": false } } } } } }}Invalid
Section titled “Invalid”const didUpdate = true;code-block.js:1:7 lint/nursery/useConsistentBooleanName ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
ℹ didUpdate holds a boolean, but its name doesn't start with a boolean prefix.
> 1 │ const didUpdate = true;
│ ^^^^^^^^^
2 │
ℹ A boolean prefix tells readers that the value is either true or false.
ℹ Rename it so that its name starts with one of these prefixes: are, can, does, has, have, is, requires, should, was, were, will, needs.
ℹ This rule belongs to the nursery group, which means it is not yet stable and may change in the future. Visit https://biomejs.dev/linter/#nursery for more information.
const needsUpdate = "yes";code-block.js:1:7 lint/nursery/useConsistentBooleanName ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
ℹ needsUpdate starts with the boolean prefix needs, but doesn't hold a boolean.
> 1 │ const needsUpdate = "yes";
│ ^^^^^^^^^^^
2 │
ℹ Readers expect a name that starts with needs to be either true or false.
ℹ Rename it so that it doesn't start with needs.
ℹ This rule belongs to the nursery group, which means it is not yet stable and may change in the future. Visit https://biomejs.dev/linter/#nursery for more information.
const needsUpdate = true;const didUpdate = "yes";ignore
Section titled “ignore”Default: []
Names that the rule doesn’t check. A name must match an entry exactly.
{ "linter": { "rules": { "nursery": { "useConsistentBooleanName": { "level": "on", "options": { "ignore": [ "value", "completed" ] } } } } }}const value = true;const completed = true;Related links
Section titled “Related links”Copyright (c) 2023-present Biome Developers and Contributors.