AI-Studio/app/SourceCodeRules/SourceCodeRules/UsageAnalyzers/ThisUsageAnalyzer.cs

253 lines
9.4 KiB
C#
Raw Normal View History

2025-02-25 18:58:17 +00:00
using System.Collections.Immutable;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using Microsoft.CodeAnalysis.Diagnostics;
namespace SourceCodeRules.UsageAnalyzers;
#pragma warning disable RS1038
[DiagnosticAnalyzer(LanguageNames.CSharp)]
#pragma warning restore RS1038
public sealed class ThisUsageAnalyzer : DiagnosticAnalyzer
{
private const string DIAGNOSTIC_ID = Identifier.THIS_USAGE_ANALYZER;
private static readonly string TITLE = "`this.` must be used";
private static readonly string MESSAGE_FORMAT = "`this.` must be used to access variables, methods, and properties";
private static readonly string DESCRIPTION = MESSAGE_FORMAT;
private const string CATEGORY = "Usage";
private static readonly DiagnosticDescriptor RULE = new(DIAGNOSTIC_ID, TITLE, MESSAGE_FORMAT, CATEGORY, DiagnosticSeverity.Error, isEnabledByDefault: true, description: DESCRIPTION);
public override ImmutableArray<DiagnosticDescriptor> SupportedDiagnostics => [RULE];
public override void Initialize(AnalysisContext context)
{
context.ConfigureGeneratedCodeAnalysis(GeneratedCodeAnalysisFlags.None);
context.EnableConcurrentExecution();
context.RegisterSyntaxNodeAction(this.AnalyzeIdentifier, SyntaxKind.IdentifierName);
context.RegisterSyntaxNodeAction(this.AnalyzeGenericName, SyntaxKind.GenericName);
}
private void AnalyzeGenericName(SyntaxNodeAnalysisContext context)
{
var genericNameSyntax = (GenericNameSyntax)context.Node;
2025-04-24 11:50:14 +00:00
// Skip if already part of a 'this' expression:
2025-02-25 18:58:17 +00:00
if (IsAccessedThroughThis(genericNameSyntax))
return;
if (IsWithinInitializer(genericNameSyntax))
return;
if (IsPartOfMemberAccess(genericNameSyntax))
return;
2025-04-24 11:50:14 +00:00
// Skip if it's the 'T' translation method
if (IsTranslationMethod(genericNameSyntax))
return;
// Get symbol info for the generic name:
2025-02-25 18:58:17 +00:00
var symbolInfo = context.SemanticModel.GetSymbolInfo(genericNameSyntax);
var symbol = symbolInfo.Symbol;
if (symbol == null)
return;
// Skip static methods
if (symbol.IsStatic)
return;
// Skip local functions
if (symbol is IMethodSymbol methodSymbol && IsLocalFunction(methodSymbol))
return;
// Get the containing type of the current context
var containingSymbol = context.ContainingSymbol;
var currentType = containingSymbol?.ContainingType;
// If we're in a static context, allow accessing members without this
if (IsInStaticContext(containingSymbol))
return;
if (symbol is IMethodSymbol)
{
var containingType = symbol.ContainingType;
// If the symbol is a member of the current type or a base type, then require this
if (currentType != null && (SymbolEqualityComparer.Default.Equals(containingType, currentType) ||
IsBaseTypeOf(containingType, currentType)))
{
var diagnostic = Diagnostic.Create(RULE, genericNameSyntax.Identifier.GetLocation());
context.ReportDiagnostic(diagnostic);
}
}
}
2025-04-24 11:50:14 +00:00
private static bool IsTranslationMethod(SyntaxNode node)
{
// Check if this is a method called 'T' (translation method)
if (node is IdentifierNameSyntax { Identifier.Text: "T" })
return true;
return false;
}
2025-02-25 18:58:17 +00:00
private void AnalyzeIdentifier(SyntaxNodeAnalysisContext context)
{
var identifierNameSyntax = (IdentifierNameSyntax)context.Node;
2025-04-24 11:50:14 +00:00
// Skip if this identifier is part of a generic name - we'll handle that separately:
2025-02-25 18:58:17 +00:00
if (identifierNameSyntax.Parent is GenericNameSyntax)
return;
2025-04-24 11:50:14 +00:00
// Skip if already part of a 'this' expression:
2025-02-25 18:58:17 +00:00
if (IsAccessedThroughThis(identifierNameSyntax))
return;
if (IsWithinInitializer(identifierNameSyntax))
return;
if (IsPartOfMemberAccess(identifierNameSyntax))
return;
2025-04-24 11:50:14 +00:00
// Also skip if it's part of static import statements:
2025-02-25 18:58:17 +00:00
if (IsPartOfUsingStaticDirective(identifierNameSyntax))
return;
2025-04-24 11:50:14 +00:00
// Skip if it's part of a namespace or type name:
2025-02-25 18:58:17 +00:00
if (IsPartOfNamespaceOrTypeName(identifierNameSyntax))
return;
2025-04-24 11:50:14 +00:00
// Skip if it's the 'T' translation method:
if (IsTranslationMethod(identifierNameSyntax))
return;
// Get symbol info:
2025-02-25 18:58:17 +00:00
var symbolInfo = context.SemanticModel.GetSymbolInfo(identifierNameSyntax);
var symbol = symbolInfo.Symbol;
if (symbol == null)
return;
2025-04-24 11:50:14 +00:00
// Skip local variables, parameters, and range variables:
2025-02-25 18:58:17 +00:00
if (symbol.Kind is SymbolKind.Local or SymbolKind.Parameter or SymbolKind.RangeVariable or SymbolKind.TypeParameter)
return;
2025-04-24 11:50:14 +00:00
// Skip types and namespaces:
2025-02-25 18:58:17 +00:00
if (symbol.Kind is SymbolKind.NamedType or SymbolKind.Namespace)
return;
2025-04-24 11:50:14 +00:00
// Explicitly check if this is a local function:
2025-02-25 18:58:17 +00:00
if (symbol is IMethodSymbol methodSymbol && IsLocalFunction(methodSymbol))
return;
2025-04-24 11:50:14 +00:00
// Get the containing type of the current context:
2025-02-25 18:58:17 +00:00
var containingSymbol = context.ContainingSymbol;
var currentType = containingSymbol?.ContainingType;
2025-04-24 11:50:14 +00:00
// If we're in a static context, allow accessing members without this:
2025-02-25 18:58:17 +00:00
if (IsInStaticContext(containingSymbol))
return;
2025-04-24 11:50:14 +00:00
// Now check if the symbol is an instance member of the current class:
2025-02-25 18:58:17 +00:00
if (symbol is IFieldSymbol or IPropertySymbol or IMethodSymbol or IEventSymbol)
{
2025-04-24 11:50:14 +00:00
// Skip static members:
2025-02-25 18:58:17 +00:00
if (symbol.IsStatic)
return;
2025-04-24 11:50:14 +00:00
// Skip constants:
2025-02-25 18:58:17 +00:00
if (symbol is IFieldSymbol { IsConst: true })
return;
var containingType = symbol.ContainingType;
2025-04-24 11:50:14 +00:00
// If the symbol is a member of the current type or a base type, then require this:
2025-02-25 18:58:17 +00:00
if (currentType != null && (SymbolEqualityComparer.Default.Equals(containingType, currentType) ||
IsBaseTypeOf(containingType, currentType)))
{
var diagnostic = Diagnostic.Create(RULE, identifierNameSyntax.GetLocation());
context.ReportDiagnostic(diagnostic);
}
}
}
private static bool IsLocalFunction(IMethodSymbol methodSymbol) => methodSymbol.MethodKind is MethodKind.LocalFunction;
private static bool IsBaseTypeOf(INamedTypeSymbol baseType, INamedTypeSymbol derivedType)
{
var currentType = derivedType.BaseType;
while (currentType != null)
{
if (SymbolEqualityComparer.Default.Equals(currentType, baseType))
return true;
currentType = currentType.BaseType;
}
return false;
}
private static bool IsInStaticContext(ISymbol? containingSymbol) => containingSymbol?.IsStatic is true;
private static bool IsAccessedThroughThis(SyntaxNode node)
{
if (node.Parent is MemberAccessExpressionSyntax memberAccess)
if (memberAccess.Expression is ThisExpressionSyntax && memberAccess.Name == node)
return true;
return false;
}
private static bool IsWithinInitializer(SyntaxNode node)
{
for (var current = node.Parent; current != null; current = current.Parent)
if (current is InitializerExpressionSyntax)
return true;
return false;
}
private static bool IsPartOfMemberAccess(SyntaxNode node)
{
// Check if the node is part of a member access expression where the expression is not 'this':
if (node.Parent is MemberAccessExpressionSyntax memberAccess)
{
// If the member access expression is 'this', it's allowed:
if (memberAccess.Expression is ThisExpressionSyntax)
return false;
// If the member access expression is something else (e.g., instance.Member), skip:
if (memberAccess.Name == node)
return true;
}
// Also check for conditional access expressions (e.g., instance?.Member):
if (node.Parent is ConditionalAccessExpressionSyntax)
return true;
return false;
}
private static bool IsPartOfUsingStaticDirective(SyntaxNode node)
{
for (var current = node.Parent; current != null; current = current.Parent)
if (current is UsingDirectiveSyntax)
return true;
return false;
}
private static bool IsPartOfNamespaceOrTypeName(SyntaxNode node)
{
// Check if a node is part of a namespace, class, or type declaration:
if (node.Parent is NameSyntax && node.Parent is not MemberAccessExpressionSyntax)
return true;
return false;
}
}