-
Notifications
You must be signed in to change notification settings - Fork 393
/
Copy pathUseProcessBlockForPipelineCommand.cs
96 lines (83 loc) · 3.64 KB
/
UseProcessBlockForPipelineCommand.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
using System;
using System.Collections.Generic;
using System.Management.Automation.Language;
using Microsoft.Windows.PowerShell.ScriptAnalyzer.Generic;
#if !CORECLR
using System.ComponentModel.Composition;
#endif
using System.Globalization;
namespace Microsoft.Windows.PowerShell.ScriptAnalyzer.BuiltinRules
{
#if !CORECLR
[Export(typeof(IScriptRule))]
#endif
public class UseProcessBlockForPipelineCommand : IScriptRule
{
public IEnumerable<DiagnosticRecord> AnalyzeScript(Ast ast, string fileName)
{
if (ast == null) throw new ArgumentNullException(Strings.NullAstErrorMessage);
IEnumerable<Ast> functionAsts = ast.FindAll(testAst => testAst is FunctionDefinitionAst, true);
foreach (FunctionDefinitionAst funcAst in functionAsts)
{
if
(
funcAst.Body.ParamBlock == null
|| funcAst.Body.ParamBlock.Attributes == null
|| funcAst.Body.ParamBlock.Parameters == null
|| funcAst.Body.ProcessBlock != null
)
{ continue; }
foreach (var paramAst in funcAst.Body.ParamBlock.Parameters)
{
foreach (var paramAstAttribute in paramAst.Attributes)
{
if (!(paramAstAttribute is AttributeAst)) { continue; }
var namedArguments = (paramAstAttribute as AttributeAst).NamedArguments;
if (namedArguments == null) { continue; }
foreach (NamedAttributeArgumentAst namedArgument in namedArguments)
{
if
(
!namedArgument.ArgumentName.Equals("valuefrompipeline", StringComparison.OrdinalIgnoreCase)
&& !namedArgument.ArgumentName.Equals("valuefrompipelinebypropertyname", StringComparison.OrdinalIgnoreCase)
)
{ continue; }
yield return new DiagnosticRecord(
string.Format(CultureInfo.CurrentCulture, Strings.UseProcessBlockForPipelineCommandError, paramAst.Name.VariablePath.UserPath),
paramAst.Name.Extent,
GetName(),
DiagnosticSeverity.Warning,
fileName,
paramAst.Name.VariablePath.UserPath
);
}
}
}
}
}
public string GetName()
{
return string.Format(CultureInfo.CurrentCulture, Strings.NameSpaceFormat, GetSourceName(), Strings.UseProcessBlockForPipelineCommandName);
}
public string GetCommonName()
{
return string.Format(CultureInfo.CurrentCulture, Strings.UseProcessBlockForPipelineCommandCommonName);
}
public string GetDescription()
{
return string.Format(CultureInfo.CurrentCulture, Strings.UseProcessBlockForPipelineCommandDescription);
}
public SourceType GetSourceType()
{
return SourceType.Builtin;
}
public RuleSeverity GetSeverity()
{
return RuleSeverity.Warning;
}
public string GetSourceName()
{
return string.Format(CultureInfo.CurrentCulture, Strings.SourceName);
}
}
}