Giter Site home page Giter Site logo

dbolin / apex.analyzers Goto Github PK

View Code? Open in Web Editor NEW
14.0 3.0 2.0 120 KB

Roslyn powered analyzers for C# to support convention defined architecture

License: MIT License

C# 96.68% PowerShell 3.32%
roslyn roslyn-analyzer roslyn-analyzers csharp architecture analyzer immutable type

apex.analyzers's Introduction

Apex.Analyzers

Roslyn powered analyzers for C# to support convention defined architecture

Immutable Types

Build Status

Nuget Package

Provides an ImmutableAttribute type which can be applied to classes, structs, and interfaces. The analyzer ensures that the following rules hold for types marked with the attribute.

ID Severity Rule Code Fix
IMM001 Error Fields in an immutable type must be readonly Yes
IMM002 Error Auto properties in an immutable type must not define a set method Yes
IMM003 Error Types of fields in an immutable type must be immutable No
IMM004 Error Types of auto properties in an immutable type must be immutable No
IMM005 Warning 'This' should not be passed out of the constructor of an immutable type No
IMM006 Error The base type of an immutable type must be 'object' or immutable No
IMM007 Error Types derived from an immutable type must be immutable No
IMM008 Warning 'This' should not be passed out of an init only property method of an immutable type No

Whitelisting types via additional files

The immutable types analyzer allows specifying types to be whitelisted in an Additional File.

The name of the additional file is "ImmutableTypes.txt" and the format of the file is one namespace qualified type name per line. For example:

System.Xml.Linq.XName
System.Func`1

apex.analyzers's People

Contributors

dbolin avatar dependabot-preview[bot] avatar dependabot[bot] avatar mwelsh1118 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

apex.analyzers's Issues

Version 1.1 does not work with Visual Studio 16.4

The new Microsoft.CodeAnalysis package that's referenced in 1.1 (Microsoft.CodeAnalysis 3.5) isn't included in Visual Studio 2019 Update 4, so compiling with that version of MSBuild produces this warning --

CSC : warning CS8032: An instance of analyzer Apex.Analyzers.Immutable.ApexAnalyzersImmutableAnalyzer cannot be created from C:\Users\markwelsh\source\repos\ApexExample\packages\Apex.Analyzers.Immutable.1.1.0\analyzers\dotnet\cs\Apex.Analyzers.Immutable.dll : Could not load file or assembly 'Microsoft.CodeAnalysis, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35' or one of its dependencies. The system cannot find the file specified..

Is there a way to work around this? Or would it be possible to roll back to the version supported by 16.4? That's the most recent servicing baseline.

Referencing the analyzer package with PrivateAssets="All"

Currently you have to reference the analyzer with PrivateAssets="All" to prevent the analyzer from flowing to referencing projects. However, this means referencing projects don't reference the Attributes package, which can cause issues with runtime reflection.

PrivateAssets="analyzers" would be the preferred solution but needs NuGet/Home#6279 to be resolved.

One workaround is to reference the Attributes package directly in projects that need it.

Could you add a strong name?

Hi, thanks for this handy tool!

Could you add strong name to both Apex.Analyzers.Immutable and Apex.Analyzers.Immutable.Attributes?
My project is strong named so I get CS8002 error when I try to use your assemblies.

error CS8002: Referenced assembly 'Apex.Analyzers.Immutable.Attributes, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null' does not have a strong name.

Helpers could be exposed publicly (or via separate NuGet package)

We're using your immutability analyzers to detect mutability problems in our code. We have our own set of analyzers that build on top of yours. Specifically, we are trying to prevent static state that is mutable. Our analyzers use similar logic to what is defined in your Helpers class. Could this be made public or exposed as a separate Apex.Analyzers.Helpers package?

Whitelist is hard-coded

Could the list of whitelisted types be configurable? Similar to Microsoft's BannedApiAnalyzer, there could be an additional file added to the analyzed project that could extend the list of white-listed types. Happy to implement this. Just don't want to go down this road unless it's likely to be accepted.

C# 9 Init-only property setters trigger IMM002

The following auto-property will trigger IMM002, even though the property is immutable after the object is initialized:

public int Property { get; init; }

I can look at creating a Pull Request to address this if you'd like.

Delegates with mutable parameters are treated as mutable

We now have a way to mark arbitrary types immutable via configuration. This allows us to configure System.Action`1, for example, as immutable without impacting other consumers of the analyzers. However, we're still experiencing a fair amount of pain due to the generic type argument checking.

For example this is a violation:

[Immutable]
class Test
{
    public Action<NotImmutable> Action { get; }
}

Even though Action`1 is treated as immutable, its generic type argument (NotImmutable) is not, and so it produces an IMM004.

I know that delegates are treated as mutable as a conscious design choice, but might this be reconsidered? Another corner case (like state-capturing lambdas) that is not covered would be:

[Immutable]
class Test
{
    private readonly object _random;

    public Test(object random)
    {
        _random = random;
    }
    public int Value => ((System.Random)_random).Next(0, 10);
}

I'm not arguing that this should be caught -- simply that there will always be ways to subvert the rules if someone is truly determined to do so.

Alternately, could we add configuration to control the treatment of delegates? The Microsoft analyzers now support options defined in an .editorconfig. Unfortunately their implementation is internal, so cannot be called directly. But the code is open-source and could be incorporated.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.