Skip to content

Commit

Permalink
fix(linter): handle loops in getter-return rule (#5517)
Browse files Browse the repository at this point in the history
- Fixes #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.
  • Loading branch information
camchenry authored Sep 6, 2024
1 parent e8bdd12 commit 088733b
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 0 deletions.
57 changes: 57 additions & 0 deletions crates/oxc_linter/src/rules/eslint/getter_return.rs
Original file line number Diff line number Diff line change
Expand Up @@ -257,6 +257,7 @@ impl GetterReturn {
e.weight(),
EdgeType::Jump
| EdgeType::Normal
| EdgeType::Backedge
| EdgeType::Error(ErrorEdgeKind::Explicit)
)
}) {
Expand Down Expand Up @@ -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![
Expand Down Expand Up @@ -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)
Expand Down
25 changes: 25 additions & 0 deletions crates/oxc_linter/src/snapshots/getter_return.snap
Original file line number Diff line number Diff line change
Expand Up @@ -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]
2var 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]
2var 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.

0 comments on commit 088733b

Please sign in to comment.