Skip to content

Commit da89014

Browse files
committed
Fix leading trivia in accessors, add tests
1 parent e348906 commit da89014

File tree

2 files changed

+87
-5
lines changed

2 files changed

+87
-5
lines changed

src/CommunityToolkit.Mvvm.CodeFixers/UsePartialPropertyForSemiAutoPropertyCodeFixer.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,8 +116,9 @@ private static async Task<Document> ConvertToPartialProperty(Document document,
116116
.WithBody(null)
117117
.WithExpressionBody(null)
118118
.WithSemicolonToken(Token(SyntaxKind.SemicolonToken))
119+
.WithTrailingTrivia(propertyDeclaration.AccessorList.Accessors[1].GetTrailingTrivia())
119120
.WithAdditionalAnnotations(Formatter.Annotation)
120-
])));
121+
])).WithTrailingTrivia(propertyDeclaration.AccessorList.GetTrailingTrivia()));
121122

122123
// Create an editor to perform all mutations
123124
SyntaxEditor editor = new(root, document.Project.Solution.Workspace.Services);

tests/CommunityToolkit.Mvvm.SourceGenerators.Roslyn4120.UnitTests/Test_UseObservablePropertyOnSemiAutoPropertyCodeFixer.cs

Lines changed: 85 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -260,8 +260,80 @@ public partial class SampleViewModel : ObservableObject
260260

261261
test.FixedState.ExpectedDiagnostics.AddRange(new[]
262262
{
263-
// /0/Test0.cs(8,27): error CS9248: Partial property 'SampleViewModel.Name' must have an implementation part.
264-
DiagnosticResult.CompilerError("CS9248").WithSpan(8, 27, 8, 31).WithArguments("MyApp.SampleViewModel.Name"),
263+
// /0/Test0.cs(8,27): error CS9248: Partial property 'SampleViewModel.FirstName' must have an implementation part.
264+
DiagnosticResult.CompilerError("CS9248").WithSpan(8, 27, 8, 36).WithArguments("MyApp.SampleViewModel.FirstName"),
265+
266+
// /0/Test0.cs(11,27): error CS9248: Partial property 'SampleViewModel.LastName' must have an implementation part.
267+
DiagnosticResult.CompilerError("CS9248").WithSpan(11, 27, 11, 35).WithArguments("MyApp.SampleViewModel.LastName"),
268+
});
269+
270+
await test.RunAsync();
271+
}
272+
273+
[TestMethod]
274+
public async Task SimpleProperty_Multiple_OnlyTriggersOnFirstOne()
275+
{
276+
string original = """
277+
using CommunityToolkit.Mvvm.ComponentModel;
278+
279+
namespace MyApp;
280+
281+
public class SampleViewModel : ObservableObject
282+
{
283+
public string FirstName
284+
{
285+
get => field;
286+
set => SetProperty(ref field, value);
287+
}
288+
289+
private string _lastName;
290+
291+
public string LastName
292+
{
293+
get => _lastName;
294+
set => SetProperty(ref _lastName, value);
295+
}
296+
}
297+
""";
298+
299+
string @fixed = """
300+
using CommunityToolkit.Mvvm.ComponentModel;
301+
302+
namespace MyApp;
303+
304+
public partial class SampleViewModel : ObservableObject
305+
{
306+
[ObservableProperty]
307+
public partial string FirstName { get; set; }
308+
309+
private string _lastName;
310+
311+
public string LastName
312+
{
313+
get => _lastName;
314+
set => SetProperty(ref _lastName, value);
315+
}
316+
}
317+
""";
318+
319+
CSharpCodeFixTest test = new(LanguageVersion.Preview)
320+
{
321+
TestCode = original,
322+
FixedCode = @fixed,
323+
ReferenceAssemblies = ReferenceAssemblies.Net.Net80,
324+
};
325+
326+
test.TestState.AdditionalReferences.Add(typeof(ObservableObject).Assembly);
327+
test.ExpectedDiagnostics.AddRange(new[]
328+
{
329+
// /0/Test0.cs(7,19): info MVVMTK0056: The semi-auto property MyApp.SampleViewModel.FirstName can be converted to a partial property using [ObservableProperty], which is recommended (doing so makes the code less verbose and results in more optimized code)
330+
CSharpCodeFixVerifier.Diagnostic().WithSpan(7, 19, 7, 28).WithArguments("MyApp.SampleViewModel", "FirstName"),
331+
});
332+
333+
test.FixedState.ExpectedDiagnostics.AddRange(new[]
334+
{
335+
// /0/Test0.cs(8,27): error CS9248: Partial property 'SampleViewModel.FirstName' must have an implementation part.
336+
DiagnosticResult.CompilerError("CS9248").WithSpan(8, 27, 8, 36).WithArguments("MyApp.SampleViewModel.FirstName"),
265337
});
266338

267339
await test.RunAsync();
@@ -447,8 +519,11 @@ public partial class SampleViewModel : ObservableObject
447519

448520
test.FixedState.ExpectedDiagnostics.AddRange(new[]
449521
{
450-
// /0/Test0.cs(8,27): error CS9248: Partial property 'SampleViewModel.Name' must have an implementation part.
451-
DiagnosticResult.CompilerError("CS9248").WithSpan(8, 27, 8, 31).WithArguments("MyApp.SampleViewModel.Name"),
522+
// /0/Test0.cs(8,27): error CS9248: Partial property 'SampleViewModel.FirstName' must have an implementation part.
523+
DiagnosticResult.CompilerError("CS9248").WithSpan(8, 27, 8, 36).WithArguments("MyApp.SampleViewModel.FirstName"),
524+
525+
// /0/Test0.cs(11,27): error CS9248: Partial property 'SampleViewModel.LastName' must have an implementation part.
526+
DiagnosticResult.CompilerError("CS9248").WithSpan(11, 27, 11, 35).WithArguments("MyApp.SampleViewModel.LastName"),
452527
});
453528

454529
await test.RunAsync();
@@ -616,6 +691,12 @@ public class TestAttribute(string text) : Attribute;
616691

617692
// /0/Test0.cs(32,27): error CS9248: Partial property 'SampleViewModel.Prop5' must have an implementation part.
618693
DiagnosticResult.CompilerError("CS9248").WithSpan(32, 27, 32, 32).WithArguments("MyApp.SampleViewModel.Prop5"),
694+
695+
// /0/Test0.cs(36,27): error CS9248: Partial property 'SampleViewModel.Prop6' must have an implementation part.
696+
DiagnosticResult.CompilerError("CS9248").WithSpan(36, 27, 36, 32).WithArguments("MyApp.SampleViewModel.Prop6"),
697+
698+
// /0/Test0.cs(39,27): error CS9248: Partial property 'SampleViewModel.Prop7' must have an implementation part.
699+
DiagnosticResult.CompilerError("CS9248").WithSpan(39, 27, 39, 32).WithArguments("MyApp.SampleViewModel.Prop7"),
619700
});
620701

621702
await test.RunAsync();

0 commit comments

Comments
 (0)