@@ -56,31 +56,28 @@ public IEnumerable<DiagnosticRecord> AnalyzeScript(Ast ast, string fileName)
5656 IEnumerable < Ast > scriptBlockAsts = ast . FindAll ( testAst => testAst is ScriptBlockAst , true ) ;
5757
5858 string funcName ;
59+ IEnumerable < DiagnosticRecord > diagnosticRecords = Enumerable . Empty < DiagnosticRecord > ( ) ;
5960
6061 foreach ( FunctionDefinitionAst funcDefAst in funcDefAsts )
6162 {
6263 funcName = funcDefAst . Name ;
6364
6465 if ( funcDefAst . Parameters != null )
6566 {
66- foreach ( ParameterAst parameter in funcDefAst . Parameters )
67- {
68- if ( WrongCredentialUsage ( parameter ) )
69- {
70- yield return new DiagnosticRecord ( string . Format ( CultureInfo . CurrentCulture , Strings . UsePSCredentialTypeError , funcName ) , funcDefAst . Extent , GetName ( ) , DiagnosticSeverity . Warning , fileName ) ;
71- }
72- }
67+ diagnosticRecords . Concat ( GetViolations (
68+ funcDefAst . Parameters ,
69+ funcDefAst ,
70+ string . Format ( CultureInfo . CurrentCulture , Strings . UsePSCredentialTypeError , funcName ) ,
71+ fileName ) ) ;
7372 }
7473
7574 if ( funcDefAst . Body . ParamBlock != null )
7675 {
77- foreach ( ParameterAst parameter in funcDefAst . Body . ParamBlock . Parameters )
78- {
79- if ( WrongCredentialUsage ( parameter ) )
80- {
81- yield return new DiagnosticRecord ( string . Format ( CultureInfo . CurrentCulture , Strings . UsePSCredentialTypeError , funcName ) , funcDefAst . Extent , GetName ( ) , DiagnosticSeverity . Warning , fileName ) ;
82- }
83- }
76+ diagnosticRecords . Concat ( GetViolations (
77+ funcDefAst . Body . ParamBlock . Parameters ,
78+ funcDefAst ,
79+ string . Format ( CultureInfo . CurrentCulture , Strings . UsePSCredentialTypeError , funcName ) ,
80+ fileName ) ) ;
8481 }
8582 }
8683
@@ -94,15 +91,38 @@ public IEnumerable<DiagnosticRecord> AnalyzeScript(Ast ast, string fileName)
9491
9592 if ( scriptBlockAst . ParamBlock != null && scriptBlockAst . ParamBlock . Parameters != null )
9693 {
97- foreach ( ParameterAst parameter in scriptBlockAst . ParamBlock . Parameters )
94+ diagnosticRecords . Concat ( GetViolations (
95+ scriptBlockAst . ParamBlock . Parameters ,
96+ scriptBlockAst ,
97+ string . Format ( CultureInfo . CurrentCulture , Strings . UsePSCredentialTypeErrorSB ) ,
98+ fileName ) ) ;
99+ }
100+ }
101+
102+ foreach ( var dr in diagnosticRecords )
103+ {
104+ yield return dr ;
105+ }
106+ }
107+
108+ private IEnumerable < DiagnosticRecord > GetViolations (
109+ IEnumerable < ParameterAst > parameterAsts ,
110+ Ast parentAst ,
111+ string errorMessage ,
112+ string fileName )
113+ {
114+ foreach ( ParameterAst parameter in parameterAsts )
115+ {
116+ if ( WrongCredentialUsage ( parameter ) )
98117 {
99- if ( WrongCredentialUsage ( parameter ) )
100- {
101- yield return new DiagnosticRecord ( string . Format ( CultureInfo . CurrentCulture , Strings . UsePSCredentialTypeErrorSB ) , scriptBlockAst . Extent , GetName ( ) , DiagnosticSeverity . Warning , fileName ) ;
102- }
118+ yield return new DiagnosticRecord (
119+ errorMessage ,
120+ parentAst . Extent ,
121+ GetName ( ) ,
122+ DiagnosticSeverity . Warning ,
123+ fileName ) ;
103124 }
104125 }
105- }
106126 }
107127
108128 private bool WrongCredentialUsage ( ParameterAst parameter )
0 commit comments