Skip to content

Fix NullReferenceException for class type #1182

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i think this should probably be { Invoke-ScriptAnalyzer -ScriptDefinition $scriptDefinition -ErrorAction Stop } | Should -Not Throw

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

-ErrorAction Stop already takes care of making the test throw. Jacub who maintains Pester has said that there is no purpose for using Should -Not Throw because Pester will fail the test if the error is terminating (hence the -ErrorAction Stop

}
}
}
}