-
Notifications
You must be signed in to change notification settings - Fork 393
/
Copy pathFormatter.cs
92 lines (82 loc) · 3.39 KB
/
Formatter.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
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
using System;
using System.Collections;
using System.Management.Automation;
using System.Management.Automation.Language;
namespace Microsoft.Windows.PowerShell.ScriptAnalyzer
{
/// <summary>
/// A class to provide code formatting capability.
/// </summary>
public class Formatter
{
/// <summary>
/// Format a powershell script.
/// </summary>
/// <param name="scriptDefinition">A string representing a powershell script.</param>
/// <param name="settings">Settings to be used for formatting</param>
/// <param name="range">The range in which formatting should take place.</param>
/// <param name="cmdlet">The cmdlet object that calls this method.</param>
/// <returns></returns>
public static string Format<TCmdlet>(
string scriptDefinition,
Settings settings,
Range range,
TCmdlet cmdlet) where TCmdlet : PSCmdlet, IOutputWriter
{
// todo implement notnull attribute for such a check
ValidateNotNull(scriptDefinition, "scriptDefinition");
ValidateNotNull(settings, "settings");
ValidateNotNull(cmdlet, "cmdlet");
Helper.Instance = new Helper(cmdlet.SessionState.InvokeCommand);
Helper.Instance.Initialize();
var ruleOrder = new string[]
{
"PSPlaceCloseBrace",
"PSPlaceOpenBrace",
"PSUseConsistentWhitespace",
"PSUseConsistentIndentation",
"PSAlignAssignmentStatement",
"PSUseCorrectCasing",
"PSAvoidUsingCmdletAliases",
"PSAvoidUsingDoubleQuotesForConstantString",
"PSAvoidSemicolonsAsLineTerminators",
"PSAvoidExclaimOperator",
};
var text = new EditableText(scriptDefinition);
ScriptBlockAst scriptAst = null;
Token[] scriptTokens = null;
bool skipParsing = false;
foreach (var rule in ruleOrder)
{
if (!settings.RuleArguments.ContainsKey(rule))
{
continue;
}
var currentSettings = GetCurrentSettings(settings, rule);
ScriptAnalyzer.Instance.UpdateSettings(currentSettings);
ScriptAnalyzer.Instance.Initialize(cmdlet, null, null, null, null, true, SuppressionPreference.Omit);
text = ScriptAnalyzer.Instance.Fix(text, range, skipParsing, out Range updatedRange, out bool fixesWereApplied, ref scriptAst, ref scriptTokens, skipVariableAnalysis: true);
skipParsing = !fixesWereApplied;
range = updatedRange;
}
return text.ToString();
}
private static void ValidateNotNull<T>(T obj, string name)
{
if (obj == null)
{
throw new ArgumentNullException(name);
}
}
private static Settings GetCurrentSettings(Settings settings, string rule)
{
return new Settings(new Hashtable()
{
{"IncludeRules", new string[] {rule}},
{"Rules", new Hashtable() { { rule, new Hashtable(settings.RuleArguments[rule]) } } }
});
}
}
}