eNeRGy164 / LivingDocumentation

Generate documentation based on your dotnet source code!
MIT License
54 stars 8 forks source link

How to deal with contextual keywords (nameof() or dynamic) in the InvocationExpressionSyntax #63

Closed hideintheclouds closed 1 month ago

hideintheclouds commented 10 months ago

Consider the following code snippet:

if (string.IsNullOrWhiteSpace(filename))
                throw new ArgumentException(@"Filename cannot be null or whitespace.", nameof(filename));

Within C#, nameof is a contextual keyword. This means there is no way to distinguish the nameof keyword from a call to a method that happens to be named nameof.

When the previous code gets visited, the nameof(filename) node gets processed by the VisitInvocationExpression as an InvocationExpressionSyntax. https://github.com/eNeRGy164/LivingDocumentation/blob/main/src/LivingDocumentation.Analyzer/Analyzers/InvocationsAnalyzer.cs

The GetExpressionWithSymbol method returns null (this.semanticModel.GetSymbolInfo(expression).Symbol is null).

When running in verbose, the output will display all the occurences of nameof(): WARN: Could not resolve type of invocation of the following block:

The question is: should these particular cases be ignored altogether, or should they be handled?

Note: a similar situation is encounted with dynamic types:

 public void MyMethod(dynamic dynamicPayload)
{
    string realPayload = JsonConvert.SerializeObject(dynamicPayload);
}
hideintheclouds commented 10 months ago

I think that the dynamic and/or nameof() is the root-cause for the ArgumentNullException on InvocationDescription #56

eNeRGy164 commented 10 months ago

When I was looking the upgrade to dotnet 8, I also encountered the warning for nameof. Basically, it should move up in the method as there is no use to get the SymbolInfo for this call.

I never tried dynamic with LivingDocumentation, so not sure if we can so anything useful.

eNeRGy164 commented 1 month ago

nameof detection is fixed with https://github.com/dendrodocs/dotnet-tool/pull/3