Skip to content
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
15 changes: 15 additions & 0 deletions crates/oxc_linter/src/rules/eslint/no_unused_vars/allowed.rs
Original file line number Diff line number Diff line change
Expand Up @@ -294,4 +294,19 @@ impl NoUnusedVars {
_ => false,
}
}

/// Returns `true` if this binding rest element should be allowed (i.e. not
/// reported). Currently, this handles the case where a rest element is part
/// of a TS function declaration.
pub(super) fn is_allowed_binding_rest_element(symbol: &Symbol) -> bool {
for parent in symbol.iter_parents() {
// If this is a binding rest element that is part of a TS function parameter,
// for example: `function foo(...messages: string[]) {}`, then we will allow it.
if let AstKind::Function(f) = parent.kind() {
return f.is_typescript_syntax();
}
}

false
}
}
6 changes: 6 additions & 0 deletions crates/oxc_linter/src/rules/eslint/no_unused_vars/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -297,6 +297,12 @@ impl NoUnusedVars {
}
ctx.diagnostic(diagnostic::param(symbol));
}
AstKind::BindingRestElement(_) => {
if NoUnusedVars::is_allowed_binding_rest_element(symbol) {
return;
}
ctx.diagnostic(diagnostic::declared(symbol));
}
AstKind::Class(_) | AstKind::Function(_) => {
if self.is_allowed_class_or_function(symbol) {
return;
Expand Down
19 changes: 19 additions & 0 deletions crates/oxc_linter/src/rules/eslint/no_unused_vars/tests/oxc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -495,12 +495,31 @@ fn test_functions() {
",
"const foo = () => function bar() { }\nfoo()",
"module.exports.foo = () => function bar() { }",
// https://github.com/oxc-project/oxc/issues/5406
"
export function log(message: string, ...interpolations: unknown[]): void;
export function log(message: string, ...interpolations: unknown[]): void {
console.log(message, interpolations);
}
",
"declare function func(strings: any, ...values: any[]): object"
];

let fail = vec![
"function foo() {}",
"function foo() { foo() }",
"const foo = () => { function bar() { } }\nfoo()",
"
export function log(message: string, ...interpolations: unknown[]): void;
export function log(message: string, ...interpolations: unknown[]): void {
console.log(message);
}
",
"
export function log(...messages: unknown[]): void {
return;
}
",
];

let fix = vec![
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,23 @@ source: crates/oxc_linter/src/tester.rs
2 │ foo()
╰────
help: Consider removing this declaration.

⚠ eslint(no-unused-vars): Variable 'interpolations' is declared but never used.
╭─[no_unused_vars.tsx:3:46]
2 │ export function log(message: string, ...interpolations: unknown[]): void;
3 │ export function log(message: string, ...interpolations: unknown[]): void {
· ──────────────┬─────────────
· ╰── 'interpolations' is declared here
4 │ console.log(message);
╰────
help: Consider removing this declaration.

⚠ eslint(no-unused-vars): Variable 'messages' is declared but never used.
╭─[no_unused_vars.tsx:2:29]
1 │
2 │ export function log(...messages: unknown[]): void {
· ───────────┬──────────
· ╰── 'messages' is declared here
3 │ return;
╰────
help: Consider removing this declaration.