-
Notifications
You must be signed in to change notification settings - Fork 382
/
AvoidGlobalFunctions.cs
109 lines (94 loc) · 3.61 KB
/
AvoidGlobalFunctions.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
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
using System;
using System.Collections.Generic;
#if !CORECLR
using System.ComponentModel.Composition;
#endif
using System.Globalization;
using System.Management.Automation.Language;
using Microsoft.Windows.PowerShell.ScriptAnalyzer.Generic;
namespace Microsoft.Windows.PowerShell.ScriptAnalyzer.BuiltinRules
{
/// <summary>
/// AvoidGlobalFunctions: Checks that global functions are not used within modules.
/// </summary>
#if !CORECLR
[Export(typeof(IScriptRule))]
#endif
public class AvoidGlobalFunctions : AstVisitor, IScriptRule
{
private List<DiagnosticRecord> records;
private string fileName;
/// <summary>
/// Analyzes the ast to check that global functions are not used within modules.
/// </summary>
/// <param name="ast">The script's ast</param>
/// <param name="fileName">The script's file name</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);
}
records = new List<DiagnosticRecord>();
this.fileName = fileName;
if (fileName != null && Helper.IsModuleScript(fileName))
{
ast.Visit(this);
}
return records;
}
#region VisitCommand functions
/// <summary>
/// Analyzes a FunctionDefinitionAst, if it is declared global a diagnostic record is created.
/// </summary>
/// <param name="functionDefinitionAst">FunctionDefinitionAst to be analyzed</param>
/// <returns>AstVisitAction to continue analysis</returns>
public override AstVisitAction VisitFunctionDefinition(FunctionDefinitionAst functionDefinitionAst)
{
if (functionDefinitionAst.Name.StartsWith("Global:", StringComparison.OrdinalIgnoreCase))
{
var functionNameExtent = Helper.Instance.GetScriptExtentForFunctionName(functionDefinitionAst);
records.Add(new DiagnosticRecord(
string.Format(CultureInfo.CurrentCulture, Strings.AvoidGlobalFunctionsError),
functionNameExtent,
GetName(),
DiagnosticSeverity.Warning,
fileName,
functionDefinitionAst.Name));
}
return AstVisitAction.Continue;
}
#endregion
public string GetCommonName()
{
return string.Format(CultureInfo.CurrentCulture, Strings.AvoidGlobalFunctionsCommonName);
}
public string GetDescription()
{
return string.Format(CultureInfo.CurrentCulture, Strings.AvoidGlobalFunctionsDescription);
}
public string GetName()
{
return string.Format(
CultureInfo.CurrentCulture,
Strings.NameSpaceFormat,
GetSourceName(),
Strings.AvoidGlobalFunctionsName);
}
public RuleSeverity GetSeverity()
{
return RuleSeverity.Warning;
}
public string GetSourceName()
{
return string.Format(CultureInfo.CurrentCulture, Strings.SourceName);
}
public SourceType GetSourceType()
{
return SourceType.Builtin;
}
}
}