|
| 1 | +using System.Collections.Immutable; |
| 2 | +using Microsoft.CodeAnalysis; |
| 3 | +using Microsoft.CodeAnalysis.CSharp; |
| 4 | +using Microsoft.CodeAnalysis.CSharp.Syntax; |
| 5 | +using Microsoft.CodeAnalysis.Diagnostics; |
| 6 | +using Microsoft.CodeAnalysis.Text; |
| 7 | + |
| 8 | +namespace Faithlife.Analyzers; |
| 9 | + |
| 10 | +[DiagnosticAnalyzer(LanguageNames.CSharp)] |
| 11 | +public sealed class LambdaOperatorAnalyzer : DiagnosticAnalyzer |
| 12 | +{ |
| 13 | + public override void Initialize(AnalysisContext context) |
| 14 | + { |
| 15 | + context.EnableConcurrentExecution(); |
| 16 | + context.ConfigureGeneratedCodeAnalysis(GeneratedCodeAnalysisFlags.None); |
| 17 | + |
| 18 | + context.RegisterSyntaxNodeAction(AnalyzeSyntax, |
| 19 | + SyntaxKind.ArrowExpressionClause, |
| 20 | + SyntaxKind.SimpleLambdaExpression, |
| 21 | + SyntaxKind.ParenthesizedLambdaExpression, |
| 22 | + SyntaxKind.SwitchExpressionArm); |
| 23 | + } |
| 24 | + |
| 25 | + public const string DiagnosticId = "FL0024"; |
| 26 | + |
| 27 | + public override ImmutableArray<DiagnosticDescriptor> SupportedDiagnostics => [s_rule]; |
| 28 | + |
| 29 | + private static void AnalyzeSyntax(SyntaxNodeAnalysisContext context) |
| 30 | + { |
| 31 | + var lambdaOperatorToken = GetLambdaOperatorToken(context.Node); |
| 32 | + if (!lambdaOperatorToken.IsKind(SyntaxKind.EqualsGreaterThanToken)) |
| 33 | + return; |
| 34 | + |
| 35 | + var previousToken = lambdaOperatorToken.GetPreviousToken(); |
| 36 | + if (previousToken.IsKind(SyntaxKind.None)) |
| 37 | + return; |
| 38 | + |
| 39 | + var sourceText = lambdaOperatorToken.SyntaxTree.GetText(context.CancellationToken); |
| 40 | + if (!IsFirstNonWhitespaceOnLine(sourceText, lambdaOperatorToken) || |
| 41 | + !ContainsOnlyWhitespace(sourceText, TextSpan.FromBounds(previousToken.Span.End, lambdaOperatorToken.SpanStart))) |
| 42 | + { |
| 43 | + return; |
| 44 | + } |
| 45 | + |
| 46 | + context.ReportDiagnostic(Diagnostic.Create(s_rule, lambdaOperatorToken.GetLocation())); |
| 47 | + } |
| 48 | + |
| 49 | + private static SyntaxToken GetLambdaOperatorToken(SyntaxNode node) => |
| 50 | + node switch |
| 51 | + { |
| 52 | + ArrowExpressionClauseSyntax arrowExpressionClause => arrowExpressionClause.ArrowToken, |
| 53 | + ParenthesizedLambdaExpressionSyntax parenthesizedLambdaExpression => parenthesizedLambdaExpression.ArrowToken, |
| 54 | + SimpleLambdaExpressionSyntax simpleLambdaExpression => simpleLambdaExpression.ArrowToken, |
| 55 | + SwitchExpressionArmSyntax switchExpressionArm => switchExpressionArm.EqualsGreaterThanToken, |
| 56 | + _ => default, |
| 57 | + }; |
| 58 | + |
| 59 | + private static bool IsFirstNonWhitespaceOnLine(SourceText sourceText, SyntaxToken token) |
| 60 | + { |
| 61 | + var line = sourceText.Lines.GetLineFromPosition(token.SpanStart); |
| 62 | + return ContainsOnlyWhitespace(sourceText, TextSpan.FromBounds(line.Start, token.SpanStart)); |
| 63 | + } |
| 64 | + |
| 65 | + private static bool ContainsOnlyWhitespace(SourceText sourceText, TextSpan span) |
| 66 | + { |
| 67 | + for (int index = span.Start; index < span.End; index++) |
| 68 | + { |
| 69 | + if (!char.IsWhiteSpace(sourceText[index])) |
| 70 | + return false; |
| 71 | + } |
| 72 | + |
| 73 | + return true; |
| 74 | + } |
| 75 | + |
| 76 | + private static readonly DiagnosticDescriptor s_rule = new( |
| 77 | + id: DiagnosticId, |
| 78 | + title: "Lambda operator should end the previous line", |
| 79 | + messageFormat: "Move => to the end of the previous line", |
| 80 | + category: "Style", |
| 81 | + defaultSeverity: DiagnosticSeverity.Warning, |
| 82 | + isEnabledByDefault: true, |
| 83 | + helpLinkUri: $"https://github.com/Faithlife/FaithlifeAnalyzers/blob/-/docs/{DiagnosticId}.md"); |
| 84 | +} |
0 commit comments