-
Notifications
You must be signed in to change notification settings - Fork 508
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Update SA1121 and SA1404 to detect global using aliases
- Loading branch information
1 parent
b209e4e
commit 0d855cd
Showing
8 changed files
with
333 additions
and
8 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
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
66 changes: 66 additions & 0 deletions
66
StyleCop.Analyzers/StyleCop.Analyzers/Lightup/IImportScopeWrapper.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,66 @@ | ||
// 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 IImportScopeWrapper | ||
{ | ||
internal const string WrappedTypeName = "Microsoft.CodeAnalysis.IImportScope"; | ||
private static readonly Type WrappedType; | ||
|
||
private static readonly Func<object?, ImmutableArray<IAliasSymbol>> AliasesAccessor; | ||
|
||
private readonly object node; | ||
|
||
static IImportScopeWrapper() | ||
{ | ||
WrappedType = WrapperHelper.GetWrappedType(typeof(IImportScopeWrapper)); | ||
|
||
AliasesAccessor = LightupHelpers.CreateSyntaxPropertyAccessor<object?, ImmutableArray<IAliasSymbol>>(WrappedType, nameof(Aliases)); | ||
} | ||
|
||
private IImportScopeWrapper(object node) | ||
{ | ||
this.node = node; | ||
} | ||
|
||
public ImmutableArray<IAliasSymbol> Aliases | ||
{ | ||
get | ||
{ | ||
if (this.node == null && WrappedType == null) | ||
{ | ||
// Gracefully fall back to a collection with no values | ||
return ImmutableArray<IAliasSymbol>.Empty; | ||
} | ||
|
||
return AliasesAccessor(this.node); | ||
} | ||
} | ||
|
||
// NOTE: Referenced via reflection | ||
public static IImportScopeWrapper FromObject(object node) | ||
{ | ||
if (node == null) | ||
{ | ||
return default; | ||
} | ||
|
||
if (!IsInstance(node)) | ||
{ | ||
throw new InvalidCastException($"Cannot cast '{node.GetType().FullName}' to '{WrappedTypeName}'"); | ||
} | ||
|
||
return new IImportScopeWrapper(node); | ||
} | ||
|
||
public static bool IsInstance(object obj) | ||
{ | ||
return obj != null && LightupHelpers.CanWrapObject(obj, WrappedType); | ||
} | ||
} | ||
} |
181 changes: 181 additions & 0 deletions
181
StyleCop.Analyzers/StyleCop.Analyzers/Lightup/SemanticModelExtensions.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,181 @@ | ||
// 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 System.Linq; | ||
using System.Linq.Expressions; | ||
using System.Reflection; | ||
using System.Threading; | ||
using Microsoft.CodeAnalysis; | ||
|
||
internal static class SemanticModelExtensions | ||
{ | ||
private static readonly Func<SemanticModel, int, CancellationToken, ImmutableArray<IImportScopeWrapper>> GetImportScopesAccessor; | ||
|
||
static SemanticModelExtensions() | ||
{ | ||
CreateGetImportScopesAccessor(out var accessor, out var isSupported); | ||
GetImportScopesAccessor = accessor; | ||
SupportsGetImportScopes = isSupported; | ||
} | ||
|
||
public static bool SupportsGetImportScopes { get; } | ||
|
||
public static ImmutableArray<IImportScopeWrapper> GetImportScopes(this SemanticModel semanticModel, int position, CancellationToken cancellationToken = default) | ||
{ | ||
return GetImportScopesAccessor(semanticModel, position, cancellationToken); | ||
} | ||
|
||
private static void CreateGetImportScopesAccessor( | ||
out Func<SemanticModel, int, CancellationToken, ImmutableArray<IImportScopeWrapper>> accessor, | ||
out bool isSupported) | ||
{ | ||
// Very error-prone code and potentially brittle regarding future API changes. | ||
// Wrapping everything in a try statement just in case something breaks later on. | ||
try | ||
{ | ||
var semanticModelType = typeof(SemanticModel); | ||
|
||
var codeAnalysisWorkspacesAssembly = semanticModelType.GetTypeInfo().Assembly; | ||
var nativeImportScopeType = codeAnalysisWorkspacesAssembly.GetType("Microsoft.CodeAnalysis.IImportScope"); | ||
if (nativeImportScopeType is null) | ||
{ | ||
accessor = FallbackAccessor; | ||
isSupported = false; | ||
} | ||
|
||
var methods = semanticModelType.GetTypeInfo().GetDeclaredMethods("GetImportScopes").Where(IsCorrectGetImportScopesMethod); | ||
var method = methods.FirstOrDefault(); | ||
if (method == null) | ||
{ | ||
accessor = FallbackAccessor; | ||
isSupported = false; | ||
} | ||
|
||
var importScopeWrapperFromObjectMethod = typeof(IImportScopeWrapper).GetTypeInfo().GetDeclaredMethod("FromObject"); | ||
var nativeImportScopeArrayType = typeof(ImmutableArray<>).MakeGenericType(nativeImportScopeType); | ||
var nativeImportScopeArrayGetItemMethod = nativeImportScopeArrayType.GetTypeInfo().GetDeclaredMethod("get_Item"); | ||
var wrapperImportScopeArrayType = typeof(ImmutableArray<IImportScopeWrapper>); | ||
var wrapperImportScopeArrayBuilderType = typeof(ImmutableArray<IImportScopeWrapper>.Builder); | ||
var wrapperImportScopeArrayBuilderAddMethod = wrapperImportScopeArrayBuilderType.GetTypeInfo().GetDeclaredMethod("Add"); | ||
var wrapperImportScopeArrayBuilderToImmutableMethod = wrapperImportScopeArrayBuilderType.GetTypeInfo().GetDeclaredMethod("ToImmutable"); | ||
var arrayCreateImportScopeBuilderMethod = typeof(ImmutableArray).GetTypeInfo().GetDeclaredMethods("CreateBuilder").Single(IsCorrectCreateBuilderMethod).MakeGenericMethod(typeof(IImportScopeWrapper)); | ||
|
||
var semanticModelParameter = Expression.Parameter(typeof(SemanticModel), "semanticModel"); | ||
var positionParameter = Expression.Parameter(typeof(int), "position"); | ||
var cancellationTokenParameter = Expression.Parameter(typeof(CancellationToken), "cancellationToken"); | ||
|
||
var nativeImportScopesVariable = Expression.Variable(nativeImportScopeArrayType, "nativeImportScopes"); | ||
var nativeImportScopeVariable = Expression.Variable(nativeImportScopeType, "nativeImportScope"); | ||
var countVariable = Expression.Variable(typeof(int), "count"); | ||
var indexVariable = Expression.Variable(typeof(int), "index"); | ||
var wrapperImportScopesBuilderVariable = Expression.Variable(wrapperImportScopeArrayBuilderType, "wrapperImportScopesBuilder"); | ||
var wrapperImportScopesVariable = Expression.Variable(wrapperImportScopeArrayType, "wrapperImoprtScopes"); | ||
var wrapperImportScopeVariable = Expression.Variable(typeof(IImportScopeWrapper), "wrapperImportScope"); | ||
|
||
var breakLabel = Expression.Label("break"); | ||
var block = Expression.Block( | ||
new[] { nativeImportScopesVariable, countVariable, indexVariable, wrapperImportScopesBuilderVariable, wrapperImportScopesVariable }, | ||
//// nativeImportScopes = semanticModel.GetImportScopes(position, cancellationToken); | ||
Expression.Assign( | ||
nativeImportScopesVariable, | ||
Expression.Call( | ||
semanticModelParameter, | ||
method, | ||
new[] { positionParameter, cancellationTokenParameter })), | ||
//// index = 0; | ||
Expression.Assign(indexVariable, Expression.Constant(0)), | ||
//// count = nativeImportScopes.Length; | ||
Expression.Assign( | ||
countVariable, | ||
Expression.Property(nativeImportScopesVariable, "Length")), | ||
//// wrapperImportScopesBuilder = ImmutableArray.CreateBuilder<IImportScopesWrapper>(); | ||
Expression.Assign( | ||
wrapperImportScopesBuilderVariable, | ||
Expression.Call(null, arrayCreateImportScopeBuilderMethod)), | ||
Expression.Loop( | ||
Expression.Block( | ||
new[] { nativeImportScopeVariable, wrapperImportScopeVariable }, | ||
//// if (index >= count) break; | ||
Expression.IfThen( | ||
Expression.GreaterThanOrEqual(indexVariable, countVariable), | ||
Expression.Break(breakLabel)), | ||
//// nativeImportScope = nativeImportScopes[index]; | ||
Expression.Assign( | ||
nativeImportScopeVariable, | ||
Expression.Call( | ||
nativeImportScopesVariable, | ||
nativeImportScopeArrayGetItemMethod, | ||
indexVariable)), | ||
//// wrapperImportScope = IImportScopeWrapper.FromObject(nativeImportScope); | ||
Expression.Assign( | ||
wrapperImportScopeVariable, | ||
Expression.Call( | ||
null, | ||
importScopeWrapperFromObjectMethod, | ||
nativeImportScopeVariable)), | ||
//// wrapperImportScopesBuilder.Add(wrapperImportScope); | ||
Expression.Call( | ||
wrapperImportScopesBuilderVariable, | ||
wrapperImportScopeArrayBuilderAddMethod, | ||
new[] { wrapperImportScopeVariable }), | ||
//// index++; | ||
Expression.PostIncrementAssign(indexVariable)), | ||
breakLabel), | ||
//// wrapperImportScopes = wrapperImportScopesBuilder.ToImmutable(); | ||
Expression.Assign( | ||
wrapperImportScopesVariable, | ||
Expression.Call( | ||
wrapperImportScopesBuilderVariable, | ||
wrapperImportScopeArrayBuilderToImmutableMethod))); | ||
|
||
var lambda = Expression.Lambda<Func<SemanticModel, int, CancellationToken, ImmutableArray<IImportScopeWrapper>>>( | ||
block, | ||
new[] { semanticModelParameter, positionParameter, cancellationTokenParameter }); | ||
|
||
accessor = lambda.Compile(); | ||
isSupported = true; | ||
} | ||
catch (Exception) | ||
{ | ||
accessor = FallbackAccessor; | ||
isSupported = false; | ||
} | ||
|
||
// NOTE: At time of implementation, there is no other overload of GetImportScopes, but check in case more are added later on | ||
static bool IsCorrectGetImportScopesMethod(MethodInfo method) | ||
{ | ||
var parameters = method.GetParameters(); | ||
if (parameters.Length != 2) | ||
{ | ||
return false; | ||
} | ||
|
||
return | ||
parameters[0].ParameterType == typeof(int) && | ||
parameters[1].ParameterType == typeof(CancellationToken); | ||
} | ||
|
||
static bool IsCorrectCreateBuilderMethod(MethodInfo method) | ||
{ | ||
var parameters = method.GetParameters(); | ||
return parameters.Length == 0; | ||
} | ||
|
||
static ImmutableArray<IImportScopeWrapper> FallbackAccessor(SemanticModel semanticModel, int position, CancellationToken cancellationToken) | ||
{ | ||
if (semanticModel == null) | ||
{ | ||
// Unlike an extension method which would throw ArgumentNullException here, the light-up | ||
// behavior needs to match the behavior of the underlying method. | ||
throw new NullReferenceException(); | ||
} | ||
|
||
return ImmutableArray<IImportScopeWrapper>.Empty; | ||
} | ||
} | ||
} | ||
} |
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