1- function Convert-LineNumber {
1+ function ConvertTo-SourceLineNumber {
22 <#
33 . SYNOPSIS
44 Convert the line number in a built module to a file and line number in source
@@ -7,43 +7,53 @@ function Convert-LineNumber {
77 . EXAMPLE
88 Convert-LineNumber -PositionMessage "At C:\Users\Joel\OneDrive\Documents\PowerShell\Modules\ErrorMaker\ErrorMaker.psm1:27 char:4"
99 #>
10+ [Alias (" Convert-LineNumber" )]
1011 [CmdletBinding (DefaultParameterSetName = " FromString" )]
1112 param (
1213 # A position message as found in PowerShell's error messages, ScriptStackTrace, or InvocationInfo
1314 [Parameter (Mandatory , ValueFromPipeline , ValueFromPipelineByPropertyName , ParameterSetName = " FromString" )]
1415 [string ]$PositionMessage ,
1516
17+ # The SourceFile (from an InvocationInfo) is the module psm1 path
1618 [Parameter (Mandatory , ValueFromPipelineByPropertyName , Position = 0 , ParameterSetName = " FromInvocationInfo" )]
1719 [Alias (" PSCommandPath" , " File" , " ScriptName" , " Script" )]
1820 [string ]$SourceFile ,
1921
22+ # The SourceLineNumber (from an InvocationInfo) is the module line number
2023 [Parameter (Mandatory , ValueFromPipelineByPropertyName , Position = 1 , ParameterSetName = " FromInvocationInfo" )]
2124 [Alias (" LineNumber" , " Line" , " ScriptLineNumber" )]
2225 [int ]$SourceLineNumber ,
2326
27+ # The actual InvocationInfo
2428 [Parameter (ValueFromPipeline , DontShow, ParameterSetName = " FromInvocationInfo" )]
2529 [psobject ]$InputObject ,
2630
31+ # If set, passes through the InputObject, overwriting the SourceFile and SourceLineNumber.
32+ # Otherwise, creates a new SourceLocation object with just those properties.
2733 [Parameter (ParameterSetName = " FromInvocationInfo" )]
2834 [switch ]$Passthru
2935 )
3036 begin {
3137 $filemap = @ {}
3238 }
3339 process {
34- if ($PSCmdlet.ParameterSetName -eq " FromString" ) {
40+ if ($PSCmdlet.ParameterSetName -eq " FromString" ) {
3541 $Invocation = ParseLineNumber $PositionMessage
3642 $SourceFile = $Invocation.SourceFile
3743 $SourceLineNumber = $Invocation.SourceLineNumber
3844 }
39- if (! (Test-Path $SourceFile )) {
45+ if (! (Test-Path $SourceFile )) {
4046 throw " '$SourceFile ' does not exist"
4147 }
4248 Push-Location (Split-Path $SourceFile )
4349 try {
4450 if (! $filemap.ContainsKey ($SourceFile )) {
4551 # Note: the new pattern is #Region but the old one was # BEGIN
4652 $regions = Select-String ' ^(?:#Region|# BEGIN) (?<SourceFile>.*) (?<LineNumber>\d+)?$' - Path $SourceFile
53+ if ($regions.Count -eq 0 ) {
54+ Write-Warning " No SourceMap for $SourceFile "
55+ return
56+ }
4757 $filemap [$SourceFile ] = @ ($regions.ForEach {
4858 [PSCustomObject ]@ {
4959 PSTypeName = " BuildSourceMapping"
0 commit comments