Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove dead code and simplify #1856

Merged
merged 7 commits into from
Sep 11, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 1 addition & 3 deletions Engine/CommandInfoCache.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,15 @@ namespace Microsoft.Windows.PowerShell.ScriptAnalyzer
internal class CommandInfoCache : IDisposable
{
private readonly ConcurrentDictionary<CommandLookupKey, Lazy<CommandInfo>> _commandInfoCache;
private readonly Helper _helperInstance;
private readonly RunspacePool _runspacePool;
private bool disposed = false;

/// <summary>
/// Create a fresh command info cache instance.
/// </summary>
public CommandInfoCache(Helper pssaHelperInstance)
public CommandInfoCache()
{
_commandInfoCache = new ConcurrentDictionary<CommandLookupKey, Lazy<CommandInfo>>();
_helperInstance = pssaHelperInstance;
_runspacePool = RunspaceFactory.CreateRunspacePool(1, 10);
_runspacePool.Open();
}
Expand Down
5 changes: 2 additions & 3 deletions Engine/Commands/GetScriptAnalyzerRuleCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -84,13 +84,12 @@ protected override void BeginProcessing()

// Initialize helper
Helper.Instance = new Helper(
SessionState.InvokeCommand,
this);
SessionState.InvokeCommand);
Helper.Instance.Initialize();

string[] rulePaths = Helper.ProcessCustomRulePaths(customRulePath,
this.SessionState, recurseCustomRulePath);
ScriptAnalyzer.Instance.Initialize(this, rulePaths, null, null, null, null == rulePaths ? true : false);
ScriptAnalyzer.Instance.Initialize(this, rulePaths, null, null, null, null == rulePaths);
}

/// <summary>
Expand Down
6 changes: 0 additions & 6 deletions Engine/Commands/InvokeFormatterCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -124,11 +124,5 @@ protected override void StopProcessing()
ScriptAnalyzer.Instance.CleanUp();
base.StopProcessing();
}

private void ValidateInputSettings()
{
// todo implement this
return;
}
}
}
3 changes: 1 addition & 2 deletions Engine/Commands/InvokeScriptAnalyzerCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -285,8 +285,7 @@ protected override void BeginProcessing()
}
#endif
Helper.Instance = new Helper(
SessionState.InvokeCommand,
this);
SessionState.InvokeCommand);
Helper.Instance.Initialize();

var psVersionTable = this.SessionState.PSVariable.GetValue("PSVersionTable") as Hashtable;
Expand Down
2 changes: 1 addition & 1 deletion Engine/Formatter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public static string Format<TCmdlet>(
ValidateNotNull(settings, "settings");
ValidateNotNull(cmdlet, "cmdlet");

Helper.Instance = new Helper(cmdlet.SessionState.InvokeCommand, cmdlet);
Helper.Instance = new Helper(cmdlet.SessionState.InvokeCommand);
Helper.Instance.Initialize();

var ruleOrder = new string[]
Expand Down
3 changes: 0 additions & 3 deletions Engine/Generic/ModuleDependencyHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ public class ModuleDependencyHandler : IDisposable
private string moduleRepository;
private string tempPath; // path to the user temporary directory
private string tempModulePath; // path to temp directory containing modules
Dictionary<string, PSObject> modulesFound;
private string localAppdataPath;
private string pssaAppDataPath;
private const string symLinkName = "TempModuleDir";
Expand Down Expand Up @@ -271,8 +270,6 @@ public ModuleDependencyHandler(
? "PSScriptAnalyzer"
: pssaAppDataPath);

modulesFound = new Dictionary<string, PSObject>(StringComparer.OrdinalIgnoreCase);

// TODO Add PSSA Version in the path
symLinkPath = Path.Combine(pssaAppDataPath, symLinkName);
SetupPSSAAppData();
Expand Down
12 changes: 3 additions & 9 deletions Engine/Helper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ public class Helper
#region Private members

private CommandInvocationIntrinsics invokeCommand;
private IOutputWriter outputWriter;
private readonly static Version minSupportedPSVersion = new Version(3, 0);
private Dictionary<string, Dictionary<string, object>> ruleArguments;
private PSVersionTable psVersionTable;
Expand Down Expand Up @@ -115,7 +114,7 @@ internal set
/// </summary>
private Helper()
{
_commandInfoCacheLazy = new Lazy<CommandInfoCache>(() => new CommandInfoCache(pssaHelperInstance: this));
_commandInfoCacheLazy = new Lazy<CommandInfoCache>(() => new CommandInfoCache());
}

/// <summary>
Expand All @@ -125,16 +124,11 @@ private Helper()
/// A CommandInvocationIntrinsics instance for use in gathering
/// information about available commands and aliases.
/// </param>
/// <param name="outputWriter">
/// An IOutputWriter instance for use in writing output
/// to the PowerShell environment.
/// </param>
public Helper(
CommandInvocationIntrinsics invokeCommand,
IOutputWriter outputWriter) : this()
CommandInvocationIntrinsics invokeCommand
): this()
{
this.invokeCommand = invokeCommand;
this.outputWriter = outputWriter;
}

