From 6667d899f74a427642a88e1426abca63b7070aa0 Mon Sep 17 00:00:00 2001 From: Staffan Gustafsson Date: Thu, 14 May 2020 09:06:38 +0200 Subject: [PATCH 1/3] IDictionary -> IDictionary for FunctionTable --- PowerShell.sln.DotSettings | 1 + .../engine/CommandSearcher.cs | 12 ++++++------ .../engine/GetCommandCommand.cs | 16 ++++++++-------- .../engine/Modules/ModuleCmdletBase.cs | 4 ++-- .../engine/SessionStateFunctionAPIs.cs | 2 +- .../namespaces/FunctionProvider.cs | 2 +- 6 files changed, 19 insertions(+), 18 deletions(-) create mode 100644 PowerShell.sln.DotSettings diff --git a/PowerShell.sln.DotSettings b/PowerShell.sln.DotSettings new file mode 100644 index 00000000000..5f282702bb0 --- /dev/null +++ b/PowerShell.sln.DotSettings @@ -0,0 +1 @@ + \ No newline at end of file diff --git a/src/System.Management.Automation/engine/CommandSearcher.cs b/src/System.Management.Automation/engine/CommandSearcher.cs index 093a2fbe993..7f098459b44 100644 --- a/src/System.Management.Automation/engine/CommandSearcher.cs +++ b/src/System.Management.Automation/engine/CommandSearcher.cs @@ -778,19 +778,19 @@ private CommandInfo GetNextFunction() _commandName, WildcardOptions.IgnoreCase); - foreach (DictionaryEntry functionEntry in _context.EngineSessionState.GetFunctionTable()) + foreach (var functionEntry in _context.EngineSessionState.GetFunctionTable()) { - if (functionMatcher.IsMatch((string)functionEntry.Key) || + if (functionMatcher.IsMatch(functionEntry.Key) || (_commandResolutionOptions.HasFlag(SearchResolutionOptions.FuzzyMatch) && - FuzzyMatcher.IsFuzzyMatch(functionEntry.Key.ToString(), _commandName))) + FuzzyMatcher.IsFuzzyMatch(functionEntry.Key, _commandName))) { - matchingFunction.Add((CommandInfo)functionEntry.Value); + matchingFunction.Add(functionEntry.Value); } else if (_commandResolutionOptions.HasFlag(SearchResolutionOptions.UseAbbreviationExpansion)) { - if (_commandName.Equals(ModuleUtils.AbbreviateName((string)functionEntry.Key), StringComparison.OrdinalIgnoreCase)) + if (_commandName.Equals(ModuleUtils.AbbreviateName(functionEntry.Key), StringComparison.OrdinalIgnoreCase)) { - matchingFunction.Add((CommandInfo)functionEntry.Value); + matchingFunction.Add(functionEntry.Value); } } } diff --git a/src/System.Management.Automation/engine/GetCommandCommand.cs b/src/System.Management.Automation/engine/GetCommandCommand.cs index 709c84b973c..907d5f835c6 100644 --- a/src/System.Management.Automation/engine/GetCommandCommand.cs +++ b/src/System.Management.Automation/engine/GetCommandCommand.cs @@ -591,12 +591,12 @@ private PSObject GetSyntaxObject(CommandInfo command) if (this.Name != null && !Array.Exists(this.Name, name => name.Equals(command.Name, StringComparison.InvariantCultureIgnoreCase))) { string aliasName = _nameContainsWildcard ? command.Name : this.Name[0]; - + IDictionary aliasTable = SessionState.Internal.GetAliasTable(); foreach (KeyValuePair tableEntry in aliasTable) { - if ((Array.Exists(this.Name, name => name.Equals(tableEntry.Key, StringComparison.InvariantCultureIgnoreCase)) && - tableEntry.Value.Definition == command.Name) || + if ((Array.Exists(this.Name, name => name.Equals(tableEntry.Key, StringComparison.InvariantCultureIgnoreCase)) && + tableEntry.Value.Definition == command.Name) || (_nameContainsWildcard && tableEntry.Value.Definition == command.Name)) { aliasName = tableEntry.Key; @@ -635,7 +635,7 @@ private PSObject GetSyntaxObject(CommandInfo command) break; } - + syntax = PSObject.AsPSObject(replacedSyntax); } @@ -1444,15 +1444,15 @@ private IEnumerable GetMatchingCommandsFromModules(string commandNa // Look in function table if ((this.CommandType & (CommandTypes.Function | CommandTypes.Filter | CommandTypes.Configuration)) != 0) { - foreach (DictionaryEntry function in module.SessionState.Internal.GetFunctionTable()) + foreach (var function in module.SessionState.Internal.GetFunctionTable()) { - FunctionInfo func = (FunctionInfo)function.Value; + FunctionInfo func = function.Value; - if (matcher.IsMatch((string)function.Key) && func.IsImported) + if (matcher.IsMatch(function.Key) && func.IsImported) { // make sure function doesn't come from the current module's nested module if (func.Module.Path.Equals(module.Path, StringComparison.OrdinalIgnoreCase)) - yield return (CommandInfo)function.Value; + yield return function.Value; } } } diff --git a/src/System.Management.Automation/engine/Modules/ModuleCmdletBase.cs b/src/System.Management.Automation/engine/Modules/ModuleCmdletBase.cs index a4b30616132..1124c219cf1 100644 --- a/src/System.Management.Automation/engine/Modules/ModuleCmdletBase.cs +++ b/src/System.Management.Automation/engine/Modules/ModuleCmdletBase.cs @@ -5074,9 +5074,9 @@ internal void RemoveModule(PSModuleInfo module, string moduleNameInRemoveModuleC // Remove the imported functions from SessionState... // (can't just go through module.SessionState.Internal.ExportedFunctions, // because the names of the functions might have been changed by the -Prefix parameter of Import-Module) - foreach (DictionaryEntry entry in ss.GetFunctionTable()) + foreach (var entry in ss.GetFunctionTable()) { - FunctionInfo func = (FunctionInfo)entry.Value; + FunctionInfo func = entry.Value; if (func.Module == null) { continue; diff --git a/src/System.Management.Automation/engine/SessionStateFunctionAPIs.cs b/src/System.Management.Automation/engine/SessionStateFunctionAPIs.cs index b04e5e8d275..1b08aab2a2c 100644 --- a/src/System.Management.Automation/engine/SessionStateFunctionAPIs.cs +++ b/src/System.Management.Automation/engine/SessionStateFunctionAPIs.cs @@ -39,7 +39,7 @@ internal void AddSessionStateEntry(SessionStateFunctionEntry entry) /// /// An IDictionary representing the visible functions. /// - internal IDictionary GetFunctionTable() + internal IDictionary GetFunctionTable() { SessionStateScopeEnumerator scopeEnumerator = new SessionStateScopeEnumerator(_currentScope); diff --git a/src/System.Management.Automation/namespaces/FunctionProvider.cs b/src/System.Management.Automation/namespaces/FunctionProvider.cs index d12808cf7c0..9de462c2e9e 100644 --- a/src/System.Management.Automation/namespaces/FunctionProvider.cs +++ b/src/System.Management.Automation/namespaces/FunctionProvider.cs @@ -309,7 +309,7 @@ internal override object GetValueOfItem(object item) /// internal override IDictionary GetSessionStateTable() { - return SessionState.Internal.GetFunctionTable(); + return (IDictionary)SessionState.Internal.GetFunctionTable(); } /// From 1e10f6f40ff17037efc70d8dc640df689605fbf4 Mon Sep 17 00:00:00 2001 From: Staffan Gustafsson Date: Thu, 14 May 2020 09:23:20 +0200 Subject: [PATCH 2/3] removing dotsettings --- PowerShell.sln.DotSettings | 1 - 1 file changed, 1 deletion(-) delete mode 100644 PowerShell.sln.DotSettings diff --git a/PowerShell.sln.DotSettings b/PowerShell.sln.DotSettings deleted file mode 100644 index 5f282702bb0..00000000000 --- a/PowerShell.sln.DotSettings +++ /dev/null @@ -1 +0,0 @@ - \ No newline at end of file From b6a8c498b281b75e53f4ca72fa4fa359d3cdc0c1 Mon Sep 17 00:00:00 2001 From: Staffan Gustafsson Date: Wed, 20 May 2020 00:11:28 +0200 Subject: [PATCH 3/3] Making types obvious --- .../engine/CommandSearcher.cs | 12 ++++++------ .../engine/GetCommandCommand.cs | 10 ++++------ .../engine/Modules/ModuleCmdletBase.cs | 16 ++++++++-------- 3 files changed, 18 insertions(+), 20 deletions(-) diff --git a/src/System.Management.Automation/engine/CommandSearcher.cs b/src/System.Management.Automation/engine/CommandSearcher.cs index 7f098459b44..58a460483d7 100644 --- a/src/System.Management.Automation/engine/CommandSearcher.cs +++ b/src/System.Management.Automation/engine/CommandSearcher.cs @@ -778,19 +778,19 @@ private CommandInfo GetNextFunction() _commandName, WildcardOptions.IgnoreCase); - foreach (var functionEntry in _context.EngineSessionState.GetFunctionTable()) + foreach ((string functionName, FunctionInfo functionInfo) in _context.EngineSessionState.GetFunctionTable()) { - if (functionMatcher.IsMatch(functionEntry.Key) || + if (functionMatcher.IsMatch(functionName) || (_commandResolutionOptions.HasFlag(SearchResolutionOptions.FuzzyMatch) && - FuzzyMatcher.IsFuzzyMatch(functionEntry.Key, _commandName))) + FuzzyMatcher.IsFuzzyMatch(functionName, _commandName))) { - matchingFunction.Add(functionEntry.Value); + matchingFunction.Add(functionInfo); } else if (_commandResolutionOptions.HasFlag(SearchResolutionOptions.UseAbbreviationExpansion)) { - if (_commandName.Equals(ModuleUtils.AbbreviateName(functionEntry.Key), StringComparison.OrdinalIgnoreCase)) + if (_commandName.Equals(ModuleUtils.AbbreviateName(functionName), StringComparison.OrdinalIgnoreCase)) { - matchingFunction.Add(functionEntry.Value); + matchingFunction.Add(functionInfo); } } } diff --git a/src/System.Management.Automation/engine/GetCommandCommand.cs b/src/System.Management.Automation/engine/GetCommandCommand.cs index 907d5f835c6..12472a11a6b 100644 --- a/src/System.Management.Automation/engine/GetCommandCommand.cs +++ b/src/System.Management.Automation/engine/GetCommandCommand.cs @@ -1444,15 +1444,13 @@ private IEnumerable GetMatchingCommandsFromModules(string commandNa // Look in function table if ((this.CommandType & (CommandTypes.Function | CommandTypes.Filter | CommandTypes.Configuration)) != 0) { - foreach (var function in module.SessionState.Internal.GetFunctionTable()) + foreach ((string functionName, FunctionInfo functionInfo) in module.SessionState.Internal.GetFunctionTable()) { - FunctionInfo func = function.Value; - - if (matcher.IsMatch(function.Key) && func.IsImported) + if (matcher.IsMatch(functionName) && functionInfo.IsImported) { // make sure function doesn't come from the current module's nested module - if (func.Module.Path.Equals(module.Path, StringComparison.OrdinalIgnoreCase)) - yield return function.Value; + if (functionInfo.Module.Path.Equals(module.Path, StringComparison.OrdinalIgnoreCase)) + yield return functionInfo; } } } diff --git a/src/System.Management.Automation/engine/Modules/ModuleCmdletBase.cs b/src/System.Management.Automation/engine/Modules/ModuleCmdletBase.cs index 1124c219cf1..11436624465 100644 --- a/src/System.Management.Automation/engine/Modules/ModuleCmdletBase.cs +++ b/src/System.Management.Automation/engine/Modules/ModuleCmdletBase.cs @@ -5074,29 +5074,29 @@ internal void RemoveModule(PSModuleInfo module, string moduleNameInRemoveModuleC // Remove the imported functions from SessionState... // (can't just go through module.SessionState.Internal.ExportedFunctions, // because the names of the functions might have been changed by the -Prefix parameter of Import-Module) - foreach (var entry in ss.GetFunctionTable()) + foreach ((var _, FunctionInfo functionInfo) in ss.GetFunctionTable()) { - FunctionInfo func = entry.Value; - if (func.Module == null) + if (functionInfo.Module == null) { continue; } - if (func.Module.Path.Equals(module.Path, StringComparison.OrdinalIgnoreCase)) + if (functionInfo.Module.Path.Equals(module.Path, StringComparison.OrdinalIgnoreCase)) { + string functionName = functionInfo.Name; try { - ss.RemoveFunction(func.Name, true); + ss.RemoveFunction(functionName, true); - string memberMessage = StringUtil.Format(Modules.RemovingImportedFunction, func.Name); + string memberMessage = StringUtil.Format(Modules.RemovingImportedFunction, functionName); WriteVerbose(memberMessage); } catch (SessionStateUnauthorizedAccessException e) { - string message = StringUtil.Format(Modules.UnableToRemoveModuleMember, func.Name, module.Name, e.Message); + string message = StringUtil.Format(Modules.UnableToRemoveModuleMember, functionName, module.Name, e.Message); InvalidOperationException memberNotRemoved = new InvalidOperationException(message, e); ErrorRecord er = new ErrorRecord(memberNotRemoved, "Modules_MemberNotRemoved", - ErrorCategory.PermissionDenied, func.Name); + ErrorCategory.PermissionDenied, functionName); WriteError(er); } }