-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Measure-ParamBlock
- New Check (#20)
- Loading branch information
1 parent
73bb393
commit 1000b22
Showing
13 changed files
with
490 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
39 changes: 39 additions & 0 deletions
39
Source/Private/Test-StatementEmptyParenthsesHasWhitespace.ps1
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
|
||
<# | ||
.SYNOPSIS | ||
Helper function for the Measure-*Block PSScriptAnalyzer rules. | ||
Test a single statement block for whitespace inside the parentheses. | ||
.EXAMPLE | ||
Test-StatementEmptyParenthsesHasWhitespace -StatementBlock $ScriptBlockAst.Extent | ||
.INPUTS | ||
[System.String] | ||
.OUTPUTS | ||
[System.Boolean] | ||
.NOTES | ||
None | ||
#> | ||
|
||
function Test-StatementEmptyParenthsesHasWhitespace | ||
{ | ||
[CmdletBinding()] | ||
[OutputType([System.Boolean])] | ||
param | ||
( | ||
[Parameter(Mandatory = $true)] | ||
[ValidateNotNullOrEmpty()] | ||
[System.String] | ||
$StatementBlock | ||
) | ||
|
||
# Check that the parentheses does not contain whitespace. | ||
if ($statementBlock -match '\(\s+\)') | ||
{ | ||
return $true | ||
} # if | ||
|
||
return $false | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
44 changes: 44 additions & 0 deletions
44
Source/Private/Test-StatementOpeningParenthsesOnSameLine.ps1
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
|
||
<# | ||
.SYNOPSIS | ||
Helper function for the Measure-*Block PSScriptAnalyzer rules. | ||
Test a single statement block for opening parentheses on the same line. | ||
.EXAMPLE | ||
Test-StatementOpeningParenthsesOnSameLine -StatementBlock $ScriptBlockAst.Extent | ||
.INPUTS | ||
[System.String] | ||
.OUTPUTS | ||
[System.Boolean] | ||
.NOTES | ||
None | ||
#> | ||
|
||
function Test-StatementOpeningParenthsesOnSameLine | ||
{ | ||
[CmdletBinding()] | ||
[OutputType([System.Boolean])] | ||
param | ||
( | ||
[Parameter(Mandatory = $true)] | ||
[ValidateNotNullOrEmpty()] | ||
[System.String] | ||
$StatementBlock | ||
) | ||
|
||
[System.String[]] $statementBlockRows = Get-StatementBlockAsRow -StatementBlock $StatementBlock | ||
if ($statementBlockRows.Count) | ||
{ | ||
|
||
# Check so that an opening brace does not exist on the same line as the statement. | ||
if ($statementBlockRows[0] -match '\(') | ||
{ | ||
return $true | ||
} # if | ||
} # if | ||
|
||
return $false | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
<# | ||
.SYNOPSIS | ||
Validates the param-block parentheses and new lines around parentheses. | ||
.DESCRIPTION | ||
Each param-block should have the opening parentheses on the same line if empty. | ||
If the param-block has values then the parentheses should be on a new line. | ||
.EXAMPLE | ||
Measure-ParamBlock -ParamBlockAst $ScriptBlockAst | ||
.INPUTS | ||
[System.Management.Automation.Language.ParamBlockAst] | ||
.OUTPUTS | ||
[Microsoft.Windows.Powershell.ScriptAnalyzer.Generic.DiagnosticRecord[]] | ||
.NOTES | ||
None | ||
#> | ||
function Measure-ParamBlock | ||
{ | ||
[CmdletBinding()] | ||
[OutputType([Microsoft.Windows.Powershell.ScriptAnalyzer.Generic.DiagnosticRecord[]])] | ||
param | ||
( | ||
[Parameter(Mandatory = $true)] | ||
[ValidateNotNullOrEmpty()] | ||
[System.Management.Automation.Language.ParamBlockAst] | ||
$ParamBlockAst | ||
) | ||
|
||
try | ||
{ | ||
$script:diagnosticRecord['Extent'] = $ParamBlockAst.Extent | ||
$script:diagnosticRecord['RuleName'] = $PSCmdlet.MyInvocation.InvocationName | ||
|
||
$testParameters = @{ | ||
StatementBlock = $ParamBlockAst.Extent | ||
} | ||
|
||
if ($ParamBlockAst.Parameters) | ||
{ | ||
if (Test-StatementOpeningParenthsesOnSameLine @testParameters) | ||
{ | ||
$script:diagnosticRecord['Message'] = $localizedData.ParamBlockNotEmptyParenthesesShouldBeOnNewLine | ||
$script:diagnosticRecord -as $diagnosticRecordType | ||
} # if | ||
} | ||
else | ||
{ | ||
if (-not (Test-StatementOpeningParenthsesOnSameLine @testParameters)) | ||
{ | ||
$script:diagnosticRecord['Message'] = $localizedData.ParamBlockEmptyParenthesesShouldBeOnSameLine | ||
$script:diagnosticRecord -as $diagnosticRecordType | ||
} # if | ||
|
||
if (Test-StatementEmptyParenthsesHasWhitespace @testParameters) | ||
{ | ||
$script:diagnosticRecord['Message'] = $localizedData.ParamBlockEmptyParenthesesShouldNotHaveWhitespace | ||
$script:diagnosticRecord -as $diagnosticRecordType | ||
} # if | ||
} | ||
} | ||
catch | ||
{ | ||
$PSCmdlet.ThrowTerminatingError($PSItem) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
62 changes: 62 additions & 0 deletions
62
tests/Unit/Private/Test-StatementEmptyParenthsesHasWhitespace.Tests.ps1
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
$ProjectPath = "$PSScriptRoot\..\..\.." | Convert-Path | ||
$ProjectName = ((Get-ChildItem -Path $ProjectPath\*\*.psd1).Where{ | ||
($_.Directory.Name -match 'source|src' -or $_.Directory.Name -eq $_.BaseName) -and | ||
$(try | ||
{ | ||
Test-ModuleManifest -Path $_.FullName -ErrorAction Stop | ||
} | ||
catch | ||
{ | ||
$false | ||
} ) | ||
}).BaseName | ||
|
||
|
||
Import-Module $ProjectName | ||
|
||
InModuleScope $ProjectName { | ||
Describe 'Test-StatementEmptyParenthsesHasWhitespace' { | ||
Context 'When statement has just whitespace in parentheses' { | ||
It 'Should return $true' { | ||
$testStatementEmptyParenthsesHasWhitespaceParameters = @{ | ||
StatementBlock = ` | ||
'param ( )' | ||
} | ||
|
||
$testStatementEmptyParenthsesHasWhitespaceResult = ` | ||
Test-StatementEmptyParenthsesHasWhitespace @testStatementEmptyParenthsesHasWhitespaceParameters | ||
|
||
$testStatementEmptyParenthsesHasWhitespaceResult | Should -Be $true | ||
} | ||
} | ||
|
||
Context 'When statement has a newline in parentheses' { | ||
It 'Should return $true' { | ||
$testStatementEmptyParenthsesHasWhitespaceParameters = @{ | ||
StatementBlock = ` | ||
'param ( | ||
)' | ||
} | ||
|
||
$testStatementEmptyParenthsesHasWhitespaceResult = ` | ||
Test-StatementEmptyParenthsesHasWhitespace @testStatementEmptyParenthsesHasWhitespaceParameters | ||
|
||
$testStatementEmptyParenthsesHasWhitespaceResult | Should -Be $true | ||
} | ||
} | ||
|
||
Context 'When statement follows style guideline' { | ||
It 'Should return $false' { | ||
$testStatementEmptyParenthsesHasWhitespaceParameters = @{ | ||
StatementBlock = ` | ||
'param ()' | ||
} | ||
|
||
$testStatementEmptyParenthsesHasWhitespaceResult = ` | ||
Test-StatementEmptyParenthsesHasWhitespace @testStatementEmptyParenthsesHasWhitespaceParameters | ||
|
||
$testStatementEmptyParenthsesHasWhitespaceResult | Should -Be $false | ||
} | ||
} | ||
} | ||
} |
48 changes: 48 additions & 0 deletions
48
tests/Unit/Private/Test-StatementOpeningParenthsesOnSameLine.Tests.ps1
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
$ProjectPath = "$PSScriptRoot\..\..\.." | Convert-Path | ||
$ProjectName = ((Get-ChildItem -Path $ProjectPath\*\*.psd1).Where{ | ||
($_.Directory.Name -match 'source|src' -or $_.Directory.Name -eq $_.BaseName) -and | ||
$(try | ||
{ | ||
Test-ModuleManifest -Path $_.FullName -ErrorAction Stop | ||
} | ||
catch | ||
{ | ||
$false | ||
} ) | ||
}).BaseName | ||
|
||
|
||
Import-Module $ProjectName | ||
|
||
InModuleScope $ProjectName { | ||
Describe 'Test-StatementOpeningParenthsesOnSameLine' { | ||
Context 'When statement has an opening parentheses on the same line' { | ||
It 'Should return $true' { | ||
$testStatementOpeningParenthsesOnSameLineParameters = @{ | ||
StatementBlock = ` | ||
'param ()' | ||
} | ||
|
||
$testStatementOpeningParenthsesOnSameLineResult = ` | ||
Test-StatementOpeningParenthsesOnSameLine @testStatementOpeningParenthsesOnSameLineParameters | ||
|
||
$testStatementOpeningParenthsesOnSameLineResult | Should -Be $true | ||
} | ||
} | ||
|
||
Context 'When statement does not have an opening parentheses on the same line' { | ||
It 'Should return $false' { | ||
$testStatementOpeningParenthsesOnSameLineParameters = @{ | ||
StatementBlock = ` | ||
'param | ||
()' | ||
} | ||
|
||
$testStatementOpeningParenthsesOnSameLineResult = ` | ||
Test-StatementOpeningParenthsesOnSameLine @testStatementOpeningParenthsesOnSameLineParameters | ||
|
||
$testStatementOpeningParenthsesOnSameLineResult | Should -Be $false | ||
} | ||
} | ||
} | ||
} |
Oops, something went wrong.