Closed wondering639 closed 3 years ago
@sharwell is there a fixall in Stylecop analyzers for SA1507?
I just read SA15007 and it's about multiple empty lines following each other directly. In my case it's only one blank line followed by code (well, there's both in my code; expectation would be too remove most of them like e.g. Prettifier in the JavaScript world does it).
@jmarolf SA1507 does have a code fix (as listed in the status table), but it's not the only rule that applies here. The blank lines before and after most {
and }
chararacters would be removed by SA1012 and SA1013, respectively.
With that said, none of these rules would apply unless StyleCop Analyzers is installed. 16.10 is the first release where Roslyn's built-in analyzers consider blank line removal, and only in a small set of cases.
@sharwell Being new to these things, do I understand correctly that I could add https://github.com/DotNetAnalyzers/StyleCopAnalyzers to my project and dotnet-format would than have more rules/functionality?
do I understand correctly that I could add https://github.com/DotNetAnalyzers/StyleCopAnalyzers to my project and dotnet-format would than have more rules/functionality?
@wondering639 That is right. You can add a package reference to the StyleCopAnalyzers in your projects and fix issues by using the --fix-analyzers
option. In fact, we test the StyleCop empty line fixers in our unit tests.
similar request https://github.com/dotnet/format/issues/67
Closing this issue as StyleCopAnalyzers can be used to remove blank lines.
Not working for me. What I did was
SomeClass.cs
<PackageReference Include="StyleCop.Analyzers" Version="1.1.118">
dotnet format --fix-analyzers --fix-style
and variations on that.Result: SomeClass.cs
still has the blank lines afterwards.
All I get is Warnings were encountered while loading the workspace.
@anthony-steele-cko You will need to configure the analyzers to report a sufficient severity. You will need to either add or update your editorconfig to include the following configuration. If you do not want them to be error
s in your IDE, you can configure them as warning
s and invoke dotnet-format with --fix-analyzers warn
instead.
.editorconfig
root = true
[*.cs]
# Two or more consecutive blank lines: Remove down to one blank line. SA1507
dotnet_diagnostic.SA1507.severity = error
# Blank line immediately before or after a { line: remove it. SA1505, SA1509
dotnet_diagnostic.SA1505.severity = error
dotnet_diagnostic.SA1509.severity = error
# Blank line immediately before a } line: remove it. SA1508
dotnet_diagnostic.SA1508.severity = error
I confirm that this works for me. This time I have a Directory.Build.Props
file containing:
<Project>
<ItemGroup>
<PackageReference Include="StyleCop.Analyzers" Version="1.1.118" />
</ItemGroup>
</Project>
In the .editorconfig
I have file markup as given above.
Note that when opening the soltution in VS, I have errors for e.g.
Error SA1508 A closing brace should not be preceded by a blank line.
Error SA1505 An opening brace should not be followed by a blank line.
Error SA1507 Code should not contain multiple blank lines in a row
I run dotnet format --fix-analyzers
These are all fixed! :tada: Blank lines are gone.
More conventional spacing, issues e.g. public SomeClass ( string pk , string sk )
was not fixed by this run, so I run dotnet format
a second time with no args, and they are fixed, I get public SomeClass(string pk, string sk)
I can also confirm that without the .editorconfig
markup to make these errors not warnings, dotnet format --fix-analyzers warn
will remove the blank lines.
Thank you.
@anthony-steele-cko Note that I would not recommend ever setting analyzer diagnostics to error
. This setting will impair unit testing and debugging experiences, and make it difficult to distinguish build warnings (like these) from true errors (e.g. forgetting a ;
). You can set Treat Warnings as Errors to true specifically for CI builds to avoid letting warnings get merged to the product.
@sharwell I agree: as proof of concept, it works; as a way to remove blank lines using dotnet format
, it comes with a huge overhead.
@anthony-steele-cko Glad things are working for you now. To fix both whitespace formatting and analyzer reported issues in the same run, you can use the command dotnet format --fix-whitespace --fix-analyzers warn
.
This seems like a good thing to have without having to bring in style cop.
Agree with @niemyjski. This seems like such a basic feature, why rely on StyleCop?
@JoeRobich can you please reopen
@niemyjski If the request is to have these empty line formatting options in the box, then it would be best to open a feature request on Roslyn (https://github.com/dotnet/roslyn/issues). I know Roslyn has experimental analyzers for consecutive empty line and some other empty line scenarios (see https://github.com/dotnet/roslyn/pull/50358).
Shouldn't this be part of the dotnet format whitespace
option?
Especially considering that dotnet format is quite slow on large solutions. The only way for it to be acceptable as a pre-commit hook is to use the whitespace
mode.
@norberthuethmayr whitespace options for dotnet format
are handled by https://github.com/dotnet/roslyn, so this would still be considered external.
Just installed dotnet-format and tested with the following code (empty lines intended). Used command was
dotnet format Test.sln --fix-whitespace --fix-style
. I expected the empty lines to be removed, but they remained. How to apply a more strict formatting that removes unnecessary empty lines?Tested on this code, which is input and output at the sae time (meaning output remains unchanged)