realvizu / NsDepCop

NsDepCop is a static code analysis tool that helps to enforce namespace dependency rules in C# projects. No more unplanned or unnoticed dependencies in your system.
GNU General Public License v2.0
200 stars 32 forks source link

Add Allowed Types/Members to build warning #67

Closed kirides closed 1 year ago

kirides commented 1 year ago

Currently if we only allow users a subset of Types in a namespace, the user won't know which types are allowed unless they read through configs and documentation.

<Allowed From="*" To="MassTransit">
    <VisibleMembers>
        <Type Name="IPublishEndpoint" />
        <Type Name="ISendEndpointProvider" />
        <Type Name="ISendEndpoint" />
    </VisibleMembers>
</Allowed>

i propose that the emitted error in such cases

Error NSDEPCOP01 : Illegal namespace reference: Service.Api->MassTransit (Type: SomeController->IBus)

becomes more like this:

Error NSDEPCOP01 : Illegal namespace reference: Service.Api->MassTransit (Type: SomeController->IBus) Allowed Types are: IPublishEndpoint, ISendEndpoint, ...

This would improve consuming such warnings/errors much easier

realvizu commented 1 year ago

Thanks for the proposal, this would be a helpful enhancement indeed.

If anyone wants to contribute, I'm happy to help you figure out how to implement and unit test it.

SteveDunn commented 1 year ago

I'd be happy to take a look at this one! I was just replying to someone and mentioned nsdepcop, and spotted this issue. I'd be happy to contribute to such a great utility!

realvizu commented 1 year ago

Great! Thanks for volunteering!

Here are some thoughts that may help you get started.

Regarding unit testing:

Hope I didn't overlook any serious roadblocks. Anyway, if you run into problems, then don't hesitate to reach out!

realvizu commented 1 year ago

Here's a quick overview of the types involved: image

SteveDunn commented 1 year ago

Many thank for the detailed information! I'll hopefully have some time this week. Thanks again!

realvizu commented 1 year ago

The new version is live on NuGet: https://www.nuget.org/packages/NsDepCop/2.3.0 Thanks for the proposal and the implementation!