@@ -21,13 +21,17 @@ namespace Microsoft.Windows.PowerShell.ScriptAnalyzer.Commands
21
21
/// </summary>
22
22
[ Cmdlet ( VerbsLifecycle . Invoke ,
23
23
"ScriptAnalyzer" ,
24
- DefaultParameterSetName = "File" ,
24
+ DefaultParameterSetName = ParameterSet_Path_SuppressedOnly ,
25
25
SupportsShouldProcess = true ,
26
26
HelpUri = "https://go.microsoft.com/fwlink/?LinkId=525914" ) ]
27
- [ OutputType ( typeof ( DiagnosticRecord ) ) ]
28
- [ OutputType ( typeof ( SuppressedRecord ) ) ]
27
+ [ OutputType ( typeof ( DiagnosticRecord ) , typeof ( SuppressedRecord ) ) ]
29
28
public class InvokeScriptAnalyzerCommand : PSCmdlet , IOutputWriter
30
29
{
30
+ private const string ParameterSet_Path_SuppressedOnly = nameof ( Path ) + "_" + nameof ( SuppressedOnly ) ;
31
+ private const string ParameterSet_Path_IncludeSuppressed = nameof ( Path ) + "_" + nameof ( IncludeSuppressed ) ;
32
+ private const string ParameterSet_ScriptDefinition_SuppressedOnly = nameof ( ScriptDefinition ) + "_" + nameof ( SuppressedOnly ) ;
33
+ private const string ParameterSet_ScriptDefinition_IncludeSuppressed = nameof ( ScriptDefinition ) + "_" + nameof ( IncludeSuppressed ) ;
34
+
31
35
#region Private variables
32
36
List < string > processedPaths ;
33
37
#endregion // Private variables
@@ -37,7 +41,12 @@ public class InvokeScriptAnalyzerCommand : PSCmdlet, IOutputWriter
37
41
/// Path: The path to the file or folder to invoke PSScriptAnalyzer on.
38
42
/// </summary>
39
43
[ Parameter ( Position = 0 ,
40
- ParameterSetName = "File" ,
44
+ ParameterSetName = ParameterSet_Path_IncludeSuppressed ,
45
+ Mandatory = true ,
46
+ ValueFromPipeline = true ,
47
+ ValueFromPipelineByPropertyName = true ) ]
48
+ [ Parameter ( Position = 0 ,
49
+ ParameterSetName = ParameterSet_Path_SuppressedOnly ,
41
50
Mandatory = true ,
42
51
ValueFromPipeline = true ,
43
52
ValueFromPipelineByPropertyName = true ) ]
@@ -54,7 +63,12 @@ public string Path
54
63
/// ScriptDefinition: a script definition in the form of a string to run rules on.
55
64
/// </summary>
56
65
[ Parameter ( Position = 0 ,
57
- ParameterSetName = "ScriptDefinition" ,
66
+ ParameterSetName = ParameterSet_ScriptDefinition_IncludeSuppressed ,
67
+ Mandatory = true ,
68
+ ValueFromPipeline = true ,
69
+ ValueFromPipelineByPropertyName = true ) ]
70
+ [ Parameter ( Position = 0 ,
71
+ ParameterSetName = ParameterSet_ScriptDefinition_SuppressedOnly ,
58
72
Mandatory = true ,
59
73
ValueFromPipeline = true ,
60
74
ValueFromPipelineByPropertyName = true ) ]
@@ -84,7 +98,6 @@ public string[] CustomRulePath
84
98
/// RecurseCustomRulePath: Find rules within subfolders under the path
85
99
/// </summary>
86
100
[ Parameter ( Mandatory = false ) ]
87
- [ SuppressMessage ( "Microsoft.Performance" , "CA1819:PropertiesShouldNotReturnArrays" ) ]
88
101
public SwitchParameter RecurseCustomRulePath
89
102
{
90
103
get { return recurseCustomRulePath ; }
@@ -96,7 +109,6 @@ public SwitchParameter RecurseCustomRulePath
96
109
/// IncludeDefaultRules: Invoke default rules along with Custom rules
97
110
/// </summary>
98
111
[ Parameter ( Mandatory = false ) ]
99
- [ SuppressMessage ( "Microsoft.Performance" , "CA1819:PropertiesShouldNotReturnArrays" ) ]
100
112
public SwitchParameter IncludeDefaultRules
101
113
{
102
114
get { return includeDefaultRules ; }
@@ -143,11 +155,15 @@ public string[] Severity
143
155
}
144
156
private string [ ] severity ;
145
157
158
+ // TODO: This should be only in the Path parameter sets, and is ignored otherwise,
159
+ // but we already have a test that depends on it being otherwise
160
+ //[Parameter(ParameterSetName = ParameterSet_Path_IncludeSuppressed)]
161
+ //[Parameter(ParameterSetName = ParameterSet_Path_SuppressedOnly)]
162
+ //
146
163
/// <summary>
147
164
/// Recurse: Apply to all files within subfolders under the path
148
165
/// </summary>
149
- [ Parameter ( Mandatory = false ) ]
150
- [ SuppressMessage ( "Microsoft.Performance" , "CA1819:PropertiesShouldNotReturnArrays" ) ]
166
+ [ Parameter ]
151
167
public SwitchParameter Recurse
152
168
{
153
169
get { return recurse ; }
@@ -158,19 +174,22 @@ public SwitchParameter Recurse
158
174
/// <summary>
159
175
/// ShowSuppressed: Show the suppressed message
160
176
/// </summary>
161
- [ Parameter ( Mandatory = false ) ]
162
- [ SuppressMessage ( "Microsoft.Performance" , "CA1819:PropertiesShouldNotReturnArrays" ) ]
163
- public SwitchParameter SuppressedOnly
164
- {
165
- get { return suppressedOnly ; }
166
- set { suppressedOnly = value ; }
167
- }
168
- private bool suppressedOnly ;
177
+ [ Parameter ( ParameterSetName = ParameterSet_Path_SuppressedOnly ) ]
178
+ [ Parameter ( ParameterSetName = ParameterSet_ScriptDefinition_SuppressedOnly ) ]
179
+ public SwitchParameter SuppressedOnly { get ; set ; }
180
+
181
+ /// <summary>
182
+ /// Include suppressed diagnostics in the output.
183
+ /// </summary>
184
+ [ Parameter ( ParameterSetName = ParameterSet_Path_IncludeSuppressed , Mandatory = true ) ]
185
+ [ Parameter ( ParameterSetName = ParameterSet_ScriptDefinition_IncludeSuppressed , Mandatory = true ) ]
186
+ public SwitchParameter IncludeSuppressed { get ; set ; }
169
187
170
188
/// <summary>
171
189
/// Resolves rule violations automatically where possible.
172
190
/// </summary>
173
- [ Parameter ( Mandatory = false , ParameterSetName = "File" ) ]
191
+ [ Parameter ( Mandatory = false , ParameterSetName = ParameterSet_Path_IncludeSuppressed ) ]
192
+ [ Parameter ( Mandatory = false , ParameterSetName = ParameterSet_Path_SuppressedOnly ) ]
174
193
public SwitchParameter Fix
175
194
{
176
195
get { return fix ; }
@@ -334,14 +353,20 @@ protected override void BeginProcessing()
334
353
this . settings ) ) ;
335
354
}
336
355
356
+ SuppressionPreference suppressionPreference = SuppressedOnly
357
+ ? SuppressionPreference . SuppressedOnly
358
+ : IncludeSuppressed
359
+ ? SuppressionPreference . Include
360
+ : SuppressionPreference . Omit ;
361
+
337
362
ScriptAnalyzer . Instance . Initialize (
338
363
this ,
339
364
combRulePaths ,
340
365
this . includeRule ,
341
366
this . excludeRule ,
342
367
this . severity ,
343
368
combRulePaths == null || combIncludeDefaultRules ,
344
- this . suppressedOnly ) ;
369
+ suppressionPreference ) ;
345
370
}
346
371
347
372
/// <summary>
@@ -402,29 +427,32 @@ protected override void StopProcessing()
402
427
403
428
private void ProcessInput ( )
404
429
{
405
- IEnumerable < DiagnosticRecord > diagnosticsList = Enumerable . Empty < DiagnosticRecord > ( ) ;
406
- if ( IsFileParameterSet ( ) )
430
+ WriteToOutput ( RunAnalysis ( ) ) ;
431
+ }
432
+
433
+ private IEnumerable < DiagnosticRecord > RunAnalysis ( )
434
+ {
435
+ if ( ! IsFileParameterSet ( ) )
407
436
{
408
- foreach ( var p in processedPaths )
409
- {
410
- if ( fix )
411
- {
412
- ShouldProcess ( p , $ "Analyzing and fixing path with Recurse={ this . recurse } ") ;
413
- diagnosticsList = ScriptAnalyzer . Instance . AnalyzeAndFixPath ( p , this . ShouldProcess , this . recurse ) ;
414
- }
415
- else
416
- {
417
- ShouldProcess ( p , $ "Analyzing path with Recurse={ this . recurse } ") ;
418
- diagnosticsList = ScriptAnalyzer . Instance . AnalyzePath ( p , this . ShouldProcess , this . recurse ) ;
419
- }
420
- WriteToOutput ( diagnosticsList ) ;
421
- }
437
+ return ScriptAnalyzer . Instance . AnalyzeScriptDefinition ( scriptDefinition , out _ , out _ ) ;
422
438
}
423
- else if ( String . Equals ( this . ParameterSetName , "ScriptDefinition" , StringComparison . OrdinalIgnoreCase ) )
439
+
440
+ var diagnostics = new List < DiagnosticRecord > ( ) ;
441
+ foreach ( string path in this . processedPaths )
424
442
{
425
- diagnosticsList = ScriptAnalyzer . Instance . AnalyzeScriptDefinition ( scriptDefinition , out _ , out _ ) ;
426
- WriteToOutput ( diagnosticsList ) ;
443
+ if ( fix )
444
+ {
445
+ ShouldProcess ( path , $ "Analyzing and fixing path with Recurse={ this . recurse } ") ;
446
+ diagnostics . AddRange ( ScriptAnalyzer . Instance . AnalyzeAndFixPath ( path , this . ShouldProcess , this . recurse ) ) ;
447
+ }
448
+ else
449
+ {
450
+ ShouldProcess ( path , $ "Analyzing path with Recurse={ this . recurse } ") ;
451
+ diagnostics . AddRange ( ScriptAnalyzer . Instance . AnalyzePath ( path , this . ShouldProcess , this . recurse ) ) ;
452
+ }
427
453
}
454
+
455
+ return diagnostics ;
428
456
}
429
457
430
458
private void WriteToOutput ( IEnumerable < DiagnosticRecord > diagnosticRecords )
@@ -497,10 +525,7 @@ private void ProcessPath()
497
525
}
498
526
}
499
527
500
- private bool IsFileParameterSet ( )
501
- {
502
- return String . Equals ( this . ParameterSetName , "File" , StringComparison . OrdinalIgnoreCase ) ;
503
- }
528
+ private bool IsFileParameterSet ( ) => Path is not null ;
504
529
505
530
private bool OverrideSwitchParam ( bool paramValue , string paramName )
506
531
{
0 commit comments