Skip to content

Commit be2dedd

Browse files
author
Kapil Borle
committed
Handle DSC configurations with parse errors
1 parent d4bc9ef commit be2dedd

File tree

1 file changed

+21
-22
lines changed

1 file changed

+21
-22
lines changed

Rules/AlignAssignmentStatement.cs

Lines changed: 21 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -146,29 +146,31 @@ private IEnumerable<DiagnosticRecord> FindHashtableViolations(TokenOperations to
146146
{
147147
var hashtableAsts = tokenOps.Ast.FindAll(ast => ast is HashtableAst, true);
148148
var groups = new List<List<Tuple<IScriptExtent, IScriptExtent>>>();
149-
if (hashtableAsts == null || !hashtableAsts.Any())
149+
if (hashtableAsts != null)
150150
{
151-
var configAsts = tokenOps.Ast.FindAll(ast => ast is ConfigurationDefinitionAst, true);
152-
if (configAsts != null)
151+
foreach (var astItem in hashtableAsts)
153152
{
154-
// There are probably parse errors caused by an "Undefined DSC resource"
155-
// which prevents the parser from detecting the property value pairs as
156-
// hashtable. Hence, this is a workaround to format configurations which
157-
// have "Undefined DSC resource" parse errors.
158-
159-
// TODO break down the function and reuse the alignment logic.
160-
161-
// find all commandAsts of the form "prop" "=" "val" that have the same parent
162-
// and format those pairs.
163-
foreach (var configAst in configAsts)
164-
{
165-
groups.AddRange(GetCommandElementExtentGroups(configAst));
166-
}
153+
groups.Add(GetExtents(tokenOps, (HashtableAst)astItem));
167154
}
155+
}
168156

169-
yield break;
157+
var configAsts = tokenOps.Ast.FindAll(ast => ast is ConfigurationDefinitionAst, true);
158+
if (configAsts != null)
159+
{
160+
// There are probably parse errors caused by an "Undefined DSC resource"
161+
// which prevents the parser from detecting the property value pairs as
162+
// hashtable. Hence, this is a workaround to format configurations which
163+
// have "Undefined DSC resource" parse errors.
164+
165+
// find all commandAsts of the form "prop" "=" "val" that have the same parent
166+
// and format those pairs.
167+
foreach (var configAst in configAsts)
168+
{
169+
groups.AddRange(GetCommandElementExtentGroups(configAst));
170+
}
170171
}
171172

173+
172174
// it is probably much easier have a hashtable writer that formats the hashtable and writes it
173175
// but it makes handling comments hard. So we need to use this approach.
174176

@@ -183,11 +185,8 @@ private IEnumerable<DiagnosticRecord> FindHashtableViolations(TokenOperations to
183185
// make sure all the assignment operators are in the same column as that of the longest left hand.
184186

185187

186-
foreach (var astItem in hashtableAsts)
188+
foreach (var extentTuples in groups)
187189
{
188-
var hashtableAst = (HashtableAst)astItem;
189-
var extentTuples = GetExtents(tokenOps, hashtableAst);
190-
191190
if (!HasPropertiesOnSeparateLines(extentTuples))
192191
{
193192
continue;
@@ -294,7 +293,7 @@ private string GetError()
294293
return String.Format(CultureInfo.CurrentCulture, Strings.AlignAssignmentStatementError);
295294
}
296295

297-
private static IList<Tuple<IScriptExtent, IScriptExtent>> GetExtents(
296+
private static List<Tuple<IScriptExtent, IScriptExtent>> GetExtents(
298297
TokenOperations tokenOps,
299298
HashtableAst hashtableAst)
300299
{

0 commit comments

Comments
 (0)