noOuterScopeInIsolatedFunctions (JavaScript)
Summary
Section titled “Summary”- Diagnostic Category:
lint/nursery/noOuterScopeInIsolatedFunctions - This rule doesn’t have a fix.
- The default severity of this rule is error.
- Sources:
- Inspired from
unicorn/isolated-functions
- Inspired from
How to configure
Section titled “How to configure”{ "linter": { "rules": { "nursery": { "noOuterScopeInIsolatedFunctions": "error" } } }}Description
Section titled “Description”Disallow using variables, this, and super from outside an isolated function.
Some functions don’t run where they’re written. A library can convert a function
to text and run that text somewhere else, such as in a worker, in another process,
or inside a browser page. A function that runs this way is called an isolated function.
Variables declared outside an isolated function don’t exist where it runs,
so using one throws an error. For the same reason, this and super don’t refer
to the object or class around the function.
The rule treats these functions as isolated:
- Functions passed to
makeSynchronous()orworkerize(). - The first function passed to
browser.execute()orpage.evaluate(). - The
funcfunction passed tochrome.scripting.executeScript()orbrowser.scripting.executeScript(). - Functions, variables holding a function, and object or class members (methods, getters,
setters, and constructors) that have an
@isolatedcomment right before them. The comment can contain only the tag, or the tag followed by-and an explanation, such as// @isolated - runs in a worker.
The rule only checks variables declared in the same file, including imports.
It ignores global variables, such as Array or fetch, types,
and variables declared with TypeScript’s declare keyword.
Examples
Section titled “Examples”Invalid
Section titled “Invalid”import makeSynchronous from "make-synchronous";
const url = "https://example.com";
const getText = makeSynchronous(async () => { const response = await fetch(url); return response.text();});code-block.js:6:34 lint/nursery/noOuterScopeInIsolatedFunctions ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
✖ The variable url is declared outside of this isolated function.
5 │ const getText = makeSynchronous(async () => {
> 6 │ const response = await fetch(url);
│ ^^^
7 │ return response.text();
8 │ });
ℹ The variable is declared here.
1 │ import makeSynchronous from "make-synchronous";
2 │
> 3 │ const url = "https://example.com";
│ ^^^
4 │
5 │ const getText = makeSynchronous(async () => {
ℹ Functions passed to makeSynchronous() run apart from the code around them.
ℹ Variables declared outside of the function don't exist where it runs, so using one throws an error.
ℹ Declare the variable inside the function, or pass its value to the function as an argument.
ℹ 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 greeting = "hi";
/** @isolated */function greet() { return greeting.toUpperCase();}code-block.js:5:12 lint/nursery/noOuterScopeInIsolatedFunctions ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
✖ The variable greeting is declared outside of this isolated function.
3 │ /** @isolated */
4 │ function greet() {
> 5 │ return greeting.toUpperCase();
│ ^^^^^^^^
6 │ }
7 │
ℹ The variable is declared here.
> 1 │ const greeting = "hi";
│ ^^^^^^^^
2 │
3 │ /** @isolated */
ℹ The @isolated comment marks this function as one that runs apart from the code around it.
ℹ Variables declared outside of the function don't exist where it runs, so using one throws an error.
ℹ Declare the variable inside the function, or pass its value to the function as an argument.
ℹ 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 counter = { count: 0, /** @isolated */ increment() { return this.count + 1; },};code-block.js:5:16 lint/nursery/noOuterScopeInIsolatedFunctions ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
✖ this is used inside an isolated function.
3 │ /** @isolated */
4 │ increment() {
> 5 │ return this.count + 1;
│ ^^^^
6 │ },
7 │ };
ℹ The @isolated comment marks this function as one that runs apart from the code around it.
ℹ Where the function runs, this doesn't refer to the object or class around it.
ℹ Pass the values the function needs as arguments instead.
ℹ 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 makeSynchronous from "make-synchronous";
const getText = makeSynchronous(async () => { const url = "https://example.com"; const response = await fetch(url); return response.text();});
const getTextFrom = makeSynchronous(async (url) => { const response = await fetch(url); return response.text();});
/** @isolated */function greet(greeting) { return greeting.toUpperCase();}See Also
Section titled “See Also”- If you use Playwright and only want to check the functions passed to
page.evaluate(), seenoPlaywrightUnsafeReferences.
Related links
Section titled “Related links”Copyright (c) 2023-present Biome Developers and Contributors.