Skip to content

Commit 4b4c3b5

Browse files
Improve formatting comparison in Invoke-DbatoolsFormatter
Enhances the formatter to compare processed, formatted content rather than raw content, ensuring that only meaningful formatting changes trigger file writes. Also applies CBH (Comment-Based Help) fixes and whitespace normalization to both the original and formatted content for accurate comparison.
1 parent b07ac96 commit 4b4c3b5

File tree

1 file changed

+40
-11
lines changed

1 file changed

+40
-11
lines changed

public/Invoke-DbatoolsFormatter.ps1

Lines changed: 40 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ function Invoke-DbatoolsFormatter {
6868
'PSUseConsistentIndentation',
6969
'PSUseConsistentWhitespace'
7070
)
71-
Rules = @{
71+
Rules = @{
7272
PSPlaceOpenBrace = @{
7373
Enable = $true
7474
OnSameLine = $true
@@ -150,56 +150,85 @@ function Invoke-DbatoolsFormatter {
150150
}
151151
}
152152

153-
#strip ending empty lines
153+
# Strip ending empty lines from both original and working content
154154
$content = $content -replace "(?s)$OSEOL\s*$"
155+
$originalStripped = $originalContent -replace "(?s)$OSEOL\s*$"
156+
155157
try {
156-
# Use custom settings instead of CodeFormattingOTBS
158+
# Format the content
157159
$content = Invoke-Formatter -ScriptDefinition $content -Settings $customSettings -ErrorAction Stop
160+
# Also format the original to compare
161+
$originalFormatted = Invoke-Formatter -ScriptDefinition $originalStripped -Settings $customSettings -ErrorAction Stop
158162
} catch {
159163
Write-Message -Level Warning "Unable to format $realPath : $($_.Exception.Message)"
160164
continue
161165
}
162166

163-
# Ensure $content is a string before processing
167+
# Ensure both contents are strings before processing
164168
if (-not $content -or $content -isnot [string]) {
165169
Write-Message -Level Warning "Formatter returned unexpected content type for $realPath"
166170
continue
167171
}
168172

169-
#match the ending indentation of CBH with the starting one, see #4373
173+
if (-not $originalFormatted -or $originalFormatted -isnot [string]) {
174+
Write-Message -Level Warning "Formatter returned unexpected content type for original in $realPath"
175+
continue
176+
}
177+
178+
# Apply CBH fix to formatted content
170179
$CBH = $CBHRex.Match($content).Value
171180
if ($CBH) {
172-
#get starting spaces
173181
$startSpaces = $CBHStartRex.Match($CBH).Groups['spaces']
174182
if ($startSpaces) {
175-
#get end
176183
$newCBH = $CBHEndRex.Replace($CBH, "$startSpaces#>")
177184
if ($newCBH) {
178-
#replace the CBH
179185
$content = $content.Replace($CBH, $newCBH)
180186
}
181187
}
182188
}
189+
190+
# Apply CBH fix to original formatted content
191+
$originalCBH = $CBHRex.Match($originalFormatted).Value
192+
if ($originalCBH) {
193+
$startSpaces = $CBHStartRex.Match($originalCBH).Groups['spaces']
194+
if ($startSpaces) {
195+
$newOriginalCBH = $CBHEndRex.Replace($originalCBH, "$startSpaces#>")
196+
if ($newOriginalCBH) {
197+
$originalFormatted = $originalFormatted.Replace($originalCBH, $newOriginalCBH)
198+
}
199+
}
200+
}
201+
183202
$Utf8NoBomEncoding = New-Object System.Text.UTF8Encoding $False
184203
$correctCase = @(
185204
'DbaInstanceParameter'
186205
'PSCredential'
187206
'PSCustomObject'
188207
'PSItem'
189208
)
209+
210+
# Process the formatted content
190211
$realContent = @()
191212
foreach ($line in $content.Split("`n")) {
192213
foreach ($item in $correctCase) {
193214
$line = $line -replace $item, $item
194215
}
195-
#trim whitespace lines
196216
$realContent += $line.Replace("`t", " ").TrimEnd()
197217
}
198-
199218
$finalContent = $realContent -Join "$OSEOL"
200219

220+
# Process the original formatted content the same way
221+
$originalProcessed = @()
222+
foreach ($line in $originalFormatted.Split("`n")) {
223+
foreach ($item in $correctCase) {
224+
$line = $line -replace $item, $item
225+
}
226+
$originalProcessed += $line.Replace("`t", " ").TrimEnd()
227+
}
228+
$originalFinalContent = $originalProcessed -Join "$OSEOL"
229+
201230
# Only write the file if there are actual changes
202-
if ($finalContent -ne $originalContent) {
231+
if ($finalContent -ne $originalFinalContent) {
203232
try {
204233
Write-Message -Level Verbose "Formatting changes detected in $realPath"
205234
[System.IO.File]::WriteAllText($realPath, $finalContent, $Utf8NoBomEncoding)

0 commit comments

Comments
 (0)