-
Notifications
You must be signed in to change notification settings - Fork 382
/
UseOutputTypeCorrectly.cs
200 lines (167 loc) · 7.58 KB
/
UseOutputTypeCorrectly.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Management.Automation.Language;
using Microsoft.Windows.PowerShell.ScriptAnalyzer.Generic;
#if !CORECLR
using System.ComponentModel.Composition;
#endif
using System.Globalization;
using System.Management.Automation;
namespace Microsoft.Windows.PowerShell.ScriptAnalyzer.BuiltinRules
{
/// <summary>
/// UseOutputTypeCorrectly: Checks that objects returned in a cmdlet have their types declared in OutputType Attribute.
/// </summary>
#if !CORECLR
[Export(typeof(IScriptRule))]
#endif
public class UseOutputTypeCorrectly : SkipTypeDefinition, IScriptRule
{
#if !(PSV3||PSV4)
private IEnumerable<TypeDefinitionAst> _classes;
#endif
/// <summary>
/// AnalyzeScript: Checks that objects returned in a cmdlet have their types declared in OutputType Attribute
/// </summary>
/// <param name="ast">The script's ast</param>
/// <param name="fileName">The name of the script</param>
/// <returns>A List of diagnostic results of this rule</returns>
public IEnumerable<DiagnosticRecord> AnalyzeScript(Ast ast, string fileName)
{
if (ast == null) throw new ArgumentNullException(Strings.NullAstErrorMessage);
DiagnosticRecords.Clear();
this.fileName = fileName;
#if !(PSV3||PSV4)
_classes = ast.FindAll(item => item is TypeDefinitionAst && ((item as TypeDefinitionAst).IsClass), true).Cast<TypeDefinitionAst>();
#endif
ast.Visit(this);
return DiagnosticRecords;
}
/// <summary>
/// Visit function and checks that it is a cmdlet. If yes, then checks that any object returns must have a type declared
/// in the output type (the only exception is if the type is object)
/// </summary>
/// <param name="funcAst"></param>
/// <returns></returns>
public override AstVisitAction VisitFunctionDefinition(FunctionDefinitionAst funcAst)
{
if (funcAst == null || funcAst.Body == null || funcAst.Body.ParamBlock == null
|| funcAst.Body.ParamBlock.Attributes == null || funcAst.Body.ParamBlock.Attributes.Count == 0
|| !funcAst.Body.ParamBlock.Attributes.Any(attr => attr.TypeName.GetReflectionType() == typeof(CmdletBindingAttribute)))
{
return AstVisitAction.Continue;
}
HashSet<string> outputTypes = new HashSet<string>();
foreach (AttributeAst attrAst in funcAst.Body.ParamBlock.Attributes)
{
if (attrAst.TypeName != null && attrAst.TypeName.GetReflectionType() == typeof(OutputTypeAttribute)
&& attrAst.PositionalArguments != null)
{
foreach (ExpressionAst expAst in attrAst.PositionalArguments)
{
if (expAst is StringConstantExpressionAst)
{
Type type = Type.GetType((expAst as StringConstantExpressionAst).Value);
if (type != null)
{
outputTypes.Add(type.FullName);
}
}
else
{
TypeExpressionAst typeAst = expAst as TypeExpressionAst;
if (typeAst != null && typeAst.TypeName != null)
{
if (typeAst.TypeName.GetReflectionType() != null)
{
outputTypes.Add(typeAst.TypeName.GetReflectionType().FullName);
}
else
{
outputTypes.Add(typeAst.TypeName.FullName);
}
}
}
}
}
}
#if PSV3
List<Tuple<string, StatementAst>> returnTypes = FindPipelineOutput.OutputTypes(funcAst);
#else
List<Tuple<string, StatementAst>> returnTypes = FindPipelineOutput.OutputTypes(funcAst, _classes);
#endif
HashSet<string> specialTypes = new HashSet<string>(StringComparer.OrdinalIgnoreCase);
specialTypes.Add(typeof(Unreached).FullName);
specialTypes.Add(typeof(Undetermined).FullName);
specialTypes.Add(typeof(object).FullName);
specialTypes.Add(typeof(void).FullName);
specialTypes.Add(typeof(PSCustomObject).FullName);
specialTypes.Add(typeof(PSObject).FullName);
foreach (Tuple<string, StatementAst> returnType in returnTypes)
{
string typeName = returnType.Item1;
if (String.IsNullOrEmpty(typeName)
|| specialTypes.Contains(typeName)
|| outputTypes.Contains(typeName, StringComparer.OrdinalIgnoreCase))
{
continue;
}
else
{
DiagnosticRecords.Add(new DiagnosticRecord(string.Format(CultureInfo.CurrentCulture, Strings.UseOutputTypeCorrectlyError,
funcAst.Name, typeName), returnType.Item2.Extent, GetName(), DiagnosticSeverity.Information, fileName));
}
}
return AstVisitAction.Continue;
}
/// <summary>
/// GetName: Retrieves the name of this rule.
/// </summary>
/// <returns>The name of this rule</returns>
public string GetName()
{
return string.Format(CultureInfo.CurrentCulture, Strings.NameSpaceFormat, GetSourceName(), Strings.UseOutputTypeCorrectlyName);
}
/// <summary>
/// GetCommonName: Retrieves the common name of this rule.
/// </summary>
/// <returns>The common name of this rule</returns>
public string GetCommonName()
{
return string.Format(CultureInfo.CurrentCulture, Strings.UseOutputTypeCorrectlyCommonName);
}
/// <summary>
/// GetDescription: Retrieves the description of this rule.
/// </summary>
/// <returns>The description of this rule</returns>
public string GetDescription()
{
return string.Format(CultureInfo.CurrentCulture, Strings.UseOutputTypeCorrectlyDescription);
}
/// <summary>
/// Method: Retrieves the type of the rule: builtin, managed or module.
/// </summary>
public SourceType GetSourceType()
{
return SourceType.Builtin;
}
/// <summary>
/// GetSeverity: Retrieves the severity of the rule: error, warning of information.
/// </summary>
/// <returns></returns>
public RuleSeverity GetSeverity()
{
return RuleSeverity.Information;
}
/// <summary>
/// Method: Retrieves the module/assembly name the rule is from.
/// </summary>
public string GetSourceName()
{
return string.Format(CultureInfo.CurrentCulture, Strings.SourceName);
}
}
}