|
| 1 | +// Copyright (c) Tunnel Vision Laboratories, LLC. All Rights Reserved. |
| 2 | +// Licensed under the Apache License, Version 2.0. See LICENSE in the project root for license information. |
| 3 | + |
| 4 | +namespace StyleCop.Analyzers.Lightup |
| 5 | +{ |
| 6 | + using System; |
| 7 | + using System.Reflection; |
| 8 | + using Microsoft.CodeAnalysis; |
| 9 | + using Microsoft.CodeAnalysis.CSharp; |
| 10 | + using Microsoft.CodeAnalysis.CSharp.Syntax; |
| 11 | + |
| 12 | + internal struct CasePatternSwitchLabelSyntaxWrapper : ISyntaxWrapper<SwitchLabelSyntax> |
| 13 | + { |
| 14 | + private const string CasePatternSwitchLabelSyntaxTypeName = "Microsoft.CodeAnalysis.CSharp.Syntax.CasePatternSwitchLabelSyntax"; |
| 15 | + private static readonly Type CasePatternSwitchLabelSyntaxType; |
| 16 | + |
| 17 | + private static readonly Func<SwitchLabelSyntax, CSharpSyntaxNode> PatternAccessor; |
| 18 | + private static readonly Func<SwitchLabelSyntax, CSharpSyntaxNode> WhenClauseAccessor; |
| 19 | + private static readonly Func<SwitchLabelSyntax, SyntaxToken, SwitchLabelSyntax> WithKeywordAccessor; |
| 20 | + private static readonly Func<SwitchLabelSyntax, SyntaxToken, SwitchLabelSyntax> WithColonTokenAccessor; |
| 21 | + private static readonly Func<SwitchLabelSyntax, CSharpSyntaxNode, SwitchLabelSyntax> WithPatternAccessor; |
| 22 | + private static readonly Func<SwitchLabelSyntax, CSharpSyntaxNode, SwitchLabelSyntax> WithWhenClauseAccessor; |
| 23 | + |
| 24 | + private readonly SwitchLabelSyntax node; |
| 25 | + |
| 26 | + static CasePatternSwitchLabelSyntaxWrapper() |
| 27 | + { |
| 28 | + CasePatternSwitchLabelSyntaxType = typeof(CSharpSyntaxNode).GetTypeInfo().Assembly.GetType(CasePatternSwitchLabelSyntaxTypeName); |
| 29 | + PatternAccessor = LightupHelpers.CreateSyntaxPropertyAccessor<SwitchLabelSyntax, CSharpSyntaxNode>(CasePatternSwitchLabelSyntaxType, nameof(Pattern)); |
| 30 | + WhenClauseAccessor = LightupHelpers.CreateSyntaxPropertyAccessor<SwitchLabelSyntax, CSharpSyntaxNode>(CasePatternSwitchLabelSyntaxType, nameof(WhenClause)); |
| 31 | + WithKeywordAccessor = LightupHelpers.CreateSyntaxWithPropertyAccessor<SwitchLabelSyntax, SyntaxToken>(CasePatternSwitchLabelSyntaxType, nameof(SwitchLabelSyntax.Keyword)); |
| 32 | + WithColonTokenAccessor = LightupHelpers.CreateSyntaxWithPropertyAccessor<SwitchLabelSyntax, SyntaxToken>(CasePatternSwitchLabelSyntaxType, nameof(SwitchLabelSyntax.ColonToken)); |
| 33 | + WithPatternAccessor = LightupHelpers.CreateSyntaxWithPropertyAccessor<SwitchLabelSyntax, CSharpSyntaxNode>(CasePatternSwitchLabelSyntaxType, nameof(Pattern)); |
| 34 | + WithWhenClauseAccessor = LightupHelpers.CreateSyntaxWithPropertyAccessor<SwitchLabelSyntax, CSharpSyntaxNode>(CasePatternSwitchLabelSyntaxType, nameof(WhenClause)); |
| 35 | + } |
| 36 | + |
| 37 | + private CasePatternSwitchLabelSyntaxWrapper(SwitchLabelSyntax node) |
| 38 | + { |
| 39 | + this.node = node; |
| 40 | + } |
| 41 | + |
| 42 | + public SwitchLabelSyntax SyntaxNode => this.node; |
| 43 | + |
| 44 | + public PatternSyntaxWrapper Pattern |
| 45 | + { |
| 46 | + get |
| 47 | + { |
| 48 | + return (PatternSyntaxWrapper)PatternAccessor(this.SyntaxNode); |
| 49 | + } |
| 50 | + } |
| 51 | + |
| 52 | + public WhenClauseSyntaxWrapper WhenClause |
| 53 | + { |
| 54 | + get |
| 55 | + { |
| 56 | + return (WhenClauseSyntaxWrapper)WhenClauseAccessor(this.SyntaxNode); |
| 57 | + } |
| 58 | + } |
| 59 | + |
| 60 | + public static explicit operator CasePatternSwitchLabelSyntaxWrapper(SyntaxNode node) |
| 61 | + { |
| 62 | + if (node == null) |
| 63 | + { |
| 64 | + return default(CasePatternSwitchLabelSyntaxWrapper); |
| 65 | + } |
| 66 | + |
| 67 | + if (!IsInstance(node)) |
| 68 | + { |
| 69 | + throw new InvalidCastException($"Cannot cast '{node.GetType().FullName}' to '{CasePatternSwitchLabelSyntaxTypeName}'"); |
| 70 | + } |
| 71 | + |
| 72 | + return new CasePatternSwitchLabelSyntaxWrapper((SwitchLabelSyntax)node); |
| 73 | + } |
| 74 | + |
| 75 | + public static implicit operator SwitchLabelSyntax(CasePatternSwitchLabelSyntaxWrapper wrapper) |
| 76 | + { |
| 77 | + return wrapper.node; |
| 78 | + } |
| 79 | + |
| 80 | + public static bool IsInstance(SyntaxNode node) |
| 81 | + { |
| 82 | + return node != null && LightupHelpers.CanWrapNode(node, CasePatternSwitchLabelSyntaxType); |
| 83 | + } |
| 84 | + |
| 85 | + public CasePatternSwitchLabelSyntaxWrapper WithKeyword(SyntaxToken keyword) |
| 86 | + { |
| 87 | + return new CasePatternSwitchLabelSyntaxWrapper(WithKeywordAccessor(this.SyntaxNode, keyword)); |
| 88 | + } |
| 89 | + |
| 90 | + public CasePatternSwitchLabelSyntaxWrapper WithColonToken(SyntaxToken colonToken) |
| 91 | + { |
| 92 | + return new CasePatternSwitchLabelSyntaxWrapper(WithColonTokenAccessor(this.SyntaxNode, colonToken)); |
| 93 | + } |
| 94 | + |
| 95 | + public CasePatternSwitchLabelSyntaxWrapper WithPattern(PatternSyntaxWrapper pattern) |
| 96 | + { |
| 97 | + return new CasePatternSwitchLabelSyntaxWrapper(WithPatternAccessor(this.SyntaxNode, pattern)); |
| 98 | + } |
| 99 | + |
| 100 | + public CasePatternSwitchLabelSyntaxWrapper WithWhenClause(WhenClauseSyntaxWrapper whenClause) |
| 101 | + { |
| 102 | + return new CasePatternSwitchLabelSyntaxWrapper(WithWhenClauseAccessor(this.SyntaxNode, whenClause)); |
| 103 | + } |
| 104 | + } |
| 105 | +} |
0 commit comments