fix(linter): handle loops in getter-return rule (#5517)

- Fixes https://github.com/oxc-project/oxc/issues/3935

Prior to this, it didn't seem like we were correctly continuing to
search the graph after we encountered a backedge (loop). Now, when
encountering a cycle, we will not automatically break or prune the
search.
This commit is contained in:
Cam McHenry 2024-09-05 22:59:04 -04:00 committed by GitHub
parent e8bdd12438
commit 088733b3c8
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 82 additions and 0 deletions

View file

@ -257,6 +257,7 @@ impl GetterReturn {
e.weight(),
EdgeType::Jump
| EdgeType::Normal
| EdgeType::Backedge
| EdgeType::Error(ErrorEdgeKind::Explicit)
)
}) {
@ -351,6 +352,45 @@ 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 +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;
}
}
}", None),
("
var foo = {
get bar() {
let i = 0;
while (i < 10) {
return i;
}
}
}", None),
];
Tester::new(GetterReturn::NAME, pass, fail)

View file

@ -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: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.