Skip to content

Commit 352f744

Browse files
author
Kapil Borle
committed
Get groups of CommandAsts that resemble prop value pairs
1 parent ab31260 commit 352f744

File tree

1 file changed

+61
-1
lines changed

1 file changed

+61
-1
lines changed

Rules/AlignAssignmentStatement.cs

Lines changed: 61 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -145,8 +145,26 @@ public override SourceType GetSourceType()
145145
private IEnumerable<DiagnosticRecord> FindHashtableViolations(TokenOperations tokenOps)
146146
{
147147
var hashtableAsts = tokenOps.Ast.FindAll(ast => ast is HashtableAst, true);
148-
if (hashtableAsts == null)
148+
if (hashtableAsts == null || !hashtableAsts.Any())
149149
{
150+
var configAsts = tokenOps.Ast.FindAll(ast => ast is ConfigurationDefinitionAst, true);
151+
if (configAsts != null)
152+
{
153+
// There are probably parse errors caused by an "Undefined DSC resource"
154+
// which prevents the parser from detecting the property value pairs as
155+
// hashtable. Hence, this is a workaround to format configurations which
156+
// have "Undefined DSC resource" parse errors.
157+
158+
// TODO break down the function and reuse the alignment logic.
159+
160+
// find all commandAsts of the form "prop" "=" "val" that have the same parent
161+
// and format those pairs.
162+
foreach (var configAst in configAsts)
163+
{
164+
var groups = GetCommandElementGroups(configAst);
165+
}
166+
}
167+
150168
yield break;
151169
}
152170

@@ -204,6 +222,48 @@ private IEnumerable<DiagnosticRecord> FindHashtableViolations(TokenOperations to
204222
}
205223
}
206224

225+
private List<List<CommandAst>> GetCommandElementGroups(Ast configAst)
226+
{
227+
var result = new List<List<CommandAst>>();
228+
var parents = new HashSet<Ast>();
229+
var parentChildrenGroup = configAst.FindAll(ast => IsPropertyValueCommandAst(ast), true)?
230+
.Select(ast => ast as CommandAst)
231+
.GroupBy(ast => ast.Parent.Parent); // parent is pipeline and pipeline's parent is namedblockast
232+
if (parentChildrenGroup == null)
233+
{
234+
return result;
235+
}
236+
237+
// var parentChildrenMap = new Dictionary<Ast, List<Ast>>();
238+
// foreach (var commandAst in commandAsts)
239+
// {
240+
// var parent = commandAst.Parent;
241+
// if (parentChildrenMap.ContainsKey(parent))
242+
// {
243+
// parentChildrenMap[parent].Add(commandAst);
244+
// }
245+
// else
246+
// {
247+
// parentChildrenMap.Add(parent, new List<Ast>())
248+
// }
249+
// }
250+
251+
foreach (var group in parentChildrenGroup)
252+
{
253+
result.Add(group.ToList());
254+
}
255+
256+
return result;
257+
}
258+
259+
private bool IsPropertyValueCommandAst(Ast ast)
260+
{
261+
var commandAst = ast as CommandAst;
262+
return commandAst != null
263+
&& commandAst.CommandElements.Count() == 3
264+
&& commandAst.CommandElements[1].Extent.Text.Equals("=");
265+
}
266+
207267
private IEnumerable<CorrectionExtent> GetHashtableCorrections(
208268
Tuple<IScriptExtent, IScriptExtent> extentTuple,
209269
int expectedStartColumnNumber)

0 commit comments

Comments
 (0)