Skip to content
This repository was archived by the owner on Feb 24, 2021. It is now read-only.

Commit e3b80e1

Browse files
authored
Merge pull request #347 from SSvilen/KeywordSpaceAfterCheck
PSScriptAnalyzer: One Space Between Keyword and Parenthesis check
2 parents 9c3c406 + 011b38a commit e3b80e1

File tree

4 files changed

+79
-5
lines changed

4 files changed

+79
-5
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@
7878
- Removed the legacy `CHANGELOG` link in the `README.md` table of contents.
7979
- Fixed broken links and formatting in the `README.md` file.
8080
- Added Measure-Keyword function to check if all keywords are in lower case.
81+
- If a keyword is followed by parentheses,there should be a single space between them.
8182

8283
## 0.3.0.0
8384

DscResource.AnalyzerRules/DscResource.AnalyzerRules.psm1

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1036,14 +1036,24 @@ function Measure-Keyword
10361036
$script:diagnosticRecord['RuleName'] = $PSCmdlet.MyInvocation.InvocationName
10371037

10381038
$keywordFlag = [System.Management.Automation.Language.TokenFlags]::Keyword
1039-
$upperCaseTokens = $Token.Where( { $_.TokenFlags.HasFlag($keywordFlag) -and $_.Text -cmatch '[A-Z]+' })
1039+
$keywords = $Token.Where{ $_.TokenFlags.HasFlag($keywordFlag) }
1040+
$upperCaseTokens = $keywords.Where{ $_.Text -cmatch '[A-Z]+' }
1041+
1042+
$tokenWithNoSpace = $keywords.Where{ $_.Extent.StartScriptPosition.Line -match "$($_.Extent.Text)\(.*" }
10401043

10411044
foreach ($item in $upperCaseTokens)
10421045
{
10431046
$script:diagnosticRecord['Extent'] = $item.Extent
10441047
$script:diagnosticRecord['Message'] = $localizedData.StatementsContainsUpperCaseLetter -f $item.Text
10451048
$script:diagnosticRecord -as $diagnosticRecordType
1046-
} #if
1049+
}
1050+
1051+
foreach ($item in $tokenWithNoSpace)
1052+
{
1053+
$script:diagnosticRecord['Extent'] = $item.Extent
1054+
$script:diagnosticRecord['Message'] = $localizedData.OneSpaceBetweenKeywordAndParenthesis
1055+
$script:diagnosticRecord -as $diagnosticRecordType
1056+
}
10471057
}
10481058
catch
10491059
{

DscResource.AnalyzerRules/en-US/DscResource.AnalyzerRules.psd1

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,4 +42,5 @@ EnumOpeningBraceShouldBeFollowedByOnlyOneNewLine = Opening brace on Enum should
4242
ClassOpeningBraceNotOnSameLine = Class should not have the open brace on the same line as the declaration. See https://github.com/PowerShell/DscResources/blob/master/StyleGuidelines.md#one-newline-before-braces
4343
ClassOpeningBraceShouldBeFollowedByNewLine = Opening brace on Class should be followed by a new line. See https://github.com/PowerShell/DscResources/blob/master/StyleGuidelines.md#one-newline-after-opening-brace
4444
ClassOpeningBraceShouldBeFollowedByOnlyOneNewLine = Opening brace on Class should only be followed by one new line. See https://github.com/PowerShell/DscResources/blob/master/StyleGuidelines.md#one-newline-after-opening-brace
45+
OneSpaceBetweenKeywordAndParenthesis = If a keyword is followed by a parenthesis, there should be single space between the keyword and the parenthesis. See https://github.com/PowerShell/DscResources/blob/master/StyleGuidelines.md#one-newline-after-opening-brace
4546
'@

Tests/Unit/DscResource.AnalyzerRules.Tests.ps1

Lines changed: 65 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3315,6 +3315,23 @@ Describe 'Measure-Keyword' {
33153315
}
33163316
}
33173317

