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

Commit

Permalink
fix ts property parameter
Browse files Browse the repository at this point in the history
  • Loading branch information
xunilrj committed Nov 30, 2022
1 parent a76031f commit 0d4c2a0
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 61 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,9 @@ pub enum SuggestedFix {
PrefixUnderscore,
}

fn is_function_that_is_ok_parameter_not_be_used(parent_function: Option<JsAnyParameterParentFunction>) -> bool {
fn is_function_that_is_ok_parameter_not_be_used(
parent_function: Option<JsAnyParameterParentFunction>,
) -> bool {
matches!(
parent_function,
Some(
Expand Down Expand Up @@ -173,25 +175,29 @@ fn suggested_fix_if_unused(binding: &AnyJsIdentifierBinding) -> Option<Suggested

// Some parameters are ok to not be used
AnyJsBindingDeclaration::TsPropertyParameter(parameter) => {
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 {
None
}
}
AnyJsBindingDeclaration::JsFormalParameter(parameter) => {
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 {
None
}
}
AnyJsBindingDeclaration::JsRestParameter(parameter) => {
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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Original file line number Diff line number Diff line change
Expand Up @@ -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));
```


10 changes: 9 additions & 1 deletion crates/rome_js_syntax/src/binding_ext.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,15 @@ fn declaration(node: &JsSyntaxNode) -> Option<AnyJsBindingDeclaration> {
)
})?;

AnyJsBindingDeclaration::cast(possible_declarator)
match AnyJsBindingDeclaration::cast(possible_declarator)? {
AnyJsBindingDeclaration::JsFormalParameter(parameter) => {
match parameter.parent::<TsPropertyParameter>() {
Some(parameter) => Some(AnyJsBindingDeclaration::TsPropertyParameter(parameter)),
None => Some(AnyJsBindingDeclaration::JsFormalParameter(parameter)),
}
}
declaration => Some(declaration),
}
}

fn is_under_pattern_binding(node: &JsSyntaxNode) -> Option<bool> {
Expand Down

0 comments on commit 0d4c2a0

Please sign in to comment.