Skip to content

Commit c76cb92

Browse files
author
N. Taylor Mullen
committed
Add support for increment/decrement [Parameter] usage detection.
- Didn't realize there was an increment or decrement operation kind. - Added tests to cover this scenario #12543
1 parent 143c101 commit c76cb92

File tree

2 files changed

+76
-3
lines changed

2 files changed

+76
-3
lines changed

src/Components/Analyzers/src/ComponentParameterUsageAnalyzer.cs

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,18 @@ public override void Initialize(AnalysisContext context)
3838
{
3939
startBlockContext.RegisterOperationAction(context =>
4040
{
41-
var assignmentOperation = (IAssignmentOperation)context.Operation;
42-
var leftHandSide = assignmentOperation.Target;
41+
IOperation leftHandSide;
42+
43+
if (context.Operation is IAssignmentOperation assignmentOperation)
44+
{
45+
leftHandSide = assignmentOperation.Target;
46+
}
47+
else
48+
{
49+
var incrementOrDecrementOperation = (IIncrementOrDecrementOperation)context.Operation;
50+
leftHandSide = incrementOrDecrementOperation.Target;
51+
}
52+
4353
if (leftHandSide == null)
4454
{
4555
// Malformed assignment, no left hand side.
@@ -96,7 +106,7 @@ public override void Initialize(AnalysisContext context)
96106
DiagnosticDescriptors.ComponentParametersShouldNotBeSetOutsideOfTheirDeclaredComponent,
97107
propertyReference.Syntax.GetLocation(),
98108
propertyReference.Member.Name));
99-
}, OperationKind.SimpleAssignment, OperationKind.CompoundAssignment, OperationKind.CoalesceAssignment);
109+
}, OperationKind.SimpleAssignment, OperationKind.CompoundAssignment, OperationKind.CoalesceAssignment, OperationKind.Increment, OperationKind.Decrement);
100110
});
101111
});
102112
}

src/Components/Analyzers/test/ComponentParameterUsageAnalyzerTest.cs

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ namespace ConsoleApplication1
1919
class TestComponent : IComponent
2020
{{
2121
[Parameter] public string TestProperty {{ get; set; }}
22+
[Parameter] public int TestInt {{ get; set; }}
2223
public string NonParameter {{ get; set; }}
2324
}}
2425
}}" + ComponentsTestDeclarations.Source;
@@ -119,6 +120,68 @@ void Render()
119120
});
120121
}
121122

123+
[Fact]
124+
public void ComponentPropertyIncrement_Warns()
125+
{
126+
var test = $@"
127+
namespace ConsoleApplication1
128+
{{
129+
using {typeof(ParameterAttribute).Namespace};
130+
class OtherComponent : IComponent
131+
{{
132+
private TestComponent _testComponent;
133+
void Render()
134+
{{
135+
_testComponent = new TestComponent();
136+
_testComponent.TestInt++;
137+
}}
138+
}}
139+
}}" + ComponentTestSource;
140+
141+
VerifyCSharpDiagnostic(test,
142+
new DiagnosticResult
143+
{
144+
Id = DiagnosticDescriptors.ComponentParametersShouldNotBeSetOutsideOfTheirDeclaredComponent.Id,
145+
Message = "Component parameter 'TestInt' should not be set outside of its component.",
146+
Severity = DiagnosticSeverity.Warning,
147+
Locations = new[]
148+
{
149+
new DiagnosticResultLocation("Test0.cs", 11, 17)
150+
}
151+
});
152+
}
153+
154+
[Fact]
155+
public void ComponentPropertyDecrement_Warns()
156+
{
157+
var test = $@"
158+
namespace ConsoleApplication1
159+
{{
160+
using {typeof(ParameterAttribute).Namespace};
161+
class OtherComponent : IComponent
162+
{{
163+
private TestComponent _testComponent;
164+
void Render()
165+
{{
166+
_testComponent = new TestComponent();
167+
_testComponent.TestInt--;
168+
}}
169+
}}
170+
}}" + ComponentTestSource;
171+
172+
VerifyCSharpDiagnostic(test,
173+
new DiagnosticResult
174+
{
175+
Id = DiagnosticDescriptors.ComponentParametersShouldNotBeSetOutsideOfTheirDeclaredComponent.Id,
176+
Message = "Component parameter 'TestInt' should not be set outside of its component.",
177+
Severity = DiagnosticSeverity.Warning,
178+
Locations = new[]
179+
{
180+
new DiagnosticResultLocation("Test0.cs", 11, 17)
181+
}
182+
});
183+
}
184+
122185
[Fact]
123186
public void ComponentPropertyExpression_Ignores()
124187
{

0 commit comments

Comments
 (0)