Title: PSUseConsistentIndentation: Hashtable inside method call gets double-indented · Issue #2159 · PowerShell/PSScriptAnalyzer · GitHub
Open Graph Title: PSUseConsistentIndentation: Hashtable inside method call gets double-indented · Issue #2159 · PowerShell/PSScriptAnalyzer
X Title: PSUseConsistentIndentation: Hashtable inside method call gets double-indented · Issue #2159 · PowerShell/PSScriptAnalyzer
Description: Summary PSUseConsistentIndentation incorrectly indents hashtable body content when the hashtable is inside a method call parenthesis (e.g., .Add(@{ ... })). The formatter treats the @{ opening as an additional indentation level relative ...
Open Graph Description: Summary PSUseConsistentIndentation incorrectly indents hashtable body content when the hashtable is inside a method call parenthesis (e.g., .Add(@{ ... })). The formatter treats the @{ opening as a...
X Description: Summary PSUseConsistentIndentation incorrectly indents hashtable body content when the hashtable is inside a method call parenthesis (e.g., .Add(@{ ... })). The formatter treats the @{ opening as a...
Opengraph URL: https://github.com/PowerShell/PSScriptAnalyzer/issues/2159
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"PSUseConsistentIndentation: Hashtable inside method call gets double-indented","articleBody":"## Summary\n\n`PSUseConsistentIndentation` incorrectly indents hashtable body content when the hashtable is inside a method call parenthesis (e.g., `.Add(@{ ... })`). The formatter treats the `@{` opening as an additional indentation level relative to the method call position, producing excessive indentation instead of indenting consistently from the line start.\n\nThis was originally reported as [PowerShell/vscode-powershell#5397](https://github.com/PowerShell/vscode-powershell/issues/5397), where a maintainer confirmed it belongs in PSScriptAnalyzer. That issue was auto-closed awaiting author feedback.\n\n## Steps to Reproduce\n\n```powershell\n# PSScriptAnalyzer 1.24.0 / PowerShell 7.5.4\n\n# Minimal repro - only PSUseConsistentIndentation enabled\n$code = @'\n$list.Add([PSCustomObject]@{\n Name = \"Test\"\n Value = 123\n})\n'@\n\n$settings = @{\n IncludeRules = @(\"PSUseConsistentIndentation\")\n Rules = @{\n PSUseConsistentIndentation = @{\n Enable = $true\n IndentationSize = 4\n Kind = \"space\"\n }\n }\n}\n\nInvoke-Formatter -ScriptDefinition $code -Settings $settings\n```\n\n## Expected Behavior\n\nHashtable properties should be indented by one level (4 spaces) from the line start, regardless of where `@{` appears on the line:\n\n```powershell\n$list.Add([PSCustomObject]@{\n Name = \"Test\"\n Value = 123\n})\n```\n\n## Actual Behavior\n\nProperties are indented to the `@{` column position (8 spaces), and the closing `})` gets its own indentation level too:\n\n```powershell\n$list.Add([PSCustomObject]@{\n Name = \"Test\"\n Value = 123\n })\n```\n\n## Additional Test Cases\n\nThe problem compounds with deeper nesting:\n\n```powershell\n# Input:\nforeach ($item in $items) {\n $results.Add([PSCustomObject]@{\n Name = $item.Name\n Value = $item.Value\n Status = \"OK\"\n })\n}\n\n# Formatted (actual) - 12 spaces for hashtable body:\nforeach ($item in $items) {\n $results.Add([PSCustomObject]@{\n Name = $item.Name\n Value = $item.Value\n Status = \"OK\"\n })\n}\n\n# Expected - 8 spaces (one level deeper than the foreach body):\nforeach ($item in $items) {\n $results.Add([PSCustomObject]@{\n Name = $item.Name\n Value = $item.Value\n Status = \"OK\"\n })\n}\n```\n\nStandalone hashtables (not inside method calls) format correctly:\n\n```powershell\n# This formats correctly with 4-space indent:\n$obj = [PSCustomObject]@{\n Name = \"Test\"\n Value = 123\n}\n```\n\n## Root Cause Analysis\n\n`PSUseConsistentIndentation` appears to count `(` as an indentation-increasing token, then also counts `@{` as another level. For standalone hashtables like `$x = @{`, the `=` doesn't increase indentation so only `@{` counts — producing 4 spaces correctly. But inside `.Add(@{`, the `(` adds one level and `@{` adds another, producing 8 spaces (2 × 4).\n\n## Impact\n\nThis is a common PowerShell pattern — building lists of `[PSCustomObject]` via `.Add()` is idiomatic. The excessive indentation forces a choice between:\n1. Disabling `PSUseConsistentIndentation` entirely\n2. Accepting unintuitively deep indentation\n3. Disabling `powershell.codeFormatting.alignPropertyValuePairs` (which also disables desirable alignment on standalone hashtables)\n4. Disabling format-on-save for PowerShell files\n\nWhen combined with `PSAlignAssignmentStatement.CheckHashtable`, the problem is amplified because value alignment is calculated from the already-wrong base indentation.\n\n## Workaround\n\nSetting `powershell.codeFormatting.alignPropertyValuePairs: false` in VS Code mitigates the worst visual impact but also disables alignment on standalone hashtables where it's desirable.\n\n## Environment\n\n- PSScriptAnalyzer: 1.24.0\n- PowerShell: 7.5.4\n- OS: Linux (also reported on Windows 11 in vscode-powershell#5397)\n","author":{"url":"https://github.com/quasarea","@type":"Person","name":"quasarea"},"datePublished":"2026-03-05T01:07:43Z","url":"https://github.com/PowerShell/PSScriptAnalyzer/issues/2159"}
| react-profiling | 0 |
| react-import-map | react,react-dom,react-dom/client,react-dom/profiling,react-is,react/compiler-runtime,react/jsx-dev-runtime,react/jsx-runtime,scheduler@b8488ee2c322 |
| route-pattern | /_view_fragments/issues/show/:user_id/:repository/:id/issue_layout(.:format) |
| route-controller | voltron_issues_fragments |
| route-action | issue_layout |
| fetch-nonce | v2:17d07dd7-45ed-3711-09b8-869d8a3cc07b |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | A43A:37F963:82B21:ADCC7:6AFCD73C |
| html-safe-nonce | 9261d893f0b0c1c5ada12a8d8f88c9a9f3d5f089960c8312f3497d6d729dd47f |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJBNDNBOjM3Rjk2Mzo4MkIyMTpBRENDNzo2QUZDRDczQyIsInZpc2l0b3JfaWQiOiI4OTY5ODQwODA2MzYxNjE4MjM2IiwicmVnaW9uX2VkZ2UiOiJpYWQiLCJyZWdpb25fcmVuZGVyIjoiaWFkIn0= |
| visitor-hmac | 604dc77a2d43f035add30d508ec4e2881932c27d72da879c9b864704aeecd4da |
| hovercard-subject-tag | issue:4025052321 |
| github-keyboard-shortcuts | repository,issues,copilot |
| google-site-verification | Apib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I |
| octolytics-url | https://collector.github.com/github/collect |
| analytics-location | / |
| fb:app_id | 1401488693436528 |
| apple-itunes-app | app-id=1477376905, app-argument=https://github.com/_view_fragments/issues/show/PowerShell/PSScriptAnalyzer/2159/issue_layout |
| twitter:image | https://opengraph.githubassets.com/b89beab20380553a41e9da7cb06af90ca6b9a03738004e04cc4a9136fd64f11a/PowerShell/PSScriptAnalyzer/issues/2159 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/b89beab20380553a41e9da7cb06af90ca6b9a03738004e04cc4a9136fd64f11a/PowerShell/PSScriptAnalyzer/issues/2159 |
| og:image:alt | Summary PSUseConsistentIndentation incorrectly indents hashtable body content when the hashtable is inside a method call parenthesis (e.g., .Add(@{ ... })). The formatter treats the @{ opening as a... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | quasarea |
| hostname | github.com |
| expected-hostname | github.com |
| None | b5b7bc7e41e0a604bbdb5035c7ed9620c2d28b9c8a18747182e2262a75abfd91 |
| turbo-cache-control | no-preview |
| go-import | github.com/PowerShell/PSScriptAnalyzer git https://github.com/PowerShell/PSScriptAnalyzer.git |
| octolytics-dimension-user_id | 11524380 |
| octolytics-dimension-user_login | PowerShell |
| octolytics-dimension-repository_id | 33149177 |
| octolytics-dimension-repository_nwo | PowerShell/PSScriptAnalyzer |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 33149177 |
| octolytics-dimension-repository_network_root_nwo | PowerShell/PSScriptAnalyzer |
| turbo-body-classes | logged-out env-production page-responsive |
| disable-turbo | false |
| browser-stats-url | https://api.github.com/_private/browser/stats |
| browser-errors-url | https://api.github.com/_private/browser/errors |
| release | ff5022cc3aa767e6115451d9c322dd975506d40e |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
Viewport: width=device-width