Skip to content

Commit 48f308b

Browse files
JustinGroteCopilot
andauthored
Normalize functiondefinitionxt to nullable
Co-authored-by: Copilot <[email protected]>
1 parent 664b264 commit 48f308b

1 file changed

Lines changed: 27 additions & 9 deletions

File tree

src/PowerShellEditorServices/Utility/AstExtensions.cs

Lines changed: 27 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -295,23 +295,41 @@ public static IEnumerable<Ast> FindParents(this Ast ast, params Type[] types)
295295
{
296296
FunctionDefinitionAst? funcDef = scope.FindAll
297297
(
298-
ast => ast is FunctionDefinitionAst funcDef
299-
&& funcDef.StartsBefore(target)
300-
&& funcDef.Name.Equals(functionName, StringComparison.CurrentCultureIgnoreCase)
301-
&& (funcDef.Parameters ?? funcDef.Body.ParamBlock.Parameters)
302-
.SingleOrDefault(
303-
param => param.Name.GetUnqualifiedName().Equals(parameterName, StringComparison.CurrentCultureIgnoreCase)
304-
) is not null
298+
ast =>
299+
{
300+
if (ast is not FunctionDefinitionAst funcDef
301+
|| !funcDef.StartsBefore(target)
302+
|| !funcDef.Name.Equals(functionName, StringComparison.CurrentCultureIgnoreCase))
303+
{
304+
return false;
305+
}
306+
307+
IReadOnlyList<ParameterAst>? parameters = funcDef.Parameters ?? funcDef.Body?.ParamBlock?.Parameters;
308+
if (parameters is null || parameters.Count == 0)
309+
{
310+
return false;
311+
}
312+
313+
return parameters.SingleOrDefault(
314+
param => param.Name.GetUnqualifiedName().Equals(parameterName, StringComparison.CurrentCultureIgnoreCase)
315+
) is not null;
316+
}
305317
, false
306318
).LastOrDefault() as FunctionDefinitionAst;
307319

308320
if (funcDef is not null)
309321
{
310-
return (funcDef.Parameters ?? funcDef.Body.ParamBlock.Parameters)
322+
IReadOnlyList<ParameterAst>? parameters = funcDef.Parameters ?? funcDef.Body?.ParamBlock?.Parameters;
323+
if (parameters is null || parameters.Count == 0)
324+
{
325+
return null;
326+
}
327+
328+
return parameters
311329
.SingleOrDefault
312330
(
313331
param => param.Name.GetUnqualifiedName().Equals(parameterName, StringComparison.CurrentCultureIgnoreCase)
314-
)?.Name; //Should not be null at this point
332+
)?.Name; // Should not be null at this point
315333
}
316334

317335
scope = scope.GetScopeBoundary();

0 commit comments

Comments
 (0)