-
Notifications
You must be signed in to change notification settings - Fork 240
Expand file tree
/
Copy pathBlazorQueryParameterRoutableComponent.cs
More file actions
105 lines (93 loc) · 4.45 KB
/
BlazorQueryParameterRoutableComponent.cs
File metadata and controls
105 lines (93 loc) · 4.45 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
/*
* SonarAnalyzer for .NET
* Copyright (C) 2015-2024 SonarSource SA
* mailto: contact AT sonarsource DOT com
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 3 of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
namespace SonarAnalyzer.Rules.CSharp;
[Obsolete("This rule has been deprecated since 9.25")]
[DiagnosticAnalyzer(LanguageNames.CSharp)]
public sealed class BlazorQueryParameterRoutableComponent : SonarDiagnosticAnalyzer
{
private const string NoRouteQueryDiagnosticId = "S6803";
private const string NoRouteQueryMessageFormat = "Component parameters can only receive query parameter values in routable components.";
private const string QueryTypeDiagnosticId = "S6797";
private const string QueryTypeMessageFormat = "Query parameter type '{0}' is not supported.";
private static readonly DiagnosticDescriptor S6803Rule = DescriptorFactory.Create(NoRouteQueryDiagnosticId, NoRouteQueryMessageFormat);
private static readonly DiagnosticDescriptor S6797Rule = DescriptorFactory.Create(QueryTypeDiagnosticId, QueryTypeMessageFormat);
private static readonly ISet<KnownType> SupportedQueryTypes = new HashSet<KnownType>
{
KnownType.System_Boolean,
KnownType.System_DateTime,
KnownType.System_Decimal,
KnownType.System_Double,
KnownType.System_Single,
KnownType.System_Int32,
KnownType.System_Int64,
KnownType.System_String,
KnownType.System_Guid
};
public override ImmutableArray<DiagnosticDescriptor> SupportedDiagnostics => ImmutableArray.Create(S6803Rule, S6797Rule);
protected override void Initialize(SonarAnalysisContext context) =>
context.RegisterCompilationStartAction(c =>
{
if (c.Compilation.GetTypeByMetadataName(KnownType.Microsoft_AspNetCore_Components_RouteAttribute) is not null)
{
context.RegisterSymbolAction(CheckQueryProperties, SymbolKind.Property);
}
});
private static void CheckQueryProperties(SonarSymbolReportingContext c)
{
var property = (IPropertySymbol)c.Symbol;
if (property.HasAttribute(KnownType.Microsoft_AspNetCore_Components_SupplyParameterFromQueryAttribute)
&& property.HasAttribute(KnownType.Microsoft_AspNetCore_Components_ParameterAttribute))
{
if (!property.ContainingType.HasAttribute(KnownType.Microsoft_AspNetCore_Components_RouteAttribute))
{
foreach (var location in property.Locations)
{
c.ReportIssue(Diagnostic.Create(S6803Rule, location));
}
}
else if (!SupportedQueryTypes.Any(x => IsSupportedType(property.Type, x)))
{
foreach (var propertyType in property.DeclaringSyntaxReferences.Select(x => ((PropertyDeclarationSyntax)x.GetSyntax()).Type))
{
c.ReportIssue(Diagnostic.Create(S6797Rule, propertyType.GetLocation(), GetTypeName(propertyType)));
}
}
}
}
private static bool IsSupportedType(ITypeSymbol type, KnownType supportType)
{
if (type is IArrayTypeSymbol arrayTypeSymbol)
{
type = arrayTypeSymbol.ElementType;
}
if (KnownType.System_Nullable_T.Matches(type))
{
type = ((INamedTypeSymbol)type).TypeArguments[0];
}
return supportType.Matches(type);
}
private static string GetTypeName(TypeSyntax propertyType) =>
propertyType switch
{
GenericNameSyntax genericSyntax when propertyType.NameIs(KnownType.System_Nullable_T.TypeName) => genericSyntax.TypeArgumentList.Arguments[0].GetName(),
{} tuple when TupleTypeSyntaxWrapper.IsInstance(tuple) => KnownType.System_ValueTuple.TypeName,
_ => propertyType.GetName()
};
}