dotvvm
dotvvm copied to clipboard
Roslyn Analyzers for DotVVM
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.
Check the usage of [Authorize]
within viewmodels.
Rule: Use AuthorizeAttribute
defined in DotVVM namespace
Idea by @martindybal
Check the usage of [FromRoute]
, [FromQuery]
,...
Rule: Use these attributes from DotVVM namespace as well
It would be nice to have an analyzer that warns you in these cases:
- Used interface in ViewModel.
- 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
- 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
- Used [JsonProperty] on props or other Newtonsoft.JSON attributes that alter serialization.
- When a property does not have a setter but does not use [Bind(Direction)] attribute.
- When a property is of type of abstract class (can be serialized but cannot be deserialized)
one more:
- Check that
DotvvmProperty.Register<DeclaringType, ...>
is referencing the current type. This is a common error that is super annoying to debug.
Infrastructure added in PR #1156