#region Methods
Expand Down
3 changes: 1 addition & 2 deletions Engine/ScriptAnalyzer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -168,8 +168,7 @@ public void Initialize(

//initialize helper
Helper.Instance = new Helper(
runspace.SessionStateProxy.InvokeCommand,
outputWriter);
runspace.SessionStateProxy.InvokeCommand);
Helper.Instance.Initialize();

SuppressionPreference suppressionPreference = suppressedOnly
Expand Down
11 changes: 0 additions & 11 deletions Engine/Settings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -285,17 +285,6 @@ private Dictionary<string, object> GetDictionaryFromHashtable(Hashtable hashtabl
return dictionary;
}

private bool IsStringOrStringArray(object val)
{
if (val is string)
{
return true;
}

var valArr = val as object[];
return val == null ? false : valArr.All(x => x is string);
}

private List<string> GetData(object val, string key)
{
// value must be either string or or an array of strings
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -351,18 +351,6 @@ private Architecture GetOSArchitecture()
#endif
}

private DotnetRuntime GetDotnetRuntime()
{
#if CoreCLR
// Our CoreCLR is actuall .NET Standard, so we could be loaded into net47
return RuntimeInformation.FrameworkDescription.StartsWith(".NET Core")
? DotnetRuntime.Core
: DotnetRuntime.Framework;
#else
return DotnetRuntime.Framework;
#endif
}

