-
Notifications
You must be signed in to change notification settings - Fork 410
Expand file tree
/
Copy pathMissingTryBlock.cs
More file actions
83 lines (71 loc) · 3.25 KB
/
MissingTryBlock.cs
File metadata and controls
83 lines (71 loc) · 3.25 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
// 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;
#if !CORECLR
using System.ComponentModel.Composition;
#endif
namespace Microsoft.Windows.PowerShell.ScriptAnalyzer.BuiltinRules
{
#if !CORECLR
[Export(typeof(IScriptRule))]
#endif
/// <summary>
/// Rule that warns when catch or finally blocks are used without a corresponding try block
/// </summary>
public class MissingTryBlock : IScriptRule
{
/// <summary>
/// Analyzes the PowerShell AST for catch or finally blocks that miss the try block.
/// </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 missingTryAsts = ast.FindAll(testAst =>
// Normally should be part of a TryStatementAst
testAst is StringConstantExpressionAst stringAst &&
// Catch or finally are reserved keywords and should be bare words
stringAst.StringConstantType == StringConstantType.BareWord &&
(
String.Equals(stringAst.Value, "catch", StringComparison.OrdinalIgnoreCase) ||
String.Equals(stringAst.Value, "finally", StringComparison.OrdinalIgnoreCase)
) &&
stringAst.Parent is CommandAst commandAst &&
// Only violate if the catch or finally is the first command element
commandAst.CommandElements[0] == stringAst,
true
);
foreach (StringConstantExpressionAst missingTryAst in missingTryAsts)
{
yield return new DiagnosticRecord(
string.Format(
CultureInfo.CurrentCulture,
Strings.MissingTryBlockError,
CultureInfo.CurrentCulture.TextInfo.ToTitleCase(missingTryAst.Value)),
missingTryAst.Extent,
GetName(),
DiagnosticSeverity.Warning,
fileName,
missingTryAst.Value
);
}
}
public string GetCommonName() => Strings.MissingTryBlockCommonName;
public string GetDescription() => Strings.MissingTryBlockDescription;
public string GetName() => string.Format(
CultureInfo.CurrentCulture,
Strings.NameSpaceFormat,
GetSourceName(),
Strings.MissingTryBlockName);
public RuleSeverity GetSeverity() => RuleSeverity.Warning;
public string GetSourceName() => Strings.SourceName;
public SourceType GetSourceType() => SourceType.Builtin;
}
}