-
Notifications
You must be signed in to change notification settings - Fork 510
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add pattern matching syntax types for C# 8
- Loading branch information
Showing
9 changed files
with
843 additions
and
50 deletions.
There are no files selected for viewing
100 changes: 50 additions & 50 deletions
100
StyleCop.Analyzers/StyleCop.Analyzers/Lightup/CSharp8.md
Large diffs are not rendered by default.
Oops, something went wrong.
82 changes: 82 additions & 0 deletions
82
StyleCop.Analyzers/StyleCop.Analyzers/Lightup/DiscardPatternSyntaxWrapper.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | ||
} | ||
} | ||
} |
113 changes: 113 additions & 0 deletions
113
StyleCop.Analyzers/StyleCop.Analyzers/Lightup/PositionalPatternClauseSyntaxWrapper.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | ||
} | ||
} | ||
} |
113 changes: 113 additions & 0 deletions
113
StyleCop.Analyzers/StyleCop.Analyzers/Lightup/PropertyPatternClauseSyntaxWrapper.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | ||
} | ||
} | ||
} |
Oops, something went wrong.