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
This commit is contained in:
N. Taylor Mullen 2019-07-24 16:21:19 -07:00
parent 143c101693
commit c76cb9248d
2 changed files with 76 additions and 3 deletions

View File

@ -38,8 +38,18 @@ namespace Microsoft.AspNetCore.Components.Analyzers
{
startBlockContext.RegisterOperationAction(context =>
{
var assignmentOperation = (IAssignmentOperation)context.Operation;
var leftHandSide = assignmentOperation.Target;
IOperation leftHandSide;
if (context.Operation is IAssignmentOperation assignmentOperation)
{
leftHandSide = assignmentOperation.Target;
}
else
{
var incrementOrDecrementOperation = (IIncrementOrDecrementOperation)context.Operation;
leftHandSide = incrementOrDecrementOperation.Target;
}
if (leftHandSide == null)
{
// Malformed assignment, no left hand side.
@ -96,7 +106,7 @@ namespace Microsoft.AspNetCore.Components.Analyzers
DiagnosticDescriptors.ComponentParametersShouldNotBeSetOutsideOfTheirDeclaredComponent,
propertyReference.Syntax.GetLocation(),
propertyReference.Member.Name));
}, OperationKind.SimpleAssignment, OperationKind.CompoundAssignment, OperationKind.CoalesceAssignment);
}, OperationKind.SimpleAssignment, OperationKind.CompoundAssignment, OperationKind.CoalesceAssignment, OperationKind.Increment, OperationKind.Decrement);
});
});
}

View File

@ -19,6 +19,7 @@ namespace Microsoft.AspNetCore.Components.Analyzers
class TestComponent : IComponent
{{
[Parameter] public string TestProperty {{ get; set; }}
[Parameter] public int TestInt {{ get; set; }}
public string NonParameter {{ get; set; }}
}}
}}" + ComponentsTestDeclarations.Source;
@ -119,6 +120,68 @@ namespace Microsoft.AspNetCore.Components.Analyzers
});
}
[Fact]
public void ComponentPropertyIncrement_Warns()
{
var test = $@"
namespace ConsoleApplication1
{{
using {typeof(ParameterAttribute).Namespace};
class OtherComponent : IComponent
{{
private TestComponent _testComponent;
void Render()
{{
_testComponent = new TestComponent();
_testComponent.TestInt++;
}}
}}
}}" + ComponentTestSource;
VerifyCSharpDiagnostic(test,
new DiagnosticResult
{
Id = DiagnosticDescriptors.ComponentParametersShouldNotBeSetOutsideOfTheirDeclaredComponent.Id,
Message = "Component parameter 'TestInt' should not be set outside of its component.",
Severity = DiagnosticSeverity.Warning,
Locations = new[]
{
new DiagnosticResultLocation("Test0.cs", 11, 17)
}
});
}
[Fact]
public void ComponentPropertyDecrement_Warns()
{
var test = $@"
namespace ConsoleApplication1
{{
using {typeof(ParameterAttribute).Namespace};
class OtherComponent : IComponent
{{
private TestComponent _testComponent;
void Render()
{{
_testComponent = new TestComponent();
_testComponent.TestInt--;
}}
}}
}}" + ComponentTestSource;
VerifyCSharpDiagnostic(test,
new DiagnosticResult
{
Id = DiagnosticDescriptors.ComponentParametersShouldNotBeSetOutsideOfTheirDeclaredComponent.Id,
Message = "Component parameter 'TestInt' should not be set outside of its component.",
Severity = DiagnosticSeverity.Warning,
Locations = new[]
{
new DiagnosticResultLocation("Test0.cs", 11, 17)
}
});
}
[Fact]
public void ComponentPropertyExpression_Ignores()
{