Browse Source

Merge pull request #449 from mansoor-s/resolve_source_map_fix

Fix nil pointer dereference panic when parsing sourcemaps sources with whitespaces
Dmitry Panov 3 years ago
parent
commit
e66ebd086f
2 changed files with 14 additions and 5 deletions
  1. 12 5
      file/file.go
  2. 2 0
      file/file_test.go

+ 12 - 5
file/file.go

@@ -6,6 +6,7 @@ import (
 	"net/url"
 	"net/url"
 	"path"
 	"path"
 	"sort"
 	"sort"
+	"strings"
 	"sync"
 	"sync"
 
 
 	"github.com/go-sourcemap/sourcemap"
 	"github.com/go-sourcemap/sourcemap"
@@ -158,8 +159,14 @@ func (fl *File) Position(offset int) Position {
 
 
 	if fl.sourceMap != nil {
 	if fl.sourceMap != nil {
 		if source, _, row, col, ok := fl.sourceMap.Source(row, col); ok {
 		if source, _, row, col, ok := fl.sourceMap.Source(row, col); ok {
+			sourceUrlStr := source
+			sourceURL := ResolveSourcemapURL(fl.Name(), source)
+			if sourceURL != nil {
+				sourceUrlStr = sourceURL.String()
+			}
+
 			return Position{
 			return Position{
-				Filename: ResolveSourcemapURL(fl.Name(), source).String(),
+				Filename: sourceUrlStr,
 				Line:     row,
 				Line:     row,
 				Column:   col,
 				Column:   col,
 			}
 			}
@@ -175,14 +182,14 @@ func (fl *File) Position(offset int) Position {
 
 
 func ResolveSourcemapURL(basename, source string) *url.URL {
 func ResolveSourcemapURL(basename, source string) *url.URL {
 	// if the url is absolute(has scheme) there is nothing to do
 	// if the url is absolute(has scheme) there is nothing to do
-	smURL, err := url.Parse(source)
+	smURL, err := url.Parse(strings.TrimSpace(source))
 	if err == nil && !smURL.IsAbs() {
 	if err == nil && !smURL.IsAbs() {
-		baseURL, err1 := url.Parse(basename)
+		baseURL, err1 := url.Parse(strings.TrimSpace(basename))
 		if err1 == nil && path.IsAbs(baseURL.Path) {
 		if err1 == nil && path.IsAbs(baseURL.Path) {
 			smURL = baseURL.ResolveReference(smURL)
 			smURL = baseURL.ResolveReference(smURL)
 		} else {
 		} else {
-			// pathological case where both are not absolute paths and using Resolve as above will produce an absolute
-			// one
+			// pathological case where both are not absolute paths and using Resolve
+			// as above will produce an absolute one
 			smURL, _ = url.Parse(path.Join(path.Dir(basename), smURL.Path))
 			smURL, _ = url.Parse(path.Join(path.Dir(basename), smURL.Path))
 		}
 		}
 	}
 	}

+ 2 - 0
file/file_test.go

@@ -61,6 +61,8 @@ func TestGetSourceFilename(t *testing.T) {
 		{"../test.js", "/somewhere/else/base.js", "/somewhere/test.js"},
 		{"../test.js", "/somewhere/else/base.js", "/somewhere/test.js"},
 		{"../test.js", "file:///somewhere/else/base.js", "file:///somewhere/test.js"},
 		{"../test.js", "file:///somewhere/else/base.js", "file:///somewhere/test.js"},
 		{"../test.js", "https://example.com/somewhere/else/base.js", "https://example.com/somewhere/test.js"},
 		{"../test.js", "https://example.com/somewhere/else/base.js", "https://example.com/somewhere/test.js"},
+		{"\ntest.js", "base123.js", "test.js"},
+		{"\rtest2.js\t\n  ", "base123.js", "test2.js"},
 		// TODO find something that won't parse
 		// TODO find something that won't parse
 	}
 	}
 	for _, test := range tests {
 	for _, test := range tests {