Skip to content

Commit 3eb56c6

Browse files
author
Quoc Truong
committed
Merge pull request #243 from PowerShell/FixOutputTypeError
Ignore PSObject and PSCustomObject for UseOutputTypeCorrectly rule
2 parents c85bf65 + 5d1325e commit 3eb56c6

File tree

2 files changed

+16
-5
lines changed

2 files changed

+16
-5
lines changed

Rules/UseOutputTypeCorrectly.cs

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -102,15 +102,20 @@ public override AstVisitAction VisitFunctionDefinition(FunctionDefinitionAst fun
102102

103103
List<Tuple<string, StatementAst>> returnTypes = FindPipelineOutput.OutputTypes(funcAst, _classes);
104104

105+
HashSet<string> specialTypes = new HashSet<string>(StringComparer.OrdinalIgnoreCase);
106+
specialTypes.Add(typeof(Unreached).FullName);
107+
specialTypes.Add(typeof(Undetermined).FullName);
108+
specialTypes.Add(typeof(object).FullName);
109+
specialTypes.Add(typeof(void).FullName);
110+
specialTypes.Add(typeof(PSCustomObject).FullName);
111+
specialTypes.Add(typeof(PSObject).FullName);
112+
105113
foreach (Tuple<string, StatementAst> returnType in returnTypes)
106114
{
107115
string typeName = returnType.Item1;
108116

109117
if (String.IsNullOrEmpty(typeName)
110-
|| String.Equals(typeof(Unreached).FullName, typeName, StringComparison.OrdinalIgnoreCase)
111-
|| String.Equals(typeof(Undetermined).FullName, typeName, StringComparison.OrdinalIgnoreCase)
112-
|| String.Equals(typeof(object).FullName, typeName, StringComparison.OrdinalIgnoreCase)
113-
|| String.Equals(typeof(void).FullName, typeName, StringComparison.OrdinalIgnoreCase)
118+
|| specialTypes.Contains(typeName)
114119
|| outputTypes.Contains(typeName, StringComparer.OrdinalIgnoreCase))
115120
{
116121
continue;

Tests/Rules/GoodCmdlet.ps1

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ function Get-File
2828
HelpUri = 'http://www.microsoft.com/',
2929
ConfirmImpact='Medium')]
3030
[Alias()]
31-
[OutputType([String], [System.Double], [Hashtable])]
31+
[OutputType([String], [System.Double], [Hashtable], "MyCustom.OutputType")]
3232
[OutputType("System.Int32", ParameterSetName="ID")]
3333

3434
Param
@@ -91,6 +91,12 @@ function Get-File
9191

9292
Write-Debug @b
9393

94+
[pscustomobject]@{
95+
PSTypeName = 'MyCustom.OutputType'
96+
Prop1 = 'SomeValue'
97+
Prop2 = 'OtherValue'
98+
}
99+
94100
return @{"hash"="true"}
95101
}
96102
End

0 commit comments

Comments
 (0)