3318+
Context 'When keyword is not followed by a single space' {
3319+
It 'Should write the correct error record' {
3320+
$definition = '
3321+
if("example" -eq "example" -or "magic")
3322+
{
3323+
Write-Verbose -Message "Example found."
3324+
}
3325+
'
3326+
3327+
$token = Get-TokensFromDefinition -ScriptDefinition $definition
3328+
$record = Measure-Keyword -Token $token
3329+
($record | Measure-Object).Count | Should -Be 1
3330+
$record.Message | Should -Be $localizedData.OneSpaceBetweenKeywordAndParenthesis
3331+
$record.RuleName | Should -Be $ruleName
3332+
}
3333+
}
3334+
33183335
Context 'When keyword does not contain upper case letters' {
33193336
It 'Should not return an error record' {
33203337
$definition = '
@@ -3329,12 +3346,28 @@ Describe 'Measure-Keyword' {
33293346
($record | Measure-Object).Count | Should -Be 0
33303347
}
33313348
}
3349+
3350+
Context 'When keyword is followed by a single space' {
3351+
It 'Should not return an error record' {
3352+
$definition = '
3353+
if ("example" -eq "example" -or "magic")
3354+
{
3355+
Write-Verbose -Message "Example found."
3356+
}
3357+
'
3358+
3359+
$token = Get-TokensFromDefinition -ScriptDefinition $definition
3360+
$record = Measure-Keyword -Token $token
3361+
($record | Measure-Object).Count | Should -Be 0
3362+
}
3363+
}
33323364
}
33333365

33343366
Context 'When calling PSScriptAnalyzer' {
33353367
BeforeAll {
33363368
$invokeScriptAnalyzerParameters = @{
33373369
CustomRulePath = $modulePath
3370+
IncludeRule = 'Measure-Keyword'
33383371
}
33393372
$ruleName = "$($script:ModuleName)\Measure-Keyword"
33403373
}
@@ -3356,19 +3389,48 @@ Describe 'Measure-Keyword' {
33563389
}
33573390
}
33583391

3392+
Context 'When keyword is not followed by a single space' {
3393+
It 'Should write the correct error record' {
3394+
$invokeScriptAnalyzerParameters['ScriptDefinition'] = '
3395+
if("example" -eq "example" -or "magic")
3396+
{
3397+
Write-Verbose -Message "Example found."
3398+
}
3399+
'
3400+
3401+
$record = Invoke-ScriptAnalyzer @invokeScriptAnalyzerParameters
3402+
($record | Measure-Object).Count | Should -Be 1
3403+
$record.Message | Should -Be $localizedData.OneSpaceBetweenKeywordAndParenthesis
3404+
$record.RuleName | Should -Be $ruleName
3405+
}
3406+
}
3407+
33593408
Context 'When keyword does not contain upper case letters' {
33603409
It 'Should not return an error record' {
3361-
$definition = '
3410+
$invokeScriptAnalyzerParameters['ScriptDefinition'] = '
33623411
function Test
33633412
{
33643413
return $true
33653414
}
33663415
'
33673416

33683417
$record = Invoke-ScriptAnalyzer @invokeScriptAnalyzerParameters
3369-
($record | Measure-Object).Count | Should -BeExactly 1
3418+
($record | Measure-Object).Count | Should -BeExactly 0
33703419
}
3371-
}
3420+
}
3421+
3422+
Context 'When keyword is followed by a single space' {
3423+
It 'Should not return an error record' {
3424+
$invokeScriptAnalyzerParameters['ScriptDefinition'] = '
3425+
if ("example" -eq "example" -or "magic")
3426+
{
3427+
Write-Verbose -Message "Example found."
3428+
}
3429+
'
3430+
$record = Invoke-ScriptAnalyzer @invokeScriptAnalyzerParameters
3431+
($record | Measure-Object).Count | Should -Be 0
3432+
}
3433+
}
33723434
}
33733435
}
33743436
}

0 commit comments

Comments
 (0)