-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Expand file tree
/
Copy pathPSScriptAnalyzerCustomRules.psm1
More file actions
84 lines (75 loc) · 3.85 KB
/
PSScriptAnalyzerCustomRules.psm1
File metadata and controls
84 lines (75 loc) · 3.85 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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
<#
.SYNOPSIS
Use PS5-safe escape codes instead of `e and `u
.DESCRIPTION
`e and `u were added in PS 6, so they will not work in PS5 strings
.EXAMPLE
.INPUTS
.OUTPUTS
.NOTES
#>
function Test-StringEscapeCode {
[CmdletBinding()]
[OutputType([Microsoft.Windows.PowerShell.ScriptAnalyzer.Generic.DiagnosticRecord[]])]
Param
(
[Parameter(Mandatory = $true)]
[ValidateNotNullOrEmpty()]
[System.Management.Automation.Language.ScriptBlockAst]
$ScriptBlockAst
)
Process {
[Microsoft.Windows.PowerShell.ScriptAnalyzer.Generic.DiagnosticRecord[]]$results = @()
try {
$expressions = $ScriptBlockAst.FindAll({
param ([System.Management.Automation.Language.Ast]$Ast)
($Ast.GetType().Name -Match 'StringConstantExpression|ExpandableStringExpression') -and ($Ast.StringConstantType -eq 'DoubleQuoted')
}, $false)
foreach ($expr in $expressions) {
$extent = $expr.Extent
if ($extent.Text -Match '([^`]|^)`e' ) {
$correction = $expr.Extent.Text -Replace '(?<lead>[^`]|^)`e', '${lead}$([char]0x1b)'
$objParams = @{
TypeName = 'Microsoft.Windows.PowerShell.ScriptAnalyzer.Generic.CorrectionExtent'
ArgumentList = $extent.StartLineNumber, $extent.EndLineNumber, $extent.StartColumnNumber,
$extent.EndColumnNumber, $correction, $MyInvocation.MyCommand.Definition
}
$correctionExtent = New-Object @objParams
$suggestedCorrections = New-Object System.Collections.ObjectModel.Collection[$($objParams.TypeName)]
$suggestedCorrections.add($correctionExtent) | Out-Null
$result = [Microsoft.Windows.PowerShell.ScriptAnalyzer.Generic.DiagnosticRecord]@{
'Message' = 'The escape sequence "`e" is not supported in powershell 5'
'Extent' = $extent
'RuleName' = $PSCmdlet.MyInvocation.InvocationName
'Severity' = 'Warning'
'SuggestedCorrections' = $suggestedCorrections
}
$results += $result
} elseif ($extent.Text -Match '([^`]|^)`u{(?<sequence>[A-Za-z0-9]+)}') {
$sequence = $Matches.sequence
$correction = -Replace '(?<lead>[^`]|^)`u\{(?<code>[A-Za-z0-9]+)}', '${lead}$([char]0x${code})'
$objParams = @{
TypeName = 'Microsoft.Windows.PowerShell.ScriptAnalyzer.Generic.CorrectionExtent'
ArgumentList = $extent.StartLineNumber, $extent.EndLineNumber, $extent.StartColumnNumber,
$extent.EndColumnNumber, $correction, $MyInvocation.MyCommand.Definition
}
$correctionExtent = New-Object @objParams
$suggestedCorrections = New-Object System.Collections.ObjectModel.Collection[$($objParams.TypeName)]
$suggestedCorrections.add($correctionExtent) | Out-Null
$result = [Microsoft.Windows.PowerShell.ScriptAnalyzer.Generic.DiagnosticRecord]@{
'Message' = "The escape sequence `"``u{$sequence}`" is not supported in powershell 5"
'Extent' = $assignmentAst.Extent
'RuleName' = $PSCmdlet.MyInvocation.InvocationName
'Severity' = 'Warning'
'SuggestedCorrections' = $suggestedCorrections
}
$results += $result
}
}
return $results
} catch {
$PSCmdlet.ThrowTerminatingError($PSItem)
}
}
}
Export-ModuleMember -Function Test-StringEscapeCode