@@ -44,40 +44,51 @@ public IEnumerable<DiagnosticRecord> AnalyzeScript(Ast ast, string fileName)
44
44
if ( ast == null ) throw new ArgumentNullException ( Strings . NullAstErrorMessage ) ;
45
45
46
46
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
+
47
55
if ( sbAst != null
48
56
&& sbAst . ScriptRequirements != null
49
57
&& sbAst . ScriptRequirements . RequiredPSVersion != null
50
58
&& sbAst . ScriptRequirements . RequiredPSVersion . Major == 5 )
51
59
{
52
- yield break ;
60
+ if ( requiresTransformationAttribute )
61
+ {
62
+ yield break ;
63
+ }
53
64
}
54
65
55
66
IEnumerable < Ast > funcDefAsts = ast . FindAll ( testAst => testAst is FunctionDefinitionAst , true ) ;
56
67
IEnumerable < Ast > scriptBlockAsts = ast . FindAll ( testAst => testAst is ScriptBlockAst , true ) ;
57
68
58
- string funcName ;
59
- IEnumerable < DiagnosticRecord > diagnosticRecords = Enumerable . Empty < DiagnosticRecord > ( ) ;
69
+ List < DiagnosticRecord > diagnosticRecords = new List < DiagnosticRecord > ( ) ;
60
70
61
71
foreach ( FunctionDefinitionAst funcDefAst in funcDefAsts )
62
72
{
63
- funcName = funcDefAst . Name ;
64
-
73
+ IEnumerable < ParameterAst > parameterAsts = null ;
65
74
if ( funcDefAst . Parameters != null )
66
75
{
67
- diagnosticRecords . Concat ( GetViolations (
68
- funcDefAst . Parameters ,
69
- funcDefAst ,
70
- string . Format ( CultureInfo . CurrentCulture , Strings . UsePSCredentialTypeError , funcName ) ,
71
- fileName ) ) ;
76
+ parameterAsts = funcDefAst . Parameters ;
72
77
}
73
78
74
- if ( funcDefAst . Body . ParamBlock != null )
79
+ if ( funcDefAst . Body . ParamBlock != null
80
+ && funcDefAst . Body . ParamBlock . Parameters != null )
75
81
{
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 ) ) ;
81
92
}
82
93
}
83
94
@@ -91,11 +102,11 @@ public IEnumerable<DiagnosticRecord> AnalyzeScript(Ast ast, string fileName)
91
102
92
103
if ( scriptBlockAst . ParamBlock != null && scriptBlockAst . ParamBlock . Parameters != null )
93
104
{
94
- diagnosticRecords . Concat ( GetViolations (
105
+ diagnosticRecords . AddRange ( GetViolations (
95
106
scriptBlockAst . ParamBlock . Parameters ,
96
- scriptBlockAst ,
97
107
string . Format ( CultureInfo . CurrentCulture , Strings . UsePSCredentialTypeErrorSB ) ,
98
- fileName ) ) ;
108
+ fileName ,
109
+ requiresTransformationAttribute ) ) ;
99
110
}
100
111
}
101
112
@@ -107,35 +118,47 @@ public IEnumerable<DiagnosticRecord> AnalyzeScript(Ast ast, string fileName)
107
118
108
119
private IEnumerable < DiagnosticRecord > GetViolations (
109
120
IEnumerable < ParameterAst > parameterAsts ,
110
- Ast parentAst ,
111
121
string errorMessage ,
112
- string fileName )
122
+ string fileName ,
123
+ bool requiresTransformationAttribute )
113
124
{
114
125
foreach ( ParameterAst parameter in parameterAsts )
115
126
{
116
- if ( WrongCredentialUsage ( parameter ) )
127
+ if ( WrongCredentialUsage ( parameter , requiresTransformationAttribute ) )
117
128
{
118
129
yield return new DiagnosticRecord (
119
130
errorMessage ,
120
- parentAst . Extent ,
131
+ parameter . Extent ,
121
132
GetName ( ) ,
122
133
DiagnosticSeverity . Warning ,
123
134
fileName ) ;
124
135
}
125
136
}
126
137
}
127
138
128
- private bool WrongCredentialUsage ( ParameterAst parameter )
139
+ private bool WrongCredentialUsage ( ParameterAst parameter , bool requiresTransformationAttribute )
129
140
{
130
141
if ( parameter . Name . VariablePath . UserPath . Equals ( "Credential" , StringComparison . OrdinalIgnoreCase ) )
131
142
{
132
143
var psCredentialType = parameter . Attributes . FirstOrDefault ( paramAttribute => ( paramAttribute . TypeName . IsArray && ( paramAttribute . TypeName as ArrayTypeName ) . ElementType . GetReflectionType ( ) == typeof ( PSCredential ) )
133
144
|| paramAttribute . TypeName . GetReflectionType ( ) == typeof ( PSCredential ) ) ;
134
145
146
+ if ( psCredentialType == null )
147
+ {
148
+ return true ;
149
+ }
150
+
151
+ if ( ! requiresTransformationAttribute && psCredentialType != null )
152
+ {
153
+ return false ;
154
+ }
155
+
135
156
var credentialAttribute = parameter . Attributes . FirstOrDefault ( paramAttribute => paramAttribute . TypeName . GetReflectionType ( ) == typeof ( CredentialAttribute ) ) ;
136
157
137
158
// 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 )
139
162
{
140
163
return false ;
141
164
}
0 commit comments