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

Hashtables - correct formatting check #348

Merged
merged 7 commits into from
Oct 26, 2019
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
3 changes: 2 additions & 1 deletion .vscode/cSpell.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@
"subfolders",
"PSPKI",
"gitignore",
"Nuget"
"Nuget",
"Hashtable"
],
"ignoreRegExpList": [
"AppVeyor",
Expand Down
1 change: 1 addition & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
"powershell.codeFormatting.whitespaceBeforeOpenParen": true,
"powershell.codeFormatting.whitespaceAroundOperator": true,
"powershell.codeFormatting.whitespaceAfterSeparator": true,
"powershell.codeFormatting.whitespaceInsideBrace": false,
"powershell.codeFormatting.ignoreOneLineBlock": false,
"powershell.codeFormatting.preset": "Custom",
"powershell.scriptAnalysis.settingsPath": ".vscode\\analyzersettings.psd1",
Expand Down
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@
- Fixed broken links and formatting in the `README.md` file.
- Added Measure-Keyword function to check if all keywords are in lower case.
- If a keyword is followed by parentheses,there should be a single space between them.
- Added Measure-Hashtable function to check if a hashtable is correctly formatted.
- Turn on the Custom Script Analyzer Rules meta test.

## 0.3.0.0
Expand Down
76 changes: 76 additions & 0 deletions DscResource.AnalyzerRules/DscResource.AnalyzerRules.psm1
Original file line number Diff line number Diff line change
Expand Up @@ -1061,4 +1061,80 @@ function Measure-Keyword
}
}

<#
.SYNOPSIS
Validates all hashtables.

.DESCRIPTION
Hashtables should have the correct format

.EXAMPLE
PS C:\> Measure-Hashtable -HashtableAst $HashtableAst

.INPUTS
[System.Management.Automation.Language.HashtableAst]

.OUTPUTS
[Microsoft.Windows.Powershell.ScriptAnalyzer.Generic.DiagnosticRecord[]]

.NOTES
None
#>
function Measure-Hashtable
{
[CmdletBinding()]
[OutputType([Microsoft.Windows.Powershell.ScriptAnalyzer.Generic.DiagnosticRecord[]])]
param (
[Parameter(Mandatory = $true)]
[ValidateNotNullOrEmpty()]
[System.Management.Automation.Language.HashtableAst[]]
$HashtableAst
)

try
{
foreach ($hashtable in $HashtableAst)
{
# Empty hashtables should be ignored
if ($hashtable.extent.Text -eq '@{}')
{
continue
}

$script:diagnosticRecord['RuleName'] = $PSCmdlet.MyInvocation.InvocationName

$hashtableLines = $hashtable.Extent.Text -split '\n'

# Hashtable should start with '@{' and end with '}'
if ($hashtableLines[0] -notmatch '@{\r' -or $hashtableLines[-1] -notmatch '\s*}')
{
$script:diagnosticRecord['Extent'] = $hashtable.Extent
$script:diagnosticRecord['Message'] = $localizedData.HashtableShouldHaveCorrectFormat
$script:diagnosticRecord -as $diagnosticRecordType
}
else
{
# We alredy checked that the first line is correctly formatted. Getting the starting indentation here
$initialIndent = ([regex]::Match($hashtable.Extent.StartScriptPosition.Line, '(\s*)')).Length
$expectedLineIndent = $initialIndent + 5

foreach ($keyValuePair in $hashtable.KeyValuePairs)
{
if ($keyValuePair.Item1.Extent.StartColumnNumber -ne $expectedLineIndent)
{
$script:diagnosticRecord['Extent'] = $hashtable.Extent
$script:diagnosticRecord['Message'] = $localizedData.HashtableShouldHaveCorrectFormat
$script:diagnosticRecord -as $diagnosticRecordType
break
}
}
}
}
}
catch
{
$PSCmdlet.ThrowTerminatingError($PSItem)
}
}

Export-ModuleMember -Function Measure-*
Original file line number Diff line number Diff line change
Expand Up @@ -43,4 +43,5 @@ ClassOpeningBraceNotOnSameLine = Class should not have the open brace on the sam
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
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
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
HashtableShouldHaveCorrectFormat = Hashtable is not correctly formatted. See https://github.com/PowerShell/DscResources/blob/master/StyleGuidelines.md#correct-format-for-hashtables-or-objects
'@
21 changes: 13 additions & 8 deletions DscResource.CodeCoverage/CodeCovIo.psm1
Original file line number Diff line number Diff line change
Expand Up @@ -71,12 +71,17 @@ function Add-UniqueFileLineToTable

if (!$FileLine.ContainsKey($fileKey))
{
$FileLine.add($fileKey, @{ $TableName = @{ } })
$FileLine.add(
$fileKey,
@{
$TableName = @{}
}
)
}

if (!$FileLine.$fileKey.ContainsKey($TableName))
{
$FileLine.$fileKey.Add($TableName, @{ })
$FileLine.$fileKey.Add($TableName, @{})
}

$lines = $FileLine.($fileKey).$TableName
Expand Down Expand Up @@ -176,7 +181,7 @@ function Export-CodeCovIoJson
}

# A table of the file key then a sub-tables of `misses` and `hits` lines.
$FileLine = @{ }
$FileLine = @{}

# define common parameters
$addUniqueFileLineParams = @{
Expand All @@ -194,8 +199,8 @@ function Export-CodeCovIoJson
Add-UniqueFileLineToTable -Command $CodeCoverage.HitCommands -TableName 'hits' @addUniqueFileLineParams

# Create the results structure
$resultLineData = @{ }
$resultMessages = @{ }
$resultLineData = @{}
$resultMessages = @{}
$result = @{
coverage = $resultLineData
messages = $resultMessages
Expand All @@ -209,14 +214,14 @@ function Export-CodeCovIoJson
Write-Verbose -Message "summarizing for file: $file"

# Get the hits, if they exist
$hits = @{ }
$hits = @{}
if ($FileLine.$file.ContainsKey('hits'))
{
$hits = $FileLine.$file.hits
}

# Get the misses, if they exist
$misses = @{ }
$misses = @{}
if ($FileLine.$file.ContainsKey('misses'))
{
$misses = $FileLine.$file.misses
Expand All @@ -236,7 +241,7 @@ function Export-CodeCovIoJson
}

$lineData = @()
$messages = @{ }
$messages = @{}

<#
produce the results
Expand Down
3 changes: 2 additions & 1 deletion DscResource.Container/DscResource.Container.psm1
Original file line number Diff line number Diff line change
Expand Up @@ -724,7 +724,8 @@ function Out-MissedCommand

[PSCustomObject[]] $MissedCommand = $MissedCommand |
Select-Object -Property @{
Name = 'File'; Expression = {
Name = 'File'
Expression = {
$_.File -replace ("$env:APPVEYOR_BUILD_FOLDER\" -replace '\\', '\\')
}
}, Function, Line, Command
Expand Down
Loading