Git Product home page Git Product logo

nopen.net's Introduction

Nope'n.NET!

A Roslyn analyzer which requires that classes be sealed, abstract, or attributed with [Open].

Design and document for inheritance or else prohibit it

โ€“ Item 19, Effective Java, Third Edition

Build Status

Usage

C# creates new types as open by default which can be dangerous. This analyzer ensures that the intent to leave a class open is explicitly declared.

using Nopen.NET.Attributes;

sealed class Foo {}
abstract class Bar {}
[Open] class Baz {}

Non-sealed, non-abstract classes without the [Open] attribute will be marked with an error.

class Bad {}
Bad.cs(1,1): error Nopen: Class 'Bad' is implicitly open. Classes should be explicitly marked sealed, abstract, or [Open].

For more information see Item 19 of Effective Java, Third Edition.

Download

NuGet

<ItemGroup>
  <PackageReference Include="Nopen.NET" Version="<version_here>" PrivateAssets="All" />
</ItemGroup>

Credits

Thanks to Jake Wharton and his Nope'n project for the idea.

License

Copyright 2019 Marius Volkhart

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

nopen.net's People

Contributors

mariusvolkhart avatar notuxnobux avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

nopen.net's Issues

Option for excluding namespace

There is no option to exclude some namespaces where I don't want to run this analyzer on.

[assembly: NopenExclude("App.Migrations")]

This would be useful to have. And I don't think it would be hard to implement.

Support for records?

I noticed there is errors for non sealed records in this analyzers? Is it possible to add support for records?

Question: Compatibility with .NET Standard projects

Hi

Thanks for this neat package!

I see that there is an analyzer DLL and a runtime DLL. I'm assuming that the latter is what contains the OpenAttribute because code bases that use this analyzer need to be able to add said attribute to classes so the Roslyn analyzer can pick it up later to mark classes as explicitly open. Is it possible, however, that the DLL for the OpenAttribute also erroneously contains the analyzer code? To visually state what I mean:

  • Roslyn Analyzer DLL (analyzers subfolder of the NuGet package)
    • OpenAttribute.cs
    • OpenClassAnalyzer.cs
    • OpenClassCodeFixProvider.cs
    • ...
  • Code DLL (lib subfolder of the NuGet package)
    • OpenAttribute.cs
    • OpenClassAnalyzer.cs (should this be present?)
    • OpenClassCodeFixProvider.cs (should this be present?)

I notice this because I'm trying to use both analyzer and library in a Unity project, which uses .NET Standard 2.1. The analyzer runs fine, but Unity is complaining about the accompanying library:

Assembly 'Assets/Packages/NuGet/nopen.net/1.1.0-beta.23/lib/netstandard2.0/Nopen.NET.dll' will not be loaded due to errors:
Unable to resolve reference 'Microsoft.CodeAnalysis'. Is the assembly missing or incompatible with the current platform?
Reference validation can be disabled in the Plugin Inspector.
Nopen.NET references strong named System.Collections.Immutable Assembly references: 1.2.3.0 Found in project: 6.0.0.0.
Assembly Version Validation can be disabled in Player Settings "Assembly Version Validation"
Unable to resolve reference 'Microsoft.CodeAnalysis.CSharp'. Is the assembly missing or incompatible with the current platform?
Reference validation can be disabled in the Plugin Inspector.
Unable to resolve reference 'Microsoft.CodeAnalysis.Workspaces'. Is the assembly missing or incompatible with the current platform?
Reference validation can be disabled in the Plugin Inspector.
Unable to resolve reference 'System.Composition.AttributedModel'. Is the assembly missing or incompatible with the current platform?
Reference validation can be disabled in the Plugin Inspector.

I suspect the above problem since the OpenAttribute is not using said classes - only the analyzer is. IIRC immutable collections also appear to only be part of .NET Core, not .NET Standard, and require a shim on said projects, explaining the above errors.

Package installation fails due to missing Nopen.NET.Runtime

When trying to install the latest version, I get the following error:

Unable to find package Nopen.NET.Runtime. No packages exist with this id in source(s): nuget.org

I think this happened because the runtime is now a separate assembly, but it's not being pushed to NuGet separately.

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.