Skip to content

Commit

Permalink
Add pattern matching syntax types for C# 8
Browse files Browse the repository at this point in the history
  • Loading branch information
sharwell committed Sep 3, 2019
1 parent e57e1b3 commit 0475f2c
Show file tree
Hide file tree
Showing 9 changed files with 843 additions and 50 deletions.
100 changes: 50 additions & 50 deletions StyleCop.Analyzers/StyleCop.Analyzers/Lightup/CSharp8.md

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
// Copyright (c) Tunnel Vision Laboratories, LLC. All Rights Reserved.
// Licensed under the Apache License, Version 2.0. See LICENSE in the project root for license information.

namespace StyleCop.Analyzers.Lightup
{
using System;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp;

internal struct DiscardPatternSyntaxWrapper : ISyntaxWrapper<CSharpSyntaxNode>
{
internal const string WrappedTypeName = "Microsoft.CodeAnalysis.CSharp.Syntax.DiscardPatternSyntax";
private static readonly Type WrappedType;

private static readonly Func<CSharpSyntaxNode, SyntaxToken> UnderscoreTokenAccessor;
private static readonly Func<CSharpSyntaxNode, SyntaxToken, CSharpSyntaxNode> WithUnderscoreTokenAccessor;

private readonly CSharpSyntaxNode node;

static DiscardPatternSyntaxWrapper()
{
WrappedType = WrapperHelper.GetWrappedType(typeof(DiscardPatternSyntaxWrapper));
UnderscoreTokenAccessor = LightupHelpers.CreateSyntaxPropertyAccessor<CSharpSyntaxNode, SyntaxToken>(WrappedType, nameof(UnderscoreToken));
WithUnderscoreTokenAccessor = LightupHelpers.CreateSyntaxWithPropertyAccessor<CSharpSyntaxNode, SyntaxToken>(WrappedType, nameof(UnderscoreToken));
}

private DiscardPatternSyntaxWrapper(CSharpSyntaxNode node)
{
this.node = node;
}

public CSharpSyntaxNode SyntaxNode => this.node;

public SyntaxToken UnderscoreToken
{
get
{
return UnderscoreTokenAccessor(this.SyntaxNode);
}
}

public static explicit operator DiscardPatternSyntaxWrapper(PatternSyntaxWrapper node)
{
return (DiscardPatternSyntaxWrapper)node.SyntaxNode;
}

public static explicit operator DiscardPatternSyntaxWrapper(SyntaxNode node)
{
if (node == null)
{
return default;
}

if (!IsInstance(node))
{
throw new InvalidCastException($"Cannot cast '{node.GetType().FullName}' to '{WrappedTypeName}'");
}

return new DiscardPatternSyntaxWrapper((CSharpSyntaxNode)node);
}

public static implicit operator PatternSyntaxWrapper(DiscardPatternSyntaxWrapper wrapper)
{
return PatternSyntaxWrapper.FromUpcast(wrapper.node);
}

public static implicit operator CSharpSyntaxNode(DiscardPatternSyntaxWrapper wrapper)
{
return wrapper.node;
}

public static bool IsInstance(SyntaxNode node)
{
return node != null && LightupHelpers.CanWrapNode(node, WrappedType);
}

public DiscardPatternSyntaxWrapper WithUnderscoreToken(SyntaxToken identifier)
{
return new DiscardPatternSyntaxWrapper(WithUnderscoreTokenAccessor(this.SyntaxNode, identifier));
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
// Copyright (c) Tunnel Vision Laboratories, LLC. All Rights Reserved.
// Licensed under the Apache License, Version 2.0. See LICENSE in the project root for license information.

namespace StyleCop.Analyzers.Lightup
{
using System;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp;

internal struct PositionalPatternClauseSyntaxWrapper : ISyntaxWrapper<CSharpSyntaxNode>
{
internal const string WrappedTypeName = "Microsoft.CodeAnalysis.CSharp.Syntax.PositionalPatternClauseSyntax";
private static readonly Type WrappedType;

private static readonly Func<CSharpSyntaxNode, SyntaxToken> OpenParenTokenAccessor;
private static readonly Func<CSharpSyntaxNode, SeparatedSyntaxListWrapper<SubpatternSyntaxWrapper>> SubpatternsAccessor;
private static readonly Func<CSharpSyntaxNode, SyntaxToken> CloseParenTokenAccessor;

private static readonly Func<CSharpSyntaxNode, SyntaxToken, CSharpSyntaxNode> WithOpenParenTokenAccessor;
private static readonly Func<CSharpSyntaxNode, SeparatedSyntaxListWrapper<SubpatternSyntaxWrapper>, CSharpSyntaxNode> WithSubpatternsAccessor;
private static readonly Func<CSharpSyntaxNode, SyntaxToken, CSharpSyntaxNode> WithCloseParenTokenAccessor;

private readonly CSharpSyntaxNode node;

static PositionalPatternClauseSyntaxWrapper()
{
WrappedType = WrapperHelper.GetWrappedType(typeof(PositionalPatternClauseSyntaxWrapper));
OpenParenTokenAccessor = LightupHelpers.CreateSyntaxPropertyAccessor<CSharpSyntaxNode, SyntaxToken>(WrappedType, nameof(OpenParenToken));
SubpatternsAccessor = LightupHelpers.CreateSeparatedSyntaxListPropertyAccessor<CSharpSyntaxNode, SubpatternSyntaxWrapper>(WrappedType, nameof(Subpatterns));
CloseParenTokenAccessor = LightupHelpers.CreateSyntaxPropertyAccessor<CSharpSyntaxNode, SyntaxToken>(WrappedType, nameof(CloseParenToken));

WithOpenParenTokenAccessor = LightupHelpers.CreateSyntaxWithPropertyAccessor<CSharpSyntaxNode, SyntaxToken>(WrappedType, nameof(OpenParenToken));
WithSubpatternsAccessor = LightupHelpers.CreateSeparatedSyntaxListWithPropertyAccessor<CSharpSyntaxNode, SubpatternSyntaxWrapper>(WrappedType, nameof(Subpatterns));
WithCloseParenTokenAccessor = LightupHelpers.CreateSyntaxWithPropertyAccessor<CSharpSyntaxNode, SyntaxToken>(WrappedType, nameof(CloseParenToken));
}

private PositionalPatternClauseSyntaxWrapper(CSharpSyntaxNode node)
{
this.node = node;
}

public CSharpSyntaxNode SyntaxNode => this.node;

public SyntaxToken OpenParenToken
{
get
{
return OpenParenTokenAccessor(this.SyntaxNode);
}
}

public SeparatedSyntaxListWrapper<SubpatternSyntaxWrapper> Subpatterns
{
get
{
return SubpatternsAccessor(this.SyntaxNode);
}
}

public SyntaxToken CloseParenToken
{
get
{
return CloseParenTokenAccessor(this.SyntaxNode);
}
}

public static explicit operator PositionalPatternClauseSyntaxWrapper(SyntaxNode node)
{
if (node == null)
{
return default;
}

if (!IsInstance(node))
{
throw new InvalidCastException($"Cannot cast '{node.GetType().FullName}' to '{WrappedTypeName}'");
}

return new PositionalPatternClauseSyntaxWrapper((CSharpSyntaxNode)node);
}

public static implicit operator CSharpSyntaxNode(PositionalPatternClauseSyntaxWrapper wrapper)
{
return wrapper.node;
}

public static bool IsInstance(SyntaxNode node)
{
return node != null && LightupHelpers.CanWrapNode(node, WrappedType);
}

public PositionalPatternClauseSyntaxWrapper AddSubpatterns(params SubpatternSyntaxWrapper[] items)
{
return new PositionalPatternClauseSyntaxWrapper(this.WithSubpatterns(this.Subpatterns.AddRange(items)));
}

public PositionalPatternClauseSyntaxWrapper WithOpenParenToken(SyntaxToken openParenToken)
{
return new PositionalPatternClauseSyntaxWrapper(WithOpenParenTokenAccessor(this.SyntaxNode, openParenToken));
}

public PositionalPatternClauseSyntaxWrapper WithSubpatterns(SeparatedSyntaxListWrapper<SubpatternSyntaxWrapper> subpatterns)
{
return new PositionalPatternClauseSyntaxWrapper(WithSubpatternsAccessor(this.SyntaxNode, subpatterns));
}

public PositionalPatternClauseSyntaxWrapper WithCloseParenToken(SyntaxToken closeParenToken)
{
return new PositionalPatternClauseSyntaxWrapper(WithCloseParenTokenAccessor(this.SyntaxNode, closeParenToken));
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
// Copyright (c) Tunnel Vision Laboratories, LLC. All Rights Reserved.
// Licensed under the Apache License, Version 2.0. See LICENSE in the project root for license information.

namespace StyleCop.Analyzers.Lightup
{
using System;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp;

internal struct PropertyPatternClauseSyntaxWrapper : ISyntaxWrapper<CSharpSyntaxNode>
{
internal const string WrappedTypeName = "Microsoft.CodeAnalysis.CSharp.Syntax.PropertyPatternClauseSyntax";
private static readonly Type WrappedType;

private static readonly Func<CSharpSyntaxNode, SyntaxToken> OpenBraceTokenAccessor;
private static readonly Func<CSharpSyntaxNode, SeparatedSyntaxListWrapper<SubpatternSyntaxWrapper>> SubpatternsAccessor;
private static readonly Func<CSharpSyntaxNode, SyntaxToken> CloseBraceTokenAccessor;

private static readonly Func<CSharpSyntaxNode, SyntaxToken, CSharpSyntaxNode> WithOpenBraceTokenAccessor;
private static readonly Func<CSharpSyntaxNode, SeparatedSyntaxListWrapper<SubpatternSyntaxWrapper>, CSharpSyntaxNode> WithSubpatternsAccessor;
private static readonly Func<CSharpSyntaxNode, SyntaxToken, CSharpSyntaxNode> WithCloseBraceTokenAccessor;

private readonly CSharpSyntaxNode node;

static PropertyPatternClauseSyntaxWrapper()
{
WrappedType = WrapperHelper.GetWrappedType(typeof(PropertyPatternClauseSyntaxWrapper));
OpenBraceTokenAccessor = LightupHelpers.CreateSyntaxPropertyAccessor<CSharpSyntaxNode, SyntaxToken>(WrappedType, nameof(OpenBraceToken));
SubpatternsAccessor = LightupHelpers.CreateSeparatedSyntaxListPropertyAccessor<CSharpSyntaxNode, SubpatternSyntaxWrapper>(WrappedType, nameof(Subpatterns));
CloseBraceTokenAccessor = LightupHelpers.CreateSyntaxPropertyAccessor<CSharpSyntaxNode, SyntaxToken>(WrappedType, nameof(CloseBraceToken));

WithOpenBraceTokenAccessor = LightupHelpers.CreateSyntaxWithPropertyAccessor<CSharpSyntaxNode, SyntaxToken>(WrappedType, nameof(OpenBraceToken));
WithSubpatternsAccessor = LightupHelpers.CreateSeparatedSyntaxListWithPropertyAccessor<CSharpSyntaxNode, SubpatternSyntaxWrapper>(WrappedType, nameof(Subpatterns));
WithCloseBraceTokenAccessor = LightupHelpers.CreateSyntaxWithPropertyAccessor<CSharpSyntaxNode, SyntaxToken>(WrappedType, nameof(CloseBraceToken));
}

private PropertyPatternClauseSyntaxWrapper(CSharpSyntaxNode node)
{
this.node = node;
}

public CSharpSyntaxNode SyntaxNode => this.node;

public SyntaxToken OpenBraceToken
{
get
{
return OpenBraceTokenAccessor(this.SyntaxNode);
}
}

public SeparatedSyntaxListWrapper<SubpatternSyntaxWrapper> Subpatterns
{
get
{
return SubpatternsAccessor(this.SyntaxNode);
}
}

public SyntaxToken CloseBraceToken
{
get
{
return CloseBraceTokenAccessor(this.SyntaxNode);
}
}

public static explicit operator PropertyPatternClauseSyntaxWrapper(SyntaxNode node)
{
if (node == null)
{
return default;
}

if (!IsInstance(node))
{
throw new InvalidCastException($"Cannot cast '{node.GetType().FullName}' to '{WrappedTypeName}'");
}

return new PropertyPatternClauseSyntaxWrapper((CSharpSyntaxNode)node);
}

public static implicit operator CSharpSyntaxNode(PropertyPatternClauseSyntaxWrapper wrapper)
{
return wrapper.node;
}

public static bool IsInstance(SyntaxNode node)
{
return node != null && LightupHelpers.CanWrapNode(node, WrappedType);
}

public PropertyPatternClauseSyntaxWrapper AddSubpatterns(params SubpatternSyntaxWrapper[] items)
{
return new PropertyPatternClauseSyntaxWrapper(this.WithSubpatterns(this.Subpatterns.AddRange(items)));
}

public PropertyPatternClauseSyntaxWrapper WithOpenBraceToken(SyntaxToken openBraceToken)
{
return new PropertyPatternClauseSyntaxWrapper(WithOpenBraceTokenAccessor(this.SyntaxNode, openBraceToken));
}

public PropertyPatternClauseSyntaxWrapper WithSubpatterns(SeparatedSyntaxListWrapper<SubpatternSyntaxWrapper> subpatterns)
{
return new PropertyPatternClauseSyntaxWrapper(WithSubpatternsAccessor(this.SyntaxNode, subpatterns));
}

public PropertyPatternClauseSyntaxWrapper WithCloseBraceToken(SyntaxToken closeBraceToken)
{
return new PropertyPatternClauseSyntaxWrapper(WithCloseBraceTokenAccessor(this.SyntaxNode, closeBraceToken));
}
}
}
Loading

0 comments on commit 0475f2c

Please sign in to comment.