Skip to content

Commit e69fa42

Browse files
Fix hashtable initializer with no elements
If a hashtable expression does not contain elements it now generate a `New` expression instead of a `ListInit` expression.
1 parent b78074e commit e69fa42

File tree

2 files changed

+17
-1
lines changed

2 files changed

+17
-1
lines changed

src/PSLambda/CompileVisitor.cs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
using System;
1+
using System;
22
using System.Collections;
33
using System.Collections.Generic;
44
using System.Globalization;
@@ -488,6 +488,14 @@ public object VisitForStatement(ForStatementAst forStatementAst)
488488

489489
public object VisitHashtable(HashtableAst hashtableAst)
490490
{
491+
if (hashtableAst.KeyValuePairs.Count == 0)
492+
{
493+
return New(
494+
ReflectionCache.Hashtable_Ctor,
495+
Constant(0),
496+
Property(null, ReflectionCache.StringComparer_CurrentCultureIgnoreCase));
497+
}
498+
491499
var elements = new ElementInit[hashtableAst.KeyValuePairs.Count];
492500
for (var i = 0; i < elements.Length; i++)
493501
{

test/MiscLanguageFeatures.Tests.ps1

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@ Import-Module $manifestPath -Force
66
Describe 'Misc Language Features' {
77
It 'Hashtable expression' {
88
$delegate = New-PSDelegate {
9+
Context 'hashtable tests' {
10+
It 'handles varied types of values' {
11+
$delegate = New-PSDelegate {
912
return @{
1013
'string' = 'value'
1114
string2 = 10
@@ -20,6 +23,11 @@ Describe 'Misc Language Features' {
2023
$hashtable['object'] | Should -BeOfType object
2124
}
2225

26+
It 'can initialize an empty hashtable' {
27+
(New-PSDelegate { @{} }).Invoke().GetType() | Should -Be ([hashtable])
28+
}
29+
}
30+
2331
Context 'array literal' {
2432
It 'int' {
2533
$result = (New-PSDelegate { 1, 2, 3 }).Invoke()

0 commit comments

Comments
 (0)