/// <summary>
/// Get the Windows SKU ID of the current PowerShell session.
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,6 @@ namespace Microsoft.PowerShell.CrossCompatibility.Commands
/// </summary>
internal static class CommandUtilities
{
private const string COMPATIBILITY_ERROR_ID = "CompatibilityAnalysisError";

public const string MODULE_PREFIX = "PSCompatibility";

/// <summary>
Expand Down
4 changes: 2 additions & 2 deletions Rules/AvoidUserNameAndPasswordParams.cs
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ public IEnumerable<DiagnosticRecord> AnalyzeScript(Ast ast, string fileName)
{
yield return new DiagnosticRecord(
String.Format(CultureInfo.CurrentCulture, Strings.AvoidUsernameAndPasswordParamsError, funcAst.Name),
GetExtent(usernameAst, passwordAst, ast), GetName(), DiagnosticSeverity.Error, fileName);
GetExtent(usernameAst, passwordAst), GetName(), DiagnosticSeverity.Error, fileName);
}
}
}
Expand All @@ -111,7 +111,7 @@ private bool IsAttributeOfType(AttributeBaseAst attributeAst, Type type)
/// <param name="usernameAst"></param>
/// <param name="passwordAst"></param>
/// <returns>IScriptExtent</returns>
private IScriptExtent GetExtent(ParameterAst usernameAst, ParameterAst passwordAst, Ast scriptAst)
private IScriptExtent GetExtent(ParameterAst usernameAst, ParameterAst passwordAst)
{
var usrExt = usernameAst.Extent;
var pwdExt = passwordAst.Extent;
Expand Down
3 changes: 1 addition & 2 deletions Rules/AvoidUsingDeprecatedManifestFields.cs
Original file line number Diff line number Diff line change
Expand Up @@ -70,10 +70,9 @@ public IEnumerable<DiagnosticRecord> AnalyzeScript(Ast ast, string fileName)

if (value != null)
{
Version psVersion = null;

// get the version
if (Version.TryParse((value as StringConstantExpressionAst).Value, out psVersion))
if (Version.TryParse((value as StringConstantExpressionAst).Value, out Version psVersion))
{
// if version exists and version less than 3, don't raise rule
if (psVersion.Major < 3)
Expand Down
3 changes: 1 addition & 2 deletions Rules/CompatibilityRules/UseCompatibleCommands.cs
Original file line number Diff line number Diff line change
Expand Up @@ -263,8 +263,7 @@ public static CommandCompatibilityDiagnostic CreateForParameter(
PlatformData platform,
IScriptExtent extent,
string analyzedFileName,
IRule rule,
IEnumerable<CorrectionExtent> suggestedCorrections = null)
IRule rule)
{
string message = string.Format(
CultureInfo.CurrentCulture,
Expand Down
5 changes: 0 additions & 5 deletions Rules/UseConsistentIndentation.cs
Original file line number Diff line number Diff line change
Expand Up @@ -563,11 +563,6 @@ private static int ClipNegative(int x)
return x > 0 ? x : 0;
}

private int GetIndentationColumnNumber(int indentationLevel)
{
return GetIndentation(indentationLevel) + 1;
}

private int GetIndentation(int indentationLevel)
{
// todo if condition can be evaluated during rule configuration
Expand Down
2 changes: 0 additions & 2 deletions Rules/UseIdenticalMandatoryParametersDSC.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ namespace Microsoft.Windows.PowerShell.ScriptAnalyzer.BuiltinRules
public class UseIdenticalMandatoryParametersDSC : IDSCResourceRule
{
private bool isDSCClassCacheInitialized = false;
private Ast ast;
private string fileName;
private IDictionary<string, string> propAttrDict;
private IEnumerable<FunctionDefinitionAst> resourceFunctions;
Expand Down Expand Up @@ -94,7 +93,6 @@ public IEnumerable<DiagnosticRecord> AnalyzeDSCResource(Ast ast, string fileName
}

// Get the keys in the corresponding mof file
this.ast = ast;
this.fileName = fileName;
this.propAttrDict = GetKeys(fileName);
this.resourceFunctions = Helper.Instance.DscResourceFunctions(ast)
Expand Down
5 changes: 2 additions & 3 deletions Rules/UseToExportFieldsInManifest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ public IEnumerable<DiagnosticRecord> AnalyzeScript(Ast ast, string fileName)
foreach(string field in manifestFields)
{
IScriptExtent extent;
if (!HasAcceptableExportField(field, hashtableAst, ast.Extent.Text, out extent) && extent != null)
if (!HasAcceptableExportField(field, hashtableAst, out extent) && extent != null)
{
yield return new DiagnosticRecord(
GetError(field),
Expand Down Expand Up @@ -200,10 +200,9 @@ private bool HasNullInExpression(Ast ast)
/// </summary>
/// <param name="key"></param>
/// <param name="hast"></param>
/// <param name="scriptText"></param>
/// <param name="extent"></param>
/// <returns>A boolean value indicating if the the ToExport fields are explicitly set to arrays or not.</returns>
private bool HasAcceptableExportField(string key, HashtableAst hast, string scriptText, out IScriptExtent extent)
private bool HasAcceptableExportField(string key, HashtableAst hast, out IScriptExtent extent)
{
extent = null;
foreach (var pair in hast.KeyValuePairs)
Expand Down