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

Commit f9597bc

Browse files
committed
Initial vesion
1 parent 9c3c406 commit f9597bc

File tree

3 files changed

+76
-5
lines changed

3 files changed

+76
-5
lines changed

DscResource.AnalyzerRules/DscResource.AnalyzerRules.psm1

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1036,14 +1036,23 @@ 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 '[a-z]+\(.*' }
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+
foreach ($item in $tokenWithNoSpace)
1051+
{
1052+
$script:diagnosticRecord['Extent'] = $item.Extent
1053+
$script:diagnosticRecord['Message'] = $localizedData.OneSpaceBetweenKeywordAndParenthesis
1054+
$script:diagnosticRecord -as $diagnosticRecordType
1055+
}
10471056
}
10481057
catch
10491058
{

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: 64 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,6 +3346,21 @@ 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' {
@@ -3356,19 +3388,48 @@ Describe 'Measure-Keyword' {
33563388
}
33573389
}
33583390

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

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

0 commit comments

Comments
 (0)