-
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 #3227 from sharwell/operation-api
Use IOperation APIs for SA1129 when supported
- Loading branch information
Showing
11 changed files
with
562 additions
and
47 deletions.
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
50 changes: 50 additions & 0 deletions
50
StyleCop.Analyzers/StyleCop.Analyzers/Lightup/IArgumentOperationWrapper.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,50 @@ | ||
// 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 IArgumentOperationWrapper : IOperationWrapper | ||
{ | ||
internal const string WrappedTypeName = "Microsoft.CodeAnalysis.Operations.IArgumentOperation"; | ||
private static readonly Type WrappedType; | ||
|
||
private readonly IOperation operation; | ||
|
||
static IArgumentOperationWrapper() | ||
{ | ||
WrappedType = WrapperHelper.GetWrappedType(typeof(IObjectCreationOperationWrapper)); | ||
} | ||
|
||
private IArgumentOperationWrapper(IOperation operation) | ||
{ | ||
this.operation = operation; | ||
} | ||
|
||
public IOperation WrappedOperation => this.operation; | ||
|
||
public ITypeSymbol Type => this.WrappedOperation.Type; | ||
|
||
public static IArgumentOperationWrapper FromOperation(IOperation operation) | ||
{ | ||
if (operation == null) | ||
{ | ||
return default; | ||
} | ||
|
||
if (!IsInstance(operation)) | ||
{ | ||
throw new InvalidCastException($"Cannot cast '{operation.GetType().FullName}' to '{WrappedTypeName}'"); | ||
} | ||
|
||
return new IArgumentOperationWrapper(operation); | ||
} | ||
|
||
public static bool IsInstance(IOperation operation) | ||
{ | ||
return operation != null && LightupHelpers.CanWrapOperation(operation, WrappedType); | ||
} | ||
} | ||
} |
82 changes: 82 additions & 0 deletions
82
StyleCop.Analyzers/StyleCop.Analyzers/Lightup/IObjectCreationOperationWrapper.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 MIT License. See LICENSE in the project root for license information. | ||
|
||
namespace StyleCop.Analyzers.Lightup | ||
{ | ||
using System; | ||
using System.Collections.Immutable; | ||
using Microsoft.CodeAnalysis; | ||
|
||
internal readonly struct IObjectCreationOperationWrapper : IOperationWrapper | ||
{ | ||
internal const string WrappedTypeName = "Microsoft.CodeAnalysis.Operations.IObjectCreationOperation"; | ||
private static readonly Type WrappedType; | ||
|
||
private static readonly Func<IOperation, IMethodSymbol> ConstructorAccessor; | ||
private static readonly Func<IOperation, IOperation> InitializerAccessor; | ||
private static readonly Func<IOperation, ImmutableArray<IOperation>> ArgumentsAccessor; | ||
|
||
private readonly IOperation operation; | ||
|
||
static IObjectCreationOperationWrapper() | ||
{ | ||
WrappedType = WrapperHelper.GetWrappedType(typeof(IObjectCreationOperationWrapper)); | ||
ConstructorAccessor = LightupHelpers.CreateOperationPropertyAccessor<IOperation, IMethodSymbol>(WrappedType, nameof(Constructor)); | ||
InitializerAccessor = LightupHelpers.CreateOperationPropertyAccessor<IOperation, IOperation>(WrappedType, nameof(Initializer)); | ||
ArgumentsAccessor = LightupHelpers.CreateOperationListPropertyAccessor<IOperation>(WrappedType, nameof(Arguments)); | ||
} | ||
|
||
private IObjectCreationOperationWrapper(IOperation operation) | ||
{ | ||
this.operation = operation; | ||
} | ||
|
||
public IOperation WrappedOperation => this.operation; | ||
|
||
public ITypeSymbol Type => this.WrappedOperation.Type; | ||
|
||
public IMethodSymbol Constructor | ||
{ | ||
get | ||
{ | ||
return ConstructorAccessor(this.WrappedOperation); | ||
} | ||
} | ||
|
||
public IObjectOrCollectionInitializerOperationWrapper Initializer | ||
{ | ||
get | ||
{ | ||
return IObjectOrCollectionInitializerOperationWrapper.FromOperation(InitializerAccessor(this.WrappedOperation)); | ||
} | ||
} | ||
|
||
public ImmutableArray<IOperation> Arguments | ||
{ | ||
get | ||
{ | ||
return ArgumentsAccessor(this.WrappedOperation); | ||
} | ||
} | ||
|
||
public static IObjectCreationOperationWrapper FromOperation(IOperation operation) | ||
{ | ||
if (operation == null) | ||
{ | ||
return default; | ||
} | ||
|
||
if (!IsInstance(operation)) | ||
{ | ||
throw new InvalidCastException($"Cannot cast '{operation.GetType().FullName}' to '{WrappedTypeName}'"); | ||
} | ||
|
||
return new IObjectCreationOperationWrapper(operation); | ||
} | ||
|
||
public static bool IsInstance(IOperation operation) | ||
{ | ||
return operation != null && LightupHelpers.CanWrapOperation(operation, WrappedType); | ||
} | ||
} | ||
} |
62 changes: 62 additions & 0 deletions
62
...op.Analyzers/StyleCop.Analyzers/Lightup/IObjectOrCollectionInitializerOperationWrapper.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,62 @@ | ||
// 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 System.Collections.Immutable; | ||
using Microsoft.CodeAnalysis; | ||
|
||
internal readonly struct IObjectOrCollectionInitializerOperationWrapper : IOperationWrapper | ||
{ | ||
internal const string WrappedTypeName = "Microsoft.CodeAnalysis.Operations.IObjectOrCollectionInitializerOperation"; | ||
private static readonly Type WrappedType; | ||
|
||
private static readonly Func<IOperation, ImmutableArray<IOperation>> InitializersAccessor; | ||
|
||
private readonly IOperation operation; | ||
|
||
static IObjectOrCollectionInitializerOperationWrapper() | ||
{ | ||
WrappedType = WrapperHelper.GetWrappedType(typeof(IObjectOrCollectionInitializerOperationWrapper)); | ||
InitializersAccessor = LightupHelpers.CreateOperationPropertyAccessor<IOperation, ImmutableArray<IOperation>>(WrappedType, nameof(Initializers)); | ||
} | ||
|
||
private IObjectOrCollectionInitializerOperationWrapper(IOperation operation) | ||
{ | ||
this.operation = operation; | ||
} | ||
|
||
public IOperation WrappedOperation => this.operation; | ||
|
||
public ITypeSymbol Type => this.WrappedOperation.Type; | ||
|
||
public ImmutableArray<IOperation> Initializers | ||
{ | ||
get | ||
{ | ||
return InitializersAccessor(this.WrappedOperation); | ||
} | ||
} | ||
|
||
public static IObjectOrCollectionInitializerOperationWrapper FromOperation(IOperation operation) | ||
{ | ||
if (operation == null) | ||
{ | ||
return default; | ||
} | ||
|
||
if (!IsInstance(operation)) | ||
{ | ||
throw new InvalidCastException($"Cannot cast '{operation.GetType().FullName}' to '{WrappedTypeName}'"); | ||
} | ||
|
||
return new IObjectOrCollectionInitializerOperationWrapper(operation); | ||
} | ||
|
||
public static bool IsInstance(IOperation operation) | ||
{ | ||
return operation != null && LightupHelpers.CanWrapOperation(operation, WrappedType); | ||
} | ||
} | ||
} |
32 changes: 32 additions & 0 deletions
32
StyleCop.Analyzers/StyleCop.Analyzers/Lightup/IOperationWrapper.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,32 @@ | ||
// Copyright (c) Tunnel Vision Laboratories, LLC. All Rights Reserved. | ||
// Licensed under the MIT License. See LICENSE in the project root for license information. | ||
|
||
#nullable enable | ||
|
||
namespace StyleCop.Analyzers.Lightup | ||
{ | ||
using Microsoft.CodeAnalysis; | ||
|
||
internal interface IOperationWrapper | ||
{ | ||
IOperation? WrappedOperation { get; } | ||
|
||
////IOperationWrapper Parent { get; } | ||
|
||
////OperationKind Kind { get; } | ||
|
||
////SyntaxNode Syntax { get; } | ||
|
||
ITypeSymbol? Type { get; } | ||
|
||
////Optional<object> ConstantValue { get; } | ||
|
||
////IEnumerable<IOperationWrapper> Children { get; } | ||
|
||
////string Language { get; } | ||
|
||
////bool IsImplicit { get; } | ||
|
||
////SemanticModel SemanticModel { get; } | ||
} | ||
} |
61 changes: 61 additions & 0 deletions
61
...eCop.Analyzers/StyleCop.Analyzers/Lightup/ITypeParameterObjectCreationOperationWrapper.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,61 @@ | ||
// 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 ITypeParameterObjectCreationOperationWrapper : IOperationWrapper | ||
{ | ||
internal const string WrappedTypeName = "Microsoft.CodeAnalysis.Operations.ITypeParameterObjectCreationOperation"; | ||
private static readonly Type WrappedType; | ||
|
||
private static readonly Func<IOperation, IOperation> InitializerAccessor; | ||
|
||
private readonly IOperation operation; | ||
|
||
static ITypeParameterObjectCreationOperationWrapper() | ||
{ | ||
WrappedType = WrapperHelper.GetWrappedType(typeof(ITypeParameterObjectCreationOperationWrapper)); | ||
InitializerAccessor = LightupHelpers.CreateOperationPropertyAccessor<IOperation, IOperation>(WrappedType, nameof(Initializer)); | ||
} | ||
|
||
private ITypeParameterObjectCreationOperationWrapper(IOperation operation) | ||
{ | ||
this.operation = operation; | ||
} | ||
|
||
public IOperation WrappedOperation => this.operation; | ||
|
||
public ITypeSymbol Type => this.WrappedOperation.Type; | ||
|
||
public IObjectOrCollectionInitializerOperationWrapper Initializer | ||
{ | ||
get | ||
{ | ||
return IObjectOrCollectionInitializerOperationWrapper.FromOperation(InitializerAccessor(this.WrappedOperation)); | ||
} | ||
} | ||
|
||
public static ITypeParameterObjectCreationOperationWrapper FromOperation(IOperation operation) | ||
{ | ||
if (operation == null) | ||
{ | ||
return default; | ||
} | ||
|
||
if (!IsInstance(operation)) | ||
{ | ||
throw new InvalidCastException($"Cannot cast '{operation.GetType().FullName}' to '{WrappedTypeName}'"); | ||
} | ||
|
||
return new ITypeParameterObjectCreationOperationWrapper(operation); | ||
} | ||
|
||
public static bool IsInstance(IOperation operation) | ||
{ | ||
return operation != null && LightupHelpers.CanWrapOperation(operation, WrappedType); | ||
} | ||
} | ||
} |
Oops, something went wrong.