Skip to content

Commit 48d5cc4

Browse files
authored
feat: report references inside IIFEs in no-use-before-define (#21238)
1 parent 43f0ac9 commit 48d5cc4

3 files changed

Lines changed: 220 additions & 1 deletion

File tree

‎docs/src/rules/no-use-before-define.md‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,12 +148,14 @@ export { foo };
148148
This flag determines whether or not the rule checks class declarations of upper scopes.
149149
If this is `true`, the rule warns on every reference to a class before the class declaration.
150150
Otherwise, the rule ignores such references, provided the declaration is in an upper function scope.
151+
References inside a synchronous, non-generator function expression that is called directly before the declaration (an IIFE) are still reported, because the function body runs immediately.
151152
Class declarations are not hoisted, so it might be dangerous to disable this option.
152153
Default is `true`.
153154
* `variables` (`boolean`) -
154155
This flag determines whether or not the rule checks variable declarations in upper scopes.
155156
If this is `true`, the rule warns on every reference to a variable before the variable declaration.
156157
Otherwise, the rule ignores a reference if the declaration is in an upper scope, while still reporting the reference if it's in the same scope as the declaration.
158+
References inside a synchronous, non-generator function expression that is called directly before the declaration (an IIFE) are also reported, because the function body runs immediately.
157159
Default is `true`.
158160
* `allowNamedExports` (`boolean`) -
159161
If this flag is set to `true`, the rule always allows references in `export {};` declarations.
@@ -289,6 +291,13 @@ const g = function() {};
289291
}
290292
const foo = 1;
291293
}
294+
295+
{
296+
(() => {
297+
console.log(foo);
298+
})();
299+
const foo = 1;
300+
}
292301
```
293302

294303
:::

‎lib/rules/no-use-before-define.js‎

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,34 @@ function isClassStaticInitializerScope(scope) {
9393
return false;
9494
}
9595

96+
/**
97+
* Checks whether a given scope is the scope of an immediately invoked function.
98+
* The body of an immediately invoked function is evaluated where the function appears,
99+
* so it belongs to the execution context that contains it.
100+
*
101+
* Generators are excluded because calling a generator function only creates an iterator,
102+
* and the body isn't evaluated until the iterator is advanced. All async functions are excluded
103+
* as well, regardless of whether they contain `await`: code after an `await` is evaluated in a
104+
* later job, and telling which references are evaluated before the first `await` would require
105+
* code path analysis.
106+
* @param {Scope} scope A scope to check.
107+
* @returns {boolean} `true` if the scope is the scope of an immediately invoked function.
108+
*/
109+
function isImmediatelyInvokedFunctionScope(scope) {
110+
if (scope.type !== "function") {
111+
return false;
112+
}
113+
114+
// `scope.block` is a function node
115+
const node = scope.block;
116+
117+
if (node.parent.type !== "CallExpression" || node.parent.callee !== node) {
118+
return false;
119+
}
120+
121+
return !node.generator && !node.async;
122+
}
123+
96124
/**
97125
* Checks whether a given reference is evaluated in an execution context
98126
* that isn't the one where the variable it refers to is defined.
@@ -103,12 +131,17 @@ function isClassStaticInitializerScope(scope) {
103131
* - class static blocks (implicit functions)
104132
* Static class field initializers and class static blocks are automatically run during the class definition evaluation,
105133
* and therefore we'll consider them as a part of the parent execution context.
134+
* The same applies to immediately invoked functions, whose bodies are evaluated where the function appears.
106135
* Example:
107136
*
108137
* const x = 1;
109138
*
110139
* x; // returns `false`
111140
* () => x; // returns `true`
141+
* (() => x)(); // returns `false`
142+
* (() => () => x)(); // returns `true`, the inner function is deferred
143+
* (function* () { x; })(); // returns `true`, the body is not evaluated on call
144+
* (async () => { x; })(); // returns `true`, the body may be evaluated in a later job
112145
*
113146
* class C {
114147
* field = x; // returns `true`
@@ -135,7 +168,10 @@ function isFromSeparateExecutionContext(reference) {
135168

136169
// Scope#variableScope represents execution context
137170
while (variable.scope.variableScope !== scope.variableScope) {
138-
if (isClassStaticInitializerScope(scope.variableScope)) {
171+
if (
172+
isClassStaticInitializerScope(scope.variableScope) ||
173+
isImmediatelyInvokedFunctionScope(scope.variableScope)
174+
) {
139175
scope = scope.variableScope.upper;
140176
} else {
141177
return true;

‎tests/lib/rules/no-use-before-define.js‎

Lines changed: 174 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -453,6 +453,100 @@ ruleTester.run("no-use-before-define", rule, {
453453
parserOptions: { ecmaFeatures: { jsx: true } },
454454
},
455455
},
456+
457+
// IIFEs: deferred code inside an IIFE is still a separate execution context
458+
{
459+
code: "(() => () => a)(); let a;",
460+
options: [{ variables: false }],
461+
languageOptions: { ecmaVersion: 2022 },
462+
},
463+
{
464+
code: "(() => { function f() { a; } })(); let a;",
465+
options: [{ variables: false }],
466+
languageOptions: { ecmaVersion: 2022 },
467+
},
468+
{
469+
code: "(() => { class C { m() { a; } } })(); let a;",
470+
options: [{ variables: false }],
471+
languageOptions: { ecmaVersion: 2022 },
472+
},
473+
474+
// IIFEs: the function's own name is defined inside the function scope
475+
"(function fn() { fn(); })();",
476+
{
477+
code: "(function fn() { fn(); })();",
478+
options: [{ variables: false }],
479+
},
480+
481+
// not immediately invoked: the function expression is not the callee
482+
{
483+
code: "const f = () => { a; }; let a;",
484+
options: [{ variables: false }],
485+
languageOptions: { ecmaVersion: 2022 },
486+
},
487+
488+
// calling a function after its declaration but before the referenced variable is initialized is not detected
489+
{
490+
code: "const f = () => { a; }; f(); let a;",
491+
options: [{ variables: false }],
492+
languageOptions: { ecmaVersion: 2022 },
493+
},
494+
{
495+
code: "foo(() => { a; }); let a;",
496+
options: [{ variables: false }],
497+
languageOptions: { ecmaVersion: 2022 },
498+
},
499+
500+
// generators are not immediately invoked: the body is suspended before the first statement
501+
{
502+
code: "(function* () { a; })(); let a;",
503+
options: [{ variables: false }],
504+
languageOptions: { ecmaVersion: 2022 },
505+
},
506+
{
507+
code: "(async function* () { a; })(); let a;",
508+
options: [{ variables: false }],
509+
languageOptions: { ecmaVersion: 2022 },
510+
},
511+
512+
/*
513+
* async functions are not immediately invoked: the body can suspend at any
514+
* `await`, and everything that follows it is evaluated in a later job.
515+
*/
516+
{
517+
code: "(async () => { a; })(); let a;",
518+
options: [{ variables: false }],
519+
languageOptions: { ecmaVersion: 2022 },
520+
},
521+
{
522+
code: "(async () => { await 0; a; })(); let a;",
523+
options: [{ variables: false }],
524+
languageOptions: { ecmaVersion: 2022 },
525+
},
526+
{
527+
code: "await (async () => { a; })(); let a;",
528+
options: [{ variables: false }],
529+
languageOptions: { ecmaVersion: 2022, sourceType: "module" },
530+
},
531+
532+
// `.call()`/`.apply()` are not syntactically IIFEs
533+
{
534+
code: "(function () { a; }).call(null); let a;",
535+
options: [{ variables: false }],
536+
languageOptions: { ecmaVersion: 2022 },
537+
},
538+
539+
// IIFEs in an initializer, with the default options
540+
{
541+
// the generator body isn't evaluated, so `a` is already initialized
542+
code: "const a = (function* () { a; })();",
543+
languageOptions: { ecmaVersion: 2022 },
544+
},
545+
{
546+
// the async body is not evaluated during the initialization
547+
code: "const a = (async () => { a; })();",
548+
languageOptions: { ecmaVersion: 2022 },
549+
},
456550
],
457551
invalid: [
458552
{
@@ -1714,6 +1808,86 @@ ruleTester.run("no-use-before-define", rule, {
17141808
},
17151809
],
17161810
},
1811+
1812+
// IIFE bodies run in the enclosing execution context
1813+
{
1814+
code: "(() => { a; })(); let a;",
1815+
options: [{ variables: false }],
1816+
languageOptions: { ecmaVersion: 2022 },
1817+
errors: [
1818+
{
1819+
messageId: "usedBeforeDefined",
1820+
data: { name: "a" },
1821+
},
1822+
],
1823+
},
1824+
{
1825+
code: "(function () { a; })(); let a;",
1826+
options: [{ variables: false }],
1827+
languageOptions: { ecmaVersion: 2022 },
1828+
errors: [
1829+
{
1830+
messageId: "usedBeforeDefined",
1831+
data: { name: "a" },
1832+
},
1833+
],
1834+
},
1835+
{
1836+
code: "(function f() { a; })(); let a;",
1837+
options: [{ variables: false }],
1838+
languageOptions: { ecmaVersion: 2022 },
1839+
errors: [
1840+
{
1841+
messageId: "usedBeforeDefined",
1842+
data: { name: "a" },
1843+
},
1844+
],
1845+
},
1846+
{
1847+
code: "(() => { (() => { a; })(); })(); let a;",
1848+
options: [{ variables: false }],
1849+
languageOptions: { ecmaVersion: 2022 },
1850+
errors: [
1851+
{
1852+
messageId: "usedBeforeDefined",
1853+
data: { name: "a" },
1854+
},
1855+
],
1856+
},
1857+
{
1858+
code: "(() => { new C(); })(); class C {}",
1859+
options: [{ classes: false }],
1860+
languageOptions: { ecmaVersion: 2022 },
1861+
errors: [
1862+
{
1863+
messageId: "usedBeforeDefined",
1864+
data: { name: "C" },
1865+
},
1866+
],
1867+
},
1868+
{
1869+
code: "(() => { a; })?.(); let a;",
1870+
options: [{ variables: false }],
1871+
languageOptions: { ecmaVersion: 2022 },
1872+
errors: [
1873+
{
1874+
messageId: "usedBeforeDefined",
1875+
data: { name: "a" },
1876+
},
1877+
],
1878+
},
1879+
1880+
// IIFEs in an initializer are evaluated during the initialization
1881+
{
1882+
code: "const a = (() => a)();",
1883+
languageOptions: { ecmaVersion: 2022 },
1884+
errors: [
1885+
{
1886+
messageId: "usedBeforeDefined",
1887+
data: { name: "a" },
1888+
},
1889+
],
1890+
},
17171891
],
17181892
});
17191893

0 commit comments

Comments
 (0)