Skip to content
This repository has been archived by the owner on Aug 31, 2023. It is now read-only.

feat(rome_js_analyze): no unused variables supporting ts overload #3135

Merged
merged 1 commit into from
Aug 30, 2022
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
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,15 @@ declare_rule! {
/// };
/// foo();
/// ```
///
/// ```ts
/// function used_overloaded(): number;
/// function used_overloaded(s: string): string;
/// function used_overloaded(s?: string) {
/// return s;
/// }
/// used_overloaded();
/// ```
pub(crate) NoUnusedVariables {
version: "0.9.0",
name: "noUnusedVariables",
Expand All @@ -96,7 +105,8 @@ fn is_typescript_unused_ok(binding: &JsIdentifierBinding) -> Option<()> {
JsSyntaxKind::TS_METHOD_SIGNATURE_CLASS_MEMBER
| JsSyntaxKind::TS_CALL_SIGNATURE_TYPE_MEMBER
| JsSyntaxKind::TS_METHOD_SIGNATURE_TYPE_MEMBER
| JsSyntaxKind::TS_FUNCTION_TYPE => Some(()),
| JsSyntaxKind::TS_FUNCTION_TYPE
| JsSyntaxKind::TS_DECLARE_FUNCTION_DECLARATION => Some(()),
_ => None,
}
}
Expand All @@ -115,6 +125,7 @@ fn is_typescript_unused_ok(binding: &JsIdentifierBinding) -> Option<()> {
}
}
JsSyntaxKind::TS_INDEX_SIGNATURE_PARAMETER => Some(()),
JsSyntaxKind::TS_DECLARE_FUNCTION_DECLARATION => Some(()),
_ => None,
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,12 @@ class D {
}
console.log(new D());

function unused_overloaded(): number;
function unused_overloaded(s: string): string;
function unused_overloaded(s?: string) {
return s;
}

// Valid

interface A {
Expand Down Expand Up @@ -35,3 +41,10 @@ function f(fn: (title: string) => boolean) {
f();

export type Command = (...args: any[]) => unknown;

function used_overloaded(): number;
function used_overloaded(s: string): string;
function used_overloaded(s?: string) {
return s;
}
used_overloaded();
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,12 @@ class D {
}
console.log(new D());

function unused_overloaded(): number;
function unused_overloaded(s: string): string;
function unused_overloaded(s?: string) {
return s;
}

// Valid

interface A {
Expand Down Expand Up @@ -42,6 +48,13 @@ f();

export type Command = (...args: any[]) => unknown;

function used_overloaded(): number;
function used_overloaded(s: string): string;
function used_overloaded(s?: string) {
return s;
}
used_overloaded();

```

# Diagnostics
Expand Down Expand Up @@ -81,4 +94,16 @@ warning[js/noUnusedVariables]: This parameter is unused.

```

```
warning[js/noUnusedVariables]: This function is unused.
┌─ noUnusedVariables.ts:12:10
12 │ function unused_overloaded(s?: string) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe this diagnostic should not be here? If I understood the purpose of the PR correctly...

Copy link
Contributor Author

@xunilrj xunilrj Aug 30, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This one is fine because all three overloaded functions are not used. And we flag only the one with a body.

│ -----------------

= note: Unused variables usually are result of incomplete refactoring, typos and other source of bugs.


```


6 changes: 1 addition & 5 deletions crates/rome_js_semantic/src/tests/assertions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,11 +101,7 @@ use std::collections::{BTreeMap, HashMap};
/// if(true) ;/*NOEVENT*/;
/// ```
pub fn assert(code: &str, test_name: &str) {
let r = rome_js_parser::parse(
code,
0,
SourceType::js_module().with_variant(rome_js_syntax::LanguageVariant::Jsx),
);
let r = rome_js_parser::parse(code, 0, SourceType::tsx());

if r.has_errors() {
let files = SimpleFile::new(test_name.to_string(), code.into());
Expand Down
7 changes: 7 additions & 0 deletions crates/rome_js_semantic/src/tests/references.rs
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,13 @@ function f({a/*#A*/} = {a: [b/*READ B2*/,c/*READ C*/,d/*READ D*/]}, b/*#B2*/) {
console.log(a/*READ A*/, b/*READ B3*/);
}
f()",
ok_function_overloading,
"function overloaded/*#A*/(): number;
function overloaded/*#B*/(s: string): string;
function overloaded/*#C*/(s?: string) {
return s;
}
overloaded/*READ C*/();",
}

// Imports
Expand Down
9 changes: 9 additions & 0 deletions website/src/docs/lint/rules/noUnusedVariables.md

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.