@@ -44,40 +44,51 @@ public IEnumerable<DiagnosticRecord> AnalyzeScript(Ast ast, string fileName)
4444 if ( ast == null ) throw new ArgumentNullException ( Strings . NullAstErrorMessage ) ;
4545
4646 var sbAst = ast as ScriptBlockAst ;
47+
48+ var requiresTransformationAttribute = false ;
49+ var psVersion = Helper . Instance . GetPSVersion ( ) ;
50+ if ( psVersion != null && psVersion . Major < 5 )
51+ {
52+ requiresTransformationAttribute = true ;
53+ }
54+
4755 if ( sbAst != null
4856 && sbAst . ScriptRequirements != null
4957 && sbAst . ScriptRequirements . RequiredPSVersion != null
5058 && sbAst . ScriptRequirements . RequiredPSVersion . Major == 5 )
5159 {
52- yield break ;
60+ if ( requiresTransformationAttribute )
61+ {
62+ yield break ;
63+ }
5364 }
5465
5566 IEnumerable < Ast > funcDefAsts = ast . FindAll ( testAst => testAst is FunctionDefinitionAst , true ) ;
5667 IEnumerable < Ast > scriptBlockAsts = ast . FindAll ( testAst => testAst is ScriptBlockAst , true ) ;
5768
58- string funcName ;
59- IEnumerable < DiagnosticRecord > diagnosticRecords = Enumerable . Empty < DiagnosticRecord > ( ) ;
69+ List < DiagnosticRecord > diagnosticRecords = new List < DiagnosticRecord > ( ) ;
6070
6171 foreach ( FunctionDefinitionAst funcDefAst in funcDefAsts )
6272 {
63- funcName = funcDefAst . Name ;
64-
73+ IEnumerable < ParameterAst > parameterAsts = null ;
6574 if ( funcDefAst . Parameters != null )
6675 {
67- diagnosticRecords . Concat ( GetViolations (
68- funcDefAst . Parameters ,
69- funcDefAst ,
70- string . Format ( CultureInfo . CurrentCulture , Strings . UsePSCredentialTypeError , funcName ) ,
71- fileName ) ) ;
76+ parameterAsts = funcDefAst . Parameters ;
7277 }
7378
74- if ( funcDefAst . Body . ParamBlock != null )
79+ if ( funcDefAst . Body . ParamBlock != null
80+ && funcDefAst . Body . ParamBlock . Parameters != null )
7581 {
76- diagnosticRecords . Concat ( GetViolations (
77- funcDefAst . Body . ParamBlock . Parameters ,
78- funcDefAst ,
79- string . Format ( CultureInfo . CurrentCulture , Strings . UsePSCredentialTypeError , funcName ) ,
80- fileName ) ) ;
82+ parameterAsts = funcDefAst . Body . ParamBlock . Parameters ;
83+ }
84+
85+ if ( parameterAsts != null )
86+ {
87+ diagnosticRecords . AddRange ( GetViolations (
88+ parameterAsts ,
89+ string . Format ( CultureInfo . CurrentCulture , Strings . UsePSCredentialTypeError , funcDefAst . Name ) ,
90+ fileName ,
91+ requiresTransformationAttribute ) ) ;
8192 }
8293 }
8394
@@ -91,11 +102,11 @@ public IEnumerable<DiagnosticRecord> AnalyzeScript(Ast ast, string fileName)
91102
92103 if ( scriptBlockAst . ParamBlock != null && scriptBlockAst . ParamBlock . Parameters != null )
93104 {
94- diagnosticRecords . Concat ( GetViolations (
105+ diagnosticRecords . AddRange ( GetViolations (
95106 scriptBlockAst . ParamBlock . Parameters ,
96- scriptBlockAst ,
97107 string . Format ( CultureInfo . CurrentCulture , Strings . UsePSCredentialTypeErrorSB ) ,
98- fileName ) ) ;
108+ fileName ,
109+ requiresTransformationAttribute ) ) ;
99110 }
100111 }
101112
@@ -107,35 +118,47 @@ public IEnumerable<DiagnosticRecord> AnalyzeScript(Ast ast, string fileName)
107118
108119 private IEnumerable < DiagnosticRecord > GetViolations (
109120 IEnumerable < ParameterAst > parameterAsts ,
110- Ast parentAst ,
111121 string errorMessage ,
112- string fileName )
122+ string fileName ,
123+ bool requiresTransformationAttribute )
113124 {
114125 foreach ( ParameterAst parameter in parameterAsts )
115126 {
116- if ( WrongCredentialUsage ( parameter ) )
127+ if ( WrongCredentialUsage ( parameter , requiresTransformationAttribute ) )
117128 {
118129 yield return new DiagnosticRecord (
119130 errorMessage ,
120- parentAst . Extent ,
131+ parameter . Extent ,
121132 GetName ( ) ,
122133 DiagnosticSeverity . Warning ,
123134 fileName ) ;
124135 }
125136 }
126137 }
127138
128- private bool WrongCredentialUsage ( ParameterAst parameter )
139+ private bool WrongCredentialUsage ( ParameterAst parameter , bool requiresTransformationAttribute )
129140 {
130141 if ( parameter . Name . VariablePath . UserPath . Equals ( "Credential" , StringComparison . OrdinalIgnoreCase ) )
131142 {
132143 var psCredentialType = parameter . Attributes . FirstOrDefault ( paramAttribute => ( paramAttribute . TypeName . IsArray && ( paramAttribute . TypeName as ArrayTypeName ) . ElementType . GetReflectionType ( ) == typeof ( PSCredential ) )
133144 || paramAttribute . TypeName . GetReflectionType ( ) == typeof ( PSCredential ) ) ;
134145
146+ if ( psCredentialType == null )
147+ {
148+ return true ;
149+ }
150+
151+ if ( ! requiresTransformationAttribute && psCredentialType != null )
152+ {
153+ return false ;
154+ }
155+
135156 var credentialAttribute = parameter . Attributes . FirstOrDefault ( paramAttribute => paramAttribute . TypeName . GetReflectionType ( ) == typeof ( CredentialAttribute ) ) ;
136157
137158 // check that both exists and pscredentialtype comes before credential attribute
138- if ( psCredentialType != null && credentialAttribute != null && psCredentialType . Extent . EndOffset <= credentialAttribute . Extent . StartOffset )
159+ if ( psCredentialType != null
160+ && credentialAttribute != null
161+ && psCredentialType . Extent . EndOffset <= credentialAttribute . Extent . StartOffset )
139162 {
140163 return false ;
141164 }
0 commit comments