Skip to content

Commit

Permalink
Merge PrimaryConstructors into Main (#67124)
Browse files Browse the repository at this point in the history
  • Loading branch information
AlekseyTs authored Mar 1, 2023
2 parents 94bbabe + e9ceebd commit 86fb668
Show file tree
Hide file tree
Showing 161 changed files with 30,394 additions and 2,029 deletions.
56 changes: 56 additions & 0 deletions src/Analyzers/CSharp/Tests/AddParameter/AddParameterTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2925,6 +2925,22 @@ public static class IsExternalInit { }
""", parseOptions: TestOptions.Regular.WithLanguageVersion(LanguageVersion.CSharp9));
}

[Fact]
public async Task Test_PrimaryConstructor_Class()
{
await TestInRegularAndScriptAsync(@"
var b = ""B"";
var r = [|new R(1, b)|];
class R(int A);
", @"
var b = ""B"";
var r = new R(1, b);
class R(int A, string b);
", parseOptions: TestOptions.Regular.WithLanguageVersion(LanguageVersion.Preview));
}

[Fact, WorkItem(54408, "https://github.com/dotnet/roslyn/issues/54408")]
public async Task TestPositionalRecordStruct()
{
Expand All @@ -2951,6 +2967,22 @@ public static class IsExternalInit { }
""", parseOptions: TestOptions.Regular.WithLanguageVersion(LanguageVersion.CSharp9));
}

[Fact]
public async Task Test_PrimaryConstructor_Struct()
{
await TestInRegularAndScriptAsync(@"
var b = ""B"";
var r = [|new R(1, b)|];
struct R(int A);
", @"
var b = ""B"";
var r = new R(1, b);
struct R(int A, string b);
", parseOptions: TestOptions.Regular.WithLanguageVersion(LanguageVersion.Preview));
}

[Fact, WorkItem(56952, "https://github.com/dotnet/roslyn/issues/56952")]
public async Task TestRecordsNamingConventions()
{
Expand All @@ -2967,6 +2999,18 @@ record Test(string V);
""");
}

[Fact]
public async Task TestNamingConventions_PrimaryConstructor_Class()
{
await TestInRegularAndScript1Async(@"[|new Test(""repro"")|];
class Test();
", @"new Test(""repro"");
class Test(string v);
");
}

[Fact, WorkItem(56952, "https://github.com/dotnet/roslyn/issues/56952")]
public async Task TestRecordsNamingConventions_RecordStruct()
{
Expand All @@ -2983,6 +3027,18 @@ record struct Test(string V);
""");
}

[Fact]
public async Task TestNamingConventions_PrimaryConstructor_Struct()
{
await TestInRegularAndScript1Async(@"[|new Test(""repro"")|];
struct Test();
", @"new Test(""repro"");
struct Test(string v);
");
}

[Fact, WorkItem(61715, "https://github.com/dotnet/roslyn/issues/61715")]
public async Task TestMethodGroup1()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -843,5 +843,51 @@ record R(int First, int Second, int Third);
""";
await TestAsync(initial, expected);
}

[Fact, Trait(Traits.Feature, Traits.Features.CodeActionsAddDocCommentNodes)]
public async Task AddsParamTag_Class()
{
var initial = @"
/// <summary>
///
/// </summary>
/// <param name=""Second""></param>
class R(int [|First|], int Second, int Third);
";

var expected = @"
/// <summary>
///
/// </summary>
/// <param name=""First""></param>
/// <param name=""Second""></param>
/// <param name=""Third""></param>
class R(int First, int Second, int Third);
";
await TestAsync(initial, expected);
}

[Fact, Trait(Traits.Feature, Traits.Features.CodeActionsAddDocCommentNodes)]
public async Task AddsParamTag_Struct()
{
var initial = @"
/// <summary>
///
/// </summary>
/// <param name=""Second""></param>
struct R(int [|First|], int Second, int Third);
";

var expected = @"
/// <summary>
///
/// </summary>
/// <param name=""First""></param>
/// <param name=""Second""></param>
/// <param name=""Third""></param>
struct R(int First, int Second, int Third);
";
await TestAsync(initial, expected);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,16 @@ record struct S(int j)
""");
}

[Fact]
public async Task TestMissingWithMutableAndReadOnlyFieldStruct2()
{
await TestMissingAsync(
@"struct S(int j)
{
int i;
}");
}

[Fact]
public async Task TestMissingWithMutableProperty()
{
Expand Down Expand Up @@ -186,6 +196,16 @@ record struct S(int q)
""");
}

[Fact]
public async Task TestMissingWithMutablePropertyStruct2()
{
await TestMissingAsync(
@"struct S(int q)
{
int P { get; set; }
}");
}

[Fact]
public async Task TestMissingWithEmptyStruct()
{
Expand All @@ -209,7 +229,7 @@ record struct S
}

[Fact]
public async Task TestMissingWithEmptyStructPrimaryConstructor()
public async Task TestMissingWithEmptyRecordStructPrimaryConstructor()
{
await TestMissingAsync(
"""
Expand All @@ -219,6 +239,15 @@ record struct S()
""");
}

[Fact]
public async Task TestMissingWithEmptyStructPrimaryConstructor()
{
await TestMissingAsync(
@"struct S()
{
}");
}

[Fact]
public async Task TestMissingWithOtherReadonlyPartialPart()
{
Expand Down Expand Up @@ -372,6 +401,15 @@ record struct S(int i)
""");
}

[Fact]
public async Task TestMissingStructWithPrimaryConstructor()
{
await TestMissingAsync(
@"struct S(int i)
{
}");
}

[Fact]
public async Task TestMissingOnRecordStructWithPrimaryConstructorFieldAndNormalField()
{
Expand All @@ -384,6 +422,21 @@ record struct S(int i)
""");
}

[Fact]
public async Task TestOnStructWithPrimaryConstructorAndReadonlyField()
{
await TestAsync(
@"struct [|S|](int i)
{
readonly int i;
}",
@"readonly struct S(int i)
{
readonly int i;
}",
LanguageVersion.Preview);
}

[Fact]
public async Task TestNestedStructs1()
{
Expand Down
60 changes: 60 additions & 0 deletions src/Analyzers/CSharp/Tests/UpgradeProject/UpgradeProjectTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1090,6 +1090,66 @@ public record Base
new CSharpParseOptions(LanguageVersion.CSharp9));
}

[Fact]
public async Task UpgradeProjectForPrimaryConstructors_Class()
{
await TestLanguageVersionUpgradedAsync(
@"
class Program[|()|];",
LanguageVersion.Preview,
new CSharpParseOptions(LanguageVersion.CSharp11));
}

[Fact]
public async Task UpgradeProjectForPrimaryConstructors_Struct()
{
await TestLanguageVersionUpgradedAsync(
@"
struct Program[|()|];",
LanguageVersion.Preview,
new CSharpParseOptions(LanguageVersion.CSharp11));
}

[Fact]
public async Task UpgradeProjectForSemicolonBody_Class()
{
await TestLanguageVersionUpgradedAsync(
@"
class Program[|;|]",
LanguageVersion.Preview,
new CSharpParseOptions(LanguageVersion.CSharp11));
}

[Fact]
public async Task UpgradeProjectForSemicolonBody_Struct()
{
await TestLanguageVersionUpgradedAsync(
@"
struct Program[|;|]",
LanguageVersion.Preview,
new CSharpParseOptions(LanguageVersion.CSharp11));
}

[Fact]
public async Task UpgradeProjectForSemicolonBody_Interface()
{
await TestLanguageVersionUpgradedAsync(
@"
interface Program[|;|]",
LanguageVersion.Preview,
new CSharpParseOptions(LanguageVersion.CSharp11));
}

[Fact]
public async Task UpgradeProjectForSemicolonBody_Enum()
{
await TestLanguageVersionUpgradedAsync(
@"
enum Program[|;|]",
LanguageVersion.Preview,
new CSharpParseOptions(LanguageVersion.CSharp11));
}

[Fact]
public async Task UpgradeProjectForTargetTypedNew()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,17 +98,19 @@ private static void ComputeDeclarations(

return;
}
case SyntaxKind.ClassDeclaration:
case SyntaxKind.StructDeclaration:
case SyntaxKind.RecordDeclaration:
case SyntaxKind.RecordStructDeclaration:
{
if (associatedSymbol is IMethodSymbol ctor)
{
var recordDeclaration = (RecordDeclarationSyntax)node;
Debug.Assert(ctor.MethodKind == MethodKind.Constructor && recordDeclaration.ParameterList is object);
var typeDeclaration = (TypeDeclarationSyntax)node;
Debug.Assert(ctor.MethodKind == MethodKind.Constructor && typeDeclaration.ParameterList is object);

var codeBlocks = GetParameterListInitializersAndAttributes(recordDeclaration.ParameterList);
var codeBlocks = GetParameterListInitializersAndAttributes(typeDeclaration.ParameterList);

if (recordDeclaration.BaseList?.Types.FirstOrDefault() is PrimaryConstructorBaseTypeSyntax initializer)
if (typeDeclaration.BaseList?.Types.FirstOrDefault() is PrimaryConstructorBaseTypeSyntax initializer)
{
codeBlocks = codeBlocks.Concat(initializer);
}
Expand All @@ -117,10 +119,8 @@ private static void ComputeDeclarations(
return;
}

goto case SyntaxKind.ClassDeclaration;
goto case SyntaxKind.InterfaceDeclaration;
}
case SyntaxKind.ClassDeclaration:
case SyntaxKind.StructDeclaration:
case SyntaxKind.InterfaceDeclaration:
{
var t = (TypeDeclarationSyntax)node;
Expand Down
Loading

0 comments on commit 86fb668

Please sign in to comment.