From 69498906d3f2853343256e8e4623f2b3050505a4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Olav=20R=C3=B8nnestad=20Birkeland?= <6450056+o-l-a-v@users.noreply.github.com> Date: Sun, 1 Mar 2026 12:00:24 +0100 Subject: [PATCH 1/6] feat: added new code formatting preset OTPS --- Engine/Settings/CodeFormattingOTPS.psd1 | 55 +++++++++++++++++++++++++ 1 file changed, 55 insertions(+) create mode 100644 Engine/Settings/CodeFormattingOTPS.psd1 diff --git a/Engine/Settings/CodeFormattingOTPS.psd1 b/Engine/Settings/CodeFormattingOTPS.psd1 new file mode 100644 index 000000000..83514c7f9 --- /dev/null +++ b/Engine/Settings/CodeFormattingOTPS.psd1 @@ -0,0 +1,55 @@ +@{ + IncludeRules = @( + 'PSPlaceOpenBrace', + 'PSPlaceCloseBrace', + 'PSUseConsistentWhitespace', + 'PSUseConsistentIndentation', + 'PSAlignAssignmentStatement', + 'PSUseCorrectCasing' + ) + + Rules = @{ + PSPlaceOpenBrace = @{ + Enable = $true + OnSameLine = $true + NewLineAfter = $true + IgnoreOneLineBlock = $true + } + + PSPlaceCloseBrace = @{ + Enable = $true + NewLineAfter = $true + IgnoreOneLineBlock = $true + NoEmptyLineBefore = $false + } + + PSUseConsistentIndentation = @{ + Enable = $true + Kind = 'space' + PipelineIndentation = 'IncreaseIndentationForFirstPipeline' + IndentationSize = 4 + } + + PSUseConsistentWhitespace = @{ + Enable = $true + CheckInnerBrace = $true + CheckOpenBrace = $true + CheckOpenParen = $true + CheckOperator = $true + CheckPipe = $true + CheckPipeForRedundantWhitespace = $false + CheckSeparator = $true + CheckParameter = $false + IgnoreAssignmentOperatorInsideHashTable = $true + } + + PSAlignAssignmentStatement = @{ + Enable = $true + CheckHashtable = $true + } + + PSUseCorrectCasing = @{ + Enable = $true + } + } +} From 5e8ed23c0ad5374ba291a287bd3b7ddbcf118126 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Olav=20R=C3=B8nnestad=20Birkeland?= <6450056+o-l-a-v@users.noreply.github.com> Date: Sun, 1 Mar 2026 12:00:50 +0100 Subject: [PATCH 2/6] feat: added new code formatting preset OTPS to some tests where i found OTBS mentioned --- Tests/Rules/PlaceOpenBrace.tests.ps1 | 3 +++ 1 file changed, 3 insertions(+) diff --git a/Tests/Rules/PlaceOpenBrace.tests.ps1 b/Tests/Rules/PlaceOpenBrace.tests.ps1 index 4c8206102..424e511b2 100644 --- a/Tests/Rules/PlaceOpenBrace.tests.ps1 +++ b/Tests/Rules/PlaceOpenBrace.tests.ps1 @@ -56,6 +56,7 @@ foreach ($x in $y) { Invoke-Formatter -ScriptDefinition $scriptDefinition -Settings $settings | Should -Be $expected Invoke-Formatter -ScriptDefinition $scriptDefinition -Settings 'CodeFormattingStroustrup' | Should -Be $expected Invoke-Formatter -ScriptDefinition $scriptDefinition -Settings 'CodeFormattingOTBS' | Should -Be $expected + Invoke-Formatter -ScriptDefinition $scriptDefinition -Settings 'CodeFormattingOTPS' | Should -Be $expected } It "Should correct violation when brace should be on the same line and take comment into account" { @@ -73,6 +74,7 @@ foreach ($x in $y) { # useful comment Invoke-Formatter -ScriptDefinition $scriptDefinition -Settings $settings | Should -Be $expected Invoke-Formatter -ScriptDefinition $scriptDefinition -Settings 'CodeFormattingStroustrup' | Should -Be $expected Invoke-Formatter -ScriptDefinition $scriptDefinition -Settings 'CodeFormattingOTBS' | Should -Be $expected + Invoke-Formatter -ScriptDefinition $scriptDefinition -Settings 'CodeFormattingOTPS' | Should -Be $expected } It "Should correct violation when the brace should be on the next line and take comment into account" { @@ -90,6 +92,7 @@ foreach ($x in $y) { # useful comment Invoke-Formatter -ScriptDefinition $scriptDefinition -Settings $settings | Should -Be $expected Invoke-Formatter -ScriptDefinition $scriptDefinition -Settings 'CodeFormattingStroustrup' | Should -Be $expected Invoke-Formatter -ScriptDefinition $scriptDefinition -Settings 'CodeFormattingOTBS' | Should -Be $expected + Invoke-Formatter -ScriptDefinition $scriptDefinition -Settings 'CodeFormattingOTPS' | Should -Be $expected } } From 52ba0d0c267cccba37995cf0094fd423ff71b79b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Olav=20R=C3=B8nnestad=20Birkeland?= <6450056+o-l-a-v@users.noreply.github.com> Date: Fri, 6 Mar 2026 19:35:41 +0100 Subject: [PATCH 3/6] feat(tests): added dedicated test for code formatting presets --- Tests/Rules/CodeFormattingPresets.tests.ps1 | 149 ++++++++++++++++++++ 1 file changed, 149 insertions(+) create mode 100644 Tests/Rules/CodeFormattingPresets.tests.ps1 diff --git a/Tests/Rules/CodeFormattingPresets.tests.ps1 b/Tests/Rules/CodeFormattingPresets.tests.ps1 new file mode 100644 index 000000000..96fe9ad40 --- /dev/null +++ b/Tests/Rules/CodeFormattingPresets.tests.ps1 @@ -0,0 +1,149 @@ +# Copyright (c) Microsoft Corporation. All rights reserved. +# Licensed under the MIT License. + +BeforeAll { + $testRootDirectory = Split-Path -Parent $PSScriptRoot + Import-Module (Join-Path $testRootDirectory "PSScriptAnalyzerTestHelper.psm1") + + $Allman = @' +enum Color +{ + Black, + White +} + +function Test-Code +{ + [CmdletBinding()] + param + ( + [int] $ParameterOne + ) + end + { + if (10 -gt $ParameterOne) + { + "Greater" + } + else + { + "Lesser" + } + } +} +'@ + + $OTBS = @' +enum Color { + Black, + White +} + +function Test-Code { + [CmdletBinding()] + param( + [int] $ParameterOne + ) + end { + if (10 -gt $ParameterOne) { + "Greater" + } else { + "Lesser" + } + } +} +'@ + $OTPS = @' +enum Color { + Black, + White +} + +function Test-Code { + [CmdletBinding()] + param( + [int] $ParameterOne + ) + end { + if (10 -gt $ParameterOne) { + "Greater" + } + else { + "Lesser" + } + } +} +'@ + $Stroustrup = @' +enum Color { + Black, + White +} + +function Test-Code +{ + [CmdletBinding()] + param( + [int]$ParameterOne + ) + end { + if(10 -gt $ParameterOne) { + "Greater" + } + else { + "Lesser" + } + } +} +'@ +} + +Describe "CodeFormattingPresets" { + Context "Allman" { + It "To Allman from OTBS" { + Invoke-Formatter -ScriptDefinition $OTBS -Settings 'CodeFormattingAllman' | Should -Be $Allman + } + It "To Allman from OTPS" { + Invoke-Formatter -ScriptDefinition $OTPS -Settings 'CodeFormattingAllman' | Should -Be $Allman + } + It "To Allman from Stroustrup" { + Invoke-Formatter -ScriptDefinition $Stroustrup -Settings 'CodeFormattingAllman' | Should -Be $Allman + } + } + + Context "OTBS" { + It "To OTBS from Allman" { + Invoke-Formatter -ScriptDefinition $Allman -Settings 'CodeFormattingOTBS' | Should -Be $OTBS + } + It "To OTBS from OTPS" { + Invoke-Formatter -ScriptDefinition $OTPS -Settings 'CodeFormattingOTBS' | Should -Be $OTBS + } + It "To OTBS from Stroustrup" { + Invoke-Formatter -ScriptDefinition $Stroustrup -Settings 'CodeFormattingOTBS' | Should -Be $OTBS + } + } + + Context "OTPS" { + It "To OTPS from Allman" { + Invoke-Formatter -ScriptDefinition $Allman -Settings 'CodeFormattingOTPS' | Should -Be $OTPS + } + It "To OTPS from OTBS" { + Invoke-Formatter -ScriptDefinition $OTBS -Settings 'CodeFormattingOTPS' | Should -Be $OTPS + } + It "To OTPS from Stroustrup" { + Invoke-Formatter -ScriptDefinition $Stroustrup -Settings 'CodeFormattingOTPS' | Should -Be $OTPS + } + } + + Context "Stroustrup" { + It "To Stroustrup from Allman" { + Invoke-Formatter -ScriptDefinition $Allman -Settings 'CodeFormattingStroustrup' | Should -Be $Stroustrup + } + It "To Stroustrup from OTBS" { + Invoke-Formatter -ScriptDefinition $OTBS -Settings 'CodeFormattingStroustrup' | Should -Be $Stroustrup + } + It "To Stroustrup from OTPS" { + Invoke-Formatter -ScriptDefinition $OTPS -Settings 'CodeFormattingStroustrup' | Should -Be $Stroustrup + } + } +} \ No newline at end of file From 3db1d6d81f181f910eeab3b9d6f67f8947a32ca8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Olav=20R=C3=B8nnestad=20Birkeland?= <6450056+o-l-a-v@users.noreply.github.com> Date: Fri, 6 Mar 2026 19:36:11 +0100 Subject: [PATCH 4/6] feat(tests): added context for how code formatting presets handles closing bracket and else --- Tests/Rules/PlaceCloseBrace.tests.ps1 | 50 +++++++++++++++++++++++++++ 1 file changed, 50 insertions(+) diff --git a/Tests/Rules/PlaceCloseBrace.tests.ps1 b/Tests/Rules/PlaceCloseBrace.tests.ps1 index 85ccb8979..19f018bc8 100644 --- a/Tests/Rules/PlaceCloseBrace.tests.ps1 +++ b/Tests/Rules/PlaceCloseBrace.tests.ps1 @@ -383,4 +383,54 @@ if ($true) { $violations.Count | Should -Be 0 } } + + Context "When formatting presets handles if else" { + BeforeAll { + $AllmanDefinition = @" +if (`$true) +{ + 'yes' +} +else +{ + 'no' +} +"@ + $OTBSDefinition = @" +if (`$true) { + 'yes' +} else { + 'no' +} +"@ + $OTPSAndStroustrupDefinition = @" +if (`$true) { + "yes" +} +else { + "no" +} +"@ + } + + It "Allman should have all opening and closing braces on a new line" { + Invoke-Formatter -ScriptDefinition $OTBSDefinition -Settings 'CodeFormattingAllman' | Should -Be $AllmanDefinition + Invoke-Formatter -ScriptDefinition $OTPSAndStroustrupDefinition -Settings 'CodeFormattingAllman' | Should -Be $AllmanDefinition + } + + It "OTBS should place else on same line as the if closing bracket" { + Invoke-Formatter -ScriptDefinition $AllmanDefinition -Settings 'CodeFormattingOTBS' | Should -Be $OTBSDefinition + Invoke-Formatter -ScriptDefinition $OTPSAndStroustrupDefinition -Settings 'CodeFormattingOTBS' | Should -Be $OTBSDefinition + } + + It "OTPS should place else on a new line after the if closing bracket" { + Invoke-Formatter -ScriptDefinition $AllmanDefinition -Settings 'CodeFormattingOTPS' | Should -Be $OTPSAndStroustrupDefinition + Invoke-Formatter -ScriptDefinition $OTBSDefinition -Settings 'CodeFormattingOTPS' | Should -Be $OTPSAndStroustrupDefinition + } + + It "Stroustrup should place else on a new line after the if closing bracket" { + Invoke-Formatter -ScriptDefinition $AllmanDefinition -Settings 'CodeFormattingStroustrup' | Should -Be $OTPSAndStroustrupDefinition + Invoke-Formatter -ScriptDefinition $OTBSDefinition -Settings 'CodeFormattingStroustrup' | Should -Be $OTPSAndStroustrupDefinition + } + } } From 75a4be03fe1f15868e54ac67c58ee82b049ca220 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Olav=20R=C3=B8nnestad=20Birkeland?= <6450056+o-l-a-v@users.noreply.github.com> Date: Fri, 6 Mar 2026 19:39:10 +0100 Subject: [PATCH 5/6] fix(tests): fix preset test presets --- Tests/Rules/CodeFormattingPresets.tests.ps1 | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Tests/Rules/CodeFormattingPresets.tests.ps1 b/Tests/Rules/CodeFormattingPresets.tests.ps1 index 96fe9ad40..949b7c191 100644 --- a/Tests/Rules/CodeFormattingPresets.tests.ps1 +++ b/Tests/Rules/CodeFormattingPresets.tests.ps1 @@ -84,10 +84,10 @@ function Test-Code { [CmdletBinding()] param( - [int]$ParameterOne + [int] $ParameterOne ) end { - if(10 -gt $ParameterOne) { + if (10 -gt $ParameterOne) { "Greater" } else { From 29da161183998287d100d5479b6dc3db56d835d9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Olav=20R=C3=B8nnestad=20Birkeland?= <6450056+o-l-a-v@users.noreply.github.com> Date: Sat, 7 Mar 2026 13:23:18 +0100 Subject: [PATCH 6/6] fix(tests): fixed string quotes for new test --- Tests/Rules/PlaceCloseBrace.tests.ps1 | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Tests/Rules/PlaceCloseBrace.tests.ps1 b/Tests/Rules/PlaceCloseBrace.tests.ps1 index 19f018bc8..0b2a8321d 100644 --- a/Tests/Rules/PlaceCloseBrace.tests.ps1 +++ b/Tests/Rules/PlaceCloseBrace.tests.ps1 @@ -405,10 +405,10 @@ if (`$true) { "@ $OTPSAndStroustrupDefinition = @" if (`$true) { - "yes" + 'yes' } else { - "no" + 'no' } "@ }