-
Notifications
You must be signed in to change notification settings - Fork 406
Expand file tree
/
Copy pathAvoidCmdletGeneric.cs
More file actions
98 lines (83 loc) · 3.62 KB
/
AvoidCmdletGeneric.cs
File metadata and controls
98 lines (83 loc) · 3.62 KB
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
// 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;
namespace Microsoft.Windows.PowerShell.ScriptAnalyzer.Generic
{
/// <summary>
/// Represents an abstract class for rule that checks whether the script
/// uses certain cmdlet.
/// </summary>
public abstract class AvoidCmdletGeneric : IScriptRule
{
/// <summary>
/// AnalyzeScript: Analyzes the given Ast and returns DiagnosticRecords based on the analysis.
/// </summary>
/// <param name="ast">The script's ast</param>
/// <param name="fileName">The name of the script file being analyzed</param>
/// <returns>The results of the analysis</returns>
public IEnumerable<DiagnosticRecord> AnalyzeScript(Ast ast, string fileName)
{
if (ast == null) throw new ArgumentNullException("ast");
// Finds all CommandAsts.
IEnumerable<Ast> commandAsts = ast.FindAll(testAst => testAst is CommandAst, true);
List<String> cmdletNameAndAliases = Microsoft.Windows.PowerShell.ScriptAnalyzer.Helper.Instance.CmdletNameAndAliases(GetCmdletName());
// Iterates all CommandAsts and check the command name.
foreach (CommandAst cmdAst in commandAsts)
{
if (cmdAst.GetCommandName() == null) continue;
if (cmdletNameAndAliases.Contains(cmdAst.GetCommandName(), StringComparer.OrdinalIgnoreCase))
{
yield return new DiagnosticRecord(GetError(fileName), cmdAst.Extent, GetName(), GetDiagnosticSeverity(), fileName);
}
}
}
/// <summary>
/// Retrieves the name of the cmdlet to avoid
/// </summary>
/// <returns></returns>
public abstract string GetCmdletName();
/// <summary>
/// GetError: Retrieves the error message.
/// </summary>
/// <returns></returns>
public abstract string GetError(string FileName);
/// <summary>
/// GetName: Retrieves the name of the rule.
/// </summary>
/// <returns>The name of the rule.</returns>
public abstract string GetName();
/// <summary>
/// GetCommonName: Retrieves the common name of this rule.
/// </summary>
/// <returns>The common name of this rule</returns>
public abstract string GetCommonName();
/// <summary>
/// GetDescription: Retrieves the description of the rule.
/// </summary>
/// <returns>The description of the rule.</returns>
public abstract string GetDescription();
/// <summary>
/// GetSourceName: Retrieves the source name of the rule.
/// </summary>
/// <returns>The source name of the rule.</returns>
public abstract string GetSourceName();
/// <summary>
/// GetSourceType: Retrieves the source type of the rule.
/// </summary>
/// <returns>The source type of the rule.</returns>
public abstract SourceType GetSourceType();
/// <summary>
/// GetSeverity: Retrieves the severity of the rule: error, warning of information.
/// </summary>
/// <returns></returns>
public abstract RuleSeverity GetSeverity();
/// <summary>
/// DiagnosticSeverity: Returns the severity of the rule of type DiagnosticSeverity
/// </summary>
/// <returns></returns>
public abstract DiagnosticSeverity GetDiagnosticSeverity();
}
}