Skip to content

Commit 8dc51f8

Browse files
author
James Brundage
committed
ValidateScriptBlock: Adding -NoLoop/-NoWhileLoop (Fixes #227)
1 parent f691e4b commit 8dc51f8

File tree

1 file changed

+26
-2
lines changed

1 file changed

+26
-2
lines changed

Transpilers/Parameters/ValidateScriptBlock.psx.ps1

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,16 @@ return $true
128128
})]
129129
$ExcludeType,
130130

131+
# If set, will ensure that the ScriptBlock does not contain any loops.
132+
[Alias('NoLoops')]
133+
[switch]
134+
$NoLoop,
135+
136+
# If set, will ensure that the ScriptBlock does not contain any do or while loops.
137+
[Alias('NoWhileLoops','NoDoLoops','NoDoLoop')]
138+
[switch]
139+
$NoWhileLoop,
140+
131141
# One or more AST conditions to validate.
132142
# If no results are found or the condition throws, the script block will be considered invalid.
133143
[Alias('AstConditions', 'IfAst')]
@@ -373,8 +383,22 @@ return `$true
373383
}
374384
}
375385

376-
if ($IncludeCommand -or $ExcludeCommand) {
377-
386+
if ($NoLoop) {
387+
$AstCondition += {
388+
param($ast)
389+
if ($ast -is [Management.Automation.Language.LoopStatementAst]) {
390+
throw "ScriptBlock cannot contain loops"
391+
}
392+
return $true
393+
}
394+
} elseif ($NoWhileLoop) {
395+
$AstCondition += {
396+
param($ast)
397+
if ($ast -is [Management.Automation.Language.LoopStatementAst] -and
398+
$ast.GetType().Name -match '(?>do|while)') {
399+
throw "ScriptBlock cannot contain $($ast.GetType().Name)"
400+
}
401+
}
378402
}
379403

380404
if ($AstCondition) {

0 commit comments

Comments
 (0)