PowerShell / PSScriptAnalyzer

Download ScriptAnalyzer from PowerShellGallery
https://www.powershellgallery.com/packages/PSScriptAnalyzer/
MIT License
1.8k stars 365 forks source link

Extend PSAlignAssignmentStatement to include enum types #1860

Open neggles opened 1 year ago

neggles commented 1 year ago

Summary of the new feature

As someone who writes a lot of C code and is a big fan of clang-format's AlignConsecutiveAssignments, I'm also a big fan of Invoke-Formatter's PSAlignAssignmentStatement option. There have been a couple of previous requests (#1029, #1385) to extend this to cover all assignments; personally I'd like to have the option, but the arguments against are valid.

That said, PSAlignAssignmentStatement currently applies to both hashtables and DSC configuration statements, but it does not apply to enum type declarations; these are syntactically similar to hashtables and I was surprised to find that it doesn't apply already. On top of that, enum declarations like this one:

enum CacheDiskStatus {
    CacheDiskStateUnknown = 0
    CacheDiskStateConfiguring = 1
    CacheDiskStateInitialized = 2
    CacheDiskStateInitializedAndBound = 3 # expected normal operational
    CacheDiskStateDraining = 4 # expected during RW->RO change (flushing dirty pages)
    CacheDiskStateDisabling = 5
    CacheDiskStateDisabled = 6 # expected post-disable of S2D
    CacheDiskStateMissing = 7
    CacheDiskStateOrphanedWaiting = 8
    CacheDiskStateOrphanedRecovering = 9
    CacheDiskStateFailedMediaError = 10
    CacheDiskStateFailedProvisioning = 11
    CacheDiskStateReset = 12
    CacheDiskStateRepairing = 13
    CacheDiskStateIneligibleDataPartition = 2000
    CacheDiskStateIneligibleNotGPT = 2001
    CacheDiskStateIneligibleNotEnoughSpace = 2002
    CacheDiskStateIneligibleUnsupportedSystem = 2003
    CacheDiskStateIneligibleExcludedFromS2D = 2004
    CacheDiskStateIneligibleForS2D = 2999
    CacheDiskStateSkippedBindingNoFlash = 3000
    CacheDiskStateIgnored = 3001
    CacheDiskStateNonHybrid = 3002
    CacheDiskStateInternalErrorConfiguring = 9000
    CacheDiskStateMarkedBad = 9001
    CacheDiskStateMarkedMissing = 9002
    CacheDiskStateInStorageMaintenance = 9003
}

cause me to take 1d4 psychic damage, while the aligned version:

enum CacheDiskStatus {
    CacheDiskStateUnknown                     = 0
    CacheDiskStateConfiguring                 = 1
    CacheDiskStateInitialized                 = 2
    CacheDiskStateInitializedAndBound         = 3 # expected normal operational
    CacheDiskStateDraining                    = 4 # expected during RW->RO change (flushing dirty pages)
    CacheDiskStateDisabling                   = 5
    CacheDiskStateDisabled                    = 6 # expected post-disable of S2D
    CacheDiskStateMissing                     = 7
    CacheDiskStateOrphanedWaiting             = 8
    CacheDiskStateOrphanedRecovering          = 9
    CacheDiskStateFailedMediaError            = 10
    CacheDiskStateFailedProvisioning          = 11
    CacheDiskStateReset                       = 12
    CacheDiskStateRepairing                   = 13
    CacheDiskStateIneligibleDataPartition     = 2000
    CacheDiskStateIneligibleNotGPT            = 2001
    CacheDiskStateIneligibleNotEnoughSpace    = 2002
    CacheDiskStateIneligibleUnsupportedSystem = 2003
    CacheDiskStateIneligibleExcludedFromS2D   = 2004
    CacheDiskStateIneligibleForS2D            = 2999
    CacheDiskStateSkippedBindingNoFlash       = 3000
    CacheDiskStateIgnored                     = 3001
    CacheDiskStateNonHybrid                   = 3002
    CacheDiskStateInternalErrorConfiguring    = 9000
    CacheDiskStateMarkedBad                   = 9001
    CacheDiskStateMarkedMissing               = 9002
    CacheDiskStateInStorageMaintenance        = 9003
}

is much nicer to look at, easier to read, and has a +1 dopamine bonus instead.

In some cases i've converted from an enum over to a PSCustomObject just to get this formatting, which comes with its own problems and loss of methods/strong typing.

Proposed technical implementation details (optional)

I would like PSAlignAssignmentStatement to apply to enum types as well as the existing hashtable and DSC configuration types. Due to the syntactic similarity, ISTM it would be in line with the existing hashtable/DSC behaviour.

What is the latest version of PSScriptAnalyzer at the point of writing

v2022.10.0