unicorn/isolated-functions
eslint-plugin-unicorn
No autofix
eslint.config.js
import unicorn from "eslint-plugin-unicorn";
export default [
{
plugins: { unicorn },
rules: {
"unicorn/isolated-functions": "error",
},
},
];Prevent usage of variables from outside the scope of isolated functions.
const getText = makeSynchronous(async () => {
const response = await fetch(url); // ❌ 'url' is not defined in isolated function scope
return response.text();
});/** @isolated */
function abc() {
return foo.slice(); // ❌ 'foo' is not defined in isolated function scope
}
const object = {
/** @isolated */
method() {
return this.foo; // ❌ 'this' depends on external object state
},
};const getText = makeSynchronous(async () => {
const url = 'https://example.com'; // Variable defined within function scope
const response = await fetch(url);
return response.text();
});const getText = makeSynchronous(async (url) => { // Variable passed as parameter
const response = await fetch(url);
return response.text();
});
console.log(getText('https://example.com'));/** @isolated */
function abc() {
const foo = 'hi'; // Variable defined within function scope
return foo.slice();
}Examples from the ESLint documentation.
unicorn/isolated-functions
eslint-plugin-unicorn
import unicorn from "eslint-plugin-unicorn";
export default [
{
plugins: { unicorn },
rules: {
"unicorn/isolated-functions": "error",
},
},
];Oxlint does not have this rule.
Biome does not have this rule.
Enforce consistent default export declarations.
Require or disallow logical assignment operator shorthand
Enforce consistent conditional object spread style.
Enforce replacements for variable, property, and filenames.
Enforce consistent class member order.
Disallow array accumulation with Array#concat() in loops.