@@ -21,13 +21,17 @@ namespace Microsoft.Windows.PowerShell.ScriptAnalyzer.Commands
2121 /// </summary>
2222 [ Cmdlet ( VerbsLifecycle . Invoke ,
2323 "ScriptAnalyzer" ,
24- DefaultParameterSetName = "File" ,
24+ DefaultParameterSetName = ParameterSet_Path_SuppressedOnly ,
2525 SupportsShouldProcess = true ,
2626 HelpUri = "https://go.microsoft.com/fwlink/?LinkId=525914" ) ]
27- [ OutputType ( typeof ( DiagnosticRecord ) ) ]
28- [ OutputType ( typeof ( SuppressedRecord ) ) ]
27+ [ OutputType ( typeof ( DiagnosticRecord ) , typeof ( SuppressedRecord ) ) ]
2928 public class InvokeScriptAnalyzerCommand : PSCmdlet , IOutputWriter
3029 {
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+
3135 #region Private variables
3236 List < string > processedPaths ;
3337 #endregion // Private variables
@@ -37,7 +41,12 @@ public class InvokeScriptAnalyzerCommand : PSCmdlet, IOutputWriter
3741 /// Path: The path to the file or folder to invoke PSScriptAnalyzer on.
3842 /// </summary>
3943 [ 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 ,
4150 Mandatory = true ,
4251 ValueFromPipeline = true ,
4352 ValueFromPipelineByPropertyName = true ) ]
@@ -54,7 +63,12 @@ public string Path
5463 /// ScriptDefinition: a script definition in the form of a string to run rules on.
5564 /// </summary>
5665 [ 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 ,
5872 Mandatory = true ,
5973 ValueFromPipeline = true ,
6074 ValueFromPipelineByPropertyName = true ) ]
@@ -84,7 +98,6 @@ public string[] CustomRulePath
8498 /// RecurseCustomRulePath: Find rules within subfolders under the path
8599 /// </summary>
86100 [ Parameter ( Mandatory = false ) ]
87- [ SuppressMessage ( "Microsoft.Performance" , "CA1819:PropertiesShouldNotReturnArrays" ) ]
88101 public SwitchParameter RecurseCustomRulePath
89102 {
90103 get { return recurseCustomRulePath ; }
@@ -96,7 +109,6 @@ public SwitchParameter RecurseCustomRulePath
96109 /// IncludeDefaultRules: Invoke default rules along with Custom rules
97110 /// </summary>
98111 [ Parameter ( Mandatory = false ) ]
99- [ SuppressMessage ( "Microsoft.Performance" , "CA1819:PropertiesShouldNotReturnArrays" ) ]
100112 public SwitchParameter IncludeDefaultRules
101113 {
102114 get { return includeDefaultRules ; }
@@ -143,11 +155,15 @@ public string[] Severity
143155 }
144156 private string [ ] severity ;
145157
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+ //
146163 /// <summary>
147164 /// Recurse: Apply to all files within subfolders under the path
148165 /// </summary>
149- [ Parameter ( Mandatory = false ) ]
150- [ SuppressMessage ( "Microsoft.Performance" , "CA1819:PropertiesShouldNotReturnArrays" ) ]
166+ [ Parameter ]
151167 public SwitchParameter Recurse
152168 {
153169 get { return recurse ; }
@@ -158,19 +174,22 @@ public SwitchParameter Recurse
158174 /// <summary>
159175 /// ShowSuppressed: Show the suppressed message
160176 /// </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 ; }
169187
170188 /// <summary>
171189 /// Resolves rule violations automatically where possible.
172190 /// </summary>
173- [ Parameter ( Mandatory = false , ParameterSetName = "File" ) ]
191+ [ Parameter ( Mandatory = false , ParameterSetName = ParameterSet_Path_IncludeSuppressed ) ]
192+ [ Parameter ( Mandatory = false , ParameterSetName = ParameterSet_Path_SuppressedOnly ) ]
174193 public SwitchParameter Fix
175194 {
176195 get { return fix ; }
@@ -334,14 +353,20 @@ protected override void BeginProcessing()
334353 this . settings ) ) ;
335354 }
336355
356+ SuppressionPreference suppressionPreference = SuppressedOnly
357+ ? SuppressionPreference . SuppressedOnly
358+ : IncludeSuppressed
359+ ? SuppressionPreference . Include
360+ : SuppressionPreference . Omit ;
361+
337362 ScriptAnalyzer . Instance . Initialize (
338363 this ,
339364 combRulePaths ,
340365 this . includeRule ,
341366 this . excludeRule ,
342367 this . severity ,
343368 combRulePaths == null || combIncludeDefaultRules ,
344- this . suppressedOnly ) ;
369+ suppressionPreference ) ;
345370 }
346371
347372 /// <summary>
@@ -402,29 +427,32 @@ protected override void StopProcessing()
402427
403428 private void ProcessInput ( )
404429 {
405- IEnumerable < DiagnosticRecord > diagnosticsList = Enumerable . Empty < DiagnosticRecord > ( ) ;
406- if ( IsFileParameterSet ( ) )
430+ WriteToOutput ( RunAnalysis ( ) ) ;
431+ }
432+
433+ private IEnumerable < DiagnosticRecord > RunAnalysis ( )
434+ {
435+ if ( ! IsFileParameterSet ( ) )
407436 {
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 _ ) ;
422438 }
423- else if ( String . Equals ( this . ParameterSetName , "ScriptDefinition" , StringComparison . OrdinalIgnoreCase ) )
439+
440+ var diagnostics = new List < DiagnosticRecord > ( ) ;
441+ foreach ( string path in this . processedPaths )
424442 {
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+ }
427453 }
454+
455+ return diagnostics ;
428456 }
429457
430458 private void WriteToOutput ( IEnumerable < DiagnosticRecord > diagnosticRecords )
@@ -497,10 +525,7 @@ private void ProcessPath()
497525 }
498526 }
499527
500- private bool IsFileParameterSet ( )
501- {
502- return String . Equals ( this . ParameterSetName , "File" , StringComparison . OrdinalIgnoreCase ) ;
503- }
528+ private bool IsFileParameterSet ( ) => Path is not null ;
504529
505530 private bool OverrideSwitchParam ( bool paramValue , string paramName )
506531 {
0 commit comments