Roslyn: Question: Guidelines regarding multiple diagnostics vs one diagnostic with multiple locations

Created on 24 Dec 2019  路  5Comments  路  Source: dotnet/roslyn

I have checked the wiki pages for information regarding whether to use one or multiple diagnostic reports to change my code and the issue i am having are related to unit tests.

imagine a scenario where i have an analyzer which inspects classes for their fields and provides code actions based on those fields.

class
   field1
   field2
   method1
   method2

should generate actions like this:
method1 -> refactoring action(use field1), refactoring action(use field2)
method2 -> refactoring action(use field1), refactoring action(use field2)

right now this is implemented in a way where each occurence has its own diagnostic report, so there are 4, which are localized to use parameterized messageformat messages.

the code i do have does work right now just the way i want it to, but it feels like i am either reporting diagnostics in the wrong way, or that perhaps the boilerplate for unit tests in the sdk should be updated.

the boilerplate code always depends on verifying diagnostic[0]
grafik

I've added my own parameter to support skipping actions so i can verify batch fixing of use field1 / use field 2 - however since diagnostic[0] is hard coded i would have to populate a single diagnostic with all locations and repeat analysis in my codefixprovider - also i would lose the ability to use parameterized diagnostic messages.

Am i missing something here?

Is using 4 diagnostics the right way here, because 1 diagnostics (+4 locations) appears to be wrong (no way to use parameterized diagnostics messages)?

Could it be that the boilerplate needs to be updated to be able to pick the diagnostics index when verifying codefixes?

Area-Analyzers Concept-API Question

Most helpful comment

do we plan to update the SDK to use the new testing library?

Eventually, yes

All 5 comments

the code i do have does work right now just the way i want it to, but it feels like i am either reporting diagnostics in the wrong way, or that perhaps the boilerplate for unit tests in the sdk should be updated.

Tagging @sharwell @jmarolf. They have been working on a new testing library Microsoft.CodeAnalysis.Testing that supercedes the test helper in the analyzer SDK. Sam/Jon, do we plan to update the SDK to use the new testing library?

That is very good to know, however i would be curious to know which way is the correct approach however.

do we plan to update the SDK to use the new testing library?

Eventually, yes

however i would be curious to know which way is the correct approach however.

I would generally expect two diagnostics here (one for method1, and one for method2). The code fix provider would provide two fixes for each of the two diagnostics.

The testing library can already handle any possible test scenario surrounding this implementation strategy.

however i would be curious to know which way is the correct approach however.

I would generally expect two diagnostics here (one for method1, and one for method2). The code fix provider would provide two fixes for each of the two diagnostics.

The testing library can already handle any possible test scenario surrounding this implementation strategy.

Thank you! Looking forward to the new library then i guess. I hope its' publication isn't too far in the future.

Was this page helpful?
0 / 5 - 0 ratings