-
Notifications
You must be signed in to change notification settings - Fork 382
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix NRE when custom rules omit optional properties in diagnostics (#1715
) * Fix NRE when custom rules omit optional properties in diagnostics * Fix script extent type check to prevent throwing * Fix mishandling of null extent
- Loading branch information
Showing
5 changed files
with
136 additions
and
28 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
write-host "test" | ||
$a = "asdf" | ||
$a = $a.replace('s','ssssssss') | ||
[math]::abs(-1) | ||
Function ASDF1234{ | ||
"asdf" | ||
} |
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 @@ | ||
<# | ||
.SYNOPSIS | ||
Static methods are not allowed in constrained language mode. | ||
.DESCRIPTION | ||
Static methods are not allowed in constrained language mode. | ||
To fix a violation of this rule, use a cmdlet or function instead of a static method. | ||
.EXAMPLE | ||
Test-StaticMethod -CommandAst $CommandAst | ||
.INPUTS | ||
[System.Management.Automation.Language.ScriptBlockAst] | ||
.OUTPUTS | ||
[PSCustomObject[]] | ||
.NOTES | ||
Reference: Output, CLM info. | ||
#> | ||
function Test-StaticMethod | ||
{ | ||
[CmdletBinding()] | ||
[OutputType([PSCustomObject[]])] | ||
Param | ||
( | ||
[Parameter(Mandatory = $true)] | ||
[ValidateNotNullOrEmpty()] | ||
[System.Management.Automation.Language.ScriptBlockAst] | ||
$ScriptBlockAst | ||
) | ||
|
||
Process | ||
{ | ||
try | ||
{ | ||
# Gets methods | ||
|
||
$invokedMethods = $ScriptBlockAst.FindAll({$args[0] -is [System.Management.Automation.Language.CommandExpressionAst] -and $args[0].Expression -match "^\[.*\]::" },$true) | ||
foreach ($invokedMethod in $invokedMethods) | ||
{ | ||
[PSCustomObject]@{Message = "Avoid Using Static Methods"; | ||
Extent = $invokedMethod.Extent; | ||
RuleName = $PSCmdlet.MyInvocation.InvocationName; | ||
Severity = "Warning"} | ||
} | ||
} | ||
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