useBiomeKindSets (JavaScript)
Summary
Section titled “Summary”- Rule available since:
v2.6.9 - Diagnostic Category:
lint/nursery/useBiomeKindSets - This rule doesn’t have a fix.
- The default severity of this rule is warning.
- This rule belongs to the following domains:
How to configure
Section titled “How to configure”{ "linter": { "rules": { "nursery": { "useBiomeKindSets": "error" } } }}Description
Section titled “Description”Require kind sets instead of comparing kind names one by one in Biome JS plugins.
In a Biome JS plugin, every node and token has
a kind, which is a string such as "JS_CALL_EXPRESSION" or "IDENT". To check whether
a node has one of several kinds, plugins can create a kind set with nodeKinds(), or
tokenKinds() for tokens, from @biomejs/runtime/plugin, and call its has() method.
A kind set checks the kind of a node or token directly, without comparing strings, so it’s
faster. In TypeScript, a has() check also tells TypeScript which kinds of node the value
can be, just as the comparisons do.
This rule reports checks of the same node or token against two or more kind names:
- comparisons joined with
||, such asnode.kind === "A" || node.kind === "B"; - negated comparisons joined with
&&, such asnode.kind !== "A" && node.kind !== "B"; includes()calls on an array of kind names, such as["A", "B"].includes(node.kind);has()calls on aSetof kind names, such asnew Set(["A", "B"]).has(node.kind).
The kind names, the array, and the Set can also be stored in const variables, such as
const CALL = "JS_CALL_EXPRESSION".
The rule only reports a check when every string is a node kind, or every string is a
token kind. Other objects in the plugin API also have a kind property, such as
context.sourceType.language. Their values aren’t syntax kinds, so the rule ignores
them.
Create each kind set once, in a constant at the top level of the plugin, and reuse it.
Examples
Section titled “Examples”Invalid
Section titled “Invalid”if (node.kind === "JS_CALL_EXPRESSION" || node.kind === "JS_NEW_EXPRESSION") {}code-block.js:1:5 lint/nursery/useBiomeKindSets ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
⚠ This code checks the kind of node by comparing it with several strings.
> 1 │ if (node.kind === "JS_CALL_EXPRESSION" || node.kind === "JS_NEW_EXPRESSION") {}
│ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
2 │
ℹ A kind set checks the kind directly, without comparing strings, so it's faster.
ℹ Create the set once in a top-level constant, such as const KINDS = nodeKinds(...), then replace this check with KINDS.has(node).
ℹ 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.
if (token.kind !== "EQ2" && token.kind !== "NEQ") {}code-block.js:1:5 lint/nursery/useBiomeKindSets ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
⚠ This code checks the kind of token by comparing it with several strings.
> 1 │ if (token.kind !== "EQ2" && token.kind !== "NEQ") {}
│ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
2 │
ℹ A kind set checks the kind directly, without comparing strings, so it's faster.
ℹ Create the set once in a top-level constant, such as const KINDS = tokenKinds(...), then replace this check with !KINDS.has(token).
ℹ 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 CALLS = ["JS_CALL_EXPRESSION", "JS_NEW_EXPRESSION"];if (CALLS.includes(node.kind)) {}code-block.js:2:5 lint/nursery/useBiomeKindSets ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
⚠ This code checks the kind of node by comparing it with several strings.
1 │ const CALLS = ["JS_CALL_EXPRESSION", "JS_NEW_EXPRESSION"];
> 2 │ if (CALLS.includes(node.kind)) {}
│ ^^^^^^^^^^^^^^^^^^^^^^^^^
3 │
ℹ A kind set checks the kind directly, without comparing strings, so it's faster.
ℹ Create the set once in a top-level constant, such as const KINDS = nodeKinds(...), then replace this check with KINDS.has(node).
ℹ 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 CALLS = new Set(["JS_CALL_EXPRESSION", "JS_NEW_EXPRESSION"]);if (CALLS.has(node.kind)) {}code-block.js:2:5 lint/nursery/useBiomeKindSets ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
⚠ This code checks the kind of node by comparing it with several strings.
1 │ const CALLS = new Set(["JS_CALL_EXPRESSION", "JS_NEW_EXPRESSION"]);
> 2 │ if (CALLS.has(node.kind)) {}
│ ^^^^^^^^^^^^^^^^^^^^
3 │
ℹ A kind set checks the kind directly, without comparing strings, so it's faster.
ℹ Create the set once in a top-level constant, such as const KINDS = nodeKinds(...), then replace this check with KINDS.has(node).
ℹ 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.
import { nodeKinds } from "@biomejs/runtime/plugin";
const CALLS = nodeKinds("JS_CALL_EXPRESSION", "JS_NEW_EXPRESSION");if (CALLS.has(node)) {}
if (node.kind === "JS_CALL_EXPRESSION") {}
const language = context.sourceType.language;if (language.kind === "javascript" || language.kind === "typescript") {}See Also
Section titled “See Also”- To avoid creating the same kind set again every time a function runs, see
useBiomeTopLevelTypes.
Related links
Section titled “Related links”Copyright (c) 2023-present Biome Developers and Contributors.