-
Notifications
You must be signed in to change notification settings - Fork 408
Expand file tree
/
Copy pathAvoidReservedWordsAsFunctionNames.cs
More file actions
103 lines (89 loc) · 3.94 KB
/
AvoidReservedWordsAsFunctionNames.cs
File metadata and controls
103 lines (89 loc) · 3.94 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
99
100
101
102
103
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
using Microsoft.Windows.PowerShell.ScriptAnalyzer.Generic;
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Management.Automation.Language;
using System.Linq;
#if !CORECLR
using System.ComponentModel.Composition;
#endif
namespace Microsoft.Windows.PowerShell.ScriptAnalyzer.BuiltinRules
{
#if !CORECLR
[Export(typeof(IScriptRule))]
#endif
/// <summary>
/// Rule that warns when reserved words are used as function names
/// </summary>
public class AvoidReservedWordsAsFunctionNames : IScriptRule
{
// The list of PowerShell reserved words.
// https://learn.microsoft.com/en-gb/powershell/module/microsoft.powershell.core/about/about_reserved_words
//
// The Below are omitted as they don't pose an issue being a function
// name:
// assembly, base, command, hidden, in, inlinescript, interface, module,
// namespace, private, public, static
static readonly HashSet<string> reservedWords = new HashSet<string>(
new[] {
"begin", "break", "catch", "class", "configuration",
"continue", "data", "define", "do",
"dynamicparam", "else", "elseif", "end",
"enum", "exit", "filter", "finally",
"for", "foreach", "from", "function",
"if", "parallel", "param", "process",
"return", "sequence", "switch",
"throw", "trap", "try", "type",
"until", "using","var", "while", "workflow"
},
StringComparer.OrdinalIgnoreCase
);
/// <summary>
/// Analyzes the PowerShell AST for uses of reserved words as function names.
/// </summary>
/// <param name="ast">The PowerShell Abstract Syntax Tree to analyze.</param>
/// <param name="fileName">The name of the file being analyzed (for diagnostic reporting).</param>
/// <returns>A collection of diagnostic records for each violation.</returns>
public IEnumerable<DiagnosticRecord> AnalyzeScript(Ast ast, string fileName)
{
if (ast == null)
{
throw new ArgumentNullException(Strings.NullAstErrorMessage);
}
// Find all FunctionDefinitionAst in the Ast
var functionDefinitions = ast.FindAll(
astNode => astNode is FunctionDefinitionAst,
true
).Cast<FunctionDefinitionAst>();
foreach (var function in functionDefinitions)
{
string functionName = Helper.Instance.FunctionNameWithoutScope(function.Name);
if (reservedWords.Contains(functionName))
{
yield return new DiagnosticRecord(
string.Format(
CultureInfo.CurrentCulture,
Strings.AvoidReservedWordsAsFunctionNamesError,
functionName),
Helper.Instance.GetScriptExtentForFunctionName(function) ?? function.Extent,
GetName(),
DiagnosticSeverity.Warning,
fileName
);
}
}
}
public string GetCommonName() => Strings.AvoidReservedWordsAsFunctionNamesCommonName;
public string GetDescription() => Strings.AvoidReservedWordsAsFunctionNamesDescription;
public string GetName() => string.Format(
CultureInfo.CurrentCulture,
Strings.NameSpaceFormat,
GetSourceName(),
Strings.AvoidReservedWordsAsFunctionNamesName);
public RuleSeverity GetSeverity() => RuleSeverity.Warning;
public string GetSourceName() => Strings.SourceName;
public SourceType GetSourceType() => SourceType.Builtin;
}
}