-
Notifications
You must be signed in to change notification settings - Fork 508
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
Code fix for using statements #650
Conversation
vweijsters
commented
Apr 8, 2015
- Fixes Code fix: SA1200 (UsingDirectivesMustBePlacedWithinNamespace) #291
- Fixes Code fix: SA1208 (SystemUsingDirectivesMustBePlacedBeforeOtherUsingDirectives) #292
- Fixes Code fix: SA1209 (UsingAliasDirectivesMustBePlacedAfterOtherUsingDirectives) #293
- Fixes Code fix: SA1210 (UsingDirectivesMustBeOrderedAlphabeticallyByNamespace) #294
- Fixes Code fix: SA1211 (UsingAliasDirectivesMustBeOrderedAlphabeticallyByAliasName) #295
- Fixes Code fix: SA1216 (UsingStaticDirectivesMustBePlacedAfterOtherUsingDirectives) #1107
- Fixes Code fix: SA1217 (UsingStaticDirectivesMustBeOrderedAlphabetically) #1108
|
||
var firstPartName = ((IdentifierNameSyntax)firstPart).Identifier.ValueText; | ||
|
||
if (string.CompareOrdinal(firstPartName, "System") == 0) |
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.
You could use "System".Equals(firstPartName, StringComparison.Ordinal)
her instead
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.
nameof(System)
2015-05-05 11:57 GMT+02:00 Dennis Fischer notifications@github.com:
In
StyleCop.Analyzers/StyleCop.Analyzers/OrderingRules/UsingCodeFixProvider.cs
#650 (comment)
:
else if (!usingDirective.StaticKeyword.IsKind(SyntaxKind.None))
{
staticImports.Add(usingDirective);
}
else
{
var firstPart = usingDirective.Name;
while (firstPart is QualifiedNameSyntax)
{
firstPart = ((QualifiedNameSyntax)firstPart).Left;
}
var firstPartName = ((IdentifierNameSyntax)firstPart).Identifier.ValueText;
if (string.CompareOrdinal(firstPartName, "System") == 0)
You could use "System".Equals(firstPartName, StringComparison.Ordinal)
her instead—
Reply to this email directly or view it on GitHub
https://github.com/DotNetAnalyzers/StyleCopAnalyzers/pull/650/files#r29655896
.
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 didnt even know that you could use nameof with namespaces!
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 changed the code to use nameof(System)
Updated to reflect the current state |
{ | ||
foreach (Diagnostic diagnostic in context.Diagnostics.Where(d => FixableDiagnostics.Contains(d.Id))) | ||
{ | ||
context.RegisterCodeFix(CodeAction.Create("Reorder using statements", token => GetTransformedDocumentAsync(context.Document, diagnostic, token)), diagnostic); |
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.
this should be updated with equivalence key
❓ Can you update the unit tests for the diagnostics from ❓ In the case of directive trivia surrounding using statements outside of a namespace how will these be handled? Moving the using statement into the namespace in the following example will cause a compiler error. This is handled correctly by the diagnostic. Pointing it out as a heads up.
|
@vweijsters I'd love to include this, but first I want to make sure it accounts for a couple of things to make sure we don't break users' code.
|
I've been busy the last couple of days. This one is first on my list for the coming week |
Thanks for the update. 😄 I marked it as in progress with a tentative milestone of Beta 10. |
5be3092
to
e447421
Compare
Updated with all remarks above. Also rebased/squashed. |
{ | ||
} | ||
}"; | ||
|
||
DiagnosticResult[] expectedForCompilationUnit = | ||
{ | ||
this.CSharpDiagnostic().WithLocation(1, 1) |
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.
💡 Single diagnostics can be:
var expected = this.CSharpDiagnostic().WithLocation(1, 1);
|
||
private static async Task<Document> GetTransformedDocumentAsync(Document document, Diagnostic diagnostic, CancellationToken cancellationToken) | ||
{ | ||
var syntaxRoot = await document.GetSyntaxRootAsync(cancellationToken).ConfigureAwait(false); |
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.
❓ Could syntaxRoot
be passed in?
Can you add a test that includes a file header? |
I'll send a follow-up pull request to address the final feedback. |