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 2 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
2 changes: 1 addition & 1 deletion Engine/Helper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1094,7 +1094,7 @@ 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));
psClass = classes.FirstOrDefault(item => String.Equals(item.Name, details.Type?.FullName, StringComparison.OrdinalIgnoreCase));
}

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

Describe "Handles static Singleton (issue 1182)" {
$scriptDefinition = @'
enum LogLevel
{
Error
Information
Verbose
Debug
}

class Logger
{
static [Logger] $instance
}

function Write-Log
{
[CmdletBinding()]
param(
[LogLevel] $LogLevel
)

$logger.WriteLog($LogLevel, "Hello")
}
'@
It "Does not throw or return diagnostic record" {
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

}
}
}
}