Skip to content

Commit 797bd61

Browse files
authored
Merge pull request #6039 from BZngr/PRContextExtensionsNRE
2 parents 089ad0c + 6e0834f commit 797bd61

File tree

2 files changed

+443
-7
lines changed

2 files changed

+443
-7
lines changed

Rubberduck.Parsing/ParserRuleContextExtensions.cs

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,11 @@ public static Selection GetSelection(this ParserRuleContext context)
3535
/// </summary>
3636
public static bool Contains(this ParserRuleContext context, ParserRuleContext otherContextInSameParseTree)
3737
{
38+
if (context is null || otherContextInSameParseTree is null)
39+
{
40+
return false;
41+
}
42+
3843
return context.start.TokenIndex <= otherContextInSameParseTree.start.TokenIndex
3944
&& context.stop.TokenIndex >= otherContextInSameParseTree.stop.TokenIndex;
4045
}
@@ -44,7 +49,7 @@ public static bool Contains(this ParserRuleContext context, ParserRuleContext ot
4449
/// </summary>
4550
public static IEnumerable<IToken> GetTokens(this ParserRuleContext context, CommonTokenStream tokenStream)
4651
{
47-
var sourceInterval = context.SourceInterval;
52+
var sourceInterval = context?.SourceInterval ?? Interval.Invalid;
4853
if (sourceInterval.Equals(Interval.Invalid) || sourceInterval.b < sourceInterval.a)
4954
{
5055
return new List<IToken>();
@@ -58,7 +63,7 @@ public static IEnumerable<IToken> GetTokens(this ParserRuleContext context, Comm
5863
public static string GetText(this ParserRuleContext context, ICharStream stream)
5964
{
6065
// Can be null if the input is empty it seems.
61-
if (context.Stop == null)
66+
if (context?.Stop == null)
6267
{
6368
return string.Empty;
6469
}
@@ -86,7 +91,7 @@ public static TContext GetChild<TContext>(this ParserRuleContext context) where
8691
/// <summary>
8792
/// Determines if any of the context's ancestors are the generic Type.
8893
/// </summary>
89-
public static bool IsDescendentOf<TContext>(this ParserRuleContext context)
94+
public static bool IsDescendentOf<TContext>(this ParserRuleContext context) where TContext: ParserRuleContext
9095
{
9196
if (context == null)
9297
{
@@ -129,7 +134,7 @@ private static bool IsDescendentOf_Recursive(IParseTree context, IParseTree targ
129134
/// <summary>
130135
/// Returns the context's first ancestor of the generic Type.
131136
/// </summary>
132-
public static TContext GetAncestor<TContext>(this ParserRuleContext context)
137+
public static TContext GetAncestor<TContext>(this ParserRuleContext context) where TContext: ParserRuleContext
133138
{
134139
switch (context)
135140
{
@@ -145,13 +150,13 @@ public static TContext GetAncestor<TContext>(this ParserRuleContext context)
145150
/// <summary>
146151
/// Tries to return the context's first ancestor of the generic Type.
147152
/// </summary>
148-
public static bool TryGetAncestor<TContext>(this ParserRuleContext context, out TContext ancestor)
153+
public static bool TryGetAncestor<TContext>(this ParserRuleContext context, out TContext ancestor) where TContext : ParserRuleContext
149154
{
150155
ancestor = context.GetAncestor<TContext>();
151156
return ancestor != null;
152157
}
153158

154-
private static TContext GetAncestor_Recursive<TContext>(ParserRuleContext context)
159+
private static TContext GetAncestor_Recursive<TContext>(ParserRuleContext context) where TContext: ParserRuleContext
155160
{
156161
switch (context)
157162
{
@@ -258,7 +263,7 @@ public static bool TryGetChildContext<TContext>(this ParserRuleContext ctxt, out
258263
public static VBAParser.EndOfLineContext GetFirstEndOfLine(this VBAParser.EndOfStatementContext endOfStatement)
259264
{
260265
//This dedicated method exists for performance reasons on hot-paths.
261-
var individualEndOfStatements = endOfStatement.individualNonEOFEndOfStatement();
266+
var individualEndOfStatements = endOfStatement?.individualNonEOFEndOfStatement();
262267

263268
if (individualEndOfStatements == null)
264269
{

0 commit comments

Comments
 (0)