diff --git a/crates/oxc_linter/src/rules/unicorn/consistent_function_scoping.rs b/crates/oxc_linter/src/rules/unicorn/consistent_function_scoping.rs index c7164533a79eb..8f99875ed9735 100644 --- a/crates/oxc_linter/src/rules/unicorn/consistent_function_scoping.rs +++ b/crates/oxc_linter/src/rules/unicorn/consistent_function_scoping.rs @@ -50,8 +50,10 @@ declare_oxc_lint!( /// ### Why is this bad? /// /// Moving function declarations to the highest possible scope improves - /// readability, directly improves performance and allows JavaScript engines - /// to better optimize your performance. + /// readability, directly [improves + /// performance](https://stackoverflow.com/questions/80802/does-use-of-anonymous-functions-affect-performance/81329#81329) + /// and allows JavaScript engines to better [optimize your + /// performance](https://ponyfoo.com/articles/javascript-performance-pitfalls-v8#optimization-limit). /// /// /// ### Examples @@ -90,6 +92,52 @@ declare_oxc_lint!( /// return doBar; /// } /// ``` + /// ## Options + /// + /// ### checkArrowFunctions + /// + /// Type: `boolean`\ + /// Default: `true` + /// + /// Pass `"checkArrowFunctions": false` to disable linting of arrow functions. + /// + /// ## Limitations + /// + /// This rule does not detect or remove extraneous code blocks inside of functions: + /// + /// ```js + /// function doFoo(foo) { + /// { + /// function doBar(bar) { + /// return bar; + /// } + /// } + /// + /// return foo; + /// } + /// ``` + /// + /// It also ignores functions that contain `JSXElement` references: + /// + /// ```jsx + /// function doFoo(FooComponent) { + /// function Bar() { + /// return ; + /// } + /// + /// return Bar; + /// }; + /// ``` + /// + /// [Immediately invoked function expressions (IIFE)](https://en.wikipedia.org/wiki/Immediately_invoked_function_expression) are ignored: + /// + /// ```js + /// (function () { + /// function doFoo(bar) { + /// return bar; + /// } + /// })(); + /// ``` ConsistentFunctionScoping, suspicious, pending @@ -116,13 +164,16 @@ impl Rule for ConsistentFunctionScoping { return; } + // NOTE: function.body will always be some here because of + // checks in `is_typescript_syntax` let Some(function_body) = &function.body else { return }; if let Some(binding_ident) = get_function_like_declaration(node, ctx) { ( binding_ident.symbol_id.get().unwrap(), function_body, - Span::new(function.span.start, function.span.start + 8), + // 8 for "function" + Span::sized(function.span.start, 8), ) } else if let Some(function_id) = &function.id { (function_id.symbol_id.get().unwrap(), function_body, function_id.span()) @@ -143,6 +194,7 @@ impl Rule for ConsistentFunctionScoping { } _ => return, }; + // if the function is declared at the root scope, we don't need to check anything if ctx.symbols().get_scope_id(function_declaration_symbol_id) == ctx.scopes().root_scope_id() @@ -264,284 +316,206 @@ fn test() { ("const doFoo = function() {};", None), ("const doFoo = foo => foo;", None), ("foo => foo;", None), + ("function doFoo(foo) { function doBar(bar) { return foo + bar; } return foo; }", None), ( - " - function doFoo(foo) { - function doBar(bar) { - return foo + bar; - } - return foo; + "const doFoo = function(foo) { + function doBar(bar) { + return foo + bar; } - ", + return foo; + };", None, ), ( - " - const doFoo = function(foo) { - function doBar(bar) { - return foo + bar; - } - return foo; + "const doFoo = function(foo) { + const doBar = function(bar) { + return foo + bar; }; - ", + return foo; + };", None, ), ( - " - const doFoo = function(foo) { - const doBar = function(bar) { - return foo + bar; - }; - return foo; + "function doFoo(foo) { + const doBar = function(bar) { + return foo + bar; }; - ", + return foo; + }", None, ), ( - " - function doFoo(foo) { - const doBar = function(bar) { - return foo + bar; - }; - return foo; + "function doFoo(foo) { + function doBar(bar) { + return foo + bar; } - ", + }", None, ), ( - " - function doFoo(foo) { - function doBar(bar) { - return foo + bar; - } + "function doFoo(foo = 'foo') { + function doBar(bar) { + return foo + bar; } - ", + }", None, ), ( - " - function doFoo(foo = 'foo') { - function doBar(bar) { - return foo + bar; - } + "function doFoo() { + const foo = 'foo'; + function doBar(bar) { + return foo + bar; } - ", - None, - ), - ( - " - function doFoo() { - const foo = 'foo'; - function doBar(bar) { - return foo + bar; - } - return foo; - } - ", + return foo; + }", None, ), ( - " - function doFoo(foo) { - function doBar(bar) { - function doZaz(zaz) { - return foo + bar + zaz; - } - return bar; + "function doFoo(foo) { + function doBar(bar) { + function doZaz(zaz) { + return foo + bar + zaz; } - return foo; + return bar; } - ", + return foo; + }", None, ), + ("for (let foo = 0; foo < 1; foo++) { function doBar(bar) { return bar + foo; } }", None), ( - " - for (let foo = 0; foo < 1; foo++) { - function doBar(bar) { - return bar + foo; - } + "let foo = 0; + function doFoo() { + foo = 1; + function doBar(bar) { + return foo + bar; } - ", - None, - ), - ( - " - let foo = 0; - function doFoo() { - foo = 1; - function doBar(bar) { - return foo + bar; - } - return foo; - } - ", + return foo; + }", None, ), ("const doFoo = foo => { return foo; }", None), ("const doFoo = foo => bar => foo + bar;", None), ("const doFoo = () => { return bar => bar; } ", None), ( - " - const doFoo = foo => { - const doBar = bar => { - return foo + bar; - } - return foo; + "const doFoo = foo => { + const doBar = bar => { + return foo + bar; } - ", + return foo; + }", None, ), ( - " - function doFoo() { - { - const foo = 'foo'; - function doBar(bar) { - return bar + foo; - } + "function doFoo() { + { + const foo = 'foo'; + function doBar(bar) { + return bar + foo; } } - ", + }", None, ), ( - " - function doFoo(foo) { - function doBar(bar) { - foo.bar = bar; - } - function doZaz(zaz) { - doBar(zaz); - } - - doZaz('zaz'); - }; - ", + "function doFoo(foo) { + function doBar(bar) { + foo.bar = bar; + } + function doZaz(zaz) { + doBar(zaz); + } + + doZaz('zaz'); + };", None, ), ("function doFoo() { return function doBar() {}; }", None), - ( - " - function doFoo(Foo) { - function doBar() { - return new Foo(); - } - return doBar; - }; - ", - None, - ), + ("function doFoo(Foo) { function doBar() { return new Foo(); } return doBar; };", None), ("function doFoo(FooComponent) { return ; } ", None), ("const foo = ;", None), ("function foo() { function bar() { return ; } }", None), ("function doFoo(Foo) { const doBar = () => this; return doBar(); };", None), ("function doFoo(Foo) { const doBar = () => () => this; return doBar(); };", None), - ( - " - function doFoo(Foo) { - const doBar = () => () => () => this; - return doBar(); - }; - ", - None, - ), + ("function doFoo(Foo) { const doBar = () => () => () => this; return doBar(); };", None), ("useEffect(() => { function foo() {} }, []) ", None), ("React.useEffect(() => { function foo() {} }, [])", None), ("(function() { function bar() {} })();", None), ("(function() { function bar() {} }());", None), - (" !function() { function bar() {} }();", None), + ("!function() { function bar() {} }();", None), ("(() => { function bar() {} })();", None), ("(async function() { function bar() {} })();", None), (" (async function * () { function bar() {} })();", None), + ("function doFoo() { const doBar = (function(bar) { return bar; })(); }", None), ( - " - function doFoo() { - const doBar = (function(bar) { - return bar; - })(); + "const enrichErrors = (packageName, cliArgs, f) => async (...args) => { + try { + return await f(...args); + } catch (error) { + error.packageName = packageName; + error.cliArgs = cliArgs; + throw error; } - ", - None, - ), - ( - " - const enrichErrors = (packageName, cliArgs, f) => async (...args) => { - try { - return await f(...args); - } catch (error) { - error.packageName = packageName; - error.cliArgs = cliArgs; - throw error; - } - }; - ", + };", None, ), ( - " - export const canStepForward = ([X, Y]) => ([x, y]) => direction => { - switch (direction) { - case 0: - return y !== 0 - case 1: - return x !== X - 1 - case 2: - return y !== Y - 1 - case 3: - return x !== 0 - default: - throw new Error('unknown direction') - } + "export const canStepForward = ([X, Y]) => ([x, y]) => direction => { + switch (direction) { + case 0: + return y !== 0 + case 1: + return x !== X - 1 + case 2: + return y !== Y - 1 + case 3: + return x !== 0 + default: + throw new Error('unknown direction') } - ", + }", None, ), ( " - 'use strict'; - - module.exports = function recordErrors(eventEmitter, stateArgument) { - const stateVariable = stateArgument; - function onError(error) { - stateVariable.inputError = error; - } - eventEmitter.once('error', onError); - }; - ", + 'use strict'; + module.exports = function recordErrors(eventEmitter, stateArgument) { + const stateVariable = stateArgument; + function onError(error) { + stateVariable.inputError = error; + } + eventEmitter.once('error', onError); + };", None, ), ( - " - module.exports = function recordErrors(eventEmitter, stateArgument) { - function onError(error) { - stateArgument.inputError = error; - } - function onError2(error) { - onError(error); - } + "module.exports = function recordErrors(eventEmitter, stateArgument) { + function onError(error) { + stateArgument.inputError = error; + } + function onError2(error) { + onError(error); + } - eventEmitter.once('error', onError2); - }; - ", + eventEmitter.once('error', onError2); + };", None, ), ( - " - function outer(stream) { - let content; + "function outer(stream) { + let content; - function inner() { - process.stdout.write(content); - } - - inner(); + function inner() { + process.stdout.write(content); } - ", + + inner(); + }", None, ), ( "function outer () { const inner = () => {} }", - Some(serde_json::json!([{"checkArrowFunctions": false}])), + Some(serde_json::json!([{ "checkArrowFunctions": false }])), ), ( " @@ -550,48 +524,44 @@ fn test() { type Method = 'info' | 'error' export function createLogger(name: string) { - // Two lint errors are on the next line. - const log = (method: T) => (data: Data) => { - try { - // eslint-disable-next-line no-console - console[method](JSON.stringify({ name, data })) - } catch (error) { - console.error(error) - } - } - - return { - info: log('info'), - error: log('error'), - } + // Two lint errors are on the next line. + const log = (method: T) => (data: Data) => { + try { + // eslint-disable-next-line no-console + console[method](JSON.stringify({ name, data })) + } catch (error) { + console.error(error) + } + } + + return { + info: log('info'), + error: log('error'), + } } ", None, ), ( - " - test('it works', async function(assert) { - function assertHeader(assertions) { - for (const [key, value] of Object.entries(assertions)) { - assert.strictEqual( - native[key], - value - ); - } + "test('it works', async function(assert) { + function assertHeader(assertions) { + for (const [key, value] of Object.entries(assertions)) { + assert.strictEqual( + native[key], + value + ); } + } - // ... - }); - ", + // ... + });", None, ), ( - " - export function a(x: number) { - const b = (y: number) => (z: number): number => x + y + z; - return b(1)(2); - } - ", + "export function a(x: number) { + const b = (y: number) => (z: number): number => x + y + z; + return b(1)(2); + }", None, ), // https://github.com/oxc-project/oxc/pull/4948#issuecomment-2295819822 @@ -605,192 +575,118 @@ fn test() { // declared function is inside a block statement ( - " - function doFoo(foo) { - { - function doBar(bar) { - return bar; - } + "function doFoo(foo) { + { + function doBar(bar) { + return bar; } - return foo; } - ", + return foo; + }", None, ), ( - " - function doFoo(FooComponent) { - function Bar() { - return ; - } - return Bar; - }; - ", - None, - ), - ( - " - function Foo() { - function Bar () { - return
- } - return
{ Bar() }
+ "function doFoo(FooComponent) { + function Bar() { + return ; } - ", + return Bar; + };", None, ), ( - " - function foo() { - function bar() { - return ; - } + "function Foo() { + function Bar () { + return
} - ", - None, - ), - ( - " - function doFoo(Foo) { - const doBar = () => arguments; - return doBar(); - }; - ", + return
{ Bar() }
+ }", None, ), + ("function foo() { function bar() { return ; } }", None), + ("function doFoo(Foo) { const doBar = () => arguments; return doBar(); };", None), // end of cases that eslint-plugin-unicorn passes, but we fail. ( - " - function doFoo(foo) { - function doBar(bar) { - return bar; - } - return foo; + "function doFoo(foo) { + function doBar(bar) { + return bar; } - ", + return foo; + }", None, ), ( - " - function doFoo() { - const foo = 'foo'; - function doBar(bar) { - return bar; - } - return foo; + "function doFoo() { + const foo = 'foo'; + function doBar(bar) { + return bar; } - ", + return foo; + }", None, ), ("function doFoo() { function doBar(bar) { return bar; } }", None), ("const doFoo = function() { function doBar(bar) { return bar; } };", None), ( - " - const doFoo = function() { - const doBar = function(bar) { - return bar; - }; + "const doFoo = function() { + const doBar = function(bar) { + return bar; }; - ", + };", None, ), ("function doFoo() { const doBar = function(bar) { return bar; }; }", None), ("function doFoo() { const doBar = function(bar) { return bar; }; doBar(); }", None), ("const doFoo = () => { const doBar = bar => { return bar; } }", None), - ( - " - function doFoo(Foo) { - function doBar() { - return this; - } - return doBar(); - }; - ", - None, - ), + ("function doFoo(Foo) { function doBar() { return this; } return doBar(); };", None), ( "function doFoo(Foo) { const doBar = () => (function() {return this})(); return doBar(); };", None, ), ( - " - function doFoo(Foo) { - const doBar = () => (function() {return () => this})(); - return doBar(); - }; - ", + "function doFoo(Foo) { + const doBar = () => (function() {return () => this})(); + return doBar(); + };", None, ), ( - " - function doFoo(Foo) { - function doBar() { - return arguments; - } - return doBar(); - }; - ", - None, - ), - ( - " - function doFoo(Foo) { - const doBar = () => (function() {return arguments})(); - return doBar(); - }; - ", - None, - ), - ( - " - function doFoo(foo) { - function doBar(bar) { - return doBar(bar); - } - return foo; + "function doFoo(Foo) { + function doBar() { + return arguments; } - ", + return doBar(); + };", None, ), ( - " - function doFoo(foo) { - function doBar(bar) { - return bar; - } - return doBar; - } - ", + "function doFoo(Foo) { + const doBar = () => (function() {return arguments})(); + return doBar(); + };", None, ), - ("function doFoo() { function doBar() {} }", None), ( - " - function doFoo(foo) { - { - { - function doBar(bar) { - return bar; - } - } - } - return foo; + "function doFoo(foo) { + function doBar(bar) { + return doBar(bar); } - ", + return foo; + }", None, ), ( - " - { - { - function doBar(bar) { - return bar; - } - } + "function doFoo(foo) { + function doBar(bar) { + return bar; } - ", + return doBar; + }", None, ), + ("function doFoo() { function doBar() {} }", None), + ("function doFoo(foo) { { { function doBar(bar) { return bar; } } } return foo; }", None), + ("{ { function doBar(bar) { return bar; } } }", None), ("for (let foo = 0; foo < 1; foo++) { function doBar(bar) { return bar; } }", None), ("function foo() { function bar() {} }", None), ("function foo() { async function bar() {} }", None), @@ -801,115 +697,99 @@ fn test() { ("function foo() { const bar = async () => {} }", None), ("function foo() { async function* baz() {} }", None), ( - " - useEffect(() => { - function foo() { - function bar() { - } - } - }, []) - ", - None, - ), - ( - " - (function() { - function foo() { - function bar() { - } + "useEffect(() => { + function foo() { + function bar() { } - })(); - ", + } + }, [])", None, ), ( - " - process.nextTick(() => { - function returnsZero() { - return true; + "(function() { + function foo() { + function bar() { } - process.exitCode = returnsZero(); - }); - ", + } + })();", None, ), ( - " - foo( - // This is not IIFE - function() { - function bar() { - } - }, - // This is IIFE - (function() { - function baz() { - } - })(), - ) - ", + "process.nextTick(() => { + function returnsZero() { + return true; + } + process.exitCode = returnsZero(); + });", None, ), ( - " + "foo( + // This is not an IIFE + function() { + function bar() { + } + }, // This is an IIFE (function() { - function bar() { + function baz() { } - })( - // This is not IIFE - function() { - function baz() { - } - }, - ) - ", + })(), + )", None, ), ( - " - function Foo() { - const Bar =
- function doBaz() { - return 42 - } - return
{ doBaz() }
+ "// This is an IIFE + (function() { + function bar() { } - ", + })( + // This is not IIFE + function() { + function baz() { + } + }, + )", None, ), ( - " - function Foo() { - function Bar () { - return
- } - function doBaz() { - return 42 - } - return
{ doBaz() }
+ "function Foo() { + const Bar =
+ function doBaz() { + return 42 } - ", + return
{ doBaz() }
+ }", None, ), ( - " - function fn1() { - function a() { - return ; - } - function b() {} - function c() {} + "function Foo() { + function Bar () { + return
} - function fn2() { - function foo() {} + function doBaz() { + return 42 } - ", + return
{ doBaz() }
+ }", + None, + ), + ( + "function fn1() { + function a() { + return ; + } + function b() {} + function c() {} + } + function fn2() { + function foo() {} + }", None, ), ( "const outer = () => { function inner() {} }", - Some(serde_json::json!([{"checkArrowFunctions": false}])), + Some(serde_json::json!([{ "checkArrowFunctions": false }])), ), ("function foo() { function bar() {} }", None), ("function foo() { async function bar() {} }", None), @@ -918,16 +798,7 @@ fn test() { ("function foo() { const bar = () => {} }", None), // ("const doFoo = () => bar => bar;", None), ("function foo() { const bar = async () => {} }", None), - ( - " - function doFoo() { - const doBar = function(bar) { - return bar; - }; - } - ", - None, - ), + ("function doFoo() { const doBar = function(bar) { return bar; }; }", None), ]; Tester::new(ConsistentFunctionScoping::NAME, pass, fail).test_and_snapshot(); diff --git a/crates/oxc_linter/src/snapshots/consistent_function_scoping.snap b/crates/oxc_linter/src/snapshots/consistent_function_scoping.snap index defa93f85b883..d8d6c502511f4 100644 --- a/crates/oxc_linter/src/snapshots/consistent_function_scoping.snap +++ b/crates/oxc_linter/src/snapshots/consistent_function_scoping.snap @@ -2,65 +2,61 @@ source: crates/oxc_linter/src/tester.rs --- ⚠ eslint-plugin-unicorn(consistent-function-scoping): Function does not capture any variables from the outer scope. - ╭─[consistent_function_scoping.tsx:4:34] - 3 │ { - 4 │ function doBar(bar) { - · ───── - 5 │ return bar; + ╭─[consistent_function_scoping.tsx:3:30] + 2 │ { + 3 │ function doBar(bar) { + · ───── + 4 │ return bar; ╰──── help: Move this function to the outer scope. ⚠ eslint-plugin-unicorn(consistent-function-scoping): Function does not capture any variables from the outer scope. - ╭─[consistent_function_scoping.tsx:3:30] - 2 │ function doFoo(FooComponent) { - 3 │ function Bar() { - · ─── - 4 │ return ; + ╭─[consistent_function_scoping.tsx:2:26] + 1 │ function doFoo(FooComponent) { + 2 │ function Bar() { + · ─── + 3 │ return ; ╰──── help: Move this function to the outer scope. ⚠ eslint-plugin-unicorn(consistent-function-scoping): Function does not capture any variables from the outer scope. - ╭─[consistent_function_scoping.tsx:3:30] - 2 │ function Foo() { - 3 │ function Bar () { - · ─── - 4 │ return
+ ╭─[consistent_function_scoping.tsx:2:26] + 1 │ function Foo() { + 2 │ function Bar () { + · ─── + 3 │ return
╰──── help: Move this function to the outer scope. ⚠ eslint-plugin-unicorn(consistent-function-scoping): Function does not capture any variables from the outer scope. - ╭─[consistent_function_scoping.tsx:3:30] - 2 │ function foo() { - 3 │ function bar() { - · ─── - 4 │ return ; + ╭─[consistent_function_scoping.tsx:1:27] + 1 │ function foo() { function bar() { return ; } } + · ─── ╰──── help: Move this function to the outer scope. ⚠ eslint-plugin-unicorn(consistent-function-scoping): Function does not capture any variables from the outer scope. - ╭─[consistent_function_scoping.tsx:3:27] - 2 │ function doFoo(Foo) { - 3 │ const doBar = () => arguments; - · ───── - 4 │ return doBar(); + ╭─[consistent_function_scoping.tsx:1:29] + 1 │ function doFoo(Foo) { const doBar = () => arguments; return doBar(); }; + · ───── ╰──── help: Move this function to the outer scope. ⚠ eslint-plugin-unicorn(consistent-function-scoping): Function does not capture any variables from the outer scope. - ╭─[consistent_function_scoping.tsx:3:30] - 2 │ function doFoo(foo) { - 3 │ function doBar(bar) { - · ───── - 4 │ return bar; + ╭─[consistent_function_scoping.tsx:2:26] + 1 │ function doFoo(foo) { + 2 │ function doBar(bar) { + · ───── + 3 │ return bar; ╰──── help: Move this function to the outer scope. ⚠ eslint-plugin-unicorn(consistent-function-scoping): Function does not capture any variables from the outer scope. - ╭─[consistent_function_scoping.tsx:4:30] - 3 │ const foo = 'foo'; - 4 │ function doBar(bar) { - · ───── - 5 │ return bar; + ╭─[consistent_function_scoping.tsx:3:26] + 2 │ const foo = 'foo'; + 3 │ function doBar(bar) { + · ───── + 4 │ return bar; ╰──── help: Move this function to the outer scope. @@ -79,11 +75,11 @@ source: crates/oxc_linter/src/tester.rs help: Move this function to the outer scope. ⚠ eslint-plugin-unicorn(consistent-function-scoping): Function does not capture any variables from the outer scope. - ╭─[consistent_function_scoping.tsx:3:35] - 2 │ const doFoo = function() { - 3 │ const doBar = function(bar) { - · ──────── - 4 │ return bar; + ╭─[consistent_function_scoping.tsx:2:31] + 1 │ const doFoo = function() { + 2 │ const doBar = function(bar) { + · ──────── + 3 │ return bar; ╰──── help: Move this function to the outer scope. @@ -109,11 +105,9 @@ source: crates/oxc_linter/src/tester.rs help: Move this function to the outer scope. ⚠ eslint-plugin-unicorn(consistent-function-scoping): Function does not capture any variables from the outer scope. - ╭─[consistent_function_scoping.tsx:3:30] - 2 │ function doFoo(Foo) { - 3 │ function doBar() { - · ───── - 4 │ return this; + ╭─[consistent_function_scoping.tsx:1:32] + 1 │ function doFoo(Foo) { function doBar() { return this; } return doBar(); }; + · ───── ╰──── help: Move this function to the outer scope. @@ -125,47 +119,47 @@ source: crates/oxc_linter/src/tester.rs help: Move this function to the outer scope. ⚠ eslint-plugin-unicorn(consistent-function-scoping): Function does not capture any variables from the outer scope. - ╭─[consistent_function_scoping.tsx:3:27] - 2 │ function doFoo(Foo) { - 3 │ const doBar = () => (function() {return () => this})(); - · ───── - 4 │ return doBar(); + ╭─[consistent_function_scoping.tsx:2:23] + 1 │ function doFoo(Foo) { + 2 │ const doBar = () => (function() {return () => this})(); + · ───── + 3 │ return doBar(); ╰──── help: Move this function to the outer scope. ⚠ eslint-plugin-unicorn(consistent-function-scoping): Function does not capture any variables from the outer scope. - ╭─[consistent_function_scoping.tsx:3:30] - 2 │ function doFoo(Foo) { - 3 │ function doBar() { - · ───── - 4 │ return arguments; + ╭─[consistent_function_scoping.tsx:2:26] + 1 │ function doFoo(Foo) { + 2 │ function doBar() { + · ───── + 3 │ return arguments; ╰──── help: Move this function to the outer scope. ⚠ eslint-plugin-unicorn(consistent-function-scoping): Function does not capture any variables from the outer scope. - ╭─[consistent_function_scoping.tsx:3:27] - 2 │ function doFoo(Foo) { - 3 │ const doBar = () => (function() {return arguments})(); - · ───── - 4 │ return doBar(); + ╭─[consistent_function_scoping.tsx:2:23] + 1 │ function doFoo(Foo) { + 2 │ const doBar = () => (function() {return arguments})(); + · ───── + 3 │ return doBar(); ╰──── help: Move this function to the outer scope. ⚠ eslint-plugin-unicorn(consistent-function-scoping): Function does not capture any variables from the outer scope. - ╭─[consistent_function_scoping.tsx:3:30] - 2 │ function doFoo(foo) { - 3 │ function doBar(bar) { - · ───── - 4 │ return doBar(bar); + ╭─[consistent_function_scoping.tsx:2:26] + 1 │ function doFoo(foo) { + 2 │ function doBar(bar) { + · ───── + 3 │ return doBar(bar); ╰──── help: Move this function to the outer scope. ⚠ eslint-plugin-unicorn(consistent-function-scoping): Function does not capture any variables from the outer scope. - ╭─[consistent_function_scoping.tsx:3:30] - 2 │ function doFoo(foo) { - 3 │ function doBar(bar) { - · ───── - 4 │ return bar; + ╭─[consistent_function_scoping.tsx:2:26] + 1 │ function doFoo(foo) { + 2 │ function doBar(bar) { + · ───── + 3 │ return bar; ╰──── help: Move this function to the outer scope. @@ -177,20 +171,16 @@ source: crates/oxc_linter/src/tester.rs help: Move this function to the outer scope. ⚠ eslint-plugin-unicorn(consistent-function-scoping): Function does not capture any variables from the outer scope. - ╭─[consistent_function_scoping.tsx:5:38] - 4 │ { - 5 │ function doBar(bar) { - · ───── - 6 │ return bar; + ╭─[consistent_function_scoping.tsx:1:36] + 1 │ function doFoo(foo) { { { function doBar(bar) { return bar; } } } return foo; } + · ───── ╰──── help: Move this function to the outer scope. ⚠ eslint-plugin-unicorn(consistent-function-scoping): Function does not capture any variables from the outer scope. - ╭─[consistent_function_scoping.tsx:4:34] - 3 │ { - 4 │ function doBar(bar) { - · ───── - 5 │ return bar; + ╭─[consistent_function_scoping.tsx:1:14] + 1 │ { { function doBar(bar) { return bar; } } } + · ───── ╰──── help: Move this function to the outer scope. @@ -251,101 +241,101 @@ source: crates/oxc_linter/src/tester.rs help: Move this function to the outer scope. ⚠ eslint-plugin-unicorn(consistent-function-scoping): Function does not capture any variables from the outer scope. - ╭─[consistent_function_scoping.tsx:4:34] - 3 │ function foo() { - 4 │ function bar() { - · ─── - 5 │ } + ╭─[consistent_function_scoping.tsx:3:30] + 2 │ function foo() { + 3 │ function bar() { + · ─── + 4 │ } ╰──── help: Move this function to the outer scope. ⚠ eslint-plugin-unicorn(consistent-function-scoping): Function does not capture any variables from the outer scope. - ╭─[consistent_function_scoping.tsx:4:34] - 3 │ function foo() { - 4 │ function bar() { - · ─── - 5 │ } + ╭─[consistent_function_scoping.tsx:3:30] + 2 │ function foo() { + 3 │ function bar() { + · ─── + 4 │ } ╰──── help: Move this function to the outer scope. ⚠ eslint-plugin-unicorn(consistent-function-scoping): Function does not capture any variables from the outer scope. - ╭─[consistent_function_scoping.tsx:3:30] - 2 │ process.nextTick(() => { - 3 │ function returnsZero() { - · ─────────── - 4 │ return true; + ╭─[consistent_function_scoping.tsx:2:26] + 1 │ process.nextTick(() => { + 2 │ function returnsZero() { + · ─────────── + 3 │ return true; ╰──── help: Move this function to the outer scope. ⚠ eslint-plugin-unicorn(consistent-function-scoping): Function does not capture any variables from the outer scope. - ╭─[consistent_function_scoping.tsx:5:34] - 4 │ function() { - 5 │ function bar() { - · ─── - 6 │ } + ╭─[consistent_function_scoping.tsx:4:30] + 3 │ function() { + 4 │ function bar() { + · ─── + 5 │ } ╰──── help: Move this function to the outer scope. ⚠ eslint-plugin-unicorn(consistent-function-scoping): Function does not capture any variables from the outer scope. - ╭─[consistent_function_scoping.tsx:9:34] - 8 │ function() { - 9 │ function baz() { - · ─── - 10 │ } - ╰──── + ╭─[consistent_function_scoping.tsx:8:30] + 7 │ function() { + 8 │ function baz() { + · ─── + 9 │ } + ╰──── help: Move this function to the outer scope. ⚠ eslint-plugin-unicorn(consistent-function-scoping): Function does not capture any variables from the outer scope. - ╭─[consistent_function_scoping.tsx:4:30] - 3 │ const Bar =
- 4 │ function doBaz() { - · ───── - 5 │ return 42 + ╭─[consistent_function_scoping.tsx:3:26] + 2 │ const Bar =
+ 3 │ function doBaz() { + · ───── + 4 │ return 42 ╰──── help: Move this function to the outer scope. ⚠ eslint-plugin-unicorn(consistent-function-scoping): Function does not capture any variables from the outer scope. - ╭─[consistent_function_scoping.tsx:3:30] - 2 │ function Foo() { - 3 │ function Bar () { - · ─── - 4 │ return
+ ╭─[consistent_function_scoping.tsx:2:26] + 1 │ function Foo() { + 2 │ function Bar () { + · ─── + 3 │ return
╰──── help: Move this function to the outer scope. ⚠ eslint-plugin-unicorn(consistent-function-scoping): Function does not capture any variables from the outer scope. - ╭─[consistent_function_scoping.tsx:6:30] - 5 │ } - 6 │ function doBaz() { - · ───── - 7 │ return 42 + ╭─[consistent_function_scoping.tsx:5:26] + 4 │ } + 5 │ function doBaz() { + · ───── + 6 │ return 42 ╰──── help: Move this function to the outer scope. ⚠ eslint-plugin-unicorn(consistent-function-scoping): Function does not capture any variables from the outer scope. - ╭─[consistent_function_scoping.tsx:6:30] - 5 │ } - 6 │ function b() {} - · ─ - 7 │ function c() {} + ╭─[consistent_function_scoping.tsx:5:26] + 4 │ } + 5 │ function b() {} + · ─ + 6 │ function c() {} ╰──── help: Move this function to the outer scope. ⚠ eslint-plugin-unicorn(consistent-function-scoping): Function does not capture any variables from the outer scope. - ╭─[consistent_function_scoping.tsx:7:30] - 6 │ function b() {} - 7 │ function c() {} - · ─ - 8 │ } + ╭─[consistent_function_scoping.tsx:6:26] + 5 │ function b() {} + 6 │ function c() {} + · ─ + 7 │ } ╰──── help: Move this function to the outer scope. ⚠ eslint-plugin-unicorn(consistent-function-scoping): Function does not capture any variables from the outer scope. - ╭─[consistent_function_scoping.tsx:10:30] - 9 │ function fn2() { - 10 │ function foo() {} - · ─── - 11 │ } + ╭─[consistent_function_scoping.tsx:9:26] + 8 │ function fn2() { + 9 │ function foo() {} + · ─── + 10 │ } ╰──── help: Move this function to the outer scope. @@ -399,10 +389,8 @@ source: crates/oxc_linter/src/tester.rs help: Move this function to the outer scope. ⚠ eslint-plugin-unicorn(consistent-function-scoping): Function does not capture any variables from the outer scope. - ╭─[consistent_function_scoping.tsx:3:35] - 2 │ function doFoo() { - 3 │ const doBar = function(bar) { - · ──────── - 4 │ return bar; + ╭─[consistent_function_scoping.tsx:1:34] + 1 │ function doFoo() { const doBar = function(bar) { return bar; }; } + · ──────── ╰──── help: Move this function to the outer scope.