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
Closed
Show file tree
Hide file tree
Changes from 20 commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
77f8d17
support unused using and import
RodgeFu Nov 15, 2024
0b32b3f
fix some tests
RodgeFu Nov 18, 2024
a22902a
Merge remote-tracking branch 'upstream/main' into unused-import-names…
RodgeFu Nov 18, 2024
b101142
fix some regressions
RodgeFu Nov 18, 2024
c0e36ac
add more test and fix some issues
RodgeFu Nov 19, 2024
0d1d016
some small change
RodgeFu Nov 19, 2024
f721971
fix a bug from oldprogram
RodgeFu Nov 19, 2024
b0f1db8
handle oldprogram
RodgeFu Nov 19, 2024
aab4dd3
only report diagnostics for files in project
RodgeFu Nov 19, 2024
3138407
update to only check unnecessary diagnostics in compiler's test
RodgeFu Nov 21, 2024
603e8ed
some further update
RodgeFu Nov 22, 2024
28b8643
fix sample test
RodgeFu Nov 22, 2024
179c5b4
fix some tests
RodgeFu Nov 23, 2024
eb2e1fe
fix some tests
RodgeFu Nov 23, 2024
bf90961
fix test
RodgeFu Nov 23, 2024
23933ed
Merge remote-tracking branch 'upstream/main' into unused-import-names…
RodgeFu Nov 23, 2024
8625363
add changelog for change in rest test
RodgeFu Nov 23, 2024
1954c14
Create unused-import-namespace-2024-10-23-11-18-0.md
RodgeFu Nov 23, 2024
3f66d73
Merge branch 'main' into unused-import-namespace
RodgeFu Nov 27, 2024
647f25e
Merge branch 'main' into unused-import-namespace
RodgeFu Dec 2, 2024
9f5a77d
Merge branch 'main' into unused-import-namespace
RodgeFu Dec 5, 2024
8104f1b
update to unused-import and unused-using
RodgeFu Dec 5, 2024
6ab808d
fix failed test
RodgeFu Dec 5, 2024
f5db540
Merge branch 'main' into unused-import-namespace
RodgeFu Dec 7, 2024
59182de
Merge branch 'main' into unused-import-namespace
RodgeFu Dec 10, 2024
b0af993
Merge remote-tracking branch 'upstream/main' into unused-import-names…
RodgeFu Dec 13, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
changeKind: feature
packages:
- "@typespec/compiler"
---

Support diagnostics for unused import and using statements
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
---
# Change versionKind to one of: internal, fix, dependencies, feature, deprecation, breaking
changeKind: fix
packages:
- "@typespec/http-specs"
---

Remove unnecessary "import" and "using" from tsp code
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
changeKind: internal
packages:
- "@typespec/rest"
---

Update test code to handle diagnostics for unnecessary code
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { defineCodeFix, getSourceLocation } from "../diagnostics.js";
import { type ImportStatementNode, type UsingStatementNode } from "../types.js";

/**
* Quick fix that remove unnecessary code.
*/
export function removeUnnecessaryCodeCodeFix(node: ImportStatementNode | UsingStatementNode) {
return defineCodeFix({
id: "remove-unnecessary-code",
label: `Remove unnecessary code`,
fix: (context) => {
const location = getSourceLocation(node);
return context.replaceText(location, "");
},
});
}
4 changes: 2 additions & 2 deletions packages/compiler/src/core/diagnostics.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ export type DiagnosticHandler = (diagnostic: Diagnostic) => void;
export function logDiagnostics(diagnostics: readonly Diagnostic[], logger: LogSink) {
for (const diagnostic of diagnostics) {
logger.log({
level: diagnostic.severity,
level: diagnostic.severity === "hint" ? "trace" : diagnostic.severity,
message: diagnostic.message,
code: diagnostic.code,
url: diagnostic.url,
Expand All @@ -52,7 +52,7 @@ export function formatDiagnostic(diagnostic: Diagnostic, options: FormatDiagnost
return formatLog(
{
code: diagnostic.code,
level: diagnostic.severity,
level: diagnostic.severity === "hint" ? "trace" : diagnostic.severity,
message: diagnostic.message,
url: diagnostic.url,
sourceLocation: getSourceLocation(diagnostic.target, { locateId: true }),
Expand Down
6 changes: 6 additions & 0 deletions packages/compiler/src/core/messages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.

severity: "hint",
messages: {
default: paramMessage`Unnecessary code: ${"code"}`,
},
},

/**
* Configuration
Expand Down
Loading
Loading