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.
nursery/noOuterScopeInIsolatedFunctions
nursery · since 2.6.10
{
"linter": {
"rules": {
"nursery": {
"noOuterScopeInIsolatedFunctions": "error"
}
}
}
}Enforce consistent default export declarations.
Disallow break and optionally continue in nested loops and switches inside loops.
Enforce consistent labels on tuple type elements.
Disallow duplicate adjacent branches in if chains.
Enforce consistent JSON file reads before JSON.parse().
Do not use a for loop that can be replaced with a for-of loop.