Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Report diagnostics for unused "using" and "import" so that they will be shown properly in ide #5146

Closed
wants to merge 26 commits into from

Conversation

RodgeFu
Copy link
Contributor

@RodgeFu RodgeFu commented Nov 19, 2024

When "using" and "import" are not needed, dim them and provide quickfix in IDE.

related issues:
#3373 , #4861

@RodgeFu RodgeFu changed the title Unused import namespace When "using" and "import" are not needed, dim them and provide quickfix in IDE. Nov 19, 2024
@RodgeFu RodgeFu changed the title When "using" and "import" are not needed, dim them and provide quickfix in IDE. Report diagnostics for unused "using" and "import" so that they will be shown properly in ide Nov 19, 2024
@azure-sdk
Copy link
Collaborator

azure-sdk commented Nov 19, 2024

All changed packages have been documented.

  • @typespec/compiler
  • @typespec/http-specs
  • @typespec/rest
Show changes

@typespec/compiler - feature ✏️

Support diagnostics for unused import and using statements

@typespec/http-specs - fix ✏️

Remove unnecessary "import" and "using" from tsp code

@typespec/rest - internal ✏️

Update test code to handle diagnostics for unnecessary code

@RodgeFu RodgeFu added the ide Issues for VS, VSCode, Monaco, etc. label Nov 19, 2024
@azure-sdk
Copy link
Collaborator

You can try these changes here

🛝 Playground 🌐 Website 📚 Next docs

// Don't provide unused diagnostics if customer is using projection in the project because
// the projection statements will only be processed when applying projection. There is no way to determine
// whether "import" or "using" is referenced from them, so we just skip here to avoid providing incorrect suggestions (diagnostics)
// This should be fine for now considering projection is an experiemental feature.
Copy link
Contributor Author

@RodgeFu RodgeFu Nov 22, 2024

Choose a reason for hiding this comment

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

@timotheeguerin , @chrisradek , I tried to check the project part code and can't find a way to identify the references as commented here. Please let me know if there is some way I missed to count them considering my knowledge in compiler is still limited. thanks.

@@ -576,6 +576,12 @@ const diagnostics = {
default: "The #deprecated directive cannot be used more than once on the same declaration.",
},
},
unnecessary: {
Copy link
Member

Choose a reason for hiding this comment

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

Nit: Should this be named unused? Does it make sense to have import/using - specific messages?

I prefer unused over unnecessary since the latter has a subjective connotation, and at least in TS, unused is common for linter rules.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I am fine with "unused", update "unnecessary" to "unused-import" and "unused-using". thanks.

@chrisradek
Copy link
Member

Question that might need input from others:

Do we want the unused import/using to show up as a trace diagnostic when running the compiler? It's a little surprising to see it as a trace without enabling tracing. The TypeScript compiler has a few flags for enabling some "unused" checks which then treats them as errors - wonder if we want to do something similar here.

@RodgeFu
Copy link
Contributor Author

RodgeFu commented Dec 5, 2024

Question that might need input from others:

Do we want the unused import/using to show up as a trace diagnostic when running the compiler? It's a little surprising to see it as a trace without enabling tracing. The TypeScript compiler has a few flags for enabling some "unused" checks which then treats them as errors - wonder if we want to do something similar here.

They are logged as "trace" because I can't find a better loglevel to log them. I think they are definitely not "error" or "warning". Please feel free to suggest if there is a better way to log them.

I am not quite sure I understand the "flags" part correctly, do you mean something like compilerOption? How would it help on the trace? Or do you mean only reporting these diagnostics when some compiler option (i.e. "designTimeBuild") is on?

@markcowl markcowl assigned markcowl and unassigned markcowl Dec 5, 2024
@RodgeFu RodgeFu requested a review from chrisradek December 9, 2024 01:26
@RodgeFu
Copy link
Contributor Author

RodgeFu commented Dec 20, 2024

Talked offline and we are not able to 100% determine whether an import is used because user may modify the tree in their lib or emitters, so I will

  • extract the unused-using part and file another PR for it
  • do some investigation to see whether we can express these unused-import information in a more accurate way which is still very useful to end user in most case (Or if we have type-only lib supported, then we should not have this problem)

close this PR and the issues are still open to track the 2 actions above.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
ide Issues for VS, VSCode, Monaco, etc.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants