Skip to content

Commit

Permalink
Fix NSAB/NSBB analyzers to ignore braces in interpolation.
Browse files Browse the repository at this point in the history
  • Loading branch information
maroontress-tomohisa committed Sep 21, 2020
1 parent 797bcda commit fe30070
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -36,5 +36,10 @@ static void M(string[] a)
_ = new[] { "" }!;
_ = new[] { "" }[0];
}

public void Interpolation(string m)
{
_ = $"{m}";
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,5 +31,10 @@ static void M(string[] a)

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

public void Interpolation(string m)
{
_ = $"{m}";
}
}
}
10 changes: 8 additions & 2 deletions StyleChecker/StyleChecker/Spacing/NoSpaceAfterBrace/Analyzer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -96,15 +96,21 @@ static bool DoesOpenBraceNeedSpace(SyntaxToken token)
SyntaxKind.CloseBraceToken));
}

static bool IsExceptional(SyntaxToken token)
{
return token.IsMissing
|| token.Parent.IsKind(SyntaxKind.Interpolation);
}

var root = context.Tree
.GetCompilationUnitRoot(context.CancellationToken);
var leftBraces = root.DescendantTokens()
.Where(t => t.IsKind(SyntaxKind.OpenBraceToken)
&& !t.IsMissing
&& !IsExceptional(t)
&& DoesOpenBraceNeedSpace(t));
var rightBraces = root.DescendantTokens()
.Where(t => t.IsKind(SyntaxKind.CloseBraceToken)
&& !t.IsMissing
&& !IsExceptional(t)
&& DoesCloseBraceNeedSpace(t));
var all = leftBraces.Concat(rightBraces);
foreach (var t in all)
Expand Down
10 changes: 8 additions & 2 deletions StyleChecker/StyleChecker/Spacing/NoSpaceBeforeBrace/Analyzer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -96,15 +96,21 @@ static bool DoesCloseBraceNeedSpace(SyntaxToken token)
SyntaxKind.OpenBraceToken));
}

static bool IsExceptional(SyntaxToken token)
{
return token.IsMissing
|| token.Parent.IsKind(SyntaxKind.Interpolation);
}

var root = context.Tree
.GetCompilationUnitRoot(context.CancellationToken);
var leftBraces = root.DescendantTokens()
.Where(t => t.IsKind(SyntaxKind.OpenBraceToken)
&& !t.IsMissing
&& !IsExceptional(t)
&& DoesOpenBraceNeedSpace(t));
var rightBraces = root.DescendantTokens()
.Where(t => t.IsKind(SyntaxKind.CloseBraceToken)
&& !t.IsMissing
&& !IsExceptional(t)
&& DoesCloseBraceNeedSpace(t));
var all = leftBraces.Concat(rightBraces);
foreach (var t in all)
Expand Down

0 comments on commit fe30070

Please sign in to comment.