-
-
Notifications
You must be signed in to change notification settings - Fork 261
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add analyzer RCS0062, put expression body on its own line (#1575) #1593
Open
cbersch
wants to merge
3
commits into
dotnet:main
Choose a base branch
from
cbersch:put-expression-body-on-its-own-line
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
31 changes: 31 additions & 0 deletions
31
src/Common/CSharp/Analysis/ConvertExpressionBodyAnalysis.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,31 @@ | ||
using Microsoft.CodeAnalysis; | ||
using Microsoft.CodeAnalysis.CSharp; | ||
using Microsoft.CodeAnalysis.Diagnostics; | ||
|
||
namespace Roslynator.CSharp.Analysis; | ||
|
||
internal static class ConvertExpressionBodyAnalysis | ||
{ | ||
public static bool BreakExpressionOnNewLine(SyntaxKind syntaxKind, AnalyzerConfigOptions configOptions) | ||
{ | ||
if (!configOptions.TryGetValueAsBool(ConfigOptions.ExpressionBodyStyleOnNextLine, out bool breakOnNewLine) | ||
|| !breakOnNewLine) | ||
{ | ||
return false; | ||
} | ||
|
||
switch (syntaxKind) | ||
{ | ||
case SyntaxKind.MethodDeclaration: | ||
case SyntaxKind.ConstructorDeclaration: | ||
case SyntaxKind.DestructorDeclaration: | ||
case SyntaxKind.PropertyDeclaration: | ||
case SyntaxKind.IndexerDeclaration: | ||
case SyntaxKind.OperatorDeclaration: | ||
case SyntaxKind.ConversionOperatorDeclaration: | ||
return true; | ||
default: | ||
return false; | ||
} | ||
} | ||
} |
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
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
76 changes: 76 additions & 0 deletions
76
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,76 @@ | ||
// 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 System.Reflection.Metadata; | ||
using Microsoft.CodeAnalysis; | ||
using Microsoft.CodeAnalysis.CSharp; | ||
using Microsoft.CodeAnalysis.CSharp.Syntax; | ||
using Microsoft.CodeAnalysis.Diagnostics; | ||
using Roslynator.CSharp; | ||
using Roslynator.CSharp.Analysis; | ||
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; | ||
AnalyzerConfigOptions configOptions = context.GetConfigOptions(); | ||
|
||
if (ConvertExpressionBodyAnalysis.BreakExpressionOnNewLine(arrowExpressionClause.Parent.Kind(), configOptions)) | ||
{ | ||
AnalyzeArrowExpressionClause(arrowExpressionClause.ArrowToken, context); | ||
} | ||
} | ||
|
||
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.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could you please explain why did you add this option? I think that this option is basically the same as checking if analyzer is enabled or not. If you want to decide whether to wrap line before
=>
in RCS1016 (as an example) just check if RCS0062 is effective or not.See extensions methods
IsEffective
forDiagnosticDescriptor
.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Basically you are right, but I wouldn't know how to use that information in the
CodeFixProvider
.I found usings of
DiagnosticDescriptor.IsEffective
only in analyzers. The respective information is then unambiguous via the created diagnostic.But configuring the behavior of a single diagnostic (RCS1016) in the
CodeFixProvider
is always done viaAnalyzerConfigOptions
.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You can definitely get the information whether an analyzer is effective from
CodeFixProvider
. You need aSyntaxTree
, which is property of a node andCompilationOptions
which is a property of aProject
:There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks! I'm still making my way through to understand what to get from where.
So that would work, but now I would have dependency problems:
Currently, my attempt for the (not yet working) line breaking is in
Workspaces.Common.ConvertBlockBodyToExpressionBodyRefactoring
, but theDiagnosticRules
are defined inFormatting.Analyzers
.To know, how to refactor that, I would need to know, how the actual new line is inserted. My approach doesn't work, yet.
Probably I would need to call the
SyntaxNode ConvertBlockBodyToExpressionBodyRefactoring.Refactor(SyntaxNode, AnalyzerConfigOptions)
from somewhere else, then manipulated the new syntax node, and finally replace the node in the document.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could you call
IsEffective
from code fix provider and then pass it toConvertBlockBodyToExpressionBodyRefactoring
as a parameter? Something likebool wrapLineBeforeArrowToken
?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That also doesn't work, because the RCS0062 DiagnosticRule is defined in
Formatting.Analyzers
, but the caller ofConvertBlockBodyToExpressionBodyRefactoring.RefactorAsync
, which would then have to evaluate the bool, likeUseBlockBodyOrExpressionBodyCodeFixProvider
(RCS1016) orConstructorDeclarationRefactoring
, can't reference that project.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'll move DiagnosticRules to Common project.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@cbersch I moved
DiagnosticRules
andDiagnosticIdentifiers
toRoslynator.Common
so now it's accessible from everywhere. Please re-rungenerate_code.ps1
.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Grest, thanks. I'll rework when I'm back from vacaton