Skip to content

Instantly share code, notes, and snippets.

@ninmonkey
Last active October 23, 2021 01:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ninmonkey/a445c49dadc0d7abcdaeb4a2867a1971 to your computer and use it in GitHub Desktop.
Save ninmonkey/a445c49dadc0d7abcdaeb4a2867a1971 to your computer and use it in GitHub Desktop.
Indented rules indented-automation/Indented.ScriptAnalyzerRules
# Style guide: https://docs.microsoft.com/en-us/powershell/scripting/developer/cmdlet/strongly-encouraged-development-guidelines?view=powershell-7.1
# used with: https://github.com/indented-automation/Indented.ScriptAnalyzerRules
# source: https://discord.com/channels/180528040881815552/447476117629304853/869991142001872946
Describe PSScriptAnalyzer {
BeforeDiscovery {
$moduleRoot = $PSScriptRoot.Substring(0, $PSScriptRoot.IndexOf('\tests'))
$projectRoot = $moduleRoot | Split-Path -Parent
$settings = Join-Path $projectRoot -ChildPath 'PSScriptAnalyzerSettings.psd1'
$rules = Get-ChildItem -Path $moduleRoot -File -Recurse -Include *.ps1 -Exclude *.tests.ps1 |
ForEach-Object {
Invoke-ScriptAnalyzer -Path $_.FullName -Settings $settings
} |
Where-Object RuleName -NE 'TypeNotFound' |
ForEach-Object {
@{
Rule = [PSCustomObject]@{
RuleName = $_.RuleName
Message = $_.Message -replace '(.{1,100})(?:\s|$)', "`n `$1" -replace '^\n '
ScriptName = $_.ScriptName
Line = $_.Line
ScriptPath = $_.ScriptPath
}
}
}
}
It (
@(
'<rule.RuleName>'
' <rule.Message>'
' in <rule.ScriptName> line <rule.Line>'
) | Out-String
) -TestCases $rules {
$rule.ScriptPath | Should -BeNullOrEmpty
}
}
@{
# Style guide: https://docs.microsoft.com/en-us/powershell/scripting/developer/cmdlet/strongly-encouraged-development-guidelines?view=powershell-7.1
# used with: https://github.com/indented-automation/Indented.ScriptAnalyzerRules
# CustomRulePath = @(
# '~\Documents\PowerShell\Modules\Indented.ScriptAnalyzerRules'
# )
Severity = @(
'Error'
'Warning'
'Information'
)
ExcludeRules = @(
'PSDSC*'
'PSShouldProcess'
'PSReviewUnusedParameter'
'PSUseDeclaredVarsMoreThanAssignments'
'PSUseShouldProcessForStateChangingFunctions'
'UseSyntacticallyCorrectExamples'
)
IncludeDefaultRules = $true
Rules = @{
PSAvoidUsingDoubleQuotesForConstantString = @{
Enable = $true
}
PSPlaceOpenBrace = @{
Enable = $true
OnSameLine = $true
NewLineAfter = $true
IgnoreOneLineBlock = $true
}
PSPlaceCloseBrace = @{
Enable = $true
NewLineAfter = $false
IgnoreOneLineBlock = $true
NoEmptyLineBefore = $false
}
PSUseConsistentIndentation = @{
Enable = $true
Kind = 'space'
PipelineIndentation = 'IncreaseIndentationForFirstPipeline'
IndentationSize = 4
}
PSUseConsistentWhitespace = @{
Enable = $true
CheckInnerBrace = $true
CheckOpenBrace = $false
CheckOpenParen = $false
CheckOperator = $false
CheckPipe = $true
CheckPipeForRedundantWhitespace = $false
CheckSeparator = $true
CheckParameter = $false
}
PSAlignAssignmentStatement = @{
Enable = $true
CheckHashtable = $true
}
}
}
{
"diffEditor.ignoreTrimWhitespace": true,
"editor.trimAutoWhitespace": true,
"editor.renderFinalNewline": false,
"files.trimTrailingWhitespace": true,
"files.insertFinalNewline": true,
"files.trimFinalNewlines": true,
"powershell.codeFormatting.addWhitespaceAroundPipe": true,
"powershell.codeFormatting.trimWhitespaceAroundPipe": true,
"powershell.codeFormatting.whitespaceAfterSeparator": true,
"powershell.codeFormatting.whitespaceAroundOperator": true,
"powershell.codeFormatting.whitespaceBeforeOpenBrace": true,
"powershell.codeFormatting.whitespaceBeforeOpenParen": true,
"powershell.codeFormatting.whitespaceInsideBrace": true,
"powershell.scriptAnalysis.settingsPath": "PSScriptAnalyzerSettings.psd1",
"vsicons.associations.folders": [
{
"extends": "tests",
"extensions": [
"tests"
],
"format": "svg",
"icon": "test"
}
],
"vsicons.associations.files": [
{
"extends": "tests",
"extensions": [
"tests.ps1"
],
"format": "svg",
"icon": "test"
}
],
"workbench.iconTheme": "vscode-icons"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment