-
Notifications
You must be signed in to change notification settings - Fork 509
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3229 from sharwell/operation-sa1142
Use IOperation APIs for SA1142 when supported
- Loading branch information
Showing
6 changed files
with
202 additions
and
1 deletion.
There are no files selected for viewing
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
81 changes: 81 additions & 0 deletions
81
StyleCop.Analyzers/StyleCop.Analyzers/Lightup/IFieldReferenceOperationWrapper.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,81 @@ | ||
// Copyright (c) Tunnel Vision Laboratories, LLC. All Rights Reserved. | ||
// Licensed under the MIT License. See LICENSE in the project root for license information. | ||
|
||
namespace StyleCop.Analyzers.Lightup | ||
{ | ||
using System; | ||
using Microsoft.CodeAnalysis; | ||
|
||
internal readonly struct IFieldReferenceOperationWrapper : IOperationWrapper | ||
{ | ||
internal const string WrappedTypeName = "Microsoft.CodeAnalysis.Operations.IFieldReferenceOperation"; | ||
private static readonly Type WrappedType; | ||
|
||
private static readonly Func<IOperation, IFieldSymbol> FieldAccessor; | ||
private static readonly Func<IOperation, bool> IsDeclarationAccessor; | ||
|
||
private readonly IOperation operation; | ||
|
||
static IFieldReferenceOperationWrapper() | ||
{ | ||
WrappedType = WrapperHelper.GetWrappedType(typeof(IFieldReferenceOperationWrapper)); | ||
FieldAccessor = LightupHelpers.CreateOperationPropertyAccessor<IOperation, IFieldSymbol>(WrappedType, nameof(Field)); | ||
IsDeclarationAccessor = LightupHelpers.CreateOperationPropertyAccessor<IOperation, bool>(WrappedType, nameof(IsDeclaration)); | ||
} | ||
|
||
private IFieldReferenceOperationWrapper(IOperation operation) | ||
{ | ||
this.operation = operation; | ||
} | ||
|
||
public IOperation WrappedOperation => this.operation; | ||
|
||
public ITypeSymbol Type => this.WrappedOperation.Type; | ||
|
||
public IFieldSymbol Field | ||
{ | ||
get | ||
{ | ||
return FieldAccessor(this.WrappedOperation); | ||
} | ||
} | ||
|
||
public bool IsDeclaration | ||
{ | ||
get | ||
{ | ||
return IsDeclarationAccessor(this.WrappedOperation); | ||
} | ||
} | ||
|
||
public IOperation Instance => ((IMemberReferenceOperationWrapper)this).Instance; | ||
|
||
public ISymbol Member => ((IMemberReferenceOperationWrapper)this).Member; | ||
|
||
public static explicit operator IFieldReferenceOperationWrapper(IMemberReferenceOperationWrapper wrapper) | ||
=> FromOperation(wrapper.WrappedOperation); | ||
|
||
public static implicit operator IMemberReferenceOperationWrapper(IFieldReferenceOperationWrapper wrapper) | ||
=> IMemberReferenceOperationWrapper.FromUpcast(wrapper.operation); | ||
|
||
public static IFieldReferenceOperationWrapper FromOperation(IOperation operation) | ||
{ | ||
if (operation == null) | ||
{ | ||
return default; | ||
} | ||
|
||
if (!IsInstance(operation)) | ||
{ | ||
throw new InvalidCastException($"Cannot cast '{operation.GetType().FullName}' to '{WrappedTypeName}'"); | ||
} | ||
|
||
return new IFieldReferenceOperationWrapper(operation); | ||
} | ||
|
||
public static bool IsInstance(IOperation operation) | ||
{ | ||
return operation != null && LightupHelpers.CanWrapOperation(operation, WrappedType); | ||
} | ||
} | ||
} |
76 changes: 76 additions & 0 deletions
76
StyleCop.Analyzers/StyleCop.Analyzers/Lightup/IMemberReferenceOperationWrapper.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,76 @@ | ||
// Copyright (c) Tunnel Vision Laboratories, LLC. All Rights Reserved. | ||
// Licensed under the MIT License. See LICENSE in the project root for license information. | ||
|
||
namespace StyleCop.Analyzers.Lightup | ||
{ | ||
using System; | ||
using Microsoft.CodeAnalysis; | ||
|
||
internal readonly struct IMemberReferenceOperationWrapper : IOperationWrapper | ||
{ | ||
internal const string WrappedTypeName = "Microsoft.CodeAnalysis.Operations.IMemberReferenceOperation"; | ||
private static readonly Type WrappedType; | ||
|
||
private static readonly Func<IOperation, IOperation> InstanceAccessor; | ||
private static readonly Func<IOperation, ISymbol> MemberAccessor; | ||
|
||
private readonly IOperation operation; | ||
|
||
static IMemberReferenceOperationWrapper() | ||
{ | ||
WrappedType = WrapperHelper.GetWrappedType(typeof(IFieldReferenceOperationWrapper)); | ||
InstanceAccessor = LightupHelpers.CreateOperationPropertyAccessor<IOperation, IOperation>(WrappedType, nameof(Instance)); | ||
MemberAccessor = LightupHelpers.CreateOperationPropertyAccessor<IOperation, ISymbol>(WrappedType, nameof(Member)); | ||
} | ||
|
||
private IMemberReferenceOperationWrapper(IOperation operation) | ||
{ | ||
this.operation = operation; | ||
} | ||
|
||
public IOperation WrappedOperation => this.operation; | ||
|
||
public ITypeSymbol Type => this.WrappedOperation.Type; | ||
|
||
public IOperation Instance | ||
{ | ||
get | ||
{ | ||
return InstanceAccessor(this.WrappedOperation); | ||
} | ||
} | ||
|
||
public ISymbol Member | ||
{ | ||
get | ||
{ | ||
return MemberAccessor(this.WrappedOperation); | ||
} | ||
} | ||
|
||
public static IMemberReferenceOperationWrapper FromOperation(IOperation operation) | ||
{ | ||
if (operation == null) | ||
{ | ||
return default; | ||
} | ||
|
||
if (!IsInstance(operation)) | ||
{ | ||
throw new InvalidCastException($"Cannot cast '{operation.GetType().FullName}' to '{WrappedTypeName}'"); | ||
} | ||
|
||
return new IMemberReferenceOperationWrapper(operation); | ||
} | ||
|
||
public static bool IsInstance(IOperation operation) | ||
{ | ||
return operation != null && LightupHelpers.CanWrapOperation(operation, WrappedType); | ||
} | ||
|
||
internal static IMemberReferenceOperationWrapper FromUpcast(IOperation operation) | ||
{ | ||
return new IMemberReferenceOperationWrapper(operation); | ||
} | ||
} | ||
} |
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
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
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