-
Notifications
You must be signed in to change notification settings - Fork 12.9k
Improve diagnostics deduplication #58220
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
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
49a65b1
add test for duplicate diagnostic
gabritto 165869c
improve diagnostics deduplication
gabritto 3257850
fix comparison; add test; keep deduped and sorted diagnostics upon in…
gabritto 35ed28c
more comparison fixes, more tests
gabritto ef2eb5c
rename functions
gabritto File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,120 @@ | ||
import * as ts from "../_namespaces/ts"; | ||
|
||
describe("unittests:: internalApi:: diagnosticCollection", () => { | ||
describe("add", () => { | ||
it("keeps equivalent diagnostic with elaboration", () => { | ||
const collection = ts.createDiagnosticCollection(); | ||
const file = ts.createSourceFile("index.ts", "const x = 1", ts.ScriptTarget.ESNext, /*setParentNodes*/ true); | ||
const node = file.statements[0]; | ||
|
||
const dy = ts.createDiagnosticForNode(node, ts.Diagnostics.Cannot_find_name_0, "y"); | ||
const da = ts.createDiagnosticForNode(node, ts.Diagnostics.Cannot_find_name_0, "a"); | ||
collection.add(dy); | ||
collection.add(da); | ||
|
||
const chain = ts.chainDiagnosticMessages( | ||
ts.chainDiagnosticMessages(/*details*/ undefined, ts.Diagnostics.Did_you_mean_0, "x"), | ||
ts.Diagnostics.Cannot_find_name_0, | ||
"y", | ||
); | ||
const dyBetter = ts.createDiagnosticForNodeFromMessageChain(file, node, chain); | ||
|
||
collection.add(dyBetter); | ||
const result = collection.getDiagnostics(); | ||
assert.deepEqual(result, [da, dyBetter]); | ||
}); | ||
|
||
it("keeps equivalent diagnostic with deeper elaboration", () => { | ||
const collection = ts.createDiagnosticCollection(); | ||
const file = ts.createSourceFile("index.ts", "const x = 1", ts.ScriptTarget.ESNext, /*setParentNodes*/ true); | ||
const node = file.statements[0]; | ||
|
||
const da = ts.createDiagnosticForNode(node, ts.Diagnostics.Cannot_find_name_0, "a"); | ||
collection.add(da); | ||
|
||
const chain = ts.chainDiagnosticMessages( | ||
ts.chainDiagnosticMessages(/*details*/ undefined, ts.Diagnostics.Did_you_mean_0, "a"), | ||
ts.Diagnostics.Cannot_find_name_0, | ||
"y", | ||
); | ||
const dy = ts.createDiagnosticForNodeFromMessageChain(file, node, chain); | ||
collection.add(dy); | ||
|
||
const chainBetter = ts.chainDiagnosticMessages( | ||
ts.chainDiagnosticMessages( | ||
ts.chainDiagnosticMessages(/*details*/ undefined, ts.Diagnostics.Did_you_mean_0, "x"), | ||
ts.Diagnostics.Did_you_mean_0, | ||
"b", | ||
), | ||
ts.Diagnostics.Cannot_find_name_0, | ||
"y", | ||
); | ||
const dyBetter = ts.createDiagnosticForNodeFromMessageChain(file, node, chainBetter); | ||
|
||
collection.add(dyBetter); | ||
const result = collection.getDiagnostics(); | ||
assert.deepEqual(result, [da, dyBetter]); | ||
}); | ||
|
||
it("doesn't keep equivalent diagnostic with no elaboration", () => { | ||
const collection = ts.createDiagnosticCollection(); | ||
const file = ts.createSourceFile("index.ts", "const x = 1", ts.ScriptTarget.ESNext, /*setParentNodes*/ true); | ||
const node = file.statements[0]; | ||
|
||
const chain = ts.chainDiagnosticMessages( | ||
ts.chainDiagnosticMessages(/*details*/ undefined, ts.Diagnostics.Did_you_mean_0, "x"), | ||
ts.Diagnostics.Cannot_find_name_0, | ||
"y", | ||
); | ||
const dyBetter = ts.createDiagnosticForNodeFromMessageChain(file, node, chain); | ||
const da = ts.createDiagnosticForNode(node, ts.Diagnostics.Cannot_find_name_0, "a"); | ||
collection.add(da); | ||
collection.add(dyBetter); | ||
|
||
const dy = ts.createDiagnosticForNode(node, ts.Diagnostics.Cannot_find_name_0, "y"); | ||
collection.add(dy); | ||
const result = collection.getDiagnostics(); | ||
assert.deepEqual(result, [da, dyBetter]); | ||
}); | ||
}); | ||
|
||
describe("lookup", () => { | ||
it("returns an equivalent diagnostic with more elaboration", () => { | ||
const collection = ts.createDiagnosticCollection(); | ||
const file = ts.createSourceFile("index.ts", "const x = 1", ts.ScriptTarget.ESNext, /*setParentNodes*/ true); | ||
const node = file.statements[0]; | ||
|
||
const da = ts.createDiagnosticForNode(node, ts.Diagnostics.Cannot_find_name_0, "a"); | ||
collection.add(da); | ||
|
||
const chain = ts.chainDiagnosticMessages( | ||
ts.chainDiagnosticMessages(/*details*/ undefined, ts.Diagnostics.Did_you_mean_0, "x"), | ||
ts.Diagnostics.Cannot_find_name_0, | ||
"y", | ||
); | ||
const dyBetter = ts.createDiagnosticForNodeFromMessageChain(file, node, chain); | ||
collection.add(dyBetter); | ||
|
||
const dy = ts.createDiagnosticForNode(node, ts.Diagnostics.Cannot_find_name_0, "y"); | ||
assert.equal(collection.lookup(dy), dyBetter); | ||
}); | ||
it("doesn't return an equivalent diagnostic with less elaboration", () => { | ||
const collection = ts.createDiagnosticCollection(); | ||
const file = ts.createSourceFile("index.ts", "const x = 1", ts.ScriptTarget.ESNext, /*setParentNodes*/ true); | ||
const node = file.statements[0]; | ||
|
||
const da = ts.createDiagnosticForNode(node, ts.Diagnostics.Cannot_find_name_0, "a"); | ||
collection.add(da); | ||
const dy = ts.createDiagnosticForNode(node, ts.Diagnostics.Cannot_find_name_0, "y"); | ||
collection.add(dy); | ||
|
||
const chain = ts.chainDiagnosticMessages( | ||
ts.chainDiagnosticMessages(/*details*/ undefined, ts.Diagnostics.Did_you_mean_0, "x"), | ||
ts.Diagnostics.Cannot_find_name_0, | ||
"y", | ||
); | ||
const dyBetter = ts.createDiagnosticForNodeFromMessageChain(file, node, chain); | ||
assert.equal(collection.lookup(dyBetter), undefined); | ||
}); | ||
}); | ||
}); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
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.
As far as I can tell,
DiagnosticCollection
's.add
method is still usingcompareDiagnosticsSkipRelatedInformation
, so it's just first-in-wins (with the same root message) - should that also get updated to prefer the "lesser" message like this?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.
compareDiagnosticsSkipRelatedInformation
is still the same in terms of what it considers equal - two diagnostics with the same location and head message but different elaboration will still be considered different by that function, and so both will be added viaDiagnosticCollection.add
. Only later, when we callsortAndDeduplicateDiagnostics
, will we usediagnosticsEqualityComparer
and get rid of all but one of the diagnostics that we now consider equal.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.
Now that I think about it, if we want to keep using functions like
sortAndDeduplicate
andinsertSorted
, I think we need to have two separate functions for comparison and equality: one that fully compares diagnostics for purposes of sorting (and sorting a diagnostic with more elaboration before one with less), and another for purposes of deduplication that is more permissive and only compares location and head message for equality. Otherwise we'd have to implement a function that somehow says "yes, those two diagnostics are equal, but one of them is preferred".