@@ -145,11 +145,33 @@ public override SourceType GetSourceType()
145
145
private IEnumerable < DiagnosticRecord > FindHashtableViolations ( TokenOperations tokenOps )
146
146
{
147
147
var hashtableAsts = tokenOps . Ast . FindAll ( ast => ast is HashtableAst , true ) ;
148
- if ( hashtableAsts == null )
148
+ var groups = new List < List < Tuple < IScriptExtent , IScriptExtent > > > ( ) ;
149
+ if ( hashtableAsts != null )
149
150
{
150
- yield break ;
151
+ foreach ( var astItem in hashtableAsts )
152
+ {
153
+ groups . Add ( GetExtents ( tokenOps , ( HashtableAst ) astItem ) ) ;
154
+ }
151
155
}
152
156
157
+ #if ! PSV3
158
+ var configAsts = tokenOps . Ast . FindAll ( ast => ast is ConfigurationDefinitionAst , true ) ;
159
+ if ( configAsts != null )
160
+ {
161
+ // There are probably parse errors caused by an "Undefined DSC resource"
162
+ // which prevents the parser from detecting the property value pairs as
163
+ // hashtable. Hence, this is a workaround to format configurations which
164
+ // have "Undefined DSC resource" parse errors.
165
+
166
+ // find all commandAsts of the form "prop" "=" "val" that have the same parent
167
+ // and format those pairs.
168
+ foreach ( var configAst in configAsts )
169
+ {
170
+ groups . AddRange ( GetCommandElementExtentGroups ( configAst ) ) ;
171
+ }
172
+ }
173
+ #endif
174
+
153
175
// it is probably much easier have a hashtable writer that formats the hashtable and writes it
154
176
// but it makes handling comments hard. So we need to use this approach.
155
177
@@ -162,16 +184,13 @@ private IEnumerable<DiagnosticRecord> FindHashtableViolations(TokenOperations to
162
184
// find the distance between the assignment operators and their corresponding LHS
163
185
// find the longest left expression
164
186
// make sure all the assignment operators are in the same column as that of the longest left hand.
165
-
166
- foreach ( var astItem in hashtableAsts )
187
+ foreach ( var extentTuples in groups )
167
188
{
168
- var hashtableAst = ( HashtableAst ) astItem ;
169
- if ( ! HasKeysOnSeparateLines ( hashtableAst ) )
189
+ if ( ! HasPropertiesOnSeparateLines ( extentTuples ) )
170
190
{
171
191
continue ;
172
192
}
173
193
174
- var extentTuples = GetExtents ( tokenOps , hashtableAst ) ;
175
194
if ( extentTuples == null
176
195
|| extentTuples . Count == 0
177
196
|| ! extentTuples . All ( t => t . Item1 . StartLineNumber == t . Item2 . EndLineNumber ) )
@@ -181,11 +200,12 @@ private IEnumerable<DiagnosticRecord> FindHashtableViolations(TokenOperations to
181
200
182
201
var widestKeyExtent = extentTuples
183
202
. Select ( t => t . Item1 )
184
- . Aggregate ( ( t1 , tAggregate ) => {
185
- return TokenOperations . GetExtentWidth ( tAggregate ) > TokenOperations . GetExtentWidth ( t1 )
186
- ? tAggregate
187
- : t1 ;
188
- } ) ;
203
+ . Aggregate ( ( t1 , tAggregate ) =>
204
+ {
205
+ return TokenOperations . GetExtentWidth ( tAggregate ) > TokenOperations . GetExtentWidth ( t1 )
206
+ ? tAggregate
207
+ : t1 ;
208
+ } ) ;
189
209
var expectedStartColumnNumber = widestKeyExtent . EndColumnNumber + 1 ;
190
210
foreach ( var extentTuple in extentTuples )
191
211
{
@@ -204,6 +224,53 @@ private IEnumerable<DiagnosticRecord> FindHashtableViolations(TokenOperations to
204
224
}
205
225
}
206
226
227
+ private List < List < Tuple < IScriptExtent , IScriptExtent > > > GetCommandElementExtentGroups ( Ast configAst )
228
+ {
229
+ var result = new List < List < Tuple < IScriptExtent , IScriptExtent > > > ( ) ;
230
+ var commandAstGroups = GetCommandElementGroups ( configAst ) ;
231
+ foreach ( var commandAstGroup in commandAstGroups )
232
+ {
233
+ var list = new List < Tuple < IScriptExtent , IScriptExtent > > ( ) ;
234
+ foreach ( var commandAst in commandAstGroup )
235
+ {
236
+ var elems = commandAst . CommandElements ;
237
+ list . Add ( new Tuple < IScriptExtent , IScriptExtent > ( elems [ 0 ] . Extent , elems [ 1 ] . Extent ) ) ;
238
+ }
239
+
240
+ result . Add ( list ) ;
241
+ }
242
+
243
+ return result ;
244
+ }
245
+
246
+ private List < List < CommandAst > > GetCommandElementGroups ( Ast configAst )
247
+ {
248
+ var result = new List < List < CommandAst > > ( ) ;
249
+ var astsFound = configAst . FindAll ( ast => IsPropertyValueCommandAst ( ast ) , true ) ;
250
+ if ( astsFound == null )
251
+ {
252
+ return result ;
253
+ }
254
+
255
+ var parentChildrenGroup = from ast in astsFound
256
+ select ( CommandAst ) ast into commandAst
257
+ group commandAst by commandAst . Parent . Parent ; // parent is pipeline and pipeline's parent is namedblockast
258
+ foreach ( var group in parentChildrenGroup )
259
+ {
260
+ result . Add ( group . ToList ( ) ) ;
261
+ }
262
+
263
+ return result ;
264
+ }
265
+
266
+ private bool IsPropertyValueCommandAst ( Ast ast )
267
+ {
268
+ var commandAst = ast as CommandAst ;
269
+ return commandAst != null
270
+ && commandAst . CommandElements . Count ( ) == 3
271
+ && commandAst . CommandElements [ 1 ] . Extent . Text . Equals ( "=" ) ;
272
+ }
273
+
207
274
private IEnumerable < CorrectionExtent > GetHashtableCorrections (
208
275
Tuple < IScriptExtent , IScriptExtent > extentTuple ,
209
276
int expectedStartColumnNumber )
@@ -225,7 +292,7 @@ private string GetError()
225
292
return String . Format ( CultureInfo . CurrentCulture , Strings . AlignAssignmentStatementError ) ;
226
293
}
227
294
228
- private static IList < Tuple < IScriptExtent , IScriptExtent > > GetExtents (
295
+ private static List < Tuple < IScriptExtent , IScriptExtent > > GetExtents (
229
296
TokenOperations tokenOps ,
230
297
HashtableAst hashtableAst )
231
298
{
@@ -250,18 +317,18 @@ private static IList<Tuple<IScriptExtent, IScriptExtent>> GetExtents(
250
317
return nodeTuples ;
251
318
}
252
319
253
- private bool HasKeysOnSeparateLines ( HashtableAst hashtableAst )
320
+ private bool HasPropertiesOnSeparateLines ( IEnumerable < Tuple < IScriptExtent , IScriptExtent > > tuples )
254
321
{
255
322
var lines = new HashSet < int > ( ) ;
256
- foreach ( var kvp in hashtableAst . KeyValuePairs )
323
+ foreach ( var kvp in tuples )
257
324
{
258
- if ( lines . Contains ( kvp . Item1 . Extent . StartLineNumber ) )
325
+ if ( lines . Contains ( kvp . Item1 . StartLineNumber ) )
259
326
{
260
327
return false ;
261
328
}
262
329
else
263
330
{
264
- lines . Add ( kvp . Item1 . Extent . StartLineNumber ) ;
331
+ lines . Add ( kvp . Item1 . StartLineNumber ) ;
265
332
}
266
333
}
267
334
0 commit comments