-
-
Notifications
You must be signed in to change notification settings - Fork 408
Add isolated-functions
rule
#2701
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Conversation
Actually maybe I should re-think how {
'unicorn/isolated-functions': [
'error',
{
globals: [
'node', // globals.node from `globals` package
'browser', // globals.browser from `globals` package
{foobar: true}, // also allow this global
{abc: 'off'}, // disallow this global
'xyz', // error, this doesn't exist in the `globals` package
}
]
} The above also allows for @fisker @sindresorhus what do you think? |
Users should always have |
Fixes #2214
From docs:
Prevent usage of variables from outside the scope of isolated functions
💼 This rule is enabled in the ✅
recommended
config.Some functions need to be isolated from their surrounding scope due to execution context constraints. For example, functions passed to
makeSynchronous()
are executed in a worker or subprocess and cannot access variables from outside their scope. This rule helps identify when functions are using external variables that may cause runtime errors.Common scenarios where functions must be isolated:
makeSynchronous()
(executed in worker)Function.prototype.toString()
By default, this rule allows global variables (like
console
,fetch
, etc.) in isolated functions, but prevents usage of variables from the surrounding scope.Examples
Options
Type:
object
functions
Type:
string[]
Default:
['makeSynchronous']
Array of function names that create isolated execution contexts. Functions passed as arguments to these functions will be considered isolated.
selectors
Type:
string[]
Default:
[]
Array of ESLint selectors to identify isolated functions. Useful for custom naming conventions or framework-specific patterns.
comments
Type:
string[]
Default:
['@isolated']
Array of comment strings that mark functions as isolated. Functions with JSDoc comments containing these strings will be considered isolated.
globals
Type:
boolean | string[]
Default:
true
Controls how global variables are handled:
false
: Global variables are not allowed in isolated functionstrue
(default): All globals from ESLint's language options are allowedstring[]
: Only the specified global variable names are allowedExamples
Custom function names
Lambda function naming convention
Allowing specific globals
Note: I didn't go as far as suggested in that issue #2214 (comment) - that is, to track imports from specific modules like
import makeSync from 'make-synchronous'
, to allow for arbitrary aliasing from that package. Instead I went for fairly specific naming conventions. But I thought the rule still had value while relying on naming convention, so opening a pull request now since I don't know when I'll get some more time to spend on it. I think the module-import-tracking thing could go in later as a new feature, if there's enough interest in it.