Skip to content

Commit bd79460

Browse files
authored
Remove dead code and simplify (#1856)
* Remove unused variables * simplify code and remove unused method * remove unused member * more cleanup * more cleanup * more * more
1 parent 85ad15b commit bd79460

17 files changed

+15
-70
lines changed

Engine/CommandInfoCache.cs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,17 +15,15 @@ namespace Microsoft.Windows.PowerShell.ScriptAnalyzer
1515
internal class CommandInfoCache : IDisposable
1616
{
1717
private readonly ConcurrentDictionary<CommandLookupKey, Lazy<CommandInfo>> _commandInfoCache;
18-
private readonly Helper _helperInstance;
1918
private readonly RunspacePool _runspacePool;
2019
private bool disposed = false;
2120

2221
/// <summary>
2322
/// Create a fresh command info cache instance.
2423
/// </summary>
25-
public CommandInfoCache(Helper pssaHelperInstance)
24+
public CommandInfoCache()
2625
{
2726
_commandInfoCache = new ConcurrentDictionary<CommandLookupKey, Lazy<CommandInfo>>();
28-
_helperInstance = pssaHelperInstance;
2927
_runspacePool = RunspaceFactory.CreateRunspacePool(1, 10);
3028
_runspacePool.Open();
3129
}

Engine/Commands/GetScriptAnalyzerRuleCommand.cs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -84,13 +84,12 @@ protected override void BeginProcessing()
8484

8585
// Initialize helper
8686
Helper.Instance = new Helper(
87-
SessionState.InvokeCommand,
88-
this);
87+
SessionState.InvokeCommand);
8988
Helper.Instance.Initialize();
9089

9190
string[] rulePaths = Helper.ProcessCustomRulePaths(customRulePath,
9291
this.SessionState, recurseCustomRulePath);
93-
ScriptAnalyzer.Instance.Initialize(this, rulePaths, null, null, null, null == rulePaths ? true : false);
92+
ScriptAnalyzer.Instance.Initialize(this, rulePaths, null, null, null, null == rulePaths);
9493
}
9594

9695
/// <summary>

Engine/Commands/InvokeFormatterCommand.cs

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -124,11 +124,5 @@ protected override void StopProcessing()
124124
ScriptAnalyzer.Instance.CleanUp();
125125
base.StopProcessing();
126126
}
127-
128-
private void ValidateInputSettings()
129-
{
130-
// todo implement this
131-
return;
132-
}
133127
}
134128
}

Engine/Commands/InvokeScriptAnalyzerCommand.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -285,8 +285,7 @@ protected override void BeginProcessing()
285285
}
286286
#endif
287287
Helper.Instance = new Helper(
288-
SessionState.InvokeCommand,
289-
this);
288+
SessionState.InvokeCommand);
290289
Helper.Instance.Initialize();
291290

292291
var psVersionTable = this.SessionState.PSVariable.GetValue("PSVersionTable") as Hashtable;

Engine/Formatter.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ public static string Format<TCmdlet>(
3232
ValidateNotNull(settings, "settings");
3333
ValidateNotNull(cmdlet, "cmdlet");
3434

35-
Helper.Instance = new Helper(cmdlet.SessionState.InvokeCommand, cmdlet);
35+
Helper.Instance = new Helper(cmdlet.SessionState.InvokeCommand);
3636
Helper.Instance.Initialize();
3737

3838
var ruleOrder = new string[]

Engine/Generic/ModuleDependencyHandler.cs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ public class ModuleDependencyHandler : IDisposable
2222
private string moduleRepository;
2323
private string tempPath; // path to the user temporary directory
2424
private string tempModulePath; // path to temp directory containing modules
25-
Dictionary<string, PSObject> modulesFound;
2625
private string localAppdataPath;
2726
private string pssaAppDataPath;
2827
private const string symLinkName = "TempModuleDir";
@@ -271,8 +270,6 @@ public ModuleDependencyHandler(
271270
? "PSScriptAnalyzer"
272271
: pssaAppDataPath);
273272

274-
modulesFound = new Dictionary<string, PSObject>(StringComparer.OrdinalIgnoreCase);
275-
276273
// TODO Add PSSA Version in the path
277274
symLinkPath = Path.Combine(pssaAppDataPath, symLinkName);
278275
SetupPSSAAppData();

Engine/Helper.cs

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@ public class Helper
2424
#region Private members
2525

2626
private CommandInvocationIntrinsics invokeCommand;
27-
private IOutputWriter outputWriter;
2827
private readonly static Version minSupportedPSVersion = new Version(3, 0);
2928
private Dictionary<string, Dictionary<string, object>> ruleArguments;
3029
private PSVersionTable psVersionTable;
@@ -115,7 +114,7 @@ internal set
115114
/// </summary>
116115
private Helper()
117116
{
118-
_commandInfoCacheLazy = new Lazy<CommandInfoCache>(() => new CommandInfoCache(pssaHelperInstance: this));
117+
_commandInfoCacheLazy = new Lazy<CommandInfoCache>(() => new CommandInfoCache());
119118
}
120119

121120
/// <summary>
@@ -125,16 +124,11 @@ private Helper()
125124
/// A CommandInvocationIntrinsics instance for use in gathering
126125
/// information about available commands and aliases.
127126
/// </param>
128-
/// <param name="outputWriter">
129-
/// An IOutputWriter instance for use in writing output
130-
/// to the PowerShell environment.
131-
/// </param>
132127
public Helper(
133-
CommandInvocationIntrinsics invokeCommand,
134-
IOutputWriter outputWriter) : this()
128+
CommandInvocationIntrinsics invokeCommand
129+
): this()
135130
{
136131
this.invokeCommand = invokeCommand;
137-
this.outputWriter = outputWriter;
138132
}
139133

