-
Notifications
You must be signed in to change notification settings - Fork 403
Added PSUseFullyQualifiedCmdletNames rule with fix capabilities #2122
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
base: main
Are you sure you want to change the base?
Changes from 3 commits
e73df6d
dc90c53
71cbf21
63e9bf5
26dfa4e
69a2690
971f02b
33fad88
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,213 @@ | ||||||||||||||||||||||||||||||
| //--------------------------------------------------------------------------------- | ||||||||||||||||||||||||||||||
| // Copyright (c) Microsoft Corporation. | ||||||||||||||||||||||||||||||
| // The MIT License (MIT) | ||||||||||||||||||||||||||||||
| // | ||||||||||||||||||||||||||||||
| // Permission is hereby granted, free of charge, to any person obtaining a copy | ||||||||||||||||||||||||||||||
| // of this software and associated documentation files (the "Software"), to deal | ||||||||||||||||||||||||||||||
| // in the Software without restriction, including without limitation the rights | ||||||||||||||||||||||||||||||
| // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||||||||||||||||||||||||||||||
| // copies of the Software, and to permit persons to whom the Software is | ||||||||||||||||||||||||||||||
| // furnished to do so, subject to the following conditions: | ||||||||||||||||||||||||||||||
| // | ||||||||||||||||||||||||||||||
| // The above copyright notice and this permission notice shall be included in all | ||||||||||||||||||||||||||||||
| // copies or substantial portions of the Software. | ||||||||||||||||||||||||||||||
| // | ||||||||||||||||||||||||||||||
| // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||||||||||||||||||||||||||||||
| // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||||||||||||||||||||||||||||||
| // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||||||||||||||||||||||||||||||
| // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||||||||||||||||||||||||||||||
| // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||||||||||||||||||||||||||||||
| // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||||||||||||||||||||||||||||||
| // SOFTWARE. | ||||||||||||||||||||||||||||||
| //--------------------------------------------------------------------------------- | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| using System; | ||||||||||||||||||||||||||||||
| using System.Collections.Generic; | ||||||||||||||||||||||||||||||
| using System.Collections.ObjectModel; | ||||||||||||||||||||||||||||||
| using System.Linq; | ||||||||||||||||||||||||||||||
| using System.Management.Automation; | ||||||||||||||||||||||||||||||
| using System.Management.Automation.Language; | ||||||||||||||||||||||||||||||
| using Microsoft.Windows.PowerShell.ScriptAnalyzer.Generic; | ||||||||||||||||||||||||||||||
| #if !CORECLR | ||||||||||||||||||||||||||||||
| using System.ComponentModel.Composition; | ||||||||||||||||||||||||||||||
| #endif | ||||||||||||||||||||||||||||||
| using System.Globalization; | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| namespace Microsoft.Windows.PowerShell.ScriptAnalyzer.BuiltinRules | ||||||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||||||
| /// <summary> | ||||||||||||||||||||||||||||||
| /// UseFullyQualifiedCmdletNames: Checks if cmdlet and function invocations use fully qualified module names. | ||||||||||||||||||||||||||||||
| /// </summary> | ||||||||||||||||||||||||||||||
| #if !CORECLR | ||||||||||||||||||||||||||||||
| [Export(typeof(IScriptRule))] | ||||||||||||||||||||||||||||||
| #endif | ||||||||||||||||||||||||||||||
| public class UseFullyQualifiedCmdletNames : IScriptRule | ||||||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||||||
| private Dictionary<string, string> resolutionCache = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase); | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| internal const string AnalyzerName = "Microsoft.Windows.PowerShell.ScriptAnalyzer"; | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| /// <summary> | ||||||||||||||||||||||||||||||
| /// Analyzes the given ast to find cmdlet invocations that are not fully qualified. | ||||||||||||||||||||||||||||||
| /// </summary> | ||||||||||||||||||||||||||||||
| /// <param name="ast">The script's ast</param> | ||||||||||||||||||||||||||||||
| /// <param name="fileName">The script's file name</param> | ||||||||||||||||||||||||||||||
| /// <returns>The diagnostic results of this rule</returns> | ||||||||||||||||||||||||||||||
| public IEnumerable<DiagnosticRecord> AnalyzeScript(Ast ast, string fileName) | ||||||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||||||
| if (ast == null) | ||||||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||||||
| throw new ArgumentNullException(nameof(ast)); | ||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| var commandAsts = ast.FindAll(testAst => testAst is CommandAst, true).Cast<CommandAst>(); | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| foreach (var commandAst in commandAsts) | ||||||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||||||
| var commandName = commandAst.GetCommandName(); | ||||||||||||||||||||||||||||||
| if (string.IsNullOrWhiteSpace(commandName) || commandName.Contains("\\")) | ||||||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||||||
| continue; | ||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| if (!resolutionCache.TryGetValue(commandName, out string fullyQualifiedName)) | ||||||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||||||
| var resolvedCommand = ResolveCommand(commandName); | ||||||||||||||||||||||||||||||
| if (resolvedCommand == null) | ||||||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||||||
| continue; | ||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| if (resolvedCommand.CommandType != CommandTypes.Cmdlet && | ||||||||||||||||||||||||||||||
| resolvedCommand.CommandType != CommandTypes.Function && | ||||||||||||||||||||||||||||||
| resolvedCommand.CommandType != CommandTypes.Alias) | ||||||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||||||
| continue; | ||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| string moduleName = resolvedCommand.ModuleName; | ||||||||||||||||||||||||||||||
| string actualCmdletName = resolvedCommand.Name; | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| if (resolvedCommand is AliasInfo aliasInfo) | ||||||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||||||
| if (aliasInfo.ResolvedCommand == null) | ||||||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||||||
| continue; | ||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| actualCmdletName = aliasInfo.ResolvedCommand.Name; | ||||||||||||||||||||||||||||||
| moduleName = aliasInfo.ResolvedCommand.ModuleName; | ||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| if (string.IsNullOrEmpty(moduleName) || string.IsNullOrEmpty(actualCmdletName)) | ||||||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||||||
| continue; | ||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| fullyQualifiedName = $"{moduleName}\\{actualCmdletName}"; | ||||||||||||||||||||||||||||||
| resolutionCache[commandName] = fullyQualifiedName; | ||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| var extent = commandAst.CommandElements[0].Extent; | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| bool isAlias = commandName != fullyQualifiedName.Split('\\')[1]; | ||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||
| bool isAlias = commandName != fullyQualifiedName.Split('\\')[1]; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not following..
Copilot
AI
Aug 19, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The Split('\\')[1] operation is performed for every command analysis. Since the actual cmdlet name is already available from the resolution logic above (line 99), consider storing it in a variable to avoid redundant string operations.
| bool isAlias = commandName != fullyQualifiedName.Split('\\')[1]; | |
| else | |
| { | |
| // Extract actualCmdletName from the cached fullyQualifiedName | |
| int idx = fullyQualifiedName.IndexOf('\\'); | |
| actualCmdletName = (idx >= 0 && idx < fullyQualifiedName.Length - 1) | |
| ? fullyQualifiedName.Substring(idx + 1) | |
| : fullyQualifiedName; | |
| } | |
| var extent = commandAst.CommandElements[0].Extent; | |
| bool isAlias = commandName != actualCmdletName; |
Uh oh!
There was an error while loading. Please reload this page.