-
Notifications
You must be signed in to change notification settings - Fork 507
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Update SA1121 and SA1404 to detect global using aliases, using GetImportScopes and compiling expression trees #3678
Open
bjornhellander
wants to merge
1
commit into
DotNetAnalyzers:master
Choose a base branch
from
bjornhellander:feature/global-using-alias-3
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I updated this test to verify the actual alias symbols returned from the wrapper. Used the Moq package to be able to create IAliasSymbol instances more easily. Ok to add that dependency?