140134
#region Methods

Engine/ScriptAnalyzer.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -168,8 +168,7 @@ public void Initialize(
168168

169169
//initialize helper
170170
Helper.Instance = new Helper(
171-
runspace.SessionStateProxy.InvokeCommand,
172-
outputWriter);
171+
runspace.SessionStateProxy.InvokeCommand);
173172
Helper.Instance.Initialize();
174173

175174
SuppressionPreference suppressionPreference = suppressedOnly

Engine/Settings.cs

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -285,17 +285,6 @@ private Dictionary<string, object> GetDictionaryFromHashtable(Hashtable hashtabl
285285
return dictionary;
286286
}
287287

288-
private bool IsStringOrStringArray(object val)
289-
{
290-
if (val is string)
291-
{
292-
return true;
293-
}
294-
295-
var valArr = val as object[];
296-
return val == null ? false : valArr.All(x => x is string);
297-
}
298-
299288
private List<string> GetData(object val, string key)
300289
{
301290
// value must be either string or or an array of strings

PSCompatibilityCollector/Microsoft.PowerShell.CrossCompatibility/Collection/PlatformInformationCollector.cs

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -351,18 +351,6 @@ private Architecture GetOSArchitecture()
351351
#endif
352352
}
353353

354-
private DotnetRuntime GetDotnetRuntime()
355-
{
356-
#if CoreCLR
357-
// Our CoreCLR is actuall .NET Standard, so we could be loaded into net47
358-
return RuntimeInformation.FrameworkDescription.StartsWith(".NET Core")
359-
? DotnetRuntime.Core
360-
: DotnetRuntime.Framework;
361-
#else
362-
return DotnetRuntime.Framework;
363-
#endif
364-
}
365-
366354
/// <summary>
367355
/// Get the Windows SKU ID of the current PowerShell session.
368356
/// </summary>

PSCompatibilityCollector/Microsoft.PowerShell.CrossCompatibility/Commands/CommandUtilities.cs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,6 @@ namespace Microsoft.PowerShell.CrossCompatibility.Commands
1313
/// </summary>
1414
internal static class CommandUtilities
1515
{
16-
private const string COMPATIBILITY_ERROR_ID = "CompatibilityAnalysisError";
17-
1816
public const string MODULE_PREFIX = "PSCompatibility";
1917

2018
/// <summary>

Rules/AvoidUserNameAndPasswordParams.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ public IEnumerable<DiagnosticRecord> AnalyzeScript(Ast ast, string fileName)
8686
{
8787
yield return new DiagnosticRecord(
8888
String.Format(CultureInfo.CurrentCulture, Strings.AvoidUsernameAndPasswordParamsError, funcAst.Name),
89-
GetExtent(usernameAst, passwordAst, ast), GetName(), DiagnosticSeverity.Error, fileName);
89+
GetExtent(usernameAst, passwordAst), GetName(), DiagnosticSeverity.Error, fileName);
9090
}
9191
}
9292
}
@@ -111,7 +111,7 @@ private bool IsAttributeOfType(AttributeBaseAst attributeAst, Type type)
111111
/// <param name="usernameAst"></param>
112112
/// <param name="passwordAst"></param>
113113
/// <returns>IScriptExtent</returns>
114-
private IScriptExtent GetExtent(ParameterAst usernameAst, ParameterAst passwordAst, Ast scriptAst)
114+
private IScriptExtent GetExtent(ParameterAst usernameAst, ParameterAst passwordAst)
115115
{
116116
var usrExt = usernameAst.Extent;
117117
var pwdExt = passwordAst.Extent;

Rules/AvoidUsingDeprecatedManifestFields.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,10 +70,9 @@ public IEnumerable<DiagnosticRecord> AnalyzeScript(Ast ast, string fileName)
7070

7171
if (value != null)
7272
{
73-
Version psVersion = null;
7473

7574
// get the version
76-
if (Version.TryParse((value as StringConstantExpressionAst).Value, out psVersion))
75+
if (Version.TryParse((value as StringConstantExpressionAst).Value, out Version psVersion))
7776
{
7877
// if version exists and version less than 3, don't raise rule
7978
if (psVersion.Major < 3)

Rules/CompatibilityRules/UseCompatibleCommands.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -263,8 +263,7 @@ public static CommandCompatibilityDiagnostic CreateForParameter(
263263
PlatformData platform,
264264
IScriptExtent extent,
265265
string analyzedFileName,
266-
IRule rule,
267-
IEnumerable<CorrectionExtent> suggestedCorrections = null)
266+
IRule rule)
268267
{
269268
string message = string.Format(
270269
CultureInfo.CurrentCulture,

Rules/UseConsistentIndentation.cs

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -563,11 +563,6 @@ private static int ClipNegative(int x)
563563
return x > 0 ? x : 0;
564564
}
565565

566-
private int GetIndentationColumnNumber(int indentationLevel)
567-
{
568-
return GetIndentation(indentationLevel) + 1;
569-
}
570-
571566
private int GetIndentation(int indentationLevel)
572567
{
573568
// todo if condition can be evaluated during rule configuration

Rules/UseIdenticalMandatoryParametersDSC.cs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@ namespace Microsoft.Windows.PowerShell.ScriptAnalyzer.BuiltinRules
3232
public class UseIdenticalMandatoryParametersDSC : IDSCResourceRule
3333
{
3434
private bool isDSCClassCacheInitialized = false;
35-
private Ast ast;
3635
private string fileName;
3736
private IDictionary<string, string> propAttrDict;
3837
private IEnumerable<FunctionDefinitionAst> resourceFunctions;
@@ -94,7 +93,6 @@ public IEnumerable<DiagnosticRecord> AnalyzeDSCResource(Ast ast, string fileName
9493
}
9594

9695
// Get the keys in the corresponding mof file
97-
this.ast = ast;
9896
this.fileName = fileName;
9997
this.propAttrDict = GetKeys(fileName);
10098
this.resourceFunctions = Helper.Instance.DscResourceFunctions(ast)

Rules/UseToExportFieldsInManifest.cs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ public IEnumerable<DiagnosticRecord> AnalyzeScript(Ast ast, string fileName)
6969
foreach(string field in manifestFields)
7070
{
7171
IScriptExtent extent;
72-
if (!HasAcceptableExportField(field, hashtableAst, ast.Extent.Text, out extent) && extent != null)
72+
if (!HasAcceptableExportField(field, hashtableAst, out extent) && extent != null)
7373
{
7474
yield return new DiagnosticRecord(
7575
GetError(field),
@@ -200,10 +200,9 @@ private bool HasNullInExpression(Ast ast)
200200
/// </summary>
201201
/// <param name="key"></param>
202202
/// <param name="hast"></param>
203-
/// <param name="scriptText"></param>
204203
/// <param name="extent"></param>
205204
/// <returns>A boolean value indicating if the the ToExport fields are explicitly set to arrays or not.</returns>
206-
private bool HasAcceptableExportField(string key, HashtableAst hast, string scriptText, out IScriptExtent extent)
205+
private bool HasAcceptableExportField(string key, HashtableAst hast, out IScriptExtent extent)
207206
{
208207
extent = null;
209208
foreach (var pair in hast.KeyValuePairs)

0 commit comments

Comments
 (0)