Skip to content

Fix PlaceOpenBrace rule correction to take comment at the end of line into account #929

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

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
17 changes: 14 additions & 3 deletions Rules/PlaceOpenBrace.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
namespace Microsoft.Windows.PowerShell.ScriptAnalyzer.BuiltinRules
{
/// <summary>
/// A class to walk an AST to check for violation.
/// A formatting rule about whether braces should start on the same line or not.
/// </summary>
#if !CORECLR
[Export(typeof(IScriptRule))]
Expand Down Expand Up @@ -201,14 +201,23 @@ private IEnumerable<DiagnosticRecord> FindViolationsForBraceShouldBeOnSameLine(
&& tokens[k - 1].Kind == TokenKind.NewLine
&& !tokensToIgnore.Contains(tokens[k]))
{
var precedingExpression = tokens[k - 2];
Token optionalComment = null;
// If a comment is before the open brace, then take the token before the comment
if (precedingExpression.Kind == TokenKind.Comment && k > 2)
{
precedingExpression = tokens[k - 3];
optionalComment = tokens[k - 2];
}

yield return new DiagnosticRecord(
GetError(Strings.PlaceOpenBraceErrorShouldBeOnSameLine),
tokens[k].Extent,
GetName(),
GetDiagnosticSeverity(),
fileName,
null,
GetCorrectionsForBraceShouldBeOnSameLine(tokens[k - 2], tokens[k], fileName));
GetCorrectionsForBraceShouldBeOnSameLine(precedingExpression, optionalComment, tokens[k], fileName));
}
}
}
Expand Down Expand Up @@ -336,17 +345,19 @@ private int GetStartColumnNumberOfTokenLine(Token[] tokens, int refTokenPos)

private List<CorrectionExtent> GetCorrectionsForBraceShouldBeOnSameLine(
Token precedingExpression,
Token optionalCommentOfPrecedingExpression,
Token lCurly,
string fileName)
{
var corrections = new List<CorrectionExtent>();
var optionalComment = optionalCommentOfPrecedingExpression != null ? $" {optionalCommentOfPrecedingExpression}" : string.Empty;
corrections.Add(
new CorrectionExtent(
precedingExpression.Extent.StartLineNumber,
lCurly.Extent.EndLineNumber,
precedingExpression.Extent.StartColumnNumber,
lCurly.Extent.EndColumnNumber,
precedingExpression.Text + " " + lCurly.Text,
$"{precedingExpression.Text} {lCurly.Text}{optionalComment}",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

without knowing too much about this project... should there be a space here between the }{:
{lCurly.Text}{optionalComment} for when optionalComment isn't empty string and actually is a comment?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That is the point of having the separate optionalComment variable, if it is not an empty string, then the variable itself contains the preceding space character. This way I avoid appending a space character when there is no comment.

fileName));
return corrections;
}
Expand Down
53 changes: 53 additions & 0 deletions Tests/Rules/PlaceOpenBrace.tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,59 @@ function foo ($param1)
}
}

Context "Handling of comments when using Invoke-Formatter" {
It "Should correct violation when brace should be on the same line" {
$scriptDefinition = @'
foreach ($x in $y)
{
Get-Something
}
'@
$expected = @'
foreach ($x in $y) {
Get-Something
}
'@
Invoke-Formatter -ScriptDefinition $scriptDefinition -Settings $settings | Should -Be $expected
Invoke-Formatter -ScriptDefinition $scriptDefinition -Settings 'CodeFormattingStroustrup' | Should -Be $expected
Invoke-Formatter -ScriptDefinition $scriptDefinition -Settings 'CodeFormattingOTBS' | Should -Be $expected
}

It "Should correct violation when brace should be on the same line and take comment into account" {
$scriptDefinition = @'
foreach ($x in $y) # useful comment
{
Get-Something
}
'@
$expected = @'
foreach ($x in $y) { # useful comment
Get-Something
}
'@
Invoke-Formatter -ScriptDefinition $scriptDefinition -Settings $settings | Should -Be $expected
Invoke-Formatter -ScriptDefinition $scriptDefinition -Settings 'CodeFormattingStroustrup' | Should -Be $expected
Invoke-Formatter -ScriptDefinition $scriptDefinition -Settings 'CodeFormattingOTBS' | Should -Be $expected
}

It "Should correct violation when the brace should be on the next line and take comment into account" {
$scriptDefinition = @'
foreach ($x in $y) # useful comment
{
Get-Something
}
'@
$expected = @'
foreach ($x in $y) { # useful comment
Get-Something
}
'@
Invoke-Formatter -ScriptDefinition $scriptDefinition -Settings $settings | Should -Be $expected
Invoke-Formatter -ScriptDefinition $scriptDefinition -Settings 'CodeFormattingStroustrup' | Should -Be $expected
Invoke-Formatter -ScriptDefinition $scriptDefinition -Settings 'CodeFormattingOTBS' | Should -Be $expected
}
}

Context "When an open brace must be on the same line in a switch statement" {
BeforeAll {
$def = @'
Expand Down