Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 4 commits into from
Sep 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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]
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.