Skip to content

Commit

Permalink
Merge pull request #52297 from Youssef1313/fix-get-modifiers
Browse files Browse the repository at this point in the history
Fix GetModifiers and WithModifiers to handle local functions
  • Loading branch information
CyrusNajmabadi authored Apr 22, 2021
2 parents 74d3537 + 956587f commit 95d0579
Show file tree
Hide file tree
Showing 4 changed files with 96 additions and 0 deletions.
16 changes: 16 additions & 0 deletions src/Analyzers/CSharp/Tests/OrderModifiers/OrderModifiersTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
using Microsoft.CodeAnalysis.Diagnostics;
using Microsoft.CodeAnalysis.Editor.CSharp.UnitTests.Diagnostics;
using Microsoft.CodeAnalysis.Test.Utilities;
using Roslyn.Test.Utilities;
using Xunit;
using Xunit.Abstractions;

Expand Down Expand Up @@ -374,6 +375,21 @@ await TestInRegularAndScript1Async(
@"partial class C
{
unsafe partial void M();
}");
}

[Fact, Trait(Traits.Feature, Traits.Features.CodeActionsOrderModifiers)]
[WorkItem(52297, "https://github.com/dotnet/roslyn/pull/52297")]
public async Task TestInLocalFunction()
{
// Not handled for performance reason.
await TestMissingInRegularAndScriptAsync(
@"class C
{
public static async void M()
{
[|async|] static void Local() { }
}
}");
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5561,5 +5561,80 @@ public async Task TestXmlAttributeNameSpan2()
new ClassifiedSpan(ClassificationTypeNames.XmlDocCommentDelimiter, new TextSpan(38, 1))
}, classifications);
}

[Theory, WorkItem(52290, "https://github.com/dotnet/roslyn/issues/52290")]
[CombinatorialData]
public async Task TestStaticLocalFunction(TestHost testHost)
{
var code = @"
class C
{
public static void M()
{
static void LocalFunc() { }
}
}";

await TestAsync(code,
testHost,
Keyword("class"),
Class("C"),
Punctuation.OpenCurly,
Keyword("public"),
Keyword("static"),
Keyword("void"),
Method("M"),
Static("M"),
Punctuation.OpenParen,
Punctuation.CloseParen,
Punctuation.OpenCurly,
Keyword("static"),
Keyword("void"),
Method("LocalFunc"),
Static("LocalFunc"),
Punctuation.OpenParen,
Punctuation.CloseParen,
Punctuation.OpenCurly,
Punctuation.CloseCurly,
Punctuation.CloseCurly,
Punctuation.CloseCurly);
}

[Theory, WorkItem(52290, "https://github.com/dotnet/roslyn/issues/52290")]
[CombinatorialData]
public async Task TestConstantLocalVariable(TestHost testHost)
{
var code = @"
class C
{
public static void M()
{
const int Zero = 0;
}
}";

await TestAsync(code,
testHost,
Keyword("class"),
Class("C"),
Punctuation.OpenCurly,
Keyword("public"),
Keyword("static"),
Keyword("void"),
Method("M"),
Static("M"),
Punctuation.OpenParen,
Punctuation.CloseParen,
Punctuation.OpenCurly,
Keyword("const"),
Keyword("int"),
Constant("Zero"),
Static("Zero"),
Operators.Equals,
Number("0"),
Punctuation.Semicolon,
Punctuation.CloseCurly,
Punctuation.CloseCurly);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -995,6 +995,7 @@ await TestInMethodAsync(
Keyword("const"),
Keyword("int"),
Constant("Number"),
Static("Number"),
Operators.Equals,
Number("42"),
Punctuation.Semicolon,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -952,6 +952,8 @@ public static SyntaxTokenList GetModifiers(this SyntaxNode? member)
{
case MemberDeclarationSyntax memberDecl: return memberDecl.Modifiers;
case AccessorDeclarationSyntax accessor: return accessor.Modifiers;
case LocalFunctionStatementSyntax localFunction: return localFunction.Modifiers;
case LocalDeclarationStatementSyntax localDeclaration: return localDeclaration.Modifiers;
}

return default;
Expand All @@ -963,6 +965,8 @@ public static SyntaxTokenList GetModifiers(this SyntaxNode? member)
{
case MemberDeclarationSyntax memberDecl: return memberDecl.WithModifiers(modifiers);
case AccessorDeclarationSyntax accessor: return accessor.WithModifiers(modifiers);
case LocalFunctionStatementSyntax localFunction: return localFunction.WithModifiers(modifiers);
case LocalDeclarationStatementSyntax localDeclaration: return localDeclaration.WithModifiers(modifiers);
}

return null;
Expand Down

0 comments on commit 95d0579

Please sign in to comment.