Skip to content

Commit 4ccd374

Browse files
committed
Always MoveUsingStatements
We no longer roll back using statement changes. The module was broken anyway, so we just tell you where.
1 parent ed56dca commit 4ccd374

File tree

2 files changed

+57
-67
lines changed

2 files changed

+57
-67
lines changed

Source/Private/MoveUsingStatements.ps1

Lines changed: 46 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ function MoveUsingStatements {
1616
those changes won't be applied to the file.
1717
#>
1818
[CmdletBinding()]
19-
Param(
19+
param(
2020
# Path to the PSM1 file to amend
2121
[Parameter(Mandatory, ValueFromPipelineByPropertyName, ValueFromPipeline)]
2222
[System.Management.Automation.Language.Ast]$AST,
@@ -27,54 +27,58 @@ function MoveUsingStatements {
2727

2828
# The encoding defaults to UTF8 (or UTF8NoBom on Core)
2929
[Parameter(DontShow)]
30-
[string]$Encoding = $(if ($IsCoreCLR) { "UTF8NoBom" } else { "UTF8" })
30+
[string]$Encoding = $(if ($IsCoreCLR) {
31+
"UTF8NoBom"
32+
} else {
33+
"UTF8"
34+
})
3135
)
36+
process {
37+
# Avoid modifying the file if there's no Parsing Error caused by Using Statements or other errors
38+
if (!$ParseErrors.Where{ $_.ErrorId -eq 'UsingMustBeAtStartOfScript' }) {
39+
Write-Debug "No using statement errors found."
40+
return
41+
} else {
42+
# as decided https://github.com/PoshCode/ModuleBuilder/issues/96
43+
Write-Debug "Parsing errors found. We'll still attempt to Move using statements."
44+
}
3245

33-
# Avoid modifying the file if there's no Parsing Error caused by Using Statements or other errors
34-
if (!$ParseErrors.Where{$_.ErrorId -eq 'UsingMustBeAtStartOfScript'}) {
35-
Write-Debug "No using statement errors found."
36-
return
37-
}
38-
39-
# as per issue https://github.com/PoshCode/ModuleBuilder/issues/96
40-
if ($ParseErrors.Where{$_.ErrorId -ne 'UsingMustBeAtStartOfScript'}) {
41-
Write-Verbose "Parsing errors found. We'll still attempt to Move using statements."
42-
}
43-
44-
# Find all Using statements including those non erroring (to conserve their order)
45-
$UsingStatementExtents = $AST.FindAll(
46-
{$Args[0] -is [System.Management.Automation.Language.UsingStatementAst]},
47-
$false
48-
).Extent
46+
# Find all Using statements including those non erroring (to conserve their order)
47+
$UsingStatementExtents = $AST.FindAll(
48+
{ $Args[0] -is [System.Management.Automation.Language.UsingStatementAst] },
49+
$false
50+
).Extent
4951

50-
# Edit the Script content by commenting out existing statements (conserving line numbering)
51-
$ScriptText = $AST.Extent.Text
52-
$InsertedCharOffset = 0
53-
$StatementsToCopy = New-Object System.Collections.ArrayList
54-
foreach ($UsingSatement in $UsingStatementExtents) {
55-
$ScriptText = $ScriptText.Insert($UsingSatement.StartOffset + $InsertedCharOffset, '#')
56-
$InsertedCharOffset++
52+
# Edit the Script content by commenting out existing statements (conserving line numbering)
53+
$ScriptText = $AST.Extent.Text
54+
$InsertedCharOffset = 0
55+
$StatementsToCopy = New-Object System.Collections.ArrayList
56+
foreach ($UsingSatement in $UsingStatementExtents) {
57+
$ScriptText = $ScriptText.Insert($UsingSatement.StartOffset + $InsertedCharOffset, '#')
58+
$InsertedCharOffset++
5759

58-
# Keep track of unique statements we'll need to insert at the top
59-
if (!$StatementsToCopy.Contains($UsingSatement.Text)) {
60-
$null = $StatementsToCopy.Add($UsingSatement.Text)
60+
# Keep track of unique statements we'll need to insert at the top
61+
if (!$StatementsToCopy.Contains($UsingSatement.Text)) {
62+
$null = $StatementsToCopy.Add($UsingSatement.Text)
63+
}
6164
}
62-
}
6365

64-
$ScriptText = $ScriptText.Insert(0, ($StatementsToCopy -join "`r`n") + "`r`n")
65-
66-
$ParseErrorsAfterMovingUsings = [System.Management.Automation.Language.ParseError[]]::new(0)
66+
$ScriptText = $ScriptText.Insert(0, ($StatementsToCopy -join "`r`n") + "`r`n")
67+
$null = Set-Content -Value $ScriptText -Path $RootModule -Encoding $Encoding
6768

68-
# Verify we haven't introduced new Parsing errors
69-
$null = [System.Management.Automation.Language.Parser]::ParseInput(
70-
$ScriptText,
71-
[ref]$null,
72-
[ref]$ParseErrorsAfterMovingUsings
73-
)
69+
# Verify we haven't introduced new Parsing errors
70+
$null = [System.Management.Automation.Language.Parser]::ParseFile(
71+
$RootModule,
72+
[ref]$null,
73+
[ref]$ParseErrors
74+
)
7475

75-
if ($ParseErrorsAfterMovingUsings.count -gt $ParseErrors.Count) {
76-
Write-Warning "We introduced parsing error(s) while attempting to move using statements. Cancelling changes."
77-
} else {
78-
$null = Set-Content -Value $ScriptText -Path $RootModule -Encoding $Encoding
76+
if ($ParseErrors.Count) {
77+
$Message = $ParseErrors |
78+
Format-Table -Auto @{n = "File"; expr = { $_.Extent.File | Split-Path -Leaf }},
79+
@{n = "Line"; expr = { $_.Extent.StartLineNumber }},
80+
Extent, ErrorId, Message | Out-String
81+
Write-Warning "Parse errors in build output:`n$Message"
82+
}
7983
}
8084
}

Tests/Private/MoveUsingStatements.Tests.ps1

Lines changed: 11 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,17 @@ Describe "MoveUsingStatements" {
5656
ErrorAfter = 0
5757
},
5858
@{
59-
TestCaseName = 'still move statements if there are (other) parse errors'
59+
TestCaseName = 'Move using statements even if types are used'
60+
PSM1File = "function x {`r`n}`r`n" +
61+
"using namespace System.IO`r`n`r`n" + #UsingMustBeAtStartOfScript
62+
"function y {`r`n}`r`n" +
63+
"using namespace System.Collections.Generic" + #UsingMustBeAtStartOfScript
64+
"function z { [Dictionary[String,PSObject]]::new() }" #TypeNotFound
65+
ErrorBefore = 3
66+
ErrorAfter = 0
67+
},
68+
@{
69+
TestCaseName = 'Move using statements even when there are (other) parse errors'
6070
PSM1File = "using namespace System.IO`r`n`r`n" +
6171
"function x { `r`n}`r`n" +
6272
"using namespace System.Drawing`r`n" + # UsingMustBeAtStartOfScript
@@ -116,29 +126,5 @@ Describe "MoveUsingStatements" {
116126
Assert-MockCalled -CommandName Set-Content -Times 0 -ModuleName ModuleBuilder
117127
Assert-MockCalled -CommandName Write-Debug -Times 1 -ModuleName ModuleBuilder
118128
}
119-
120-
121-
It 'Should not modify file when introducing parsing errors' {
122-
123-
$testModuleFile = "$TestDrive\MyModule.psm1"
124-
$PSM1File = "function x {}`r`nUsing namespace System.IO;"
125-
Set-Content $testModuleFile -value $PSM1File -Encoding UTF8
126-
127-
InModuleScope ModuleBuilder {
128-
$null = Mock New-Object {
129-
# Introducing Parsing Error in the file
130-
$Flag = [System.Collections.ArrayList]::new()
131-
$null = $Flag.Add("MyParsingError}")
132-
$PSCmdlet.WriteObject($Flag, $false)
133-
}
134-
}
135-
136-
&$MoveUsingStatementsCmd -RootModule $testModuleFile
137-
138-
Assert-MockCalled -CommandName Set-Content -Times 0 -ModuleName ModuleBuilder
139-
Assert-MockCalled -CommandName Write-Warning -Times 1 -ModuleName ModuleBuilder
140-
(Get-Content -Raw $testModuleFile).Trim() | Should -Be $PSM1File
141-
142-
}
143129
}
144130
}

0 commit comments

Comments
 (0)