Skip to content

Invoke-Formatter PSAlignAssignmentStatement Rule errors when a Property Name is specified as an Object Property #1003

Closed
@X-Guardian

Description

@X-Guardian

If Invoke-Formatter with the PSAlignAssignmentStatementRule enabled is run against a hashtable that contains a key name specified as an object.property, a NullReferenceException error occurs.

Steps to reproduce

$scriptDefinition = @'
$Hashtable = @{
KeyName = 'test1'
$Object.Property = 'test2'
}
'@

$settings = @{
    IncludeRules = @("PSAlignAssignmentStatement")
    Rules = @{
        PSAlignAssignmentStatement = @{
            Enable = $true
        }
    }
}

Invoke-Formatter -ScriptDefinition $scriptDefinition -Settings $settings

Expected behavior

Return the hashtable with the assignment statements aligned:

$Hashtable = @{
KeyName          = 'test1'
$Object.Property = 'test2'
}

Actual behavior

Invoke-Formatter : Object reference not set to an instance of an object.
At Test-Formatter.ps1:17 char:1
+ Invoke-Formatter -ScriptDefinition $scriptDefinition -Settings $setti ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (:) [Invoke-Formatter], NullReferenceException
    + FullyQualifiedErrorId : RULE_ERROR,Microsoft.Windows.PowerShell.ScriptAnalyzer.Commands.InvokeFormatterCommand

$Hashtable = @{
KeyName = 'test1'
$Object.Property = 'test2'
}

Error Object

$error[0] | Select-Object *
writeErrorStream      : True
PSMessageDetails      :
Exception             : System.NullReferenceException: Object reference not set to an instance of an object.
                           at Microsoft.Windows.PowerShell.ScriptAnalyzer.BuiltinRules.AlignAssignmentStatement.HasPropertie
                        sOnSeparateLines(IEnumerable`1 tuples)
                           at Microsoft.Windows.PowerShell.ScriptAnalyzer.BuiltinRules.AlignAssignmentStatement.<FindHashtab
                        leViolations>d__15.MoveNext()
                           at Microsoft.Windows.PowerShell.ScriptAnalyzer.BuiltinRules.AlignAssignmentStatement.<AnalyzeScri
                        pt>d__7.MoveNext()
                           at System.Collections.Generic.List`1..ctor(IEnumerable`1 collection)
                           at System.Linq.Enumerable.ToList[TSource](IEnumerable`1 source)
                           at Microsoft.Windows.PowerShell.ScriptAnalyzer.ScriptAnalyzer.<>c__DisplayClass78_0.<AnalyzeSynta
                        xTree>b__1()
TargetObject          :
CategoryInfo          : InvalidOperation: (:) [Invoke-Formatter], NullReferenceException
FullyQualifiedErrorId : RULE_ERROR,Microsoft.Windows.PowerShell.ScriptAnalyzer.Commands.InvokeFormatterCommand
ErrorDetails          :
InvocationInfo        : System.Management.Automation.InvocationInfo
ScriptStackTrace      : at <ScriptBlock>, \\nas01\data\users\simon\documents\WindowsPowershell\Test-Formatter.ps1: line 16
                        at <ScriptBlock>, <No file>: line 1
PipelineIterationInfo : {0, 1}

Environment data

> $PSVersionTable
Name                           Value
----                           -----
PSVersion                      5.1.17134.48
PSEdition                      Desktop
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0...}
BuildVersion                   10.0.17134.48
CLRVersion                     4.0.30319.42000
WSManStackVersion              3.0
PSRemotingProtocolVersion      2.3
SerializationVersion           1.1.0.1
> (Get-Module -ListAvailable PSScriptAnalyzer).Version | ForEach-Object { $_.ToString() }
1.16.1

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions