Skip to content
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
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

bjornhellander
Copy link
Contributor

Another attempt to fix #3594.
SA1404 also suffered from the same problem.

@bjornhellander
Copy link
Contributor Author

Tests will fail. That can be fixed and code cleaned up later on. I am more interested in the big picture right now.

Did you have something like this in mind, @sharwell? I haven't wrapped my head around your light-up code completely. I consider my own new code more or less unreadable, but it seems to work :-) I assume that some of it could be generated, but that is beyond me at the moment.

Note that GetImportScopes wasn't available until Roslyn 4.3.0, so with this approach the original issue isn't really resolved perfectly. Are you ok with these rules being broken for Roslyn versions between 4.0.0 (introduction of c# 10) and 4.3.0 (introduction of GetImportScopes)?

Regarding the failing tests: C# 10 tests fail because of the issue with GetImportScopes. What to do?

  1. Upgrade to 4.3.0 in the c# 10 test project. It is still before the 4.4.0 which added c# 11 support, but might not be desirable.
  2. Move the new tests to the c# 11 test project.
  3. ...

@bjornhellander bjornhellander changed the title Update SA1121 and SA1404 to detect global using aliases, using GetImportScopes and compiling expression trees) Update SA1121 and SA1404 to detect global using aliases, using GetImportScopes and compiling expression trees Jul 13, 2023
@codecov
Copy link

codecov bot commented Jul 13, 2023

Codecov Report

Attention: Patch coverage is 98.61751% with 3 lines in your changes are missing coverage. Please review.

Project coverage is 97.45%. Comparing base (6327501) to head (65ab771).

Additional details and impacted files
@@            Coverage Diff             @@
##           master    #3678      +/-   ##
==========================================
+ Coverage   94.37%   97.45%   +3.08%     
==========================================
  Files         924      930       +6     
  Lines      109815   110055     +240     
  Branches     3302     3308       +6     
==========================================
+ Hits       103635   107255    +3620     
+ Misses       5170     1836    -3334     
+ Partials     1010      964      -46     

@bjornhellander
Copy link
Contributor Author

I moved the tests to the c# 11 test project, because of reasons listed above. Ok?

@sharwell
Copy link
Member

I moved the tests to the c# 11 test project, because of reasons listed above. Ok?

That seems fine

using StyleCop.Analyzers.Lightup;
using Xunit;

public class IImportScopeWrapperCSharp11UnitTests
Copy link
Contributor Author

@bjornhellander bjornhellander Jan 2, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think that I missed the existing lightup test pattern. Is the idea that there are tests for each language version? Simple tests for c# 6 that verify fallback behavior etc, inherit that one for c#7 through 10, and "cut the inheritance chain" with this new test class for c# 11 (and inherit that one for c# 12 like I have done)?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I updated this, but didn't manage to make it play nicely together with the DerivedTestGenerator the way I described it above. I instead ended up with inheriting all the way from the c# 7 project and up. Does it look ok anyway?


public static bool SupportsGetImportScopes { get; }

public static ImmutableArray<IImportScopeWrapper> GetImportScopes(this SemanticModel semanticModel, int position, CancellationToken cancellationToken = default)
Copy link
Contributor Author

@bjornhellander bjornhellander Jan 2, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I didn't add any lightup tests for this class, but relied on the SA1121 and SA1404 tests instead. Does that feel good enough? Feels slightly more complicated to add these as well, since I would need to get a SemanticModel instance somehow.

@bjornhellander

This comment was marked as resolved.

var obj = CreateImportScope(numberOfAliasSymbols);
Assert.True(IImportScopeWrapper.IsInstance(obj));
var wrapper = IImportScopeWrapper.FromObject(obj);
Assert.Equal(obj.Aliases, wrapper.Aliases);
Copy link
Contributor Author

@bjornhellander bjornhellander Jan 3, 2024

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?

@bjornhellander
Copy link
Contributor Author

Any more comments on this one, @sharwell? I added comments for some concerns that I have myself.

@bjornhellander
Copy link
Contributor Author

Rebased and squashed.
Any more comments, @sharwell?

@bjornhellander
Copy link
Contributor Author

@sharwell?

@sharwell
Copy link
Member

Sorry for the delays, will try to get to this one soon.

The occasional pings are welcome.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

SA1121: Inconsistent behaviour with respect to global using aliases
2 participants