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:
parent
143c101693
commit
c76cb9248d
|
|
@ -38,8 +38,18 @@ namespace Microsoft.AspNetCore.Components.Analyzers
|
||||||
{
|
{
|
||||||
startBlockContext.RegisterOperationAction(context =>
|
startBlockContext.RegisterOperationAction(context =>
|
||||||
{
|
{
|
||||||
var assignmentOperation = (IAssignmentOperation)context.Operation;
|
IOperation leftHandSide;
|
||||||
var leftHandSide = assignmentOperation.Target;
|
|
||||||
|
if (context.Operation is IAssignmentOperation assignmentOperation)
|
||||||
|
{
|
||||||
|
leftHandSide = assignmentOperation.Target;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
var incrementOrDecrementOperation = (IIncrementOrDecrementOperation)context.Operation;
|
||||||
|
leftHandSide = incrementOrDecrementOperation.Target;
|
||||||
|
}
|
||||||
|
|
||||||
if (leftHandSide == null)
|
if (leftHandSide == null)
|
||||||
{
|
{
|
||||||
// Malformed assignment, no left hand side.
|
// Malformed assignment, no left hand side.
|
||||||
|
|
@ -96,7 +106,7 @@ namespace Microsoft.AspNetCore.Components.Analyzers
|
||||||
DiagnosticDescriptors.ComponentParametersShouldNotBeSetOutsideOfTheirDeclaredComponent,
|
DiagnosticDescriptors.ComponentParametersShouldNotBeSetOutsideOfTheirDeclaredComponent,
|
||||||
propertyReference.Syntax.GetLocation(),
|
propertyReference.Syntax.GetLocation(),
|
||||||
propertyReference.Member.Name));
|
propertyReference.Member.Name));
|
||||||
}, OperationKind.SimpleAssignment, OperationKind.CompoundAssignment, OperationKind.CoalesceAssignment);
|
}, OperationKind.SimpleAssignment, OperationKind.CompoundAssignment, OperationKind.CoalesceAssignment, OperationKind.Increment, OperationKind.Decrement);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -19,6 +19,7 @@ namespace Microsoft.AspNetCore.Components.Analyzers
|
||||||
class TestComponent : IComponent
|
class TestComponent : IComponent
|
||||||
{{
|
{{
|
||||||
[Parameter] public string TestProperty {{ get; set; }}
|
[Parameter] public string TestProperty {{ get; set; }}
|
||||||
|
[Parameter] public int TestInt {{ get; set; }}
|
||||||
public string NonParameter {{ get; set; }}
|
public string NonParameter {{ get; set; }}
|
||||||
}}
|
}}
|
||||||
}}" + ComponentsTestDeclarations.Source;
|
}}" + 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]
|
[Fact]
|
||||||
public void ComponentPropertyExpression_Ignores()
|
public void ComponentPropertyExpression_Ignores()
|
||||||
{
|
{
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue