Skip to content

Commit 5485afc

Browse files
authored
Fix NullReferenceException for class type (#1182)
* Fix NullReferenceException for class type * Add regression test * Address PR comments: add comment where the special case can happen and simplify test case to one-liner
1 parent ca95a37 commit 5485afc

File tree

2 files changed

+9
-2
lines changed

2 files changed

+9
-2
lines changed

Engine/Helper.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1093,8 +1093,8 @@ public string GetTypeFromMemberExpressionAst(MemberExpressionAst memberAst, Ast
10931093

10941094
if (details != null && classes != null)
10951095
{
1096-
// Get the class that corresponds to the name of the type (if possible)
1097-
psClass = classes.FirstOrDefault(item => String.Equals(item.Name, details.Type.FullName, StringComparison.OrdinalIgnoreCase));
1096+
// Get the class that corresponds to the name of the type (if possible, the type is not available in the case of a static Singleton)
1097+
psClass = classes.FirstOrDefault(item => String.Equals(item.Name, details.Type?.FullName, StringComparison.OrdinalIgnoreCase));
10981098
}
10991099

11001100
#endif

Tests/Engine/InvokeScriptAnalyzer.tests.ps1

+7
Original file line numberDiff line numberDiff line change
@@ -630,5 +630,12 @@ Describe "Test -EnableExit Switch" {
630630
$warnings.RuleName | Should -Be 'TypeNotFound'
631631
}
632632
}
633+
634+
Describe "Handles static Singleton (issue 1182)" {
635+
It "Does not throw or return diagnostic record" {
636+
$scriptDefinition = 'class T { static [T]$i }; function foo { [CmdletBinding()] param () $script:T.WriteLog() }'
637+
Invoke-ScriptAnalyzer -ScriptDefinition $scriptDefinition -ErrorAction Stop | Should -BeNullOrEmpty
638+
}
639+
}
633640
}
634641
}

0 commit comments

Comments
 (0)