Conversation

ghost

Part of #19392
Fixes #8165 once editors recognize the unused attribute on a diagnostic (@amcasey, @mjbvz)

We will now always create these diagnostics, but the list we add them to varies based on compiler options.
Also had to change some codeFix tests to use all of their variables to avoid getting extra fixes.

@ghost ghost requested review from amcasey and mhegazy March 6, 2018 19:41
@mhegazy

@Andy-MS can you run the perf tests as well.. i am concerned about the time and memory we spend building these error messages. one option is to have an additional flag to the checker that the LS sets, but not the command-line compiler, specially that we only need it for one file (open) and not the rest of the program that we will ignore the results of anyways.

@mhegazy

Also we need to mark the unused name diagnostic with a new flag to enable the VS tagging behavior.

@amcasey

I think that, to create a good user experience in the editor, we'll want to do more than just report the same diagnostics with a lower severity.

  1. As @mhegazy suggested, we'll want to have a flag on the diagnostics so that the editor can gray out, rather than squiggle the range.
  2. If all symbols declared by a given import are unused, we'll want to combine the diagnostics into a single diagnostic spanning the entire import.
  3. We'll generally want to report larger spans (than just the declared identifier) to make the grayed out regions more visible (e.g. "class C", rather than just "class").

Choose a reason for hiding this comment

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

I don't object to checking this in as-is, but I do think there's more work to do.

@ghost ghost force-pushed the unused_suggestion branch from a07f9f4 to 90313d2 Compare March 12, 2018 19:28
@ghost

@mhegazy Please re-review

@ghost ghost force-pushed the unused_suggestion branch from 013840f to 91033ec Compare March 12, 2018 21:13
@ghost ghost force-pushed the unused_suggestion branch from 91033ec to 8cae633 Compare March 12, 2018 21:25
getSuggestionDiagnostics: file => {
return (suggestionDiagnostics.get(file.fileName) || emptyArray).concat(getUnusedDiagnostics());
function getUnusedDiagnostics(): ReadonlyArray<Diagnostic> {
checkSourceFile(file);

Choose a reason for hiding this comment

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

So is the idea that getting suggestion diagnostics always triggers type-checking the file? You could make this slightly smarter by only checking the source file if the file itself is a declaration file.

Copy link
Author

Choose a reason for hiding this comment

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

Done

@@ -4043,6 +4044,8 @@ namespace ts {
length: number | undefined;
messageText: string | DiagnosticMessageChain;
category: DiagnosticCategory;
/** May store more in future. For now, this will simply be `true` to indicate when a diagnostic is an unused-identifier diagnostic. */
unused?: {};

Choose a reason for hiding this comment

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

I'm not big on the name here; try reportingUnused, reportsUnused, or describesUnusedSpan

@ghost ghost force-pushed the unused_suggestion branch from e4f380c to bb4332b Compare March 22, 2018 21:53
@ghost ghost force-pushed the unused_suggestion branch from bb4332b to a724cd9 Compare March 22, 2018 22:17
@ghost

@DanielRosenwasser Please re-review

@@ -4061,6 +4062,8 @@ namespace ts {
length: number | undefined;
messageText: string | DiagnosticMessageChain;
category: DiagnosticCategory;
/** May store more in future. For now, this will simply be `true` to indicate when a diagnostic is an unused-identifier diagnostic. */
reportsUnused?: {};
Copy link
Contributor

Choose a reason for hiding this comment

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

nit. why not call both this property and the one on DiagnosticMessage isUnused or isUnusedDeclaration

@amcasey what does Roslyn call this property?

Copy link
Member

Choose a reason for hiding this comment

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

I think you're asking about WellKnownDiagnosticTags.Unnecessary

Copy link
Contributor

Choose a reason for hiding this comment

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

a bit weird ... but sure.

Choose a reason for hiding this comment

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

👍 Please check with @amcasey about the name of the property. ideally we would have the same name as Roslyn here.

@mhegazy

Some tests are still failing.

@mhegazy

@Andy-MS let's get this change merged.

@StanFisher

@mhegazy In which version of TypeScript will this land? Is there a good way to determine this?

@ghost

@StanFisher Should be in 2.9.

This pull request was closed.
Sign up for free to subscribe to this conversation on . Already have an account? Sign in.
None yet
None yet

Successfully merging this pull request may close these issues.

Suggestion: Show unused imports in VS Code Editor as grayed