Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -5698,7 +5698,7 @@ internal override CompletionResult GetCompletionResult(string keyMatched, string

internal override CompletionResult GetCompletionResult(string keyMatched, string prefix, string suffix, string namespaceToRemove)
{
string completion = ToStringCodeMethods.Type(Type);
string completion = ToStringCodeMethods.Type(Type, false, keyMatched);

// If the completion included a namespace and ToStringCodeMethods.Type found
// an accelerator, then just use the type's FullName instead because the user
Expand Down
8 changes: 6 additions & 2 deletions src/System.Management.Automation/engine/MshObject.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2415,7 +2415,7 @@ private static void AddGenericArguments(StringBuilder sb, Type[] genericArgument
sb.Append(']');
}

internal static string Type(Type type, bool dropNamespaces = false)
internal static string Type(Type type, bool dropNamespaces = false, string key = null)
{
if (type == null)
return String.Empty;
Expand Down Expand Up @@ -2450,9 +2450,13 @@ internal static string Type(Type type, bool dropNamespaces = false)
}
else
{
result = TypeAccelerators.FindBuiltinAccelerator(type);
result = TypeAccelerators.FindBuiltinAccelerator(type, key);
if (result == null)
{
if (type == typeof(PSCustomObject))
{
return type.Name;
}
if (dropNamespaces)
{
if (typeinfo.IsNested)
Expand Down
23 changes: 18 additions & 5 deletions src/System.Management.Automation/engine/parser/TypeResolver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -804,18 +804,31 @@ static TypeAccelerators()
builtinTypeAccelerators.Add("psvariableproperty", typeof(PSVariableProperty));
}

internal static string FindBuiltinAccelerator(Type type)
internal static string FindBuiltinAccelerator(Type type, string expectedKey = null)
{
foreach (KeyValuePair<string, Type> entry in builtinTypeAccelerators)
// Taking attributes as special case. In this case, we only want to return the
// accelerator.
if (null == expectedKey || typeof(Attribute).IsAssignableFrom(type))
{
if (entry.Value == type)
foreach (KeyValuePair<string, Type> entry in builtinTypeAccelerators)
{
return entry.Key;
if (entry.Value == type)
{
return entry.Key;
}
}
}
else
{
Type resultType = null;
builtinTypeAccelerators.TryGetValue(expectedKey, out resultType);
if (null != resultType && resultType == type)
{
return expectedKey;
}
}
return null;
}

/// <summary>
/// Add a type accelerator.
/// </summary>
Expand Down
6 changes: 6 additions & 0 deletions test/powershell/Host/TabCompletion/BugFix.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,10 @@ Describe "Tab completion bug fix" -Tags "CI" {
$result.CompletionMatches[0].CompletionText | Should Be "-and"
$result.CompletionMatches[1].CompletionText | Should Be "-as"
}
It "Issue#2295 - '[pscu<tab>' should expand to [pscustomobject]" {
$result = TabExpansion2 -inputScript "[pscu" -cursorColumn "[pscu".Length
$result | Should Not BeNullOrEmpty
$result.CompletionMatches.Count | Should Be 1
$result.CompletionMatches[0].CompletionText | Should Be "pscustomobject"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,14 @@
@{ Name = 'Hashtable(Stored in a variable) conversion to PSCustomObject succeeds (Insertion Order is not retained)';
Cmd = "`$ht = @{one=1;two=2};[pscustomobject]`$ht";
ExpectedType = 'System.Management.automation.psobject'
},
@{ Name = 'New-Object cmdlet should accept `$null as Property argument for pscustomobject';
Cmd = "new-object pscustomobject -property `$null";
ExpectedType = 'System.Management.automation.psobject'
},
@{ Name = 'New-Object cmdlet should accept empty hashtable as property argument for pscustomobject';
Cmd = "`$ht = @{};new-object pscustomobject -property `$ht";
ExpectedType = 'System.Management.automation.psobject'
}
)

Expand Down Expand Up @@ -129,5 +137,21 @@
$obj.PSTypeNames.Count | Should Be 3
$obj.PSTypeNames[0] | Should Be 'System.Object'
}
It "new-object should fail to create object for System.Management.Automation.PSCustomObject" {

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fail to create object

Any object? -Property $null seems like a special case.

Again the same question: is it the right test or it's just the current behavior.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you are right. Corrected the test.


$errorObj = $null
$obj = $null
$ht = @{one=1;two=2}
try
{
$obj = New-Object System.Management.Automation.PSCustomObject -property $ht
}
catch
{
$errorObj = $_
}
$obj | should be $null
$errorObj.FullyQualifiedErrorId | should be "CannotFindAppropriateCtor,Microsoft.PowerShell.Commands.NewObjectCommand"

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we add a follow-up item on that to completely remove this type?
cc Jason Shirk (@lzybkr)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's probably not worth the hassle - it's a public api and hence a breaking change, and there is one valid use of the type (though that use can be expressed differently).

}
}