From 7ef554beacc405399976ac854f1b22478b6b9e28 Mon Sep 17 00:00:00 2001 From: "Christoph Bergmeister (MVP)" Date: Fri, 22 Mar 2019 02:07:22 +0000 Subject: [PATCH 1/3] Fix NullReferenceException for class type --- Engine/Helper.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Engine/Helper.cs b/Engine/Helper.cs index a3009a7ef..0ba4ee408 100644 --- a/Engine/Helper.cs +++ b/Engine/Helper.cs @@ -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 From a32f27deaa887142baef0746d7e609a8605f5be4 Mon Sep 17 00:00:00 2001 From: "Christoph Bergmeister (MVP)" Date: Fri, 22 Mar 2019 02:37:30 +0000 Subject: [PATCH 2/3] Add regression test --- Tests/Engine/InvokeScriptAnalyzer.tests.ps1 | 30 +++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/Tests/Engine/InvokeScriptAnalyzer.tests.ps1 b/Tests/Engine/InvokeScriptAnalyzer.tests.ps1 index ed71a57d2..a1070ba1f 100644 --- a/Tests/Engine/InvokeScriptAnalyzer.tests.ps1 +++ b/Tests/Engine/InvokeScriptAnalyzer.tests.ps1 @@ -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 + } + } } } From aa1d78e1960c197006a2cd7c5b74529d31f3f23c Mon Sep 17 00:00:00 2001 From: "Christoph Bergmeister (MVP)" Date: Fri, 22 Mar 2019 22:16:39 +0000 Subject: [PATCH 3/3] Address PR comments: add comment where the special case can happen and simplify test case to one-liner --- Engine/Helper.cs | 2 +- Tests/Engine/InvokeScriptAnalyzer.tests.ps1 | 25 +-------------------- 2 files changed, 2 insertions(+), 25 deletions(-) diff --git a/Engine/Helper.cs b/Engine/Helper.cs index 0ba4ee408..131ab6b77 100644 --- a/Engine/Helper.cs +++ b/Engine/Helper.cs @@ -1093,7 +1093,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) + // 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)); } diff --git a/Tests/Engine/InvokeScriptAnalyzer.tests.ps1 b/Tests/Engine/InvokeScriptAnalyzer.tests.ps1 index a1070ba1f..158af4603 100644 --- a/Tests/Engine/InvokeScriptAnalyzer.tests.ps1 +++ b/Tests/Engine/InvokeScriptAnalyzer.tests.ps1 @@ -632,31 +632,8 @@ Describe "Test -EnableExit Switch" { } 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" { + $scriptDefinition = 'class T { static [T]$i }; function foo { [CmdletBinding()] param () $script:T.WriteLog() }' Invoke-ScriptAnalyzer -ScriptDefinition $scriptDefinition -ErrorAction Stop | Should -BeNullOrEmpty } }