diff --git a/StyleCop.Analyzers/StyleCop.Analyzers/Lightup/CSharp8.md b/StyleCop.Analyzers/StyleCop.Analyzers/Lightup/CSharp8.md index f6beb0e9f..f37754660 100644 --- a/StyleCop.Analyzers/StyleCop.Analyzers/Lightup/CSharp8.md +++ b/StyleCop.Analyzers/StyleCop.Analyzers/Lightup/CSharp8.md @@ -543,14 +543,14 @@ See [Microsoft.CodeAnalysis.CSharp release/dev16.3@c955f3c99b5698c906e0700ef691b * [x] `Microsoft.CodeAnalysis.CSharp.Syntax.PropertyPatternClauseSyntax.WithCloseBraceToken(Microsoft.CodeAnalysis.SyntaxToken closeBraceToken) -> Microsoft.CodeAnalysis.CSharp.Syntax.PropertyPatternClauseSyntax` * [x] `Microsoft.CodeAnalysis.CSharp.Syntax.PropertyPatternClauseSyntax.WithOpenBraceToken(Microsoft.CodeAnalysis.SyntaxToken openBraceToken) -> Microsoft.CodeAnalysis.CSharp.Syntax.PropertyPatternClauseSyntax` * [x] `Microsoft.CodeAnalysis.CSharp.Syntax.PropertyPatternClauseSyntax.WithSubpatterns(Microsoft.CodeAnalysis.SeparatedSyntaxList subpatterns) -> Microsoft.CodeAnalysis.CSharp.Syntax.PropertyPatternClauseSyntax` -* [ ] `Microsoft.CodeAnalysis.CSharp.Syntax.RangeExpressionSyntax` -* [ ] `Microsoft.CodeAnalysis.CSharp.Syntax.RangeExpressionSyntax.LeftOperand.get -> Microsoft.CodeAnalysis.CSharp.Syntax.ExpressionSyntax` -* [ ] `Microsoft.CodeAnalysis.CSharp.Syntax.RangeExpressionSyntax.OperatorToken.get -> Microsoft.CodeAnalysis.SyntaxToken` -* [ ] `Microsoft.CodeAnalysis.CSharp.Syntax.RangeExpressionSyntax.RightOperand.get -> Microsoft.CodeAnalysis.CSharp.Syntax.ExpressionSyntax` -* [ ] `Microsoft.CodeAnalysis.CSharp.Syntax.RangeExpressionSyntax.Update(Microsoft.CodeAnalysis.CSharp.Syntax.ExpressionSyntax leftOperand, Microsoft.CodeAnalysis.SyntaxToken operatorToken, Microsoft.CodeAnalysis.CSharp.Syntax.ExpressionSyntax rightOperand) -> Microsoft.CodeAnalysis.CSharp.Syntax.RangeExpressionSyntax` -* [ ] `Microsoft.CodeAnalysis.CSharp.Syntax.RangeExpressionSyntax.WithLeftOperand(Microsoft.CodeAnalysis.CSharp.Syntax.ExpressionSyntax leftOperand) -> Microsoft.CodeAnalysis.CSharp.Syntax.RangeExpressionSyntax` -* [ ] `Microsoft.CodeAnalysis.CSharp.Syntax.RangeExpressionSyntax.WithOperatorToken(Microsoft.CodeAnalysis.SyntaxToken operatorToken) -> Microsoft.CodeAnalysis.CSharp.Syntax.RangeExpressionSyntax` -* [ ] `Microsoft.CodeAnalysis.CSharp.Syntax.RangeExpressionSyntax.WithRightOperand(Microsoft.CodeAnalysis.CSharp.Syntax.ExpressionSyntax rightOperand) -> Microsoft.CodeAnalysis.CSharp.Syntax.RangeExpressionSyntax` +* [x] `Microsoft.CodeAnalysis.CSharp.Syntax.RangeExpressionSyntax` +* [x] `Microsoft.CodeAnalysis.CSharp.Syntax.RangeExpressionSyntax.LeftOperand.get -> Microsoft.CodeAnalysis.CSharp.Syntax.ExpressionSyntax` +* [x] `Microsoft.CodeAnalysis.CSharp.Syntax.RangeExpressionSyntax.OperatorToken.get -> Microsoft.CodeAnalysis.SyntaxToken` +* [x] `Microsoft.CodeAnalysis.CSharp.Syntax.RangeExpressionSyntax.RightOperand.get -> Microsoft.CodeAnalysis.CSharp.Syntax.ExpressionSyntax` +* [x] ~~`Microsoft.CodeAnalysis.CSharp.Syntax.RangeExpressionSyntax.Update(Microsoft.CodeAnalysis.CSharp.Syntax.ExpressionSyntax leftOperand, Microsoft.CodeAnalysis.SyntaxToken operatorToken, Microsoft.CodeAnalysis.CSharp.Syntax.ExpressionSyntax rightOperand) -> Microsoft.CodeAnalysis.CSharp.Syntax.RangeExpressionSyntax`~~ +* [x] `Microsoft.CodeAnalysis.CSharp.Syntax.RangeExpressionSyntax.WithLeftOperand(Microsoft.CodeAnalysis.CSharp.Syntax.ExpressionSyntax leftOperand) -> Microsoft.CodeAnalysis.CSharp.Syntax.RangeExpressionSyntax` +* [x] `Microsoft.CodeAnalysis.CSharp.Syntax.RangeExpressionSyntax.WithOperatorToken(Microsoft.CodeAnalysis.SyntaxToken operatorToken) -> Microsoft.CodeAnalysis.CSharp.Syntax.RangeExpressionSyntax` +* [x] `Microsoft.CodeAnalysis.CSharp.Syntax.RangeExpressionSyntax.WithRightOperand(Microsoft.CodeAnalysis.CSharp.Syntax.ExpressionSyntax rightOperand) -> Microsoft.CodeAnalysis.CSharp.Syntax.RangeExpressionSyntax` * [x] `Microsoft.CodeAnalysis.CSharp.Syntax.RecursivePatternSyntax` * [x] `Microsoft.CodeAnalysis.CSharp.Syntax.RecursivePatternSyntax.AddPositionalPatternClauseSubpatterns(params Microsoft.CodeAnalysis.CSharp.Syntax.SubpatternSyntax[] items) -> Microsoft.CodeAnalysis.CSharp.Syntax.RecursivePatternSyntax` * [x] `Microsoft.CodeAnalysis.CSharp.Syntax.RecursivePatternSyntax.AddPropertyPatternClauseSubpatterns(params Microsoft.CodeAnalysis.CSharp.Syntax.SubpatternSyntax[] items) -> Microsoft.CodeAnalysis.CSharp.Syntax.RecursivePatternSyntax` diff --git a/StyleCop.Analyzers/StyleCop.Analyzers/Lightup/RangeExpressionSyntaxWrapper.cs b/StyleCop.Analyzers/StyleCop.Analyzers/Lightup/RangeExpressionSyntaxWrapper.cs new file mode 100644 index 000000000..47fcfa211 --- /dev/null +++ b/StyleCop.Analyzers/StyleCop.Analyzers/Lightup/RangeExpressionSyntaxWrapper.cs @@ -0,0 +1,108 @@ +// 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.Syntax; + + internal struct RangeExpressionSyntaxWrapper : ISyntaxWrapper + { + internal const string WrappedTypeName = "Microsoft.CodeAnalysis.CSharp.Syntax.RangeExpressionSyntax"; + private static readonly Type WrappedType; + + private static readonly Func LeftOperandAccessor; + private static readonly Func OperatorTokenAccessor; + private static readonly Func RightOperandAccessor; + + private static readonly Func WithLeftOperandAccessor; + private static readonly Func WithOperatorTokenAccessor; + private static readonly Func WithRightOperandAccessor; + + private readonly ExpressionSyntax node; + + static RangeExpressionSyntaxWrapper() + { + WrappedType = WrapperHelper.GetWrappedType(typeof(RangeExpressionSyntaxWrapper)); + LeftOperandAccessor = LightupHelpers.CreateSyntaxPropertyAccessor(WrappedType, nameof(LeftOperand)); + OperatorTokenAccessor = LightupHelpers.CreateSyntaxPropertyAccessor(WrappedType, nameof(OperatorToken)); + RightOperandAccessor = LightupHelpers.CreateSyntaxPropertyAccessor(WrappedType, nameof(RightOperand)); + + WithLeftOperandAccessor = LightupHelpers.CreateSyntaxWithPropertyAccessor(WrappedType, nameof(LeftOperand)); + WithOperatorTokenAccessor = LightupHelpers.CreateSyntaxWithPropertyAccessor(WrappedType, nameof(OperatorToken)); + WithRightOperandAccessor = LightupHelpers.CreateSyntaxWithPropertyAccessor(WrappedType, nameof(RightOperand)); + } + + private RangeExpressionSyntaxWrapper(ExpressionSyntax node) + { + this.node = node; + } + + public ExpressionSyntax SyntaxNode => this.node; + + public ExpressionSyntax LeftOperand + { + get + { + return LeftOperandAccessor(this.SyntaxNode); + } + } + + public SyntaxToken OperatorToken + { + get + { + return OperatorTokenAccessor(this.SyntaxNode); + } + } + + public ExpressionSyntax RightOperand + { + get + { + return RightOperandAccessor(this.SyntaxNode); + } + } + + public static explicit operator RangeExpressionSyntaxWrapper(ExpressionSyntax node) + { + if (node == null) + { + return default; + } + + if (!IsInstance(node)) + { + throw new InvalidCastException($"Cannot cast '{node.GetType().FullName}' to '{WrappedTypeName}'"); + } + + return new RangeExpressionSyntaxWrapper((ExpressionSyntax)node); + } + + public static implicit operator ExpressionSyntax(RangeExpressionSyntaxWrapper wrapper) + { + return wrapper.node; + } + + public static bool IsInstance(SyntaxNode node) + { + return node != null && LightupHelpers.CanWrapNode(node, WrappedType); + } + + public RangeExpressionSyntaxWrapper WithLeftOperand(ExpressionSyntax leftOperand) + { + return new RangeExpressionSyntaxWrapper(WithLeftOperandAccessor(this.SyntaxNode, leftOperand)); + } + + public RangeExpressionSyntaxWrapper WithOperatorToken(SyntaxToken operatorToken) + { + return new RangeExpressionSyntaxWrapper(WithOperatorTokenAccessor(this.SyntaxNode, operatorToken)); + } + + public RangeExpressionSyntaxWrapper WithRightOperand(ExpressionSyntax rightOperand) + { + return new RangeExpressionSyntaxWrapper(WithRightOperandAccessor(this.SyntaxNode, rightOperand)); + } + } +} diff --git a/StyleCop.Analyzers/StyleCop.Analyzers/Lightup/WrapperHelper.cs b/StyleCop.Analyzers/StyleCop.Analyzers/Lightup/WrapperHelper.cs index 8b12d1ce0..984552040 100644 --- a/StyleCop.Analyzers/StyleCop.Analyzers/Lightup/WrapperHelper.cs +++ b/StyleCop.Analyzers/StyleCop.Analyzers/Lightup/WrapperHelper.cs @@ -38,6 +38,7 @@ static WrapperHelper() builder.Add(typeof(PatternSyntaxWrapper), codeAnalysisAssembly.GetType(PatternSyntaxWrapper.WrappedTypeName)); builder.Add(typeof(PositionalPatternClauseSyntaxWrapper), codeAnalysisAssembly.GetType(PositionalPatternClauseSyntaxWrapper.WrappedTypeName)); builder.Add(typeof(PropertyPatternClauseSyntaxWrapper), codeAnalysisAssembly.GetType(PropertyPatternClauseSyntaxWrapper.WrappedTypeName)); + builder.Add(typeof(RangeExpressionSyntaxWrapper), codeAnalysisAssembly.GetType(RangeExpressionSyntaxWrapper.WrappedTypeName)); builder.Add(typeof(RecursivePatternSyntaxWrapper), codeAnalysisAssembly.GetType(RecursivePatternSyntaxWrapper.WrappedTypeName)); builder.Add(typeof(RefExpressionSyntaxWrapper), codeAnalysisAssembly.GetType(RefExpressionSyntaxWrapper.WrappedTypeName)); builder.Add(typeof(RefTypeSyntaxWrapper), codeAnalysisAssembly.GetType(RefTypeSyntaxWrapper.WrappedTypeName));