Skip to content

Commit ea09c9e

Browse files
committed
allow globals by default
1 parent 55036f1 commit ea09c9e

File tree

3 files changed

+47
-28
lines changed

3 files changed

+47
-28
lines changed

docs/rules/isolated-functions.md

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ Common scenarios where functions must be isolated:
1414
- Server actions or other remote execution contexts
1515
- Functions with specific JSDoc annotations
1616

17+
By default, this rule allows global variables (like `console`, `fetch`, etc.) in isolated functions, but prevents usage of variables from the surrounding scope.
18+
1719
## Fail
1820

1921
```js
@@ -80,6 +82,19 @@ function abc() {
8082
}
8183
```
8284

85+
```js
86+
import makeSynchronous from 'make-synchronous';
87+
88+
export const fetchSync = () => {
89+
const getText = makeSynchronous(async () => {
90+
console.log('Starting...'); // ✅ Global variables are allowed by default
91+
const res = await fetch('https://example.com'); // ✅ Global variables are allowed by default
92+
return res.text();
93+
});
94+
console.log(getText());
95+
};
96+
```
97+
8398
## Options
8499

85100
Type: `object`
@@ -130,12 +145,12 @@ Array of comment strings that mark functions as isolated. Functions with JSDoc c
130145
### globals
131146

132147
Type: `boolean | string[]`\
133-
Default: `false`
148+
Default: `true`
134149

135150
Controls how global variables are handled:
136151

137-
- `false` (default): Global variables are not allowed in isolated functions
138-
- `true`: All globals from ESLint's language options are allowed
152+
- `false`: Global variables are not allowed in isolated functions
153+
- `true` (default): All globals from ESLint's language options are allowed
139154
- `string[]`: Only the specified global variable names are allowed
140155

141156
```js

rules/isolated-functions.js

Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
'use strict';
21
import esquery from 'esquery';
32
import functionTypes from './ast/function-types.js';
43

@@ -16,26 +15,31 @@ const parseEsquerySelector = selector => {
1615
return parsedEsquerySelectors.get(selector);
1716
};
1817

18+
/** @type {{functions: string[], selectors: string[], comments: string[], globals: boolean | string[]}} */
19+
const defaultOptions = {
20+
functions: ['makeSynchronous'],
21+
selectors: [],
22+
comments: ['@isolated'],
23+
globals: true,
24+
};
25+
1926
/** @param {import('eslint').Rule.RuleContext} context */
2027
const create = context => {
2128
const {sourceCode} = context;
22-
/** @type {{functions: string[], selectors: string[], comments: string[], globals: boolean | string[]}} */
23-
const userOptions = context.options[0];
24-
/** @type {typeof userOptions} */
29+
/** @type {typeof defaultOptions} */
2530
const options = {
26-
functions: ['makeSynchronous'],
27-
selectors: [],
28-
comments: ['@isolated'],
29-
globals: false,
30-
...userOptions,
31+
...defaultOptions,
32+
...context.options[0],
3133
};
3234

3335
options.comments = options.comments.map(comment => comment.toLowerCase());
34-
const allowedGlobals = options.globals === true
35-
? Object.keys(context.languageOptions.globals)
36-
: (Array.isArray(options.globals)
37-
? options.globals
38-
: []);
36+
/** @type {string[]} */
37+
let allowedGlobals = [];
38+
if (options.globals === true) {
39+
allowedGlobals = Object.keys(context.languageOptions.globals);
40+
} else if (Array.isArray(options.globals)) {
41+
allowedGlobals = options.globals;
42+
}
3943

4044
/** @param {import('estree').Node} node */
4145
const checkForExternallyScopedVariables = node => {
@@ -155,6 +159,7 @@ export default {
155159
recommended: true,
156160
},
157161
schema,
162+
defaultOptions: [defaultOptions],
158163
messages,
159164
},
160165
};

test/isolated-functions.js

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -94,16 +94,6 @@ test({
9494
`),
9595
errors: [error({name: 'foo', reason: 'follows comment containing "@isolated"'})],
9696
},
97-
{
98-
name: 'global variables not allowed by default',
99-
languageOptions: {globals: {foo: true}},
100-
code: stripIndent(`
101-
makeSynchronous(function () {
102-
return foo.slice();
103-
});
104-
`),
105-
errors: [fooInMakeSynchronousError],
106-
},
10797
{
10898
name: 'global variables can be explicitly disallowed',
10999
languageOptions: {globals: {foo: true}},
@@ -179,7 +169,16 @@ test({
179169
`),
180170
},
181171
{
182-
name: 'can allow global variables from language options',
172+
name: 'can implicitly allow global variables from language options',
173+
languageOptions: {globals: {foo: true}},
174+
code: stripIndent(`
175+
makeSynchronous(function () {
176+
return foo.slice();
177+
});
178+
`),
179+
},
180+
{
181+
name: 'can explicitly allow global variables from language options',
183182
languageOptions: {globals: {foo: true}},
184183
options: [{globals: true}],
185184
code: stripIndent(`

0 commit comments

Comments
 (0)