Skip to content

Commit

Permalink
Adding doc on integrating with dotnet format (#437)
Browse files Browse the repository at this point in the history
Also ran dotnet format without formatting rules
  • Loading branch information
belav authored Sep 20, 2021
1 parent ea48f3b commit 5e0e5f7
Show file tree
Hide file tree
Showing 24 changed files with 70 additions and 69 deletions.
1 change: 1 addition & 0 deletions CSharpier.sln
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ ProjectSection(SolutionItems) = preProject
README.md = README.md
CONTRIBUTING.md = CONTRIBUTING.md
CHANGELOG.md = CHANGELOG.md
Docs\IntegratingWithLinters.md = Docs\IntegratingWithLinters.md
EndProjectSection
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "CSharpier.Benchmarks", "Src\CSharpier.Benchmarks\CSharpier.Benchmarks.csproj", "{A338903F-69AD-4950-B827-8EE75F98B620}"
Expand Down
17 changes: 17 additions & 0 deletions Docs/IntegratingWithLinters.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
## Integrating with Linters/Formatters
More often than not, linters contain formatting rules that conflict with CSharpier.

### Dotnet Format
[Dotnet Format](https://github.com/dotnet/format) provides command line arguments to run different subsets of rules. Running the following will skip the whitespace formatting rules and avoid conflicts with CSharpier
```bash
# Prior to .Net6
dotnet-format --fix-style info --fix-analyzers info

# .Net6
dotnet format style
dotnet format analyzers
```


### StyleCopAnalyzers
[StyleCopAnalyzers](https://github.com/DotNetAnalyzers/StyleCopAnalyzers) is a set of analyzers that implement StyleCop rules. Some of there will conflict with CSharpier. There is an [open issue](https://github.com/belav/csharpier/issues/13) to determine which rules need to be disabled
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ Install-Package CSharpier.MSBuild
[Command Line Interface](Docs/CLI.md)
[Configuration File](Docs/Configuration.md)
[Editors and CI/CD](Docs/EditorsAndCICD.md)
[Integrating with Linters/Formatters](Docs/IntegratingWithLinters.md)
[Ignoring Files](Docs/Ignore.md)
[ChangeLog](CHANGELOG.md)
[MSBuild Package](Docs/MSBuild.md)
Expand Down
4 changes: 2 additions & 2 deletions Src/CSharpier.Benchmarks/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public void IsCodeBasicallyEqual_SyntaxNodeComparer()
DisabledTextComparer.IsCodeBasicallyEqual(code, code);
}

private string code =
private readonly string code =
@"using System;
using System.Collections.Generic;
using System.Linq;
Expand Down Expand Up @@ -468,7 +468,7 @@ class Program
{
static void Main(string[] args)
{
var summary = BenchmarkRunner.Run<Benchmarks>();
_ = BenchmarkRunner.Run<Benchmarks>();
}
}
}
6 changes: 3 additions & 3 deletions Src/CSharpier.Generators/SyntaxNodeComparerGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ private CompareResult Compare(
foreach (var syntaxNodeType in syntaxNodeTypes)
{
var lowerCaseName =
syntaxNodeType.Name[0].ToString().ToLower() + syntaxNodeType.Name.Substring(1);
syntaxNodeType.Name[0].ToString().ToLower() + syntaxNodeType.Name[1..];
sourceBuilder.AppendLine(
$@" case {syntaxNodeType.Name} {lowerCaseName}:
return this.Compare{syntaxNodeType.Name}({lowerCaseName}, formattedNode as {syntaxNodeType.Name});"
Expand Down Expand Up @@ -104,7 +104,7 @@ private CompareResult Compare(
return sourceBuilder.ToString();
}

private void GenerateMethod(StringBuilder sourceBuilder, Type type)
private static void GenerateMethod(StringBuilder sourceBuilder, Type type)
{
sourceBuilder.AppendLine(
@$" private CompareResult Compare{type.Name}({type.Name} originalNode, {type.Name} formattedNode)
Expand Down Expand Up @@ -206,7 +206,7 @@ private void GenerateMethod(StringBuilder sourceBuilder, Type type)

private static string CamelCaseName(string name)
{
return name.ToLower()[0] + name.Substring(1);
return name.ToLower()[0] + name[1..];
}
}
}
2 changes: 1 addition & 1 deletion Src/CSharpier.Generators/SyntaxNodeJsonWriterGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,7 @@ private void GenerateMethod(StringBuilder sourceBuilder, Type type)

private static string CamelCaseName(string name)
{
return name.ToLower()[0] + name.Substring(1);
return name.ToLower()[0] + name[1..];
}

private static string PadToSize(string value, int size)
Expand Down
15 changes: 0 additions & 15 deletions Src/CSharpier.Playground/Controllers/FormatController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -75,21 +75,6 @@ private FormatError ConvertError(Diagnostic diagnostic)
return new FormatError { LineSpan = lineSpan, Description = diagnostic.ToString(), };
}

private string ReadAllText(string filePath)
{
return System.IO.File.ReadAllText(filePath);
}

private void WriteAllText(string filePath, string content)
{
System.IO.File.WriteAllText(filePath, content, Encoding.UTF8);
}

private bool Exists(string filePath)
{
return System.IO.File.Exists(filePath);
}

public string ExecuteApplication(string pathToExe, string workingDirectory, string args)
{
var processStartInfo = new ProcessStartInfo(pathToExe, args)
Expand Down
9 changes: 3 additions & 6 deletions Src/CSharpier.Tests/CommandLineFormatterTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,9 @@ public void Format_Writes_Unsupported()
{
WhenAFileExists("Unsupported.js", "asdfasfasdf");

var result = this.Format(directoryOrFilePaths: "Unsupported.js");
var (exitCode, lines) = this.Format(directoryOrFilePaths: "Unsupported.js");

result.lines.First()
.Should()
.Be(@"Error /Unsupported.js - Is an unsupported file type.");
lines.First().Should().Be(@"Error /Unsupported.js - Is an unsupported file type.");
}

[Test]
Expand Down Expand Up @@ -95,8 +93,7 @@ public void Format_Checks_Formatted_File()
{
const string formattedFilePath = "Formatted.cs";
WhenAFileExists(formattedFilePath, FormattedClassContent);

var (exitCode, lines) = this.Format(check: true);
var (exitCode, _) = this.Format(check: true);

exitCode.Should().Be(0);
}
Expand Down
5 changes: 3 additions & 2 deletions Src/CSharpier.Tests/ConfigurationFileOptionsTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -140,8 +140,9 @@ public void Should_Return_PrintWidth_With_Yaml()
result.PrintWidth.Should().Be(10);
}

private void ShouldHaveDefaultOptions(ConfigurationFileOptions configurationFileOptions)
{
private static void ShouldHaveDefaultOptions(
ConfigurationFileOptions configurationFileOptions
) {
configurationFileOptions.PrintWidth.Should().Be(100);
configurationFileOptions.PreprocessorSymbolSets.Should().BeNull();
}
Expand Down
2 changes: 1 addition & 1 deletion Src/CSharpier.Tests/DeepRecursionTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public void Format_Should_Return_Error_For_Deep_Recursion()
result.FailureMessage.Should().Be("We can't handle this deep of recursion yet.");
}

private string uglyLongConcatenatedString =
private readonly string uglyLongConcatenatedString =
@"public class ClassName
{
private string field = ""1"" +
Expand Down
2 changes: 1 addition & 1 deletion Src/CSharpier.Tests/DocPrinterTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ namespace CSharpier.Tests
{
public class DocPrinterTests
{
private static string NewLine = System.Environment.NewLine;
private static readonly string NewLine = System.Environment.NewLine;

[Test]
public void Lines_Allowed()
Expand Down
2 changes: 1 addition & 1 deletion Src/CSharpier.Tests/MissingTypeChecker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ public void Ensure_There_Are_No_Missing_Types()
missingTypes.Should().BeEmpty();
}

private HashSet<string> ignored =
private readonly HashSet<string> ignored =
new()
{
"AccessorDeclarationSyntax",
Expand Down
6 changes: 3 additions & 3 deletions Src/CSharpier.Tests/Samples/Samples.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,16 @@ public class Samples
[Test]
public void Scratch()
{
this.RunTest("Scratch");
RunTest("Scratch");
}

[Test]
public void AllInOne()
{
this.RunTest("AllInOne");
RunTest("AllInOne");
}

public void RunTest(string fileName)
public static void RunTest(string fileName)
{
var directory = new DirectoryInfo(Directory.GetCurrentDirectory());
while (directory.Name != "CSharpier.Tests")
Expand Down
26 changes: 13 additions & 13 deletions Src/CSharpier.Tests/SyntaxNodeComparerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public void Class_Not_Equal_Namespace()
var left = "class ClassName { }";
var right = @"namespace Namespace { }";

var result = this.AreEqual(left, right);
var result = AreEqual(left, right);

ResultShouldBe(
result,
Expand All @@ -35,7 +35,7 @@ public void Class_Not_Equal_Class_Different_Whitespace()
@"class ClassName {
}";

var result = this.AreEqual(left, right);
var result = AreEqual(left, right);

result.Should().BeEmpty();
}
Expand All @@ -62,7 +62,7 @@ public ConstructorWithBase(string value)
}
";

var result = this.AreEqual(left, right);
var result = AreEqual(left, right);

result.Should()
.Be(
Expand Down Expand Up @@ -106,7 +106,7 @@ public Resources() { }
}
";

var result = this.AreEqual(left, right);
var result = AreEqual(left, right);

ResultShouldBe(
result,
Expand Down Expand Up @@ -169,7 +169,7 @@ public DropdownAttribute(bool ignoreIfNotPresent)
}
";

var result = this.AreEqual(left, right);
var result = AreEqual(left, right);

result.Should().BeEmpty();
}
Expand All @@ -189,7 +189,7 @@ public void MissingSemiColon()
Integer,
String,
}";
var result = this.AreEqual(left, right);
var result = AreEqual(left, right);

ResultShouldBe(
result,
Expand All @@ -213,7 +213,7 @@ public void Extra_SyntaxTrivia_Should_Work()
var left = " public class ClassName { }";
var right = "public class ClassName { }";

var result = this.AreEqual(left, right);
var result = AreEqual(left, right);
result.Should().BeEmpty();
}

Expand All @@ -225,7 +225,7 @@ public void Mismatched_Syntax_Trivia_Should_Print_Error()
public class ClassName { }";
var right = "public class ClassName { }";

var result = this.AreEqual(left, right);
var result = AreEqual(left, right);
ResultShouldBe(
result,
@"----------------------------- Original: Around Line 0 -----------------------------
Expand Down Expand Up @@ -257,7 +257,7 @@ public class ClassName { }";
// 7
public class ClassName { }";

var result = this.AreEqual(left, right);
var result = AreEqual(left, right);
ResultShouldBe(
result,
@"----------------------------- Original: Around Line 5 -----------------------------
Expand Down Expand Up @@ -288,7 +288,7 @@ public void Mismatched_Line_Endings_In_Verbatim_String_Should_Not_Print_Error(st
+ start
+ "\"EndThisLineWith\nEndThisLineWith\n\";\n}";

var result = this.AreEqual(left, right);
var result = AreEqual(left, right);
result.Should().BeEmpty();
}

Expand All @@ -311,11 +311,11 @@ public void Mismatched_Disabled_Text_Should_Not_Print_Error()
}
";

var result = this.AreEqual(left, right);
var result = AreEqual(left, right);
result.Should().BeEmpty();
}

private void ResultShouldBe(string result, string be)
private static void ResultShouldBe(string result, string be)
{
if (Environment.GetEnvironmentVariable("NormalizeLineEndings") != null)
{
Expand All @@ -325,7 +325,7 @@ private void ResultShouldBe(string result, string be)
result.Should().Be(be);
}

private string AreEqual(string left, string right)
private static string AreEqual(string left, string right)
{
var result = new SyntaxNodeComparer(
left,
Expand Down
6 changes: 3 additions & 3 deletions Src/CSharpier.Tests/SyntaxPrinter/PreprocessorSymbolsTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public void AddSymbolSet_For_Basic_Elif_Adds_Symbol()
result.Should().ContainInOrder("DEBUG");
}

private string[] AddSymbolSet(ConditionalDirectiveTriviaSyntax trivia)
private static string[] AddSymbolSet(ConditionalDirectiveTriviaSyntax trivia)
{
TestablePreprocessorSymbols.Reset();

Expand All @@ -48,7 +48,7 @@ private string[] AddSymbolSet(ConditionalDirectiveTriviaSyntax trivia)
?? Array.Empty<string>();
}

private IfDirectiveTriviaSyntax WhenIfDirectiveHasCondition(string condition)
private static IfDirectiveTriviaSyntax WhenIfDirectiveHasCondition(string condition)
{
return SyntaxFactory.IfDirectiveTrivia(
SyntaxFactory.ParseExpression(condition),
Expand All @@ -58,7 +58,7 @@ private IfDirectiveTriviaSyntax WhenIfDirectiveHasCondition(string condition)
);
}

private ElifDirectiveTriviaSyntax WhenElifDirectiveHasCondition(string condition)
private static ElifDirectiveTriviaSyntax WhenElifDirectiveHasCondition(string condition)
{
return SyntaxFactory.ElifDirectiveTrivia(
SyntaxFactory.ParseExpression(condition),
Expand Down
2 changes: 1 addition & 1 deletion Src/CSharpier.Tests/Utilities/StringDifferTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ public void PrintDifference_Should_Print_Extra_Line_Difference()
);
}

private string PrintDifference(string expected, string actual)
private static string PrintDifference(string expected, string actual)
{
return StringDiffer.PrintFirstDifference(expected, actual);
}
Expand Down
6 changes: 3 additions & 3 deletions Src/CSharpier/CodeFormatter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ bool TryGetCompilationFailure(out CSharpierResult compilationResult)
{
Code = code,
Errors = diagnostics,
AST = printerOptions.IncludeAST ? this.PrintAST(rootNode) : string.Empty
AST = printerOptions.IncludeAST ? PrintAST(rootNode) : string.Empty
};

return true;
Expand Down Expand Up @@ -129,7 +129,7 @@ bool TryGetCompilationFailure(out CSharpierResult compilationResult)
DocTree = printerOptions.IncludeDocTree
? DocSerializer.Serialize(document)
: string.Empty,
AST = printerOptions.IncludeAST ? this.PrintAST(rootNode) : string.Empty
AST = printerOptions.IncludeAST ? PrintAST(rootNode) : string.Empty
};
}
catch (InTooDeepException)
Expand Down Expand Up @@ -161,7 +161,7 @@ public static string GetLineEnding(string code, PrinterOptions printerOptions)
return printerOptions.EndOfLine == EndOfLine.CRLF ? "\r\n" : "\n";
}

private string PrintAST(CompilationUnitSyntax rootNode)
private static string PrintAST(CompilationUnitSyntax rootNode)
{
var stringBuilder = new StringBuilder();
SyntaxNodeJsonWriter.WriteCompilationUnitSyntax(stringBuilder, rootNode);
Expand Down
2 changes: 1 addition & 1 deletion Src/CSharpier/CommandLineFormatter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -328,7 +328,7 @@ private void WriteFormattedResult(

private string GetPath(string file)
{
return file.Substring(this.BaseDirectoryPath.Length);
return file[this.BaseDirectoryPath.Length..];
}

private static int ReturnExitCode(
Expand Down
2 changes: 1 addition & 1 deletion Src/CSharpier/ConsoleLogger.cs
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ Func<TState, Exception, string> formatter
}
}

private ConsoleColor GetColorLevel(LogLevel logLevel) =>
private static ConsoleColor GetColorLevel(LogLevel logLevel) =>
logLevel switch
{
LogLevel.Critical => ConsoleColor.DarkRed,
Expand Down
2 changes: 1 addition & 1 deletion Src/CSharpier/DocPrinter/Indent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public Indenter(PrinterOptions printerOptions)
PrinterOptions = printerOptions;
}

public Indent GenerateRoot()
public static Indent GenerateRoot()
{
return new();
}
Expand Down
Loading

0 comments on commit 5e0e5f7

Please sign in to comment.