From 919d424a70c796ea95e49f65328af15db4437a23 Mon Sep 17 00:00:00 2001 From: Cameron McHenry Date: Thu, 5 Sep 2024 18:50:47 -0400 Subject: [PATCH 1/3] Continue walking graph after encountering loops --- .../src/rules/eslint/getter_return.rs | 77 +++++++++++++++++++ .../src/snapshots/getter_return.snap | 25 ++++++ 2 files changed, 102 insertions(+) diff --git a/crates/oxc_linter/src/rules/eslint/getter_return.rs b/crates/oxc_linter/src/rules/eslint/getter_return.rs index 45ceafceb6f58..81aa3a1c5ccd4 100644 --- a/crates/oxc_linter/src/rules/eslint/getter_return.rs +++ b/crates/oxc_linter/src/rules/eslint/getter_return.rs @@ -257,6 +257,7 @@ impl GetterReturn { e.weight(), EdgeType::Jump | EdgeType::Normal + | EdgeType::Backedge | EdgeType::Error(ErrorEdgeKind::Explicit) ) }) { @@ -351,6 +352,57 @@ fn test() { } }; ", None), + // adapted from: https://github.com/1024pix/pix/blob/1352bd8d7f6070f1ff8da79867f543c1c1926e59/mon-pix/app/components/progress-bar.js#L29-L43 + ( + " + export default class ProgressBar extends Component { + get steps() { + const steps = []; + + for (let i = 0; i < this.maxStepsNumber; i++) { + steps.push({ + stepnum: i + 1, + }); + } + + return steps; + } + } + ", + None, + ), + ( + " + var foo = { + get bar() { + for (let i = 0; i<10; i++) { + if (i === 5) { + return i; + } + } + return 0; + } + } + ", + None, + ), + ( + " + var foo = { + get bar() { + let i = 0; + while (i < 10) { + if (i === 5) { + return i; + } + i++; + } + return 0; + } + } + ", + None, + ), ]; let fail = vec![ @@ -426,6 +478,31 @@ fn test() { ), ("var foo = { get bar() { try { return a(); } catch {} } };", None), ("var foo = { get bar() { try { return a(); } catch { } finally { } } };", None), + ( + " + var foo = { + get bar() { + for (let i = 0; i<10; i++) { + return i; + } + } + } + ", + None, + ), + ( + " + var foo = { + get bar() { + let i = 0; + while (i < 10) { + return i; + } + } + } + ", + None, + ), ]; Tester::new(GetterReturn::NAME, pass, fail) diff --git a/crates/oxc_linter/src/snapshots/getter_return.snap b/crates/oxc_linter/src/snapshots/getter_return.snap index 512321bde7a44..87d367e3cbaf1 100644 --- a/crates/oxc_linter/src/snapshots/getter_return.snap +++ b/crates/oxc_linter/src/snapshots/getter_return.snap @@ -254,3 +254,28 @@ source: crates/oxc_linter/src/tester.rs · ────────────────────────────────────────────────── ╰──── help: Return a value from all code paths in getter. + + ⚠ eslint(getter-return): Expected to always return a value in getter. + ╭─[getter_return.js:3:24] + 2 │ var foo = { + 3 │ ╭─▶ get bar() { + 4 │ │ for (let i = 0; i<10; i++) { + 5 │ │ return i; + 6 │ │ } + 7 │ ╰─▶ } + 8 │ } + ╰──── + help: Return a value from all code paths in getter. + + ⚠ eslint(getter-return): Expected to always return a value in getter. + ╭─[getter_return.js:3:24] + 2 │ var foo = { + 3 │ ╭─▶ get bar() { + 4 │ │ let i = 0; + 5 │ │ while (i < 10) { + 6 │ │ return i; + 7 │ │ } + 8 │ ╰─▶ } + 9 │ } + ╰──── + help: Return a value from all code paths in getter. From 77e76ac31ac1de60d74b7fc3e49d3e82e60cc0e1 Mon Sep 17 00:00:00 2001 From: Cameron McHenry Date: Thu, 5 Sep 2024 21:07:15 -0400 Subject: [PATCH 2/3] Fix indent --- .../src/rules/eslint/getter_return.rs | 86 +++++++------------ 1 file changed, 33 insertions(+), 53 deletions(-) diff --git a/crates/oxc_linter/src/rules/eslint/getter_return.rs b/crates/oxc_linter/src/rules/eslint/getter_return.rs index 81aa3a1c5ccd4..3b1b4a6439d6c 100644 --- a/crates/oxc_linter/src/rules/eslint/getter_return.rs +++ b/crates/oxc_linter/src/rules/eslint/getter_return.rs @@ -353,8 +353,7 @@ fn test() { }; ", None), // adapted from: https://github.com/1024pix/pix/blob/1352bd8d7f6070f1ff8da79867f543c1c1926e59/mon-pix/app/components/progress-bar.js#L29-L43 - ( - " + (" export default class ProgressBar extends Component { get steps() { const steps = []; @@ -367,42 +366,31 @@ fn test() { return steps; } - } - ", - None, - ), - ( - " - var foo = { - get bar() { - for (let i = 0; i<10; i++) { - if (i === 5) { - return i; - } + }", None), + (" + var foo = { + get bar() { + for (let i = 0; i<10; i++) { + if (i === 5) { + return i; } - return 0; } + return 0; } - ", - None, - ), - ( - " - var foo = { - get bar() { - let i = 0; - while (i < 10) { - if (i === 5) { - return i; - } - i++; + }", None), + (" + var foo = { + get bar() { + let i = 0; + while (i < 10) { + if (i === 5) { + return i; } - return 0; + i++; } + return 0; } - ", - None, - ), + }", None), ]; let fail = vec![ @@ -478,31 +466,23 @@ fn test() { ), ("var foo = { get bar() { try { return a(); } catch {} } };", None), ("var foo = { get bar() { try { return a(); } catch { } finally { } } };", None), - ( - " - var foo = { - get bar() { - for (let i = 0; i<10; i++) { - return i; - } + (" + var foo = { + get bar() { + for (let i = 0; i<10; i++) { + return i; } } - ", - None, - ), - ( - " - var foo = { - get bar() { - let i = 0; - while (i < 10) { - return i; - } + }", None), + (" + var foo = { + get bar() { + let i = 0; + while (i < 10) { + return i; } } - ", - None, - ), + }", None), ]; Tester::new(GetterReturn::NAME, pass, fail) From cf482caeba7c6ba7858f8e5d591f31a24dcef81c Mon Sep 17 00:00:00 2001 From: Cameron McHenry Date: Thu, 5 Sep 2024 22:44:57 -0400 Subject: [PATCH 3/3] Update snapshots --- .../src/snapshots/getter_return.snap | 42 +++++++++---------- 1 file changed, 21 insertions(+), 21 deletions(-) diff --git a/crates/oxc_linter/src/snapshots/getter_return.snap b/crates/oxc_linter/src/snapshots/getter_return.snap index 87d367e3cbaf1..088647bfad4a6 100644 --- a/crates/oxc_linter/src/snapshots/getter_return.snap +++ b/crates/oxc_linter/src/snapshots/getter_return.snap @@ -256,26 +256,26 @@ source: crates/oxc_linter/src/tester.rs help: Return a value from all code paths in getter. ⚠ eslint(getter-return): Expected to always return a value in getter. - ╭─[getter_return.js:3:24] - 2 │ var foo = { - 3 │ ╭─▶ get bar() { - 4 │ │ for (let i = 0; i<10; i++) { - 5 │ │ return i; - 6 │ │ } - 7 │ ╰─▶ } - 8 │ } - ╰──── - help: Return a value from all code paths in getter. - - ⚠ eslint(getter-return): Expected to always return a value in getter. - ╭─[getter_return.js:3:24] - 2 │ var foo = { - 3 │ ╭─▶ get bar() { - 4 │ │ let i = 0; - 5 │ │ while (i < 10) { - 6 │ │ return i; - 7 │ │ } - 8 │ ╰─▶ } - 9 │ } + ╭─[getter_return.js:3:20] + 2 │ var foo = { + 3 │ ╭─▶ get bar() { + 4 │ │ for (let i = 0; i<10; i++) { + 5 │ │ return i; + 6 │ │ } + 7 │ ╰─▶ } + 8 │ } + ╰──── + help: Return a value from all code paths in getter. + + ⚠ eslint(getter-return): Expected to always return a value in getter. + ╭─[getter_return.js:3:20] + 2 │ var foo = { + 3 │ ╭─▶ get bar() { + 4 │ │ let i = 0; + 5 │ │ while (i < 10) { + 6 │ │ return i; + 7 │ │ } + 8 │ ╰─▶ } + 9 │ } ╰──── help: Return a value from all code paths in getter.