GithubHelp home page GithubHelp logo

lecaillon / conan.plugin.nullguard Goto Github PK

View Code? Open in Web Editor NEW
0.0 2.0 0.0 19 KB

Conan plugin adds null guard code for all methods and constructors parameters preceded by a [NonNull] attribute.

License: BSD 2-Clause "Simplified" License

C# 100.00%
roslyn roslyn-analyzer dotnet compiler-plugin conan guard null-check

conan.plugin.nullguard's Introduction

Conan.Plugin.NullGuard Build status

Conan.Plugin.NullGuard adds null guard code for all methods and constructors parameters preceded by any [NonNull] attribute.

It is based on Conan, a lightweight fork of the .NET Compiler Platform ("Roslyn") by adding a compiler plugin infrastructure. These plugins can be deployed and installed as regular Diagnostic Analyzers.

Notice

This plugin is an alpha version, whose purpose is to see where the very promising Conan compiler plugin infrastructure can bring us.

Getting started

  1. Add the package Conan.Net.Compilers to your project: This will make the Conan compiler as the default CSharp/VB compiler and replace the default Roslyn compiler (This package works for both Full framework and Core framework unlike the Roslyn packages)
  2. Add the package Conan.Plugin.NullGuard only available on AppVeyor for now.

Example

  1. Add a [NonNull] attribute to any constructor or method reference type parameter you want to check.
public class Person
{
  private readonly string _name;
  private readonly int _age;

  public Person([NonNull] string name, int age)
  {
    _name = name;
    _age = age;
  }
}
  1. At build time the Conan Compiler will automatically insert at the beginning of the constructor the statement needed to ensure the parameter name is not null.
public class Person
{
  private readonly string _name;
  private readonly int _age;

  public Person([NonNull] string name, int age)
  {
    if (name == null)
    {
      throw new ArgumentNullException("name");
    }

    _name = name;
    _age = age;
  }
}
  1. You can see rewritten documents on the disk in a sub directory of the obj folder or using ILSpy. Example: \obj\Debug\netcoreapp2.1\GeneratedFiles\Microsoft.CodeAnalysis.CSharp.Analyzers\Conan.Person.cs

Licensing

Same license than Roslyn: Apache-2.0

Credits

Alexandre MUTEL aka xoofx

conan.plugin.nullguard's People

Contributors

lecaillon avatar

Watchers

 avatar  avatar

conan.plugin.nullguard's Issues

Preserve debugging

Hi, super initiative!

So, one thing that is important for a plugin is to preserve the debugging experience, specially in the case where the code of the user is modified by a plugin.

In order to do that, you need to add a proper handling of line in your generated code with something like:

#line hidden
// your code 
#line insert_real_line_number_here

Check the latest version of Helloworld repo, it is trying to handle a few cases (method empty, method with expression body...etc.) but it is not yet perfect (there are still a few flaws).

I would like to try to find some recurrent patterns that could fit into the compiler directly for example for this case, but before going there, we need to practice it a bit more, so that's good if you can try this.

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.