Skip to content

Commit e3b70ed

Browse files
author
James Brundage
committed
Updating 'new': Supporting Extended Types (#164)
1 parent c1e6999 commit e3b70ed

File tree

1 file changed

+12
-3
lines changed

1 file changed

+12
-3
lines changed

Transpilers/Keywords/New.psx.ps1

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@
3131
.> { new ScriptBlock 'Get-Command'}
3232
.EXAMPLE
3333
.> { (new PowerShell).AddScript("Get-Command").Invoke() }
34+
.EXAMPLE
35+
.> { new 'https://schema.org/Thing' }
3436
#>
3537
[ValidateScript({
3638
$CommandAst = $_
@@ -46,15 +48,15 @@ process {
4648
$null, $newTypeName, $newArgs = $CommandAst.CommandElements
4749
$maybeGeneric = $false
4850
$propertiesToCreate = @()
49-
51+
$newTypeNameAst = $newTypeName
5052
$newTypeName =
5153
# Non-generic types will be a bareword constant
5254
if ($newTypeName.Value) {
5355
$newTypeName.Value
5456
}
5557
elseif ($newTypeName -is [Management.Automation.Language.HashtableAst]) {
5658
$propertiesToCreate += $newTypeName
57-
}
59+
}
5860
else {
5961
# generic types will be an ArrayLiteralAst
6062
$maybeGeneric = $true
@@ -135,7 +137,14 @@ process {
135137
}
136138
} elseif ($propertiesToCreate.Count -eq 1 -and -not $newTypeName) {
137139
"[PSCustomObject][Ordered]$propertiesToCreate"
138-
} else {
140+
} elseif ($newTypeNameAst -is [Management.Automation.Language.StringConstantExpressionAst]) {
141+
if ($propertiesToCreate) {
142+
"[PSCustomObject]([Ordered]@{PSTypeName=$newTypeNameAst} + ([Ordered]$propertiesToCreate))"
143+
} else {
144+
"[PSCustomObject][Ordered]@{PSTypeName=$newTypeNameAst}"
145+
}
146+
}
147+
else {
139148
Write-Error "Unknown type '$newTypeName'"
140149
return
141150
}

0 commit comments

Comments
 (0)