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

When --noUnusedLocals/--noUnusedParameters is disabled, add suggestions instead of errors #22361

Merged
12 commits merged into from
Apr 5, 2018

Conversation

ghost
Copy link

@ghost ghost commented Mar 6, 2018

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
Copy link
Contributor

mhegazy commented Mar 6, 2018

@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
Copy link
Contributor

mhegazy commented Mar 6, 2018

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

@amcasey
Copy link
Member

amcasey commented Mar 7, 2018

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").

Copy link
Member

@amcasey amcasey left a comment

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
Copy link
Author

ghost commented Mar 12, 2018

@mhegazy Please re-review

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

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?: {};
Copy link
Member

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
Copy link
Author

ghost commented Mar 22, 2018

@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.

Copy link
Contributor

@mhegazy mhegazy left a comment

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
Copy link
Contributor

mhegazy commented Apr 5, 2018

Some tests are still failing.

@mhegazy
Copy link
Contributor

mhegazy commented Apr 5, 2018

@andy-ms let's get this change merged.

@StanFisher
Copy link

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

@ghost
Copy link
Author

ghost commented May 15, 2018

@StanFisher Should be in 2.9.

This pull request was closed.
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.

Suggestion: Show unused imports in VS Code Editor as grayed
4 participants