Skip to content

Commit

Permalink
🐛 Reset-PSKoan: Fixes bug with start position offset (#310)
Browse files Browse the repository at this point in the history
* Fixes bug with start position offset

* Fixes variable name

* Adds test for aligned Start and End offset
  • Loading branch information
indented-automation authored and vexx32 committed Oct 28, 2019
1 parent 75c26ef commit 38f8c5e
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 3 deletions.
13 changes: 10 additions & 3 deletions PSKoans/Private/Get-KoanAst.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,16 @@ function Get-KoanAst {
$tokens = $errors = $null

# Remove the "using module" line. Avoids a slow call to Get-Module -ListAvailable from "using module".
$content = Get-Content -Path $Path |
Where-Object { $_ -notmatch '^\s*using module' } |
Out-String
$content = Get-Content -Path $Path -Raw
foreach ($match in [Regex]::Matches($content, 'using module \S+')) {
$content = $content.Remove(
$match.Index,
$match.Length
).Insert(
$match.Index,
' ' * $match.Length
)
}

[Parser]::ParseInput(
$content,
Expand Down
22 changes: 22 additions & 0 deletions Tests/Functions/Private/Get-KoanAst.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -31,5 +31,27 @@ InModuleScope PSKoans {

$ast.UsingStatements | Should -BeNullOrEmpty
}

It 'When reading and modifying the source, position data is consistent' {
$tokens = $errors = @()
$originalAst = [System.Management.Automation.Language.Parser]::ParseFile(
$path,
[Ref]$tokens,
[Ref]$errors
)
$originalItBlock = $originalAst.Find( {
$args[0] -is [System.Management.Automation.Language.CommandAst] -and
$args[0].GetCommandName() -eq 'It'
}, $true)

$modifiedAst = Get-KoanAst -Path $path
$modifiedItBlock = $modifiedAst.Find( {
$args[0] -is [System.Management.Automation.Language.CommandAst] -and
$args[0].GetCommandName() -eq 'It'
}, $true)

$modifiedItBlock.Extent.StartOffset | Should -Be $originalItBlock.Extent.StartOffset
$modifiedItBlock.Extent.EndOffset | Should -Be $originalItBlock.Extent.EndOffset
}
}
}

0 comments on commit 38f8c5e

Please sign in to comment.