|
10 | 10 |
|
11 | 11 | using System; |
12 | 12 | using System.Linq; |
| 13 | +using System.Text; |
13 | 14 | using System.Collections.Generic; |
14 | 15 | #if !CORECLR |
15 | 16 | using System.ComponentModel.Composition; |
16 | 17 | #endif |
17 | 18 | using System.Globalization; |
18 | 19 | using System.Management.Automation.Language; |
19 | 20 | using Microsoft.Windows.PowerShell.ScriptAnalyzer.Generic; |
| 21 | +using Microsoft.Windows.PowerShell.ScriptAnalyzer.Extensions; |
20 | 22 |
|
21 | 23 | namespace Microsoft.Windows.PowerShell.ScriptAnalyzer.BuiltinRules |
22 | 24 | { |
@@ -188,7 +190,8 @@ private List<CorrectionExtent> GetCorrections( |
188 | 190 | // remove the parameter list |
189 | 191 | // and create an equivalent param block |
190 | 192 | // add cmdletbinding attribute and add supportsshouldprocess to it. |
191 | | - correctionExtents.Add(GetCorrectionsExtentRemoveParams(funcDefnAst, ast, tokens)); |
| 193 | + correctionExtents.Add(GetCorrectionExtentRemoveParams(funcDefnAst, ast, tokens)); |
| 194 | + correctionExtents.Add(GetCorrectionExtentAddParamBlock(funcDefnAst, parameterAsts)); |
192 | 195 | } |
193 | 196 |
|
194 | 197 | // sort in descending order of start position |
@@ -222,7 +225,65 @@ private List<CorrectionExtent> GetCorrections( |
222 | 225 | // and then give the corrected extent as suggested correction. |
223 | 226 | } |
224 | 227 |
|
225 | | - private static CorrectionExtent GetCorrectionsExtentRemoveParams( |
| 228 | + private CorrectionExtent GetCorrectionExtentAddParamBlock( |
| 229 | + FunctionDefinitionAst funcDefnAst, |
| 230 | + ParameterAst[] parameterAsts) |
| 231 | + { |
| 232 | + var funcStartScriptPos = funcDefnAst.Extent.StartScriptPosition; |
| 233 | + var paramBlockText = WriteParamBlock(parameterAsts); |
| 234 | + // TODO replace this hard coding |
| 235 | + var indentation = new string(' ', funcStartScriptPos.ColumnNumber + 3); |
| 236 | + var sb = new StringBuilder(); |
| 237 | + sb.Append("{"); |
| 238 | + sb.AppendLine(); |
| 239 | + sb.Append(String.Join( |
| 240 | + Environment.NewLine, |
| 241 | + paramBlockText.GetLines().Select(line => indentation + line))); |
| 242 | + |
| 243 | + return new CorrectionExtent( |
| 244 | + funcDefnAst.Body.Extent.StartLineNumber, |
| 245 | + funcDefnAst.Body.Extent.StartLineNumber, |
| 246 | + funcDefnAst.Body.Extent.StartColumnNumber, |
| 247 | + funcDefnAst.Body.Extent.StartColumnNumber + 1, |
| 248 | + sb.ToString(), |
| 249 | + funcDefnAst.Extent.File); |
| 250 | + } |
| 251 | + |
| 252 | + private string WriteParamBlock(ParameterAst[] parameterAsts) |
| 253 | + { |
| 254 | + var sb = new StringBuilder(); |
| 255 | + sb.AppendLine("[CmdletBinding(SupportsShouldProcess)]"); |
| 256 | + sb.AppendLine("param("); |
| 257 | + // TODO replace this hard coding |
| 258 | + string indentation = new string(' ', 4); |
| 259 | + int count = 0; |
| 260 | + foreach (var paramAst in parameterAsts) |
| 261 | + { |
| 262 | + count++; |
| 263 | + if (IsWhatIf(paramAst) || IsConfirm(paramAst)) |
| 264 | + { |
| 265 | + continue; |
| 266 | + } |
| 267 | + |
| 268 | + foreach (var line in paramAst.Extent.Text.GetLines()) |
| 269 | + { |
| 270 | + sb.Append(indentation); |
| 271 | + sb.Append(line); |
| 272 | + } |
| 273 | + |
| 274 | + if (count != parameterAsts.Length) |
| 275 | + { |
| 276 | + sb.AppendLine(","); |
| 277 | + } |
| 278 | + } |
| 279 | + |
| 280 | + sb.AppendLine(); |
| 281 | + sb.AppendLine(")"); |
| 282 | + return sb.ToString(); |
| 283 | + } |
| 284 | + |
| 285 | + |
| 286 | + private static CorrectionExtent GetCorrectionExtentRemoveParams( |
226 | 287 | FunctionDefinitionAst funcDefnAst, |
227 | 288 | Ast ast, |
228 | 289 | Token[] tokens) |
|
0 commit comments