@@ -301,42 +301,48 @@ return $true}
301301param(`$ ast)
302302`$ included = $ (
303303 if (-not $IncludeType ) { ' $null' }
304- @ ($ (foreach ($incT in $IncludeType ) {
305- if ($incT -is [string ]) {
306- " '$ ( $incT -replace " '" , " ''" ) '"
304+ @ ($ (foreach ($inc in $IncludeType ) {
305+ if ($inc -is [string ]) {
306+ " '$ ( $inc -replace " '" , " ''" ) '"
307307 }
308- elseif ($incT -is [type ]) {
309- " [$ ( $incT .FullName -replace ' ^System\.' ) ]"
308+ elseif ($inc -is [type ]) {
309+ " [$ ( $inc .FullName -replace ' ^System\.' ) ]"
310310 }
311- elseif ($incT -is [regex ]) {
312- " [Regex]::new('$ ( $incT .ToString ().Replace(" '" , " ''" )) ','$ ( $incT .Options ) ','$ ( $incT .MatchTimeout ) ')"
311+ elseif ($inc -is [regex ]) {
312+ " [Regex]::new('$ ( $inc .ToString ().Replace(" '" , " ''" )) ','$ ( $inc .Options ) ','$ ( $inc .MatchTimeout ) ')"
313313 }
314314})) -join ' ,' )
315315`$ excluded = $ ( @ (
316316 if (-not $ExcludeType ) { ' $null' }
317- $ (foreach ($excT in $ExcludeType ) {
318- if ($excT -is [string ]) {
319- " '$ ( $excT -replace " '" , " ''" ) '"
317+ $ (foreach ($exc in $ExcludeType ) {
318+ if ($exc -is [string ]) {
319+ " '$ ( $exc -replace " '" , " ''" ) '"
320320 }
321- elseif ($excT -is [type ]) {
322- " [$ ( $excT .FullName -replace ' ^System\.' ) ]"
321+ elseif ($exc -is [type ]) {
322+ " [$ ( $exc .FullName -replace ' ^System\.' ) ]"
323323 }
324- elseif ($excT -is [regex ]) {
325- " [Regex]::new('$ ( $excT .ToString ().Replace(" '" , " ''" )) ','$ ( $excT .Options ) ','$ ( $excT .MatchTimeout ) ')"
324+ elseif ($exc -is [regex ]) {
325+ " [Regex]::new('$ ( $exc .ToString ().Replace(" '" , " ''" )) ','$ ( $exc .Options ) ','$ ( $exc .MatchTimeout ) ')"
326326 }
327327})) -join ' ,' )
328- if (`$ ast -is [Management.Automation.Language.TypeExpressionAst]) {
328+ if (`$ ast -is [Management.Automation.Language.TypeExpressionAst] -or
329+ `$ ast -is [Management.Automation.Language.TypeConstraintAst]) {
330+ $ ( {
331+ $astType = $ast.TypeName
332+ $reflectionType = if ($astType ) {
333+ $astType.GetReflectionType ()
334+ }
335+ })
329336$ ( if ($IncludeType ) {
330337{
331338 foreach ($inc in $included ) {
332- if ($inc -is [string ] -and $ast .TypeName -like $inc ) {
339+ if ($inc -is [string ] -and $astType -like $inc ) {
333340 return $true
334341 }
335- elseif ($inc -is [Regex ] -and $ast .TypeName -match $inc ) {
342+ elseif ($inc -is [Regex ] -and $astType -match $inc ) {
336343 return $true
337344 }
338- elseif ($inc -is [type ]){
339- $reflectionType = $ast.TypeName.GetReflectionType ()
345+ elseif ($inc -is [type ]){
340346 if ($inc -eq $reflectionType ) { return $true }
341347 if ($inc.IsSubclassOf ($reflectionType ) -or $reflectionType.IsSubclassOf ($inc )) {
342348 return $true
@@ -353,14 +359,13 @@ $(if ($IncludeType) {
353359$ ( {
354360 $throwMessage = " [$ ( $ast.Typename ) ] is not allowed"
355361 foreach ($exc in $excluded ) {
356- if ($exc -is [string ] -and $ast .TypeName -like $exc ) {
362+ if ($exc -is [string ] -and $astType -like $exc ) {
357363 throw $throwMessage
358364 }
359- elseif ($exc -is [regex ] -and $ast .TypeName -match $exc ) {
365+ elseif ($exc -is [regex ] -and $astType -match $exc ) {
360366 throw $throwMessage
361367 }
362- elseif ($exc -is [type ]) {
363- $reflectionType = $ast.TypeName.GetReflectionType ()
368+ elseif ($exc -is [type ]) {
364369 if ($ecx -eq $reflectionType ) {
365370 throw $throwMessage
366371 }
@@ -398,6 +403,7 @@ if ($ast -is [Management.Automation.Language.LoopStatementAst] -and
398403 $ast.GetType ().Name -match ' (?>do|while)' ) {
399404 throw " ScriptBlock cannot contain $ ( $ast.GetType ().Name) "
400405}
406+ return $true
401407}
402408 }
403409
0 commit comments