|
11 | 11 | using System.Globalization;
|
12 | 12 | using System.Linq;
|
13 | 13 | using System.Management.Automation;
|
| 14 | +using System.Threading.Tasks; |
| 15 | +using System.Collections.Concurrent; |
14 | 16 |
|
15 | 17 | namespace Microsoft.Windows.PowerShell.ScriptAnalyzer.BuiltinRules
|
16 | 18 | {
|
@@ -95,7 +97,8 @@ public IEnumerable<DiagnosticRecord> AnalyzeScript(Ast ast, string fileName)
|
95 | 97 | // Finds all CommandAsts.
|
96 | 98 | IEnumerable<Ast> foundAsts = ast.FindAll(testAst => testAst is CommandAst, true);
|
97 | 99 |
|
98 |
| - // Iterates all CommandAsts and check the command name. |
| 100 | + // Iterates all CommandAsts and check the command name. Expensive operations are run in background tasks |
| 101 | + var tasks = new List<Task<DiagnosticRecord>>(); |
99 | 102 | foreach (CommandAst cmdAst in foundAsts)
|
100 | 103 | {
|
101 | 104 | // Check if the command ast should be ignored
|
@@ -128,24 +131,50 @@ public IEnumerable<DiagnosticRecord> AnalyzeScript(Ast ast, string fileName)
|
128 | 131 | suggestedCorrections: GetCorrectionExtent(cmdAst, cmdletNameIfCommandNameWasAlias));
|
129 | 132 | }
|
130 | 133 |
|
131 |
| - var isNativeCommand = Helper.Instance.GetCommandInfo(commandName, CommandTypes.Application | CommandTypes.ExternalScript) != null; |
132 |
| - if (!isNativeCommand) |
| 134 | + // Checking for implicit 'Get-' aliasing is done in a background task as it can be quite expensive during a cold-start |
| 135 | + tasks.Add(Task<DiagnosticRecord>.Run(() => |
133 | 136 | {
|
134 |
| - var commdNameWithGetPrefix = $"Get-{commandName}"; |
135 |
| - var cmdletNameIfCommandWasMissingGetPrefix = Helper.Instance.GetCommandInfo($"Get-{commandName}"); |
136 |
| - if (cmdletNameIfCommandWasMissingGetPrefix != null) |
137 |
| - { |
138 |
| - yield return new DiagnosticRecord( |
139 |
| - string.Format(CultureInfo.CurrentCulture, Strings.AvoidUsingCmdletAliasesMissingGetPrefixError, commandName, commdNameWithGetPrefix), |
140 |
| - GetCommandExtent(cmdAst), |
141 |
| - GetName(), |
142 |
| - DiagnosticSeverity.Warning, |
143 |
| - fileName, |
144 |
| - commandName, |
145 |
| - suggestedCorrections: GetCorrectionExtent(cmdAst, commdNameWithGetPrefix)); |
146 |
| - } |
| 137 | + return CheckForImplicitGetAliasing(commandName, cmdAst, fileName); |
| 138 | + })); |
| 139 | + } |
| 140 | + foreach(var task in tasks) |
| 141 | + { |
| 142 | + var diagnosticRecordResult = task.Result; |
| 143 | + if (diagnosticRecordResult != null) |
| 144 | + { |
| 145 | + yield return task.Result; |
| 146 | + } |
| 147 | + } |
| 148 | + } |
| 149 | + |
| 150 | + /// <summary> |
| 151 | + /// If one omitts, 'Get-' for a command, PowerShell will pre-pend it if such a command exists, therefore relying on such implicit aliasing is not desirable. |
| 152 | + /// </summary> |
| 153 | + /// <param name="commandName"></param> |
| 154 | + /// <param name="commandAst"></param> |
| 155 | + /// <param name="fileName"></param> |
| 156 | + /// <returns></returns> |
| 157 | + private DiagnosticRecord CheckForImplicitGetAliasing(string commandName, CommandAst commandAst, string fileName) |
| 158 | + { |
| 159 | + var isNativeCommand = Helper.Instance.GetCommandInfo(commandName, CommandTypes.Application | CommandTypes.ExternalScript) != null; |
| 160 | + if (!isNativeCommand) |
| 161 | + { |
| 162 | + var commdNameWithGetPrefix = $"Get-{commandName}"; |
| 163 | + var cmdletNameIfCommandWasMissingGetPrefix = Helper.Instance.GetCommandInfo($"Get-{commandName}"); |
| 164 | + if (cmdletNameIfCommandWasMissingGetPrefix != null) |
| 165 | + { |
| 166 | + var diagnosticRecord = new DiagnosticRecord( |
| 167 | + string.Format(CultureInfo.CurrentCulture, Strings.AvoidUsingCmdletAliasesMissingGetPrefixError, commandName, commdNameWithGetPrefix), |
| 168 | + GetCommandExtent(commandAst), |
| 169 | + GetName(), |
| 170 | + DiagnosticSeverity.Warning, |
| 171 | + fileName, |
| 172 | + commandName, |
| 173 | + suggestedCorrections: GetCorrectionExtent(commandAst, commdNameWithGetPrefix)); |
| 174 | + return diagnosticRecord; |
147 | 175 | }
|
148 | 176 | }
|
| 177 | + return null; |
149 | 178 | }
|
150 | 179 |
|
151 | 180 | /// <summary>
|
|
0 commit comments