dotvvm icon indicating copy to clipboard operation
dotvvm copied to clipboard

Roslyn Analyzers for DotVVM

Open acizmarik opened this issue 4 years ago • 5 comments

We were discussing the possibility to implement custom Roslyn Analyzers. We believe that their integration with Visual Studio and the ability to mark problematic code and suggest fixes would be beneficial to users.

Motivation

  • Warn users about common problems (e.g. viewmodels must be JSON-serializable)
  • Provide fixes (if available) for identified problems
  • Help users to migrate their solutions to newer versions of DotVVM

Implementation & Distribution

We already have a PoC for a custom DotVVM analyzer (feature/roslyn-analysers). The sample features a simple serializability analyzer and adjusted test-framework to work with DotVVM references. Moreover, the solution was already tested both with a .NET Core and an OWIN project.

  • Analyzers will be distributed using NuGet packages
  • DotVVM extension / templates for Visual Studio can by default include analyzers when creating new projects

I am opening a discussion on what analyzers (checks) and corresponding fixes should be implemented for DotVVM projects.

acizmarik avatar Oct 19 '20 15:10 acizmarik

Check the usage of [Authorize] within viewmodels. Rule: Use AuthorizeAttribute defined in DotVVM namespace

Idea by @martindybal

acizmarik avatar Dec 09 '20 17:12 acizmarik

Check the usage of [FromRoute], [FromQuery],... Rule: Use these attributes from DotVVM namespace as well

mrnustik avatar Jan 20 '21 11:01 mrnustik

It would be nice to have an analyzer that warns you in these cases:

  1. Used interface in ViewModel.
  2. Used [AllowStaticCommand] on non-static method in class based on ViewModelBase - this works but it tries to trick you that you have all properties available which you do not
  3. Used [AllowStaticCommand] on static method in class based on ViewModelBase - this works but it is not nice. Much nicer is to separate it into static command service and use service directive to reference it in Dothtml
  4. Used [JsonProperty] on props or other Newtonsoft.JSON attributes that alter serialization.
  5. When a property does not have a setter but does not use [Bind(Direction)] attribute.
  6. When a property is of type of abstract class (can be serialized but cannot be deserialized)

quigamdev avatar Apr 26 '21 21:04 quigamdev

one more:

  • Check that DotvvmProperty.Register<DeclaringType, ...> is referencing the current type. This is a common error that is super annoying to debug.

exyi avatar Jul 02 '21 10:07 exyi

Infrastructure added in PR #1156

quigamdev avatar Oct 13 '21 11:10 quigamdev