Skip to content

Add rule to check for consistent whitespace style #702

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
merged 40 commits into from
Feb 6, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
40 commits
Select commit Hold shift + click to select a range
d9da319
Add skeleton of UseWhitespace rule
Jan 30, 2017
820538f
Add method to find tokens with preceding whitespace
Jan 30, 2017
294ab5c
Check for whitespace before open brace
Jan 30, 2017
4d97359
Add Ast property to TokenOperations
Jan 31, 2017
3e9f988
Add method to return token nodes
Jan 31, 2017
7af55b8
Add error strings for UseWhitespace rule
Jan 31, 2017
1b80017
Find open parenthesis violations
Jan 31, 2017
688a881
Add tests for UseWhitespace rule
Jan 31, 2017
3450c95
Update open brace violation finder implementation
Jan 31, 2017
a868ed5
Add configuration in each test case
Jan 31, 2017
e7a7b95
Update GetTokenNodes method in TokenOperations
Feb 1, 2017
7347add
Add error string for operator violation
Feb 1, 2017
b158d50
Add logic to detect operator violations
Feb 1, 2017
5cbe9a1
Add tests for operator violations
Feb 1, 2017
02a0b9d
Remove duplicate code from UseWhitespace rule
Feb 1, 2017
da59328
Add corrections for bracket violations
Feb 1, 2017
c06bf1f
Add a constructor to CorrectionExtent class
Feb 1, 2017
5e534b6
Add corrections for operator violations
Feb 1, 2017
00dd0c4
Check keywords for open paren violation
Feb 2, 2017
dca518b
Update UseWhitespace rule test cases
Feb 2, 2017
f886a29
Detect comma and semicolon violations
Feb 3, 2017
acd4de7
Add tests for comma and semicolon detection
Feb 3, 2017
d2581f6
Add UseWhitespace rule to codeformatting settings
Feb 3, 2017
d683d46
Update tests for usewhitespace rule
Feb 3, 2017
92f64b6
Update IsPreviousTokenOnSameLine method logic
Feb 3, 2017
7a421a8
Update separator violation logic
Feb 3, 2017
139754e
Add corrections for separator violations
Feb 3, 2017
18f9668
Fix corrections for operator violations
Feb 3, 2017
81a241f
Remove redundant code from operator violations
Feb 3, 2017
578fd98
Use same correction method for operator/separator violation
Feb 3, 2017
216395c
Use one corrections method for all violation kinds
Feb 3, 2017
de2af7a
Fix separator error strings
Feb 3, 2017
38e2c69
Replace only the required part in correction
Feb 3, 2017
42031f2
Add method to extract extent from string
Feb 3, 2017
0e5c668
Add correction tests for usewhitespace rule
Feb 3, 2017
1369cb6
Update rule count in GetScriptAnalyzerRule test case
Feb 3, 2017
9e16982
Update usewhitespace rule documentation
Feb 3, 2017
92d0167
Rename UseWhitespace rule to UseConsistentWhitespace
Feb 6, 2017
99a7eed
Group methods by their access modifiers
Feb 6, 2017
96b1a60
Change keyword lookup implementation
Feb 6, 2017
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
15 changes: 15 additions & 0 deletions Engine/Generic/CorrectionExtent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,22 @@ public CorrectionExtent(
ThrowIfInvalidArguments();
}

public CorrectionExtent(
IScriptExtent violationExtent,
string replacementText,
string filePath,
string description)
: this(
violationExtent.StartLineNumber,
violationExtent.EndLineNumber,
violationExtent.StartColumnNumber,
violationExtent.EndColumnNumber,
replacementText,
filePath,
description)
{

}

private void ThrowIfInvalidArguments()
{
Expand Down
12 changes: 11 additions & 1 deletion Engine/Settings/CodeFormatting.psd1
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
IncludeRules = @(
'PSPlaceOpenBrace',
'PSPlaceCloseBrace',
'PSUseConsistentIndentation'
'PSUseConsistentIndentation',
'PSUseWhitespace'
)

Rules = @{
Expand All @@ -20,5 +21,14 @@
Enable = $true
IndentationSize = 4
}

PSUseWhitespace = @{
Enable = $true
CheckOpenBrace = $true
CheckOpenParen = $true
CheckOperator = $true
CheckSeparator = $true
}

}
}
54 changes: 54 additions & 0 deletions Engine/TokenOperations.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,11 @@ namespace Microsoft.Windows.PowerShell.ScriptAnalyzer
public class TokenOperations
{
private readonly Token[] tokens;
private LinkedList<Token> tokensLL;
private readonly Ast ast;

public Ast Ast { get { return ast; } }

/// <summary>
/// Initializes the fields of the TokenOperations class.
/// </summary>
Expand All @@ -43,6 +46,7 @@ public TokenOperations(Token[] tokens, Ast ast)

this.tokens = tokens;
this.ast = ast;
this.tokensLL = new LinkedList<Token>(this.tokens);
}

/// <summary>
Expand Down Expand Up @@ -105,5 +109,55 @@ private IEnumerable<Token> GetBraceInCommandElement(TokenKind tokenKind)
}
}
}

public IEnumerable<LinkedListNode<Token>> GetTokenNodes(TokenKind kind)
{
return GetTokenNodes((token) => token.Kind == kind);
}

public IEnumerable<LinkedListNode<Token>> GetTokenNodes(Func<Token, bool> predicate)
{
var token = tokensLL.First;
while (token != null)
{
if (predicate(token.Value))
{
yield return token;
}
token = token.Next;
}
}

private IEnumerable<Tuple<Token, int>> GetTokenAndPrecedingWhitespace(TokenKind kind)
{
var lCurlyTokens = GetTokenNodes(TokenKind.LCurly);
foreach (var item in lCurlyTokens)
{
if (item.Previous == null
|| !OnSameLine(item.Previous.Value, item.Value))
{
continue;
}

yield return new Tuple<Token, int>(
item.Value,
item.Value.Extent.StartColumnNumber - item.Previous.Value.Extent.EndColumnNumber);
}
}

public IEnumerable<Tuple<Token, int>> GetOpenBracesWithWhiteSpacesBefore()
{
return GetTokenAndPrecedingWhitespace(TokenKind.LCurly);
}

public IEnumerable<Tuple<Token, int>> GetOpenParensWithWhiteSpacesBefore()
{
return GetTokenAndPrecedingWhitespace(TokenKind.LParen);
}

private bool OnSameLine(Token token1, Token token2)
{
return token1.Extent.StartLineNumber == token2.Extent.EndLineNumber;
}
}
}
37 changes: 37 additions & 0 deletions RuleDocumentation/UseConsistentWhitespace.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
# UseConsistentWhitespace
**Severity Level: Warning**

## Description
This rule enforces consistent brace, parenthesis, binary operator, assignment operator and separator styles. For more information please see the [Parameters](#parameters) section.

**Note**: This rule is not enabled by default. The user needs to enable it through settings.

## Configuration
```powershell
Rules = @{
PSUseConsistentWhitespace = @{
Enable = $true
CheckOpenBrace = $true
CheckOpenParen = $true
CheckOperator = $true
CheckSeparator = $true
}
}
```

### Parameters

#### Enable: bool (Default value is `$false`)
Enable or disable the rule during ScriptAnalyzer invocation.

#### CheckOpenBrace: bool (Default value is `$true`)
Checks if there is a space between a keyword and its corresponding open brace. E.g. `foo { }`.

#### CheckOpenParen: bool (Default value is `$true`)
Checks if there is space between a keyword and its corresponding open parenthesis. E.g. `if (true)`.

#### CheckOperator: bool (Default value is `$true`)
Checks if a binary operator is surrounded on both sides by a space. E.g. `$x = 1`.

#### CheckSeparator: bool (Default value is `$true`)
Checks if a comma or a semicolon is followed by a space. E.g. `@(1, 2, 3)` or `@{a = 1; b = 2}`.
1 change: 1 addition & 0 deletions Rules/ScriptAnalyzerBuiltinRules.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,7 @@
<Compile Include="PlaceOpenBrace.cs" />
<Compile Include="PlaceCloseBrace.cs" />
<Compile Include="UseConsistentIndentation.cs" />
<Compile Include="UseConsistentWhitespace.cs" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\Engine\ScriptAnalyzerEngine.csproj">
Expand Down
24 changes: 24 additions & 0 deletions Rules/Strings.resx
Original file line number Diff line number Diff line change
Expand Up @@ -915,4 +915,28 @@
<data name="UseConsistentIndentationError" xml:space="preserve">
<value>Indentation not consistent</value>
</data>
<data name="UseConsistentWhitespaceName" xml:space="preserve">
<value>UseConsistentWhitespace</value>
</data>
<data name="UseConsistentWhitespaceCommonName" xml:space="preserve">
<value>Use whitespaces</value>
</data>
<data name="UseConsistentWhitespaceDescription" xml:space="preserve">
<value>Check for whitespace between keyword and open paren/curly, around assigment operator ('='), around arithmetic operators and after separators (',' and ';')</value>
</data>
<data name="UseConsistentWhitespaceErrorBeforeBrace" xml:space="preserve">
<value>Use space before open brace.</value>
</data>
<data name="UseConsistentWhitespaceErrorBeforeParen" xml:space="preserve">
<value>Use space before open parenthesis.</value>
</data>
<data name="UseConsistentWhitespaceErrorOperator" xml:space="preserve">
<value>Use space before and after binary and assignment operators.</value>
</data>
<data name="UseConsistentWhitespaceErrorSeparatorComma" xml:space="preserve">
<value>Use space after a comma.</value>
</data>
<data name="UseConsistentWhitespaceErrorSeparatorSemi" xml:space="preserve">
<value>Use space after a semicolon.</value>
</data>
</root>
Loading