-
Notifications
You must be signed in to change notification settings - Fork 323
Add linter rule to detect unused imports #9149
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
Conversation
- Add getUnusedImports() method to NameResolver interface - Implement tracking of which source files' symbols are used - Create unused-import linter rule similar to unused-using - Add tests for unused import detection Co-authored-by: baywet <[email protected]>
- Use getDirectoryPath, normalizePath, resolvePath from path-utils.ts - Remove redundant transitive check logic - Add changelog entry for unused import feature Co-authored-by: baywet <[email protected]>
commit: |
|
@copilot this version has two bugs:
Start by defining unit tests for those two cases, can then implement the fixes. |
- Test multiple imports with only one unused - Test unused import before used import - Test unused import in the middle of used imports - Test enum used as generic type parameter - Test enum member access via qualified name - Test namespace member access via qualified name Co-authored-by: baywet <[email protected]>
I've added comprehensive test cases for both scenarios in commit 69d63fa. All tests pass, which suggests the current implementation handles these cases correctly:
If you're still seeing these issues, could you share a minimal reproduction case? The specific code structure may reveal an edge case I haven't covered in the tests. Warning Firewall rules blocked me from connecting to one or more addresses (expand for details)I tried to connect to the following addresses, but was blocked by firewall rules:
If you need me to access, download, or install something from one of these locations, you can either:
|
|
@copilot the latest tests you've added do not cover the issues I ran into when testing the changes in a local project. Let's start by addressing the issue of unused imports making other imports appearing as unused even though they are used. The description I provided was incomplete. When "using TypeSpec.OpenAPI;" is present, "import @typespec/openapi;" should not be marked as unused. This is a special case for this package. Start by updating the unit tests definitions. |
… namespace When a file has a using statement like "using MyLib;", the corresponding import that provides the MyLib namespace should not be marked as unused. This fixes the issue where imports for @typespec/http, @typespec/openapi, and other packages were incorrectly flagged as unused when only their namespaces were used via using statements. Added tests for: - Import with using statement referencing its namespace - Import with using statement even without symbol usage from that namespace Co-authored-by: baywet <[email protected]>
Fixed in commit b309d52. The Added tests for:
Warning Firewall rules blocked me from connecting to one or more addresses (expand for details)I tried to connect to the following addresses, but was blocked by firewall rules:
If you need me to access, download, or install something from one of these locations, you can either:
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
just leaving comment here if this get resumed but I think the test for this are not at the correct layer, it shouldn't really need to invlove the language server
|
abandonning this (see reasoning in linked issue) |
tsp formatshould clean up unused importsusingunused detection works (via name-resolver)unused-importsimilar tounused-usinggetUnusedImports()in the name resolver to track unused file importsremoveUnusedCodeCodeFix)Summary
This PR adds a new linter rule
@typespec/compiler/unused-importthat detects and reports unused import statements in TypeSpec files. When enabled, it will:usingstatementsThe implementation follows the same pattern as the existing
unused-usingrule and integrates with the language server for IDE support.Testing
Added comprehensive tests covering:
Original prompt
💬 We'd love your input! Share your thoughts on Copilot coding agent in our 2 minute survey.