Skip to content

Commit

Permalink
MA0115 detects the AddComponentParameter method
Browse files Browse the repository at this point in the history
  • Loading branch information
meziantou committed Mar 14, 2024
1 parent c0972b2 commit 7bbd9d2
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 7 deletions.
1 change: 1 addition & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ indent_size = 2
insert_final_newline = true

[*.cs]
dotnet_diagnostic.RS1041.severity = none
dotnet_diagnostic.RS2008.severity = none
dotnet_diagnostic.CA1508.severity = none

Expand Down
8 changes: 4 additions & 4 deletions Directory.Build.targets
Original file line number Diff line number Diff line change
Expand Up @@ -78,10 +78,10 @@

<Otherwise>
<ItemGroup>
<PackageReference Update="Microsoft.CodeAnalysis.Analyzers" Version="3.11.0-beta1.23525.2" />
<PackageReference Update="Microsoft.CodeAnalysis.CSharp" Version="4.8.0" />
<PackageReference Update="Microsoft.CodeAnalysis.Workspaces.Common" Version="4.8.0" />
<PackageReference Update="Microsoft.CodeAnalysis.CSharp.Workspaces" Version="4.8.0" />
<PackageReference Update="Microsoft.CodeAnalysis.Analyzers" Version="3.11.0-beta1.24122.1" />
<PackageReference Update="Microsoft.CodeAnalysis.CSharp" Version="4.9.2" />
<PackageReference Update="Microsoft.CodeAnalysis.Workspaces.Common" Version="4.9.2" />
<PackageReference Update="Microsoft.CodeAnalysis.CSharp.Workspaces" Version="4.9.2" />
</ItemGroup>
<PropertyGroup>
<DefineConstants>$(DefineConstants);ROSLYN4_8;ROSLYN_4_2_OR_GREATER;ROSLYN_4_4_OR_GREATER;ROSLYN_4_5_OR_GREATER;ROSLYN_4_6_OR_GREATER;ROSLYN_4_8_OR_GREATER</DefineConstants>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ public Context(Compilation compilation)
var consoleSymbol = compilation.GetBestTypeByMetadataName("System.Console");
if (consoleSymbol is not null)
{
ConsoleErrorAndOutSymbols = [.. consoleSymbol.GetMembers(nameof(Console.Out)), .. consoleSymbol.GetMembers(nameof(Console.Error))];
ConsoleErrorAndOutSymbols = [.. consoleSymbol.GetMembers("Out"), .. consoleSymbol.GetMembers("Error")];
}
else
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ public void AnalyzeBlockOptions(OperationAnalysisContext context)
{
currentComponent = null;
}
else if (currentComponent is not null && targetMethod.Name == "AddAttribute")
else if (currentComponent is not null && targetMethod.Name is "AddAttribute" or "AddComponentParameter")
{
if (targetMethod.Parameters.Length >= 2 && targetMethod.Parameters[1].Type.IsString())
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ private static ProjectBuilder CreateProjectBuilder()
{
return new ProjectBuilder()
.WithAnalyzer<DoNotUseUnknownParameterForRazorComponentAnalyzer>()
.WithTargetFramework(TargetFramework.AspNetCore6_0);
.WithTargetFramework(TargetFramework.AspNetCore8_0);
}

[Theory]
Expand Down Expand Up @@ -209,6 +209,26 @@ protected override void BuildRenderTree(global::Microsoft.AspNetCore.Components.
__builder.CloseComponent();
}
}
""";
await CreateProjectBuilder()
.WithSourceCode(Usings + sourceCode + ComponentWithChildContent)
.ValidateAsync();
}

[Fact]
public async Task InvalidParameterInAddComponentParameter_Net8()
{
var sourceCode = $$"""
class TypeName : ComponentBase
{
protected override void BuildRenderTree(global::Microsoft.AspNetCore.Components.Rendering.RenderTreeBuilder __builder)
{
__builder.OpenComponent<CustomComponentBase>(1);
[||]__builder.AddComponentParameter(2, "Text", "DummyDisplayText");
[||]__builder.AddComponentParameter(3, "OtherAttribute", "Test");
__builder.CloseComponent();
}
}
""";
await CreateProjectBuilder()
.WithSourceCode(Usings + sourceCode + ComponentWithChildContent)
Expand Down

0 comments on commit 7bbd9d2

Please sign in to comment.