Skip to content

Commit e348906

Browse files
committed
Only remove leading trivia, add tests
1 parent 5e3d65f commit e348906

File tree

2 files changed

+96
-1
lines changed

2 files changed

+96
-1
lines changed

src/CommunityToolkit.Mvvm.CodeFixers/UsePartialPropertyForSemiAutoPropertyCodeFixer.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ private static async Task<Document> ConvertToPartialProperty(Document document,
101101
PropertyDeclarationSyntax updatedPropertyDeclaration =
102102
propertyDeclaration
103103
.AddModifiers(Token(SyntaxKind.PartialKeyword))
104-
.WithoutTrivia()
104+
.WithoutLeadingTrivia()
105105
.WithAttributeLists(attributeLists)
106106
.WithAdditionalAnnotations(Formatter.Annotation)
107107
.WithAccessorList(AccessorList(List(

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

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -267,6 +267,75 @@ public partial class SampleViewModel : ObservableObject
267267
await test.RunAsync();
268268
}
269269

270+
[TestMethod]
271+
public async Task SimpleProperty_Multiple_OnlyTriggersOnSecondOne()
272+
{
273+
string original = """
274+
using CommunityToolkit.Mvvm.ComponentModel;
275+
276+
namespace MyApp;
277+
278+
public class SampleViewModel : ObservableObject
279+
{
280+
private string _firstName;
281+
282+
public string FirstName
283+
{
284+
get => _firstName;
285+
set => SetProperty(ref _firstName, value);
286+
}
287+
288+
public string LastName
289+
{
290+
get => field;
291+
set => SetProperty(ref field, value);
292+
}
293+
}
294+
""";
295+
296+
string @fixed = """
297+
using CommunityToolkit.Mvvm.ComponentModel;
298+
299+
namespace MyApp;
300+
301+
public partial class SampleViewModel : ObservableObject
302+
{
303+
private string _firstName;
304+
305+
public string FirstName
306+
{
307+
get => _firstName;
308+
set => SetProperty(ref _firstName, value);
309+
}
310+
311+
[ObservableProperty]
312+
public partial string LastName { get; set; }
313+
}
314+
""";
315+
316+
CSharpCodeFixTest test = new(LanguageVersion.Preview)
317+
{
318+
TestCode = original,
319+
FixedCode = @fixed,
320+
ReferenceAssemblies = ReferenceAssemblies.Net.Net80,
321+
};
322+
323+
test.TestState.AdditionalReferences.Add(typeof(ObservableObject).Assembly);
324+
test.ExpectedDiagnostics.AddRange(new[]
325+
{
326+
// /0/Test0.cs(15,19): info MVVMTK0056: The semi-auto property MyApp.SampleViewModel.LastName 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)
327+
CSharpCodeFixVerifier.Diagnostic().WithSpan(15, 19, 15, 27).WithArguments("MyApp.SampleViewModel", "LastName"),
328+
});
329+
330+
test.FixedState.ExpectedDiagnostics.AddRange(new[]
331+
{
332+
// /0/Test0.cs(16,27): error CS9248: Partial property 'SampleViewModel.LastName' must have an implementation part.
333+
DiagnosticResult.CompilerError("CS9248").WithSpan(16, 27, 16, 35).WithArguments("MyApp.SampleViewModel.LastName"),
334+
});
335+
336+
await test.RunAsync();
337+
}
338+
270339
[TestMethod]
271340
public async Task SimpleProperty_WithinPartialType()
272341
{
@@ -436,6 +505,19 @@ public string Prop5
436505
get => field;
437506
set => SetProperty(ref field, value);
438507
}
508+
509+
[Test("Attribute without trivia")]
510+
public string Prop6
511+
{
512+
get => field;
513+
set => SetProperty(ref field, value);
514+
}
515+
516+
public string Prop7
517+
{
518+
get => field;
519+
set => SetProperty(ref field, value);
520+
}
439521
}
440522
441523
public class TestAttribute(string text) : Attribute;
@@ -474,6 +556,13 @@ public partial class SampleViewModel : ObservableObject
474556
[ObservableProperty]
475557
[Test("Yet another attribute")]
476558
public partial string Prop5 { get; set; }
559+
560+
[ObservableProperty]
561+
[Test("Attribute without trivia")]
562+
public partial string Prop6 { get; set; }
563+
564+
[ObservableProperty]
565+
public partial string Prop7 { get; set; }
477566
}
478567
479568
public class TestAttribute(string text) : Attribute;
@@ -503,6 +592,12 @@ public class TestAttribute(string text) : Attribute;
503592

504593
// /0/Test0.cs(43,19): info MVVMTK0056: The semi-auto property MyApp.SampleViewModel.Prop5 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)
505594
CSharpCodeFixVerifier.Diagnostic().WithSpan(43, 19, 43, 24).WithArguments("MyApp.SampleViewModel", "Prop5"),
595+
596+
// /0/Test0.cs(50,19): info MVVMTK0056: The semi-auto property MyApp.SampleViewModel.Prop6 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)
597+
CSharpCodeFixVerifier.Diagnostic().WithSpan(50, 19, 50, 24).WithArguments("MyApp.SampleViewModel", "Prop6"),
598+
599+
// /0/Test0.cs(56,19): info MVVMTK0056: The semi-auto property MyApp.SampleViewModel.Prop7 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)
600+
CSharpCodeFixVerifier.Diagnostic().WithSpan(56, 19, 56, 24).WithArguments("MyApp.SampleViewModel", "Prop7"),
506601
});
507602

508603
test.FixedState.ExpectedDiagnostics.AddRange(new[]

0 commit comments

Comments
 (0)