diff --git a/crates/rome_js_analyze/src/semantic_analyzers/correctness/no_unused_variables.rs b/crates/rome_js_analyze/src/semantic_analyzers/correctness/no_unused_variables.rs index 3ae07c876790..b9f91b290d66 100644 --- a/crates/rome_js_analyze/src/semantic_analyzers/correctness/no_unused_variables.rs +++ b/crates/rome_js_analyze/src/semantic_analyzers/correctness/no_unused_variables.rs @@ -114,7 +114,9 @@ pub enum SuggestedFix { PrefixUnderscore, } -fn is_function_that_is_ok_parameter_not_be_used(parent_function: Option) -> bool { +fn is_function_that_is_ok_parameter_not_be_used( + parent_function: Option, +) -> bool { matches!( parent_function, Some( @@ -173,9 +175,11 @@ fn suggested_fix_if_unused(binding: &AnyJsIdentifierBinding) -> Option { + dbg!(1); let is_binding_ok = - is_function_that_is_ok_parameter_not_be_used(parameter.parent_function()) - || is_property_parameter_ok_not_be_used(parameter)?; + is_function_that_is_ok_parameter_not_be_used(parameter.parent_function()) + || is_property_parameter_ok_not_be_used(parameter)?; + dbg!(is_binding_ok); if !is_binding_ok { suggestion_for_binding(binding) } else { @@ -183,7 +187,8 @@ fn suggested_fix_if_unused(binding: &AnyJsIdentifierBinding) -> Option { - let is_binding_ok = is_function_that_is_ok_parameter_not_be_used(parameter.parent_function()); + let is_binding_ok = + is_function_that_is_ok_parameter_not_be_used(parameter.parent_function()); if !is_binding_ok { suggestion_for_binding(binding) } else { @@ -191,7 +196,8 @@ fn suggested_fix_if_unused(binding: &AnyJsIdentifierBinding) -> Option { - let is_binding_ok = is_function_that_is_ok_parameter_not_be_used(parameter.parent_function()); + let is_binding_ok = + is_function_that_is_ok_parameter_not_be_used(parameter.parent_function()); if !is_binding_ok { suggestion_for_binding(binding) } else { diff --git a/crates/rome_js_analyze/tests/specs/correctness/noUnusedVariables/validClass.ts b/crates/rome_js_analyze/tests/specs/correctness/noUnusedVariables/validClass.ts index bbb098a827e3..c4c776c568aa 100644 --- a/crates/rome_js_analyze/tests/specs/correctness/noUnusedVariables/validClass.ts +++ b/crates/rome_js_analyze/tests/specs/correctness/noUnusedVariables/validClass.ts @@ -13,6 +13,6 @@ new (class B { }) // a and b are actually properties class C { - constructor(private a, public b) {} + constructor(private a1, public b2) {} } console.log(new C(1, 2)); diff --git a/crates/rome_js_analyze/tests/specs/correctness/noUnusedVariables/validClass.ts.snap b/crates/rome_js_analyze/tests/specs/correctness/noUnusedVariables/validClass.ts.snap index 5b9ca4098d9c..4700858c95a2 100644 --- a/crates/rome_js_analyze/tests/specs/correctness/noUnusedVariables/validClass.ts.snap +++ b/crates/rome_js_analyze/tests/specs/correctness/noUnusedVariables/validClass.ts.snap @@ -19,63 +19,10 @@ new (class B { }) // a and b are actually properties class C { - constructor(private a, public b) {} + constructor(private a1, public b2) {} } console.log(new C(1, 2)); ``` -# Diagnostics -``` -validClass.ts:16:25 lint/correctness/noUnusedVariables FIXABLE ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ - - ! This parameter is unused. - - 14 │ // a and b are actually properties - 15 │ class C { - > 16 │ constructor(private a, public b) {} - │ ^ - 17 │ } - 18 │ console.log(new C(1, 2)); - - i Unused variables usually are result of incomplete refactoring, typos and other source of bugs. - - i Suggested fix: If this is intentional, prepend a with an underscore. - - 14 14 │ // a and b are actually properties - 15 15 │ class C { - 16 │ - ····constructor(private·a,·public·b)·{} - 16 │ + ····constructor(private·_a,·public·b)·{} - 17 17 │ } - 18 18 │ console.log(new C(1, 2)); - - -``` - -``` -validClass.ts:16:35 lint/correctness/noUnusedVariables FIXABLE ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ - - ! This parameter is unused. - - 14 │ // a and b are actually properties - 15 │ class C { - > 16 │ constructor(private a, public b) {} - │ ^ - 17 │ } - 18 │ console.log(new C(1, 2)); - - i Unused variables usually are result of incomplete refactoring, typos and other source of bugs. - - i Suggested fix: If this is intentional, prepend b with an underscore. - - 14 14 │ // a and b are actually properties - 15 15 │ class C { - 16 │ - ····constructor(private·a,·public·b)·{} - 16 │ + ····constructor(private·a,·public·_b)·{} - 17 17 │ } - 18 18 │ console.log(new C(1, 2)); - - -``` - diff --git a/crates/rome_js_syntax/src/binding_ext.rs b/crates/rome_js_syntax/src/binding_ext.rs index 7f8496c52ed1..d34e58fe8882 100644 --- a/crates/rome_js_syntax/src/binding_ext.rs +++ b/crates/rome_js_syntax/src/binding_ext.rs @@ -64,7 +64,15 @@ fn declaration(node: &JsSyntaxNode) -> Option { ) })?; - AnyJsBindingDeclaration::cast(possible_declarator) + match AnyJsBindingDeclaration::cast(possible_declarator)? { + AnyJsBindingDeclaration::JsFormalParameter(parameter) => { + match parameter.parent::() { + Some(parameter) => Some(AnyJsBindingDeclaration::TsPropertyParameter(parameter)), + None => Some(AnyJsBindingDeclaration::JsFormalParameter(parameter)), + } + } + declaration => Some(declaration), + } } fn is_under_pattern_binding(node: &JsSyntaxNode) -> Option {