useBiomeTopLevelTypes (JavaScript)
Summary
Section titled “Summary”- Rule available since:
v2.6.9 - Diagnostic Category:
lint/nursery/useBiomeTopLevelTypes - 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": { "useBiomeTopLevelTypes": "error" } } }}Description
Section titled “Description”Require kind sets in Biome JS plugins to be created at the top level of the module.
In a Biome JS plugin, nodeKinds() and
tokenKinds() from @biomejs/runtime/plugin create kind sets, and the union() method
of a kind set creates a new set from two sets. Creating a set takes time.
Code inside a function, such as the run() function of a rule, can run many times, for
example once for every matching node in every file. Creating the same set again on each
call repeats that work. Create each set once, in a constant at the top level of the
plugin, and reuse it.
This rule reports calls to nodeKinds(), tokenKinds(), and union() inside functions,
and in class fields that aren’t static, because those fields are set again for every new
instance of the class. The rule only checks nodeKinds() and tokenKinds() when they’re
imported from @biomejs/runtime/plugin, and union() when it’s called on a kind set that
the rule can see being created.
Examples
Section titled “Examples”Invalid
Section titled “Invalid”import { defineRule, nodeKinds } from "@biomejs/runtime/plugin";
export const rule = defineRule({ run(node) { const CALLS = nodeKinds("JS_CALL_EXPRESSION", "JS_NEW_EXPRESSION"); return CALLS.has(node.parent); },});code-block.js:5:23 lint/nursery/useBiomeTopLevelTypes ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
⚠ This nodeKinds() call creates a new kind set every time it runs.
3 │ export const rule = defineRule({
4 │ run(node) {
> 5 │ const CALLS = nodeKinds("JS_CALL_EXPRESSION", "JS_NEW_EXPRESSION");
│ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
6 │ return CALLS.has(node.parent);
7 │ },
ℹ This code can run many times, for example once for every node that the run() function of a rule checks, or once for every new instance of a class, so the same set is created again each time.
ℹ Create the set once in a top-level constant, and use that constant here.
ℹ 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");const IMPORTS = nodeKinds("JS_IMPORT_CALL_EXPRESSION");
function isCallOrImport(node) { return CALLS.union(IMPORTS).has(node);}code-block.js:7:12 lint/nursery/useBiomeTopLevelTypes ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
⚠ This union() call creates a new kind set every time it runs.
6 │ function isCallOrImport(node) {
> 7 │ return CALLS.union(IMPORTS).has(node);
│ ^^^^^^^^^^^^^^^^^^^^
8 │ }
9 │
ℹ This code can run many times, for example once for every node that the run() function of a rule checks, or once for every new instance of a class, so the same set is created again each time.
ℹ Create the set once in a top-level constant, and use that constant here.
ℹ 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 { defineRule, nodeKinds } from "@biomejs/runtime/plugin";
const CALLS = nodeKinds("JS_CALL_EXPRESSION", "JS_NEW_EXPRESSION");const CALLS_AND_IMPORTS = CALLS.union(nodeKinds("JS_IMPORT_CALL_EXPRESSION"));
export const rule = defineRule({ run(node) { return CALLS_AND_IMPORTS.has(node.parent); },});See Also
Section titled “See Also”- To use kind sets instead of comparing kind names one by one, see
useBiomeKindSets.
Related links
Section titled “Related links”Copyright (c) 2023-present Biome Developers and Contributors.