forked from dotnet/roslynator
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add analyzer RCS0062, put expression body on its own line (dotnet#1575)
- Loading branch information
Showing
7 changed files
with
400 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
81 changes: 81 additions & 0 deletions
81
src/Formatting.Analyzers/CSharp/PutExpressionBodyOnItsOwnLineAnalyzer.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
// Copyright (c) .NET Foundation and Contributors. Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. | ||
|
||
using System.Collections.Immutable; | ||
using Microsoft.CodeAnalysis; | ||
using Microsoft.CodeAnalysis.CSharp; | ||
using Microsoft.CodeAnalysis.CSharp.Syntax; | ||
using Microsoft.CodeAnalysis.Diagnostics; | ||
using Roslynator.CSharp; | ||
using Roslynator.CSharp.CodeStyle; | ||
|
||
namespace Roslynator.Formatting.CSharp; | ||
|
||
[DiagnosticAnalyzer(LanguageNames.CSharp)] | ||
public sealed class PutExpressionBodyOnItsOwnLineAnalyzer : BaseDiagnosticAnalyzer | ||
{ | ||
private static ImmutableArray<DiagnosticDescriptor> _supportedDiagnostics; | ||
|
||
public override ImmutableArray<DiagnosticDescriptor> SupportedDiagnostics | ||
{ | ||
get | ||
{ | ||
if (_supportedDiagnostics.IsDefault) | ||
Immutable.InterlockedInitialize(ref _supportedDiagnostics, DiagnosticRules.PutExpressionBodyOnItsOwnLine); | ||
|
||
return _supportedDiagnostics; | ||
} | ||
} | ||
|
||
public override void Initialize(AnalysisContext context) | ||
{ | ||
base.Initialize(context); | ||
|
||
context.RegisterSyntaxNodeAction(f => AnalyzeArrowExpressionClause(f), SyntaxKind.ArrowExpressionClause); | ||
} | ||
|
||
private static void AnalyzeArrowExpressionClause(SyntaxNodeAnalysisContext context) | ||
{ | ||
var arrowExpressionClause = (ArrowExpressionClauseSyntax)context.Node; | ||
|
||
switch (arrowExpressionClause.Parent.Kind()) | ||
{ | ||
case SyntaxKind.MethodDeclaration: | ||
case SyntaxKind.ConstructorDeclaration: | ||
case SyntaxKind.DestructorDeclaration: | ||
case SyntaxKind.PropertyDeclaration: | ||
case SyntaxKind.IndexerDeclaration: | ||
case SyntaxKind.OperatorDeclaration: | ||
case SyntaxKind.ConversionOperatorDeclaration: | ||
AnalyzeArrowExpressionClause(arrowExpressionClause.ArrowToken, context); | ||
break; | ||
} | ||
} | ||
|
||
private static void AnalyzeArrowExpressionClause(SyntaxToken arrowToken, SyntaxNodeAnalysisContext context) | ||
{ | ||
NewLinePosition newLinePosition = context.GetArrowTokenNewLinePosition(); | ||
|
||
SyntaxToken first; | ||
SyntaxToken second; | ||
if (newLinePosition == NewLinePosition.After) | ||
{ | ||
first = arrowToken; | ||
second = arrowToken.GetNextToken(); | ||
} | ||
else | ||
{ | ||
first = arrowToken.GetPreviousToken(); | ||
second = arrowToken; | ||
} | ||
|
||
TriviaBlock block = TriviaBlock.FromBetween(first, second); | ||
|
||
if (block.Kind == TriviaBlockKind.NoNewLine) | ||
{ | ||
DiagnosticHelpers.ReportDiagnostic( | ||
context, | ||
DiagnosticRules.PutExpressionBodyOnItsOwnLine, | ||
block.GetLocation()); | ||
} | ||
} | ||
} |
Oops, something went wrong.