-
Notifications
You must be signed in to change notification settings - Fork 10.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement BL0007: Component parameters should be auto property (#30102)
* Implement ASP0002: Use 'ParameterAttribute' correctly * Attempt to detect full-props that have same semantics as auto-props Fixes #26230 * Address feedback * Add tests * Few fixes * Address feedback * Rename test file and fix failing test * Add unintentionally deleted test file * Fix trailing spaces * Update ComponentParameterShouldBeAutoPropertiesTest.cs * Update src/Components/Analyzers/test/ComponentParameterShouldBeAutoPropertiesTest.cs * Switch to MIT license * Update ComponentParameterAnalyzer.cs * Update ComponentParameterShouldBeAutoPropertiesTest.cs Co-authored-by: Pranav K <[email protected]>
- Loading branch information
1 parent
0d2e5aa
commit 417f950
Showing
4 changed files
with
255 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
152 changes: 152 additions & 0 deletions
152
src/Components/Analyzers/test/ComponentParameterShouldBeAutoPropertiesTest.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,152 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
|
||
using Microsoft.CodeAnalysis.Diagnostics; | ||
using TestHelper; | ||
using Xunit; | ||
|
||
namespace Microsoft.AspNetCore.Components.Analyzers.Tests; | ||
|
||
public class ComponentParameterShouldBeAutoPropertiesTest : DiagnosticVerifier | ||
{ | ||
[Fact] | ||
public void IsAutoProperty_NoDiagnostic() | ||
{ | ||
var source = @" | ||
using Microsoft.AspNetCore.Components | ||
public class C | ||
{ | ||
[Parameter] | ||
public string MyProp { get; set; } | ||
[Parameter] | ||
public string MyProp2 { set; get; } | ||
} | ||
" + ComponentsTestDeclarations.Source; | ||
VerifyCSharpDiagnostic(source); | ||
} | ||
|
||
[Fact] | ||
public void HaveSameSemanticAsAutoProperty_NoDiagnostic() | ||
{ | ||
var source = @" | ||
using Microsoft.AspNetCore.Components | ||
public class C | ||
{ | ||
private string _myProp; | ||
private string _myProp2; | ||
private string _myProp3; | ||
[Parameter] | ||
public string MyProp | ||
{ | ||
get => _myProp; | ||
set => _myProp = value; | ||
} | ||
[Parameter] | ||
public string MyProp2 | ||
{ | ||
set => _myProp2 = value; | ||
get => _myProp2; | ||
} | ||
[Parameter] | ||
public string MyProp3 | ||
{ | ||
get | ||
{ | ||
return _myProp3; | ||
} | ||
set | ||
{ | ||
_myProp3 = value; | ||
} | ||
} | ||
} | ||
" + ComponentsTestDeclarations.Source; | ||
VerifyCSharpDiagnostic(source); | ||
} | ||
|
||
[Fact] | ||
public void HaveLogicInSetter_Diagnostic() | ||
{ | ||
var source = @" | ||
using Microsoft.AspNetCore.Components | ||
public class C | ||
{ | ||
private string _myProp; | ||
[Parameter] | ||
public string MyProp | ||
{ | ||
get | ||
{ | ||
return _myProp; | ||
} | ||
set | ||
{ | ||
DoSomething(); | ||
_myProp = value; | ||
} | ||
} | ||
private void DoSomething() { } | ||
} | ||
" + ComponentsTestDeclarations.Source; | ||
VerifyCSharpDiagnostic(source, new DiagnosticResult | ||
{ | ||
Id = DiagnosticDescriptors.ComponentParametersShouldBeAutoProperies.Id, | ||
Message = "Component parameter 'C.MyProp' should be auto property", | ||
Locations = new[] | ||
{ | ||
new DiagnosticResultLocation("Test0.cs", 9, 15), | ||
}, | ||
Severity = CodeAnalysis.DiagnosticSeverity.Warning, | ||
}); | ||
} | ||
|
||
[Fact] | ||
public void HaveLogicInGetter_Diagnostic() | ||
{ | ||
var source = @" | ||
using Microsoft.AspNetCore.Components | ||
public class C | ||
{ | ||
private string _myProp; | ||
[Parameter] | ||
public string MyProp | ||
{ | ||
get | ||
{ | ||
DoSomething(); | ||
return _myProp; | ||
} | ||
set | ||
{ | ||
_myProp = value; | ||
} | ||
} | ||
private void DoSomething() { } | ||
} | ||
" + ComponentsTestDeclarations.Source; | ||
VerifyCSharpDiagnostic(source, new DiagnosticResult | ||
{ | ||
Id = DiagnosticDescriptors.ComponentParametersShouldBeAutoProperies.Id, | ||
Message = "Component parameter 'C.MyProp' should be auto property", | ||
Locations = new[] | ||
{ | ||
new DiagnosticResultLocation("Test0.cs", 9, 15), | ||
}, | ||
Severity = CodeAnalysis.DiagnosticSeverity.Warning, | ||
}); | ||
} | ||
|
||
protected override DiagnosticAnalyzer GetCSharpDiagnosticAnalyzer() => new ComponentParameterAnalyzer(); | ||
} |