Skip to content

Commit

Permalink
Fix NullReferenceException for class type (#1182)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
bergmeister authored Apr 15, 2019
1 parent ca95a37 commit 5485afc
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 2 deletions.
4 changes: 2 additions & 2 deletions Engine/Helper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1093,8 +1093,8 @@ public string GetTypeFromMemberExpressionAst(MemberExpressionAst memberAst, Ast

if (details != null && classes != null)
{
// Get the class that corresponds to the name of the type (if possible)
psClass = classes.FirstOrDefault(item => String.Equals(item.Name, details.Type.FullName, StringComparison.OrdinalIgnoreCase));
// 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)
psClass = classes.FirstOrDefault(item => String.Equals(item.Name, details.Type?.FullName, StringComparison.OrdinalIgnoreCase));
}

#endif
Expand Down
7 changes: 7 additions & 0 deletions Tests/Engine/InvokeScriptAnalyzer.tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -630,5 +630,12 @@ Describe "Test -EnableExit Switch" {
$warnings.RuleName | Should -Be 'TypeNotFound'
}
}

Describe "Handles static Singleton (issue 1182)" {
It "Does not throw or return diagnostic record" {
$scriptDefinition = 'class T { static [T]$i }; function foo { [CmdletBinding()] param () $script:T.WriteLog() }'
Invoke-ScriptAnalyzer -ScriptDefinition $scriptDefinition -ErrorAction Stop | Should -BeNullOrEmpty
}
}
}
}

0 comments on commit 5485afc

Please sign in to comment.