-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathStyleOfKeyword.psm1
More file actions
70 lines (62 loc) · 1.86 KB
/
Copy pathStyleOfKeyword.psm1
File metadata and controls
70 lines (62 loc) · 1.86 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
using namespace Microsoft.Windows.PowerShell.ScriptAnalyzer.Generic
using namespace System.Collections.ObjectModel
using namespace System.Management.Automation.Language
<#
.SYNOPSIS
StyleOfKeyword
.DESCRIPTION
All keywords should be:
* lowercase
`param` should be:
* followed by one space
#>
function Measure-StyleOfKeyword {
[CmdletBinding()]
[OutputType([DiagnosticRecord[]])]
param (
[Parameter(Mandatory)]
[ValidateNotNullOrEmpty()]
[Token[]]
$InputToken
)
begin {
$results = @()
}
process {
for ($i = 0; $i -lt $InputToken.Count; $i++) {
$token = $InputToken[$i]
$nextToken = $InputToken[$i + 1]
# Rules for all keywords
if ($token.TokenFlags -band [TokenFlags]::Keyword) {
if ($token.Text -cmatch '[A-Z]') {
$suggestedCorrections = [Collection[CorrectionExtent]]::new()
$suggestedCorrections.Add([CorrectionExtent]::new($token.Extent, $token.Text.ToLower(), $MyInvocation.MyCommand.Definition)) | Out-Null
$results += [DiagnosticRecord]@{
Message = "Keyword ""$($token.Text)"" should be lowercase"
Extent = $token.Extent
RuleName = $PSCmdlet.MyInvocation.InvocationName -replace 'Measure-'
Severity = [DiagnosticSeverity]::Warning
SuggestedCorrections = $suggestedCorrections
}
}
}
# Rules for `param` keyword
switch ($token.Kind) {
Param {
if (($null -ne $nextToken) -and ($nextToken.Extent.StartOffset - $token.Extent.EndOffset) -ne 1) {
$results += [DiagnosticRecord]@{
Message = "Keyword ""$($token.Text)"" should be followed by one space"
Extent = $token.Extent
RuleName = $PSCmdlet.MyInvocation.InvocationName -replace 'Measure-'
Severity = [DiagnosticSeverity]::Warning
}
}
}
}
}
}
end {
$results
}
}
Export-ModuleMember -Function 'Measure-StyleOfKeyword'