Skip to content

Commit ed56dca

Browse files
gaelcolasJaykul
authored andcommitted
fix #96 by moving statements even if parsing errors exists
1 parent 5a3f1b8 commit ed56dca

File tree

2 files changed

+10
-17
lines changed

2 files changed

+10
-17
lines changed

Source/Private/MoveUsingStatements.ps1

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -35,10 +35,10 @@ function MoveUsingStatements {
3535
Write-Debug "No using statement errors found."
3636
return
3737
}
38-
# Avoid modifying the file if there's other parsing errors than Using Statements misplaced
38+
39+
# as per issue https://github.com/PoshCode/ModuleBuilder/issues/96
3940
if ($ParseErrors.Where{$_.ErrorId -ne 'UsingMustBeAtStartOfScript'}) {
40-
Write-Warning "Parsing errors found. Skipping moving using statements."
41-
return
41+
Write-Verbose "Parsing errors found. We'll still attempt to Move using statements."
4242
}
4343

4444
# Find all Using statements including those non erroring (to conserve their order)
@@ -63,14 +63,16 @@ function MoveUsingStatements {
6363

6464
$ScriptText = $ScriptText.Insert(0, ($StatementsToCopy -join "`r`n") + "`r`n")
6565

66+
$ParseErrorsAfterMovingUsings = [System.Management.Automation.Language.ParseError[]]::new(0)
67+
6668
# Verify we haven't introduced new Parsing errors
6769
$null = [System.Management.Automation.Language.Parser]::ParseInput(
6870
$ScriptText,
6971
[ref]$null,
70-
[ref]$ParseErrors
72+
[ref]$ParseErrorsAfterMovingUsings
7173
)
7274

73-
if ($ParseErrors) {
75+
if ($ParseErrorsAfterMovingUsings.count -gt $ParseErrors.Count) {
7476
Write-Warning "We introduced parsing error(s) while attempting to move using statements. Cancelling changes."
7577
} else {
7678
$null = Set-Content -Value $ScriptText -Path $RootModule -Encoding $Encoding

Tests/Private/MoveUsingStatements.Tests.ps1

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -56,13 +56,13 @@ Describe "MoveUsingStatements" {
5656
ErrorAfter = 0
5757
},
5858
@{
59-
TestCaseName = 'Not move anything if there are (other) parse errors'
59+
TestCaseName = 'still move statements if there are (other) parse errors'
6060
PSM1File = "using namespace System.IO`r`n`r`n" +
6161
"function x { `r`n}`r`n" +
6262
"using namespace System.Drawing`r`n" + # UsingMustBeAtStartOfScript
6363
"function y { `r`n}`r`n}" # Extra } at the end
6464
ErrorBefore = 2
65-
ErrorAfter = 2
65+
ErrorAfter = 1
6666
}
6767
)
6868

@@ -91,6 +91,7 @@ Describe "MoveUsingStatements" {
9191
$ErrorFound.Count | Should -Be $ErrorAfter
9292
}
9393
}
94+
9495
Context "When MoveUsingStatements should do nothing" {
9596

9697
$MoveUsingStatementsCmd = InModuleScope ModuleBuilder {
@@ -103,16 +104,6 @@ Describe "MoveUsingStatements" {
103104
}
104105
}
105106

106-
It 'Should Warn and skip when there are Parsing errors other than Using Statements' {
107-
$testModuleFile = "$TestDrive/MyModule.psm1"
108-
$PSM1File = "Using namespace System.IO`r`n function xyz {}`r`n}`r`nUsing namespace System.Drawing" # extra } Set-Content $testModuleFile -value $PSM1File -Encoding UTF8
109-
Set-Content $testModuleFile -value $PSM1File -Encoding UTF8
110-
111-
&$MoveUsingStatementsCmd -RootModule $testModuleFile
112-
Assert-MockCalled -CommandName Write-Warning -Times 1 -ModuleName ModuleBuilder
113-
Assert-MockCalled -CommandName Set-Content -Times 0 -ModuleName ModuleBuilder
114-
}
115-
116107
It 'Should not do anything when there are no using statement errors' {
117108
$testModuleFile = "$TestDrive\MyModule.psm1"
118109
$PSM1File = "using namespace System.IO; function x {}"

0 commit comments

Comments
 (0)