forked from DotNetAnalyzers/StyleCopAnalyzers
-
Notifications
You must be signed in to change notification settings - Fork 0
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
be49652
commit 7ffc577
Showing
10 changed files
with
381 additions
and
6 deletions.
There are no files selected for viewing
54 changes: 54 additions & 0 deletions
54
...nalyzers/StyleCop.Analyzers.Test.CSharp11/Lightup/IImportScopeWrapperCSharp11UnitTests.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,54 @@ | ||
// Copyright (c) Tunnel Vision Laboratories, LLC. All Rights Reserved. | ||
// Licensed under the MIT License. See LICENSE in the project root for license information. | ||
|
||
// Tests calls where nullability rules are not obeyed | ||
#pragma warning disable CS8604 // Possible null reference argument. | ||
|
||
namespace StyleCop.Analyzers.Test.Lightup | ||
{ | ||
using System; | ||
using System.Collections.Immutable; | ||
using Microsoft.CodeAnalysis; | ||
using StyleCop.Analyzers.Lightup; | ||
using Xunit; | ||
|
||
public class IImportScopeWrapperCSharp11UnitTests | ||
{ | ||
[Fact] | ||
public void TestNull() | ||
{ | ||
object? obj = null; | ||
Assert.False(IImportScopeWrapper.IsInstance(obj)); | ||
var wrapper = IImportScopeWrapper.FromObject(obj); | ||
Assert.Throws<NullReferenceException>(() => wrapper.Aliases); | ||
} | ||
|
||
[Fact] | ||
public void TestIncompatibleInstance() | ||
{ | ||
var obj = new object(); | ||
Assert.False(IImportScopeWrapper.IsInstance(obj)); | ||
Assert.Throws<InvalidCastException>(() => IImportScopeWrapper.FromObject(obj)); | ||
} | ||
|
||
[Fact] | ||
public void TestCompatibleInstance() | ||
{ | ||
var obj = new TestImportScope(); | ||
Assert.True(IImportScopeWrapper.IsInstance(obj)); | ||
var wrapper = IImportScopeWrapper.FromObject(obj); | ||
Assert.Empty(wrapper.Aliases); | ||
} | ||
|
||
private class TestImportScope : IImportScope | ||
{ | ||
public ImmutableArray<IAliasSymbol> Aliases => ImmutableArray<IAliasSymbol>.Empty; | ||
|
||
public ImmutableArray<IAliasSymbol> ExternAliases => throw new NotImplementedException(); | ||
|
||
public ImmutableArray<ImportedNamespaceOrType> Imports => throw new NotImplementedException(); | ||
|
||
public ImmutableArray<ImportedXmlNamespace> XmlNamespaces => throw new NotImplementedException(); | ||
} | ||
} | ||
} |
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
9 changes: 9 additions & 0 deletions
9
...nalyzers/StyleCop.Analyzers.Test.CSharp12/Lightup/IImportScopeWrapperCSharp12UnitTests.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,9 @@ | ||
// 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.Test.Lightup | ||
{ | ||
public partial class IImportScopeWrapperCSharp12UnitTests : IImportScopeWrapperCSharp11UnitTests | ||
{ | ||
} | ||
} |
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
54 changes: 54 additions & 0 deletions
54
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,54 @@ | ||
// 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 => 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); | ||
} | ||
} | ||
} |
Oops, something went wrong.