|
| 1 | +// |
| 2 | +// Copyright (c) Microsoft Corporation. |
| 3 | +// |
| 4 | +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 5 | +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 6 | +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 7 | +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 8 | +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 9 | +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 10 | +// THE SOFTWARE. |
| 11 | +// |
| 12 | + |
| 13 | +using System; |
| 14 | +using System.Collections.Generic; |
| 15 | +using System.Linq; |
| 16 | +using System.Management.Automation.Language; |
| 17 | +using Microsoft.Windows.Powershell.ScriptAnalyzer.Generic; |
| 18 | +using System.ComponentModel.Composition; |
| 19 | +using System.Globalization; |
| 20 | +using System.Management.Automation; |
| 21 | + |
| 22 | +namespace Microsoft.Windows.Powershell.ScriptAnalyzer.BuiltinRules |
| 23 | +{ |
| 24 | + /// <summary> |
| 25 | + /// ProvideCommentHelp: Checks that objects return in a cmdlet have their types declared in OutputType Attribute |
| 26 | + /// </summary> |
| 27 | + [Export(typeof(IScriptRule))] |
| 28 | + public class UseOutputTypeCorrectly : SkipTypeDefinition, IScriptRule |
| 29 | + { |
| 30 | + private IEnumerable<TypeDefinitionAst> _classes; |
| 31 | + |
| 32 | + /// <summary> |
| 33 | + /// AnalyzeScript: Checks that objects return in a cmdlet have their types declared in OutputType Attribute |
| 34 | + /// </summary> |
| 35 | + /// <param name="ast">The script's ast</param> |
| 36 | + /// <param name="fileName">The name of the script</param> |
| 37 | + /// <returns>A List of diagnostic results of this rule</returns> |
| 38 | + public IEnumerable<DiagnosticRecord> AnalyzeScript(Ast ast, string fileName) |
| 39 | + { |
| 40 | + if (ast == null) throw new ArgumentNullException(Strings.NullAstErrorMessage); |
| 41 | + |
| 42 | + DiagnosticRecords.Clear(); |
| 43 | + this.fileName = fileName; |
| 44 | + |
| 45 | + _classes = ast.FindAll(item => item is TypeDefinitionAst && ((item as TypeDefinitionAst).IsClass), true).Cast<TypeDefinitionAst>(); |
| 46 | + |
| 47 | + ast.Visit(this); |
| 48 | + |
| 49 | + return DiagnosticRecords; |
| 50 | + } |
| 51 | + |
| 52 | + /// <summary> |
| 53 | + /// Visit function and checks that it is a cmdlet. If yes, then checks that any object returns must have a type declared |
| 54 | + /// in the output type (the only exception is if the type is object) |
| 55 | + /// </summary> |
| 56 | + /// <param name="funcAst"></param> |
| 57 | + /// <returns></returns> |
| 58 | + public override AstVisitAction VisitFunctionDefinition(FunctionDefinitionAst funcAst) |
| 59 | + { |
| 60 | + if (funcAst == null || funcAst.Body == null || funcAst.Body.ParamBlock == null |
| 61 | + || funcAst.Body.ParamBlock.Attributes == null || funcAst.Body.ParamBlock.Attributes.Count == 0 |
| 62 | + || !funcAst.Body.ParamBlock.Attributes.Any(attr => attr.TypeName.GetReflectionType() == typeof(CmdletBindingAttribute))) |
| 63 | + { |
| 64 | + return AstVisitAction.Continue; |
| 65 | + } |
| 66 | + |
| 67 | + HashSet<string> outputTypes = new HashSet<string>(); |
| 68 | + |
| 69 | + foreach (AttributeAst attrAst in funcAst.Body.ParamBlock.Attributes) |
| 70 | + { |
| 71 | + if (attrAst.TypeName != null && attrAst.TypeName.GetReflectionType() == typeof(OutputTypeAttribute) |
| 72 | + && attrAst.PositionalArguments != null) |
| 73 | + { |
| 74 | + foreach (ExpressionAst expAst in attrAst.PositionalArguments) |
| 75 | + { |
| 76 | + if (expAst is StringConstantExpressionAst) |
| 77 | + { |
| 78 | + Type type = Type.GetType((expAst as StringConstantExpressionAst).Value); |
| 79 | + if (type != null) |
| 80 | + { |
| 81 | + outputTypes.Add(type.FullName); |
| 82 | + } |
| 83 | + } |
| 84 | + else |
| 85 | + { |
| 86 | + TypeExpressionAst typeAst = expAst as TypeExpressionAst; |
| 87 | + if (typeAst != null && typeAst.TypeName != null) |
| 88 | + { |
| 89 | + if (typeAst.TypeName.GetReflectionType() != null) |
| 90 | + { |
| 91 | + outputTypes.Add(typeAst.TypeName.GetReflectionType().FullName); |
| 92 | + } |
| 93 | + else |
| 94 | + { |
| 95 | + outputTypes.Add(typeAst.TypeName.FullName); |
| 96 | + } |
| 97 | + } |
| 98 | + } |
| 99 | + } |
| 100 | + } |
| 101 | + } |
| 102 | + |
| 103 | + List<Tuple<string, StatementAst>> returnTypes = FindPipelineOutput.OutputTypes(funcAst, _classes); |
| 104 | + |
| 105 | + foreach (Tuple<string, StatementAst> returnType in returnTypes) |
| 106 | + { |
| 107 | + string typeName = returnType.Item1; |
| 108 | + |
| 109 | + 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 | + || outputTypes.Contains(typeName, StringComparer.OrdinalIgnoreCase)) |
| 114 | + { |
| 115 | + continue; |
| 116 | + } |
| 117 | + else |
| 118 | + { |
| 119 | + DiagnosticRecords.Add(new DiagnosticRecord(string.Format(CultureInfo.CurrentCulture, Strings.UseOutputTypeCorrectlyError, |
| 120 | + funcAst.Name, typeName), returnType.Item2.Extent, GetName(), DiagnosticSeverity.Information, fileName)); |
| 121 | + } |
| 122 | + } |
| 123 | + |
| 124 | + return AstVisitAction.Continue; |
| 125 | + } |
| 126 | + |
| 127 | + /// <summary> |
| 128 | + /// GetName: Retrieves the name of this rule. |
| 129 | + /// </summary> |
| 130 | + /// <returns>The name of this rule</returns> |
| 131 | + public string GetName() |
| 132 | + { |
| 133 | + return string.Format(CultureInfo.CurrentCulture, Strings.NameSpaceFormat, GetSourceName(), Strings.UseOutputTypeCorrectlyName); |
| 134 | + } |
| 135 | + |
| 136 | + /// <summary> |
| 137 | + /// GetCommonName: Retrieves the common name of this rule. |
| 138 | + /// </summary> |
| 139 | + /// <returns>The common name of this rule</returns> |
| 140 | + public string GetCommonName() |
| 141 | + { |
| 142 | + return string.Format(CultureInfo.CurrentCulture, Strings.UseOutputTypeCorrectlyCommonName); |
| 143 | + } |
| 144 | + |
| 145 | + /// <summary> |
| 146 | + /// GetDescription: Retrieves the description of this rule. |
| 147 | + /// </summary> |
| 148 | + /// <returns>The description of this rule</returns> |
| 149 | + public string GetDescription() |
| 150 | + { |
| 151 | + return string.Format(CultureInfo.CurrentCulture, Strings.UseOutputTypeCorrectlyDescription); |
| 152 | + } |
| 153 | + |
| 154 | + /// <summary> |
| 155 | + /// Method: Retrieves the type of the rule: builtin, managed or module. |
| 156 | + /// </summary> |
| 157 | + public SourceType GetSourceType() |
| 158 | + { |
| 159 | + return SourceType.Builtin; |
| 160 | + } |
| 161 | + |
| 162 | + /// <summary> |
| 163 | + /// GetSeverity: Retrieves the severity of the rule: error, warning of information. |
| 164 | + /// </summary> |
| 165 | + /// <returns></returns> |
| 166 | + public RuleSeverity GetSeverity() |
| 167 | + { |
| 168 | + return RuleSeverity.Information; |
| 169 | + } |
| 170 | + |
| 171 | + /// <summary> |
| 172 | + /// Method: Retrieves the module/assembly name the rule is from. |
| 173 | + /// </summary> |
| 174 | + public string GetSourceName() |
| 175 | + { |
| 176 | + return string.Format(CultureInfo.CurrentCulture, Strings.SourceName); |
| 177 | + } |
| 178 | + } |
| 179 | +} |
0 commit comments