Skip to content

Support wildcards in <PolySharpIncludeGeneratedTypes> property #128

@Athari

Description

@Athari

Description

  1. Support wildcards like "System.Diagnostics.CodeAnalysis.*Null*Attribute" and "**.*Null*Attribute" in the <PolySharpIncludeGeneratedTypes> MSBuild property.
  2. Support type names without namespaces. I think it's safe to assume that there won't be name conflicts.

Rationale

Currently <PolySharpIncludeGeneratedTypes> property requires explicitly listing the full name of every type. If I want only attributes related to nullability analysis, I have to list 9 attributes, with namespaces and all. It would be nice to significantly simplify configuration with wildcards:

  1. Shorter: potentially just one short line instead of a huge block of copy-pasted namespaces.
  2. Easier: no need to remember full namespaces of every type.

Proposed API

<PolySharpIncludeGeneratedTypes>
    **.*Null*Attribute; Range; Index; System.Runtime.Versioning.*
</PolySharpIncludeGeneratedTypes>

replaces:

<PolySharpIncludeGeneratedTypes>
  System.Diagnostics.CodeAnalysis.AllowNullAttribute;
  System.Diagnostics.CodeAnalysis.DisallowNullAttribute;
  System.Diagnostics.CodeAnalysis.MaybeNullAttribute;
  System.Diagnostics.CodeAnalysis.MaybeNullWhenAttribute;
  System.Diagnostics.CodeAnalysis.MemberNotNullAttribute;
  System.Diagnostics.CodeAnalysis.MemberNotNullWhenAttribute;
  System.Diagnostics.CodeAnalysis.NotNullAttribute;
  System.Diagnostics.CodeAnalysis.NotNullIfNotNullAttribute;
  System.Diagnostics.CodeAnalysis.NotNullWhenAttribute;
  System.Range;
  System.Index;
  System.Runtime.Versioning.RequiresPreviewFeaturesAttribute;
</PolySharpIncludeGeneratedTypes>
  1. strings without dots are prefixed with **.
  2. ** turns into .+ regex
  3. * turns into \w+ regex
  4. all regexes start with ^, end with $ and are case-insensitive
  5. all other characters are escaped

Drawbacks

  1. A bit more code in the library.

Alternatives

  1. Trickery with MSBuild transforms like:
    <ItemGroup>
      <PolySystem Include="Range;Index" />
      <PolyAnalysisAttrs Include="AllowNull;DisallowNull;MaybeNull;MaybeNullWhen;MemberNotNull;MemberNotNullWhen;NotNull;NotNullIfNotNull;NotNullWhen" />
    </ItemGroup>
    
    <ItemGroup>
      <PolyInclude Include="@(PolySystem->'System.%(Identity)')" />
      <PolyInclude Include="@(PolyAnalysisAttrs->'System.Diagnostics.CodeAnalysis.%(Identity)Attribute')" />
    </ItemGroup>
    <Target Name="PolySharpIncludeTransform" BeforeTargets="ConfigurePolySharpMSBuildProperties">
      <PropertyGroup>
        <PolySharpIncludeGeneratedTypes>@(PolyInclude, ',');System.Runtime.Versioning.RequiresPreviewFeaturesAttribute</PolySharpIncludeGeneratedTypes>
      </PropertyGroup>
    </Target>
    It's marginally shorter, but 10x harder to understand and more fragile.

Other thoughts

  1. Can implement myself if approved

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions