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

feat(rome_js_analyze): improvements to noUnusedVariables to consider catch, typescript and export #3004

Merged
merged 10 commits into from
Aug 11, 2022

Conversation

xunilrj
Copy link
Contributor

@xunilrj xunilrj commented Aug 4, 2022

Summary

This expands #2979 with two extra cases:

Now we recommend removing the whole catch declaration:

warning[js/noUnusedVariables]: This variable is unused.
   ┌─ noUnusedVariables.js:39:10
   
39  } catch (e) {}
             -
Suggested fix: Remove this variable.
      | @@ -36,4 +36,4 @@
35 35 |   };
36 36 |   
37 37 |   try {
38    | - } catch (e) {}
   38 | + } catch {}

and we don´t generate false positives to interfaces and abstract methods on Typescript and exported functions.

Test Plan

cargo run -p rome_cli -- check crates/rome_js_analyze/tests/specs/js/noUnusedVariablesTypescript.ts

@xunilrj xunilrj requested a review from leops as a code owner August 4, 2022 10:17
@xunilrj xunilrj temporarily deployed to aws August 4, 2022 10:17 Inactive
@cloudflare-workers-and-pages
Copy link

cloudflare-workers-and-pages bot commented Aug 4, 2022

Deploying with  Cloudflare Pages  Cloudflare Pages

Latest commit: 1e50f87
Status: ✅  Deploy successful!
Preview URL: https://a66bd4b0.tools-8rn.pages.dev
Branch Preview URL: https://feature-no-unused-variable-i.tools-8rn.pages.dev

View logs

@xunilrj xunilrj temporarily deployed to aws August 4, 2022 10:17 Inactive
Copy link
Contributor

@ematipico ematipico left a comment

Choose a reason for hiding this comment

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

Now that we have multiple files for the same rule, I think it will work better for to create a folder called noUnusedVariables/ and have there the files? We already do this for some other rule

@github-actions
Copy link

github-actions bot commented Aug 4, 2022

@xunilrj xunilrj requested a review from a team August 4, 2022 10:43
@xunilrj xunilrj temporarily deployed to aws August 4, 2022 10:43 Inactive
@xunilrj
Copy link
Contributor Author

xunilrj commented Aug 4, 2022

Now that we have multiple files for the same rule, I think it will work better for to create a folder called noUnusedVariables/ and have there the files? We already do this for some other rule

Done. But now we strangely have a "noUnusedVariables" group. Not so sure I like that.

@ematipico
Copy link
Contributor

Now that we have multiple files for the same rule, I think it will work better for to create a folder called noUnusedVariables/ and have there the files? We already do this for some other rule

Done. But now we strangely have a "noUnusedVariables" group. Not so sure I like that.

Ah, I am not sure how things are set up, but I was referring to something like this:
https://github.com/rome/tools/tree/main/crates/rome_js_analyze/tests/specs/js/noDeadCode

@xunilrj xunilrj force-pushed the feature/no-unused-variable-improvements-catch-and-ts branch from ac87f6d to a20d3e4 Compare August 5, 2022 08:08
@xunilrj xunilrj temporarily deployed to aws August 5, 2022 08:08 Inactive
@@ -71,6 +72,43 @@ declare_rule! {
}
}

// It is ok in some Typescripts constructs for a parameter to be unused.
fn is_typescript_unused_ok(binding: &JsIdentifierBinding) -> Option<()> {
Copy link
Contributor

Choose a reason for hiding this comment

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

Should this check be moved to the semantic model builder ? It could make sense to consider that identifiers in TypeScript declarations do not create a new binding in the semantic model

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes. It will as an "is_used" query. Or something like that.

@ematipico
Copy link
Contributor

@xunilrj could you please re-open the issue or create new issues to list what we are missing in order to make the rule stable?


Some(JsRuleAction {
category: ActionCategory::QuickFix,
applicability: Applicability::Unspecified,
Copy link
Contributor

@ematipico ematipico Aug 8, 2022

Choose a reason for hiding this comment

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

We should either choose Always or MaybeIncorrect. Using Unspecified means that its applicability is unknown, which is not the case. Since the rule is still unstable, we should choose MaybeIncorrect

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done.

@xunilrj xunilrj force-pushed the feature/no-unused-variable-improvements-catch-and-ts branch from a20d3e4 to c0f2624 Compare August 10, 2022 07:46
@xunilrj xunilrj temporarily deployed to aws August 10, 2022 07:46 Inactive
@xunilrj xunilrj changed the title feat(rome_js_analyze): improvements to noUnusedVariables to consider catch and typescript feat(rome_js_analyze): improvements to noUnusedVariables to consider catch, typescript and export Aug 10, 2022
@xunilrj xunilrj temporarily deployed to aws August 10, 2022 11:40 Inactive

# Diagnostics
```
warning[no_unused_variables/noUnusedVariables]: This variable is unused.
Copy link
Contributor

Choose a reason for hiding this comment

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

Out of curiosity, is this expected? The no_unused_variables/ part. (it should be js)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

hum... I think this is part of the confusion with "groups". I will fix it.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done

Copy link
Contributor

@ematipico ematipico left a comment

Choose a reason for hiding this comment

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

A suggestion: maybe we should not recommend this rule for the time being?

@leops
Copy link
Contributor

leops commented Aug 10, 2022

A suggestion: maybe we should not recommend this rule for the time being?

This is actually a very interesting issue: our lint rules are unstable, and some of them have a number of false positive issues due to the complexity of statically analyzing JavaScript code.
On one hand it's tempting to remove these rules from the recommended set since it largely degrades the initial experience of people trying out the toolchain, even if we advertise the linter as unstable.
But people encountering these bugs also generates valuable feedback on bugs that we actually need to fix, and by disabling these rules by default I think we risk having these rules be indefinitely unstable because they're only actually enabled by a handful of users, and we do not have enough information on how the rule actually performs "in the wild" to decide that a rule is stable enough to be enabled by default.

@ematipico
Copy link
Contributor

ematipico commented Aug 10, 2022

Fortunately people can turn off rules via configuration now, so there's a good workaround in case of errors, and people won't be blocked. Less pressure for fixes. So yeah, I think it's worth turn them on and fix possible errors.

@xunilrj xunilrj force-pushed the feature/no-unused-variable-improvements-catch-and-ts branch from df5fa3c to c6ce7e5 Compare August 11, 2022 14:23
@xunilrj xunilrj temporarily deployed to aws August 11, 2022 14:23 Inactive
@xunilrj xunilrj force-pushed the feature/no-unused-variable-improvements-catch-and-ts branch from c6ce7e5 to 1e50f87 Compare August 11, 2022 16:39
@xunilrj xunilrj temporarily deployed to aws August 11, 2022 16:40 Inactive
@xunilrj xunilrj merged commit b2f6b5a into main Aug 11, 2022
@xunilrj xunilrj deleted the feature/no-unused-variable-improvements-catch-and-ts branch August 11, 2022 16:47
IWANABETHATGUY pushed a commit to IWANABETHATGUY/tools that referenced this pull request Aug 22, 2022
…catch, typescript and export (rome#3004)

* improvements to noUnusedVariables to consider catch, typescript and export.
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants