-
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
6327501
commit 41b03e7
Showing
16 changed files
with
427 additions
and
6 deletions.
There are no files selected for viewing
11 changes: 11 additions & 0 deletions
11
...nalyzers/StyleCop.Analyzers.Test.CSharp10/Lightup/IImportScopeWrapperCSharp10UnitTests.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,11 @@ | ||
// 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.CSharp10.Lightup | ||
{ | ||
using StyleCop.Analyzers.Test.CSharp9.Lightup; | ||
|
||
public partial class IImportScopeWrapperCSharp10UnitTests : IImportScopeWrapperCSharp9UnitTests | ||
{ | ||
} | ||
} |
41 changes: 41 additions & 0 deletions
41
...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,41 @@ | ||
// 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.CSharp11.Lightup | ||
{ | ||
using System.Collections.Generic; | ||
using System.Collections.Immutable; | ||
using Microsoft.CodeAnalysis; | ||
using Moq; | ||
using StyleCop.Analyzers.Lightup; | ||
using StyleCop.Analyzers.Test.CSharp10.Lightup; | ||
using Xunit; | ||
|
||
public partial class IImportScopeWrapperCSharp11UnitTests : IImportScopeWrapperCSharp10UnitTests | ||
{ | ||
[Theory] | ||
[InlineData(0)] | ||
[InlineData(1)] | ||
[InlineData(2)] | ||
public void TestCompatibleInstance(int numberOfAliasSymbols) | ||
{ | ||
var obj = CreateImportScope(numberOfAliasSymbols); | ||
Assert.True(IImportScopeWrapper.IsInstance(obj)); | ||
var wrapper = IImportScopeWrapper.FromObject(obj); | ||
Assert.Equal(obj.Aliases, wrapper.Aliases); | ||
} | ||
|
||
private static IImportScope CreateImportScope(int numberOfAliasSymbols) | ||
{ | ||
var aliasSymbolMocks = new List<IAliasSymbol>(); | ||
for (var i = 0; i < numberOfAliasSymbols; i++) | ||
{ | ||
aliasSymbolMocks.Add(Mock.Of<IAliasSymbol>()); | ||
} | ||
|
||
var importScopeMock = new Mock<IImportScope>(); | ||
importScopeMock.Setup(x => x.Aliases).Returns(aliasSymbolMocks.ToImmutableArray()); | ||
return importScopeMock.Object; | ||
} | ||
} | ||
} |
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
11 changes: 11 additions & 0 deletions
11
...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,11 @@ | ||
// 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.CSharp12.Lightup | ||
{ | ||
using StyleCop.Analyzers.Test.CSharp11.Lightup; | ||
|
||
public partial class IImportScopeWrapperCSharp12UnitTests : IImportScopeWrapperCSharp11UnitTests | ||
{ | ||
} | ||
} |
11 changes: 11 additions & 0 deletions
11
....Analyzers/StyleCop.Analyzers.Test.CSharp7/Lightup/IImportScopeWrapperCSharp7UnitTests.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,11 @@ | ||
// 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.CSharp7.Lightup | ||
{ | ||
using StyleCop.Analyzers.Test.Lightup; | ||
|
||
public partial class IImportScopeWrapperCSharp7UnitTests : IImportScopeWrapperUnitTests | ||
{ | ||
} | ||
} |
11 changes: 11 additions & 0 deletions
11
....Analyzers/StyleCop.Analyzers.Test.CSharp8/Lightup/IImportScopeWrapperCSharp8UnitTests.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,11 @@ | ||
// 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.CSharp8.Lightup | ||
{ | ||
using StyleCop.Analyzers.Test.CSharp7.Lightup; | ||
|
||
public partial class IImportScopeWrapperCSharp8UnitTests : IImportScopeWrapperCSharp7UnitTests | ||
{ | ||
} | ||
} |
11 changes: 11 additions & 0 deletions
11
....Analyzers/StyleCop.Analyzers.Test.CSharp9/Lightup/IImportScopeWrapperCSharp9UnitTests.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,11 @@ | ||
// 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.CSharp9.Lightup | ||
{ | ||
using StyleCop.Analyzers.Test.CSharp8.Lightup; | ||
|
||
public partial class IImportScopeWrapperCSharp9UnitTests : IImportScopeWrapperCSharp8UnitTests | ||
{ | ||
} | ||
} |
29 changes: 29 additions & 0 deletions
29
StyleCop.Analyzers/StyleCop.Analyzers.Test/Lightup/IImportScopeWrapperUnitTests.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,29 @@ | ||
// 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 | ||
{ | ||
using System; | ||
using StyleCop.Analyzers.Lightup; | ||
using Xunit; | ||
|
||
public class IImportScopeWrapperUnitTests | ||
{ | ||
[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)); | ||
} | ||
} | ||
} |
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.