Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package fourslash_test

import (
"testing"

"github.com/microsoft/TypeScript/tsc/internal/fourslash"
"github.com/microsoft/TypeScript/tsc/internal/testutil"
)

func TestCompletionForStringLiteralUrlQueryOrFragmentNoCrash(t *testing.T) {
t.Parallel()
defer testutil.RecoverAndFail(t, "Panic on fourslash test")
const content = `// @Filename: /a.ts
declare function require(id: string): any;
require("https://example.com/a?b/c/*1*/");
require("http://localhost/#/routes//*2*/");
require("./foo https://example.com/?x/y/*3*/");
`
f, done := fourslash.NewFourslash(t, nil /*capabilities*/, content)
defer done()
f.VerifyCompletions(t, []string{"1", "2", "3"}, nil)
}
6 changes: 5 additions & 1 deletion tsc/internal/ls/string_completions.go
Original file line number Diff line number Diff line change
Expand Up @@ -1275,7 +1275,11 @@ func (l *LanguageService) getCompletionEntriesForDirectoryFragment(

fragment = tspath.EnsureTrailingDirectorySeparator(fragment)

baseDirectory := scriptDirectory.ResolveDirectory(fragment)
baseDirectory, ok := scriptDirectory.TryResolveDirectory(fragment)
if !ok {
// A URL with a query or fragment (e.g. "https://example.com/a?b/") can't name a directory.
return result
}
if !moduleSpecifierIsRelative {
// Check for a version redirect.
packageJsonDirectory := program.GetNearestAncestorDirectoryWithPackageJson(baseDirectory)
Expand Down
9 changes: 9 additions & 0 deletions tsc/internal/tspath/rooted_path.go
Original file line number Diff line number Diff line change
Expand Up @@ -800,6 +800,15 @@ func (d RootedDirectoryPath) ResolveDirectory(path string) RootedDirectoryPath {
return ToRootedDirectoryPath(path, d)
}

// TryResolveDirectory is like ResolveDirectory, but returns false instead of
// panicking when path would resolve to a URL with a query or fragment.
func (d RootedDirectoryPath) TryResolveDirectory(path string) (RootedDirectoryPath, bool) {
if hasRootedURLSuffix(path) || GetEncodedRootLength(path) == 0 && hasURLRoot(d.AsString()) && strings.ContainsAny(path, "?#") {
return "", false
}
return d.ResolveDirectory(path), true
}

func rootedFilePathFromResolved(path string) RootedFilePath {
if hasRootedURLSuffix(path) {
panic("path must not contain a URL query or fragment")
Expand Down
27 changes: 27 additions & 0 deletions tsc/internal/tspath/typed_paths_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -778,6 +778,33 @@ func TestPathKeyConstructionMethods(t *testing.T) {
})
}

func TestTryResolveDirectoryRejectsURLQueryOrFragment(t *testing.T) {
t.Parallel()

diskDirectory := RootedDirectoryPathFromNormalized("/project/src")
urlDirectory := RootedDirectoryPathFromNormalized("http://server/base")
for _, test := range []struct {
directory RootedDirectoryPath
path string
}{
{diskDirectory, "https://example.com/a?b/"},
{diskDirectory, "http://localhost/#/routes/"},
{diskDirectory, "./foo https://example.com/?x/"},
{urlDirectory, "./dir?query/"},
{urlDirectory, "dir#fragment/"},
} {
_, ok := test.directory.TryResolveDirectory(test.path)
assert.Assert(t, !ok, "%s + %s", test.directory, test.path)
}

resolved, ok := diskDirectory.TryResolveDirectory("./dir?query/")
assert.Assert(t, ok)
assert.Equal(t, resolved, RootedDirectoryPath("/project/src/dir?query"))
resolved, ok = urlDirectory.TryResolveDirectory("../other/")
assert.Assert(t, ok)
assert.Equal(t, resolved, RootedDirectoryPath("http://server/other"))
}

func TestRootedDirectoryPathResolutionMatchesGeneralRooting(t *testing.T) {
t.Parallel()

Expand Down
Loading