Skip to content

Commit 4863d65

Browse files
authored
Merge pull request #3463 from sharwell/anonymous-interpolation
Fix support for object initializers in interpolations
2 parents 72c7ac2 + 164a00d commit 4863d65

File tree

6 files changed

+48
-51
lines changed

6 files changed

+48
-51
lines changed

StyleCop.Analyzers/StyleCop.Analyzers.Test.CSharp10/SpacingRules/SA1013CSharp10UnitTests.cs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
// Copyright (c) Tunnel Vision Laboratories, LLC. All Rights Reserved.
22
// Licensed under the MIT License. See LICENSE in the project root for license information.
33

4-
#nullable disable
5-
64
namespace StyleCop.Analyzers.Test.CSharp10.SpacingRules
75
{
86
using StyleCop.Analyzers.Test.CSharp9.SpacingRules;

StyleCop.Analyzers/StyleCop.Analyzers.Test.CSharp7/SpacingRules/SA1013CSharp7UnitTests.cs

Lines changed: 15 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
// Copyright (c) Tunnel Vision Laboratories, LLC. All Rights Reserved.
22
// Licensed under the MIT License. See LICENSE in the project root for license information.
33

4-
#nullable disable
5-
64
namespace StyleCop.Analyzers.Test.CSharp7.SpacingRules
75
{
86
using System.Threading;
@@ -32,7 +30,7 @@ public class Foo
3230
{
3331
public void TestMethod()
3432
{
35-
var values = (new[] { 3} , new[] { 3} );
33+
var values = (new[] { 3{|#0:}|} , new[] { 3{|#1:}|} );
3634
}
3735
}";
3836
const string fixedCode = @"using System;
@@ -47,8 +45,8 @@ public void TestMethod()
4745

4846
DiagnosticResult[] expected =
4947
{
50-
Diagnostic().WithLocation(7, 32).WithArguments(string.Empty, "preceded"),
51-
Diagnostic().WithLocation(7, 45).WithArguments(string.Empty, "preceded"),
48+
Diagnostic().WithLocation(0).WithArguments(string.Empty, "preceded"),
49+
Diagnostic().WithLocation(1).WithArguments(string.Empty, "preceded"),
5250
};
5351

5452
await VerifyCSharpFixAsync(testCode, expected, fixedCode, CancellationToken.None).ConfigureAwait(false);
@@ -65,12 +63,12 @@ public unsafe void TestMethod()
6563
{
6664
int* data1 = stackalloc int[] { 1 , 1 } ;
6765
int* data2 = stackalloc int[] { 1 , 1 };
68-
int* data3 = stackalloc int[] { 1 , 1} ;
69-
int* data4 = stackalloc int[] { 1 , 1};
66+
int* data3 = stackalloc int[] { 1 , 1{|#0:}|} ;
67+
int* data4 = stackalloc int[] { 1 , 1{|#1:}|};
7068
int* data5 = stackalloc int[] { 1 , 1
7169
};
7270
int* data6 = stackalloc int[]
73-
{ 1 , 1};
71+
{ 1 , 1{|#2:}|};
7472
int* data7 = stackalloc int[]
7573
{
7674
1 , 1 } ;
@@ -103,9 +101,9 @@ public unsafe void TestMethod()
103101

104102
DiagnosticResult[] expected =
105103
{
106-
Diagnostic().WithArguments(string.Empty, "preceded").WithLocation(9, 50),
107-
Diagnostic().WithArguments(string.Empty, "preceded").WithLocation(10, 50),
108-
Diagnostic().WithArguments(string.Empty, "preceded").WithLocation(14, 20),
104+
Diagnostic().WithLocation(0).WithArguments(string.Empty, "preceded"),
105+
Diagnostic().WithLocation(1).WithArguments(string.Empty, "preceded"),
106+
Diagnostic().WithLocation(2).WithArguments(string.Empty, "preceded"),
109107
};
110108

111109
await VerifyCSharpFixAsync(LanguageVersion.CSharp7_3, testCode, expected, fixedCode, CancellationToken.None).ConfigureAwait(false);
@@ -122,12 +120,12 @@ public unsafe void TestMethod()
122120
{
123121
int* data1 = stackalloc[] { 1 , 1 } ;
124122
int* data2 = stackalloc[] { 1 , 1 };
125-
int* data3 = stackalloc[] { 1 , 1} ;
126-
int* data4 = stackalloc[] { 1 , 1};
123+
int* data3 = stackalloc[] { 1 , 1{|#0:}|} ;
124+
int* data4 = stackalloc[] { 1 , 1{|#1:}|};
127125
int* data5 = stackalloc[] { 1 , 1
128126
};
129127
int* data6 = stackalloc[]
130-
{ 1 , 1};
128+
{ 1 , 1{|#2:}|};
131129
int* data7 = stackalloc[]
132130
{
133131
1 , 1 } ;
@@ -160,9 +158,9 @@ public unsafe void TestMethod()
160158

161159
DiagnosticResult[] expected =
162160
{
163-
Diagnostic().WithArguments(string.Empty, "preceded").WithLocation(9, 46),
164-
Diagnostic().WithArguments(string.Empty, "preceded").WithLocation(10, 46),
165-
Diagnostic().WithArguments(string.Empty, "preceded").WithLocation(14, 20),
161+
Diagnostic().WithLocation(0).WithArguments(string.Empty, "preceded"),
162+
Diagnostic().WithLocation(1).WithArguments(string.Empty, "preceded"),
163+
Diagnostic().WithLocation(2).WithArguments(string.Empty, "preceded"),
166164
};
167165

168166
await VerifyCSharpFixAsync(LanguageVersion.CSharp7_3, testCode, expected, fixedCode, CancellationToken.None).ConfigureAwait(false);

StyleCop.Analyzers/StyleCop.Analyzers.Test.CSharp8/SpacingRules/SA1013CSharp8UnitTests.cs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
// Copyright (c) Tunnel Vision Laboratories, LLC. All Rights Reserved.
22
// Licensed under the MIT License. See LICENSE in the project root for license information.
33

4-
#nullable disable
5-
64
namespace StyleCop.Analyzers.Test.CSharp8.SpacingRules
75
{
86
using System.Threading;
@@ -43,7 +41,7 @@ public void TestMethod(object value)
4341
break;
4442
4543
// The space before 'message' is checked
46-
case Exception { Message: { }message }:
44+
case Exception { Message: { {|#0:}|}message }:
4745
break;
4846
}
4947
}
@@ -67,7 +65,7 @@ public void TestMethod(object value)
6765
}
6866
}";
6967

70-
var expected = Diagnostic().WithSpan(14, 37, 14, 38).WithArguments(string.Empty, "followed");
68+
var expected = Diagnostic().WithLocation(0).WithArguments(string.Empty, "followed");
7169
await VerifyCSharpFixAsync(testCode, expected, fixedCode, CancellationToken.None).ConfigureAwait(false);
7270
}
7371

StyleCop.Analyzers/StyleCop.Analyzers.Test.CSharp9/SpacingRules/SA1013CSharp9UnitTests.cs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
// Copyright (c) Tunnel Vision Laboratories, LLC. All Rights Reserved.
22
// Licensed under the MIT License. See LICENSE in the project root for license information.
33

4-
#nullable disable
5-
64
namespace StyleCop.Analyzers.Test.CSharp9.SpacingRules
75
{
86
using StyleCop.Analyzers.Test.CSharp8.SpacingRules;

StyleCop.Analyzers/StyleCop.Analyzers.Test/SpacingRules/SA1013UnitTests.cs

Lines changed: 29 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
// Copyright (c) Tunnel Vision Laboratories, LLC. All Rights Reserved.
22
// Licensed under the MIT License. See LICENSE in the project root for license information.
33

4-
#nullable disable
5-
64
namespace StyleCop.Analyzers.Test.SpacingRules
75
{
86
using System.Threading;
@@ -54,15 +52,17 @@ public async Task TestStringInterpolationAsync()
5452
{
5553
public class TestClass
5654
{
57-
public void TestMethod()
55+
public void TestMethod(int a, int b)
5856
{
5957
var test = 2;
6058
var x = $""{test}"";
6159
x = $""{test}"";
6260
x = $""({test})"";
6361
x = $""({test} )"";
64-
x = $""{test }"";
65-
x = $""{test } "";
62+
x = $""{test {|#0:}|}"";
63+
x = $""{test {|#1:}|} "";
64+
x = $""{new { a, b{|#2:}|}}"";
65+
x = $""{new { a, b } {|#3:}|}"";
6666
}
6767
}
6868
}
@@ -72,7 +72,7 @@ public void TestMethod()
7272
{
7373
public class TestClass
7474
{
75-
public void TestMethod()
75+
public void TestMethod(int a, int b)
7676
{
7777
var test = 2;
7878
var x = $""{test}"";
@@ -81,15 +81,19 @@ public void TestMethod()
8181
x = $""({test} )"";
8282
x = $""{test}"";
8383
x = $""{test} "";
84+
x = $""{new { a, b }}"";
85+
x = $""{new { a, b }}"";
8486
}
8587
}
8688
}
8789
";
8890

8991
DiagnosticResult[] expected =
9092
{
91-
Diagnostic().WithLocation(12, 25).WithArguments(" not", "preceded"),
92-
Diagnostic().WithLocation(13, 25).WithArguments(" not", "preceded"),
93+
Diagnostic().WithLocation(0).WithArguments(" not", "preceded"),
94+
Diagnostic().WithLocation(1).WithArguments(" not", "preceded"),
95+
Diagnostic().WithLocation(2).WithArguments(string.Empty, "preceded"),
96+
Diagnostic().WithLocation(3).WithArguments(" not", "preceded"),
9397
};
9498

9599
await VerifyCSharpFixAsync(testCode, expected, fixedCode, CancellationToken.None).ConfigureAwait(false);
@@ -107,7 +111,7 @@ public async Task TestPropertyDeclarationAsync()
107111
public class TestClass
108112
{
109113
public int TestProperty1 { get; set; }
110-
public int TestProperty2 { get; set;}
114+
public int TestProperty2 { get; set;{|#0:}|}
111115
}
112116
}
113117
";
@@ -124,7 +128,7 @@ public class TestClass
124128

125129
DiagnosticResult[] expected =
126130
{
127-
Diagnostic().WithLocation(6, 45).WithArguments(string.Empty, "preceded"),
131+
Diagnostic().WithLocation(0).WithArguments(string.Empty, "preceded"),
128132
};
129133

130134
await VerifyCSharpFixAsync(testCode, expected, fixedCode, CancellationToken.None).ConfigureAwait(false);
@@ -146,9 +150,9 @@ public class TestClass
146150
public void TestMethod()
147151
{
148152
new Dictionary<int, int> { { 1, 1 } };
149-
new Dictionary<int, int> { { 1, 1} };
150-
new Dictionary<int, int> { { 1, 1 }};
151-
new Dictionary<int, int> { { 1, 1}};
153+
new Dictionary<int, int> { { 1, 1{|#0:}|} };
154+
new Dictionary<int, int> { { 1, 1 {|#1:}|}{|#2:}|};
155+
new Dictionary<int, int> { { 1, 1{|#3:}|}{|#4:}|};
152156
}
153157
}
154158
}
@@ -173,12 +177,12 @@ public void TestMethod()
173177

174178
DiagnosticResult[] expected =
175179
{
176-
Diagnostic().WithLocation(10, 46).WithArguments(string.Empty, "preceded"),
177-
Diagnostic().WithLocation(11, 47).WithArguments(string.Empty, "followed"),
178-
Diagnostic().WithLocation(11, 48).WithArguments(string.Empty, "preceded"),
179-
Diagnostic().WithLocation(12, 46).WithArguments(string.Empty, "preceded"),
180-
Diagnostic().WithLocation(12, 46).WithArguments(string.Empty, "followed"),
181-
Diagnostic().WithLocation(12, 47).WithArguments(string.Empty, "preceded"),
180+
Diagnostic().WithLocation(0).WithArguments(string.Empty, "preceded"),
181+
Diagnostic().WithLocation(1).WithArguments(string.Empty, "followed"),
182+
Diagnostic().WithLocation(2).WithArguments(string.Empty, "preceded"),
183+
Diagnostic().WithLocation(3).WithArguments(string.Empty, "preceded"),
184+
Diagnostic().WithLocation(3).WithArguments(string.Empty, "followed"),
185+
Diagnostic().WithLocation(4).WithArguments(string.Empty, "preceded"),
182186
};
183187

184188
await VerifyCSharpFixAsync(testCode, expected, fixedCode, CancellationToken.None).ConfigureAwait(false);
@@ -297,9 +301,9 @@ public void TestMethod1(object[] a)
297301
public void TestMethod2()
298302
{
299303
TestMethod1(new object[] { });
300-
TestMethod1(new object[] {});
304+
TestMethod1(new object[] {{|#0:}|});
301305
TestMethod1(new object[] { } );
302-
TestMethod1(new object[] {} );
306+
TestMethod1(new object[] {{|#1:}|} );
303307
}
304308
}
305309
}
@@ -327,8 +331,8 @@ public void TestMethod2()
327331
// space between closing brace and closing parenthesis should be reported by SA1009
328332
DiagnosticResult[] expected =
329333
{
330-
Diagnostic().WithLocation(12, 39).WithArguments(string.Empty, "preceded"),
331-
Diagnostic().WithLocation(14, 39).WithArguments(string.Empty, "preceded"),
334+
Diagnostic().WithLocation(0).WithArguments(string.Empty, "preceded"),
335+
Diagnostic().WithLocation(1).WithArguments(string.Empty, "preceded"),
332336
};
333337

334338
await VerifyCSharpFixAsync(testCode, expected, fixedCode, CancellationToken.None).ConfigureAwait(false);
@@ -383,12 +387,12 @@ public async Task TestMissingTokenAsync()
383387
{
384388
string testCode = @"
385389
class ClassName
386-
{
390+
{{|#0:|}
387391
";
388392

389393
DiagnosticResult[] expected =
390394
{
391-
DiagnosticResult.CompilerError("CS1513").WithMessage("} expected").WithLocation(3, 2),
395+
DiagnosticResult.CompilerError("CS1513").WithMessage("} expected").WithLocation(0),
392396
};
393397

394398
await VerifyCSharpDiagnosticAsync(testCode, expected, CancellationToken.None).ConfigureAwait(false);

StyleCop.Analyzers/StyleCop.Analyzers/SpacingRules/SA1013ClosingBracesMustBeSpacedCorrectly.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,8 @@ private static void HandleCloseBraceToken(SyntaxTreeAnalysisContext context, Syn
105105
|| (nextToken.IsKind(SyntaxKind.QuestionToken) && nextToken.GetNextToken(includeZeroWidth: true).IsKind(SyntaxKind.DotToken))
106106
|| nextToken.IsKind(SyntaxKind.CloseBracketToken)
107107
|| (nextToken.IsKind(SyntaxKind.ColonToken) && nextToken.Parent.IsKind(SyntaxKindEx.CasePatternSwitchLabel))
108-
|| (nextToken.IsKind(SyntaxKind.ExclamationToken) && nextToken.Parent.IsKind(SyntaxKindEx.SuppressNullableWarningExpression));
108+
|| (nextToken.IsKind(SyntaxKind.ExclamationToken) && nextToken.Parent.IsKind(SyntaxKindEx.SuppressNullableWarningExpression))
109+
|| (nextToken.IsKind(SyntaxKind.CloseBraceToken) && nextToken.Parent.IsKind(SyntaxKind.Interpolation));
109110
}
110111
else
111112
{

0 commit comments

Comments
 (0)