Skip to content
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 NoSpaceAfterBrace and NoSpaceBeforeBrace analyzers #175

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion StyleChecker/StyleChecker.Test/Framework/BeliefsTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -382,7 +382,7 @@ static Result ToResult(Belief b)
Assert.AreEqual(3, firstResult.Locations[0].Line);
}

private void ExpectCompilationException(string encodedSource)
private static void ExpectCompilationException(string encodedSource)
{
static Result ToResult(Belief b)
=> b.ToResult("TestId", b.Message);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
namespace StyleChecker.Test.Spacing.NoSpaceAfterBrace
{
using Microsoft.VisualStudio.TestTools.UnitTesting;
using StyleChecker.Spacing.NoSpaceAfterBrace;
using StyleChecker.Test.Framework;

[TestClass]
public sealed class AnalyzerTest : CodeFixVerifier
{
public AnalyzerTest()
: base(new Analyzer(), new CodeFixer())
{
}

[TestMethod]
public void Empty()
=> VerifyDiagnostic("", Atmosphere.Default);

[TestMethod]
public void Okay()
{
var code = ReadText("Okay");
VerifyDiagnostic(code, Atmosphere.Default);
}

[TestMethod]
public void Code()
{
var code = ReadText("Code");
var codeFix = ReadText("CodeFix");
static Result Expected(Belief b) => b.ToResult(
Analyzer.DiagnosticId,
m => $"A white space is needed after '{m}'");

VerifyDiagnosticAndFix(
code, Atmosphere.Default, Expected, codeFix);
}
}
}
34 changes: 34 additions & 0 deletions StyleChecker/StyleChecker.Test/Spacing/NoSpaceAfterBrace/Code.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
#pragma warning disable CS0162

namespace Application
{
using System;

public sealed class Code
{
public void NG()
{
string[] array = {"" };
//@ ^{

Action doNothing = () => {return; };
//@ ^{

if (array is {Length: 0 }z)
//@ ^{
//@ ^}
{
}

if (true) {return; }else{}
//@ ^{
//@ ^}

do {}while (false);
//@ ^}

{};
//@ ^}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#pragma warning disable CS0162

namespace Application
{
using System;

public sealed class Code
{
public void NG()
{
string[] array = { "" };

Action doNothing = () => { return; };

if (array is { Length: 0 } z)
{
}

if (true) { return; } else{}

do {} while (false);

{} ;
}
}
}
45 changes: 45 additions & 0 deletions StyleChecker/StyleChecker.Test/Spacing/NoSpaceAfterBrace/Okay.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
namespace Application
{
using System;

public sealed class Okay
{
public void Basic()
{
if (true)
{
}
var array = new[] { "" };
var array2 = new[] {
"" };
var array3 = new[]
{
"",
};
var array4 = new[] { new[] { "" }, };
Action doNothing = () => {};
if (array is { Length: 0 } z)
{
}
if (array is {} e)
{
}

static void M(string[] a)
{
}

M(new[] { "" });

_ = new[] { "" }.ToString();
_ = new[] { "" }?.ToString();
_ = new[] { "" }!;
_ = new[] { "" }[0];
}

public void Interpolation(string m)
{
_ = $"{m}";
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
namespace StyleChecker.Test.Spacing.NoSpaceBeforeBrace
{
using Microsoft.VisualStudio.TestTools.UnitTesting;
using StyleChecker.Spacing.NoSpaceBeforeBrace;
using StyleChecker.Test.Framework;

[TestClass]
public sealed class AnalyzerTest : CodeFixVerifier
{
public AnalyzerTest()
: base(new Analyzer(), new CodeFixer())
{
}

[TestMethod]
public void Empty()
=> VerifyDiagnostic("", Atmosphere.Default);

[TestMethod]
public void Okay()
{
var code = ReadText("Okay");
VerifyDiagnostic(code, Atmosphere.Default);
}

[TestMethod]
public void Code()
{
var code = ReadText("Code");
var codeFix = ReadText("CodeFix");
static Result Expected(Belief b) => b.ToResult(
Analyzer.DiagnosticId,
m => $"A white space is needed before '{m}'");

VerifyDiagnosticAndFix(
code, Atmosphere.Default, Expected, codeFix);
}
}
}
37 changes: 37 additions & 0 deletions StyleChecker/StyleChecker.Test/Spacing/NoSpaceBeforeBrace/Code.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
#pragma warning disable CS0162

namespace Application
{
using System;

public sealed class Code
{
public void NG()
{
string[] array ={ ""};
//@ ^{
//@ ^}

Action doNothing = () =>{ return;};
//@ ^{
//@ ^}

if (array is{ Length: 0} z)
//@ ^{
//@ ^}
{
}

if (true){ return;} else{}
//@ ^{
//@ ^}
//@ ^{

do{} while (false);
//@ ^{

;{}
//@ ^{
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#pragma warning disable CS0162

namespace Application
{
using System;

public sealed class Code
{
public void NG()
{
string[] array = { "" };

Action doNothing = () => { return; };

if (array is { Length: 0 } z)
{
}

if (true) { return; } else {}

do {} while (false);

; {}
}
}
}
40 changes: 40 additions & 0 deletions StyleChecker/StyleChecker.Test/Spacing/NoSpaceBeforeBrace/Okay.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
namespace Application
{
using System;

public sealed class Okay
{
public void Basic()
{
if (true)
{
}
var array = new[] { "" };
var array2 = new[] {
"" };
var array3 = new[]
{
"",
};
var array4 = new[] { new[] { "" }, };
Action doNothing = () => {};
if (array is { Length: 0 } z)
{
}
if (array is {} e)
{
}

static void M(string[] a)
{
}

M(new[] { "" });
}

public void Interpolation(string m)
{
_ = $"{m}";
}
}
}
24 changes: 24 additions & 0 deletions StyleChecker/StyleChecker.Test/StyleChecker.Test.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,12 @@
<Compile Remove="Spacing\NoSingleSpaceAfterTripleSlash\Code.cs" />
<Compile Remove="Spacing\NoSingleSpaceAfterTripleSlash\CodeFix.cs" />
<Compile Remove="Spacing\NoSingleSpaceAfterTripleSlash\Okay.cs" />
<Compile Remove="Spacing\NoSpaceAfterBrace\CodeFix.cs" />
<Compile Remove="Spacing\NoSpaceAfterBrace\Okay.cs" />
<Compile Remove="Spacing\NoSpaceBeforeBrace\Code.cs" />
<Compile Remove="Spacing\NoSpaceAfterBrace\Code.cs" />
<Compile Remove="Spacing\NoSpaceBeforeBrace\CodeFix.cs" />
<Compile Remove="Spacing\NoSpaceBeforeBrace\Okay.cs" />
<Compile Remove="Spacing\SpaceBeforeSemicolon\Code.cs" />
<Compile Remove="Spacing\SpaceBeforeSemicolon\CodeFix.cs" />
<Compile Remove="Spacing\NoSpaceAfterSemicolon\Code.cs" />
Expand Down Expand Up @@ -420,6 +426,24 @@
<Content Include="Spacing\NoSingleSpaceAfterTripleSlash\CodeFix.cs">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
<Content Include="Spacing\NoSpaceAfterBrace\CodeFix.cs">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
<Content Include="Spacing\NoSpaceAfterBrace\Okay.cs">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
<Content Include="Spacing\NoSpaceBeforeBrace\CodeFix.cs">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
<Content Include="Spacing\NoSpaceBeforeBrace\Code.cs">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
<Content Include="Spacing\NoSpaceAfterBrace\Code.cs">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
<Content Include="Spacing\NoSpaceBeforeBrace\Okay.cs">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
<Content Include="Spacing\SpaceBeforeSemicolon\Code.cs">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public sealed class Analyzer : AbstractAnalyzer

/// <inheritdoc/>
public override ImmutableArray<DiagnosticDescriptor>
SupportedDiagnostics => ImmutableArray.Create(Rule);
SupportedDiagnostics => [Rule];

/// <inheritdoc/>
private protected override void Register(AnalysisContext context)
Expand Down
2 changes: 1 addition & 1 deletion StyleChecker/StyleChecker/Cleaning/ByteOrderMark/BomKit.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ namespace StyleChecker.Cleaning.ByteOrderMark;
public static class BomKit
{
private static readonly ImmutableArray<byte> Utf8ByteOrderMark
= ImmutableArray.Create<byte>(0xef, 0xbb, 0xbf);
= [0xef, 0xbb, 0xbf];

/// <summary>
/// Gets whether the file of the specified path starts with UTF-8 BOM.
Expand Down
20 changes: 4 additions & 16 deletions StyleChecker/StyleChecker/Cleaning/ByteOrderMark/Globs.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,22 +17,10 @@ public static class Globs

private static readonly Regex SlashSequencePattern = new("//+");

private static readonly ImmutableHashSet<char> NeedsEscapeCharSet
= ImmutableHashSet.Create(
'\\',
'[',
']',
'.',
'*',
'+',
'?',
'^',
'$',
'(',
')',
'{',
'}',
'|');
private static readonly ImmutableHashSet<char> NeedsEscapeCharSet = [
'\\', '[', ']', '.', '*', '+', '?', '^', '$', '(', ')', '{', '}',
'|',
];

private static readonly Action DoNothing = () => {};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public sealed class Analyzer : AbstractAnalyzer

/// <inheritdoc/>
public override ImmutableArray<DiagnosticDescriptor>
SupportedDiagnostics => ImmutableArray.Create(Rule);
SupportedDiagnostics => [Rule];

/// <inheritdoc/>
private protected override void Register(AnalysisContext context)
Expand Down
Loading