-
Notifications
You must be signed in to change notification settings - Fork 715
feat(ls): fix go to definition #1710
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
package ls_test | ||
|
||
import ( | ||
"context" | ||
"testing" | ||
|
||
"github.com/microsoft/typescript-go/internal/bundled" | ||
"github.com/microsoft/typescript-go/internal/ls" | ||
"github.com/microsoft/typescript-go/internal/lsp/lsproto" | ||
"github.com/microsoft/typescript-go/internal/testutil/projecttestutil" | ||
"gotest.tools/v3/assert" | ||
) | ||
|
||
func TestInternalAliasGoToDefinition(t *testing.T) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I specifically added this to have an automated test for functionality that wasn't tested by the baselines. |
||
t.Parallel() | ||
if !bundled.Embedded { | ||
t.Skip("bundled files are not embedded") | ||
} | ||
|
||
// Source file content | ||
sourceContent := `export function helperFunction() { | ||
return "helper result"; | ||
} | ||
export const helperConstant = 42;` | ||
|
||
// Declaration file content | ||
declContent := `export declare function helperFunction(): string; | ||
export declare const helperConstant = 42; | ||
//# sourceMappingURL=utils.d.ts.map` | ||
|
||
// Source map content | ||
sourceMapContent := `{"version":3,"file":"utils.d.ts","sourceRoot":"","sources":["../../../src/internal/helpers/utils.ts"],"names":[],"mappings":"AAAA,wBAAgB,cAAc,IAAI,MAAM,CAEvC;AAED,eAAO,MAAM,cAAc,KAAK,CAAC"}` | ||
|
||
// Main file content | ||
mainContent := `import { helperFunction } from "@internal/helpers/utils"; | ||
const result = helperFunction();` | ||
|
||
// tsconfig.json with path mapping | ||
tsconfigContent := `{ | ||
"compilerOptions": { | ||
"baseUrl": ".", | ||
"paths": { | ||
"@internal/*": ["dist/internal/*", "src/internal/*"] | ||
}, | ||
"declaration": true, | ||
"declarationMap": true, | ||
"outDir": "dist" | ||
} | ||
}` | ||
|
||
// Set up test files | ||
files := map[string]any{ | ||
"/tsconfig.json": tsconfigContent, | ||
"/src/internal/helpers/utils.ts": sourceContent, | ||
"/dist/internal/helpers/utils.d.ts": declContent, | ||
"/dist/internal/helpers/utils.d.ts.map": sourceMapContent, | ||
"/main.ts": mainContent, | ||
} | ||
|
||
session, _ := projecttestutil.Setup(files) | ||
|
||
ctx := projecttestutil.WithRequestID(context.Background()) | ||
session.DidOpenFile(ctx, "file:///main.ts", 1, mainContent, lsproto.LanguageKindTypeScript) | ||
|
||
languageService, err := session.GetLanguageService(ctx, "file:///main.ts") | ||
assert.NilError(t, err) | ||
|
||
uri := lsproto.DocumentUri("file:///main.ts") | ||
lspPosition := lsproto.Position{Line: 0, Character: 9} | ||
|
||
definition, err := languageService.ProvideDefinition(ctx, uri, lspPosition) | ||
assert.NilError(t, err) | ||
|
||
if definition.Locations != nil { | ||
assert.Assert(t, len(*definition.Locations) == 1, "Expected 1 definition location, got %d", len(*definition.Locations)) | ||
|
||
location := (*definition.Locations)[0] | ||
expectedURI := ls.FileNameToDocumentURI("/src/internal/helpers/utils.ts") | ||
actualURI := location.Uri | ||
|
||
assert.Equal(t, string(expectedURI), string(actualURI), "Should resolve to source .ts file, not .d.ts file") | ||
} else if definition.Location != nil { | ||
expectedURI := ls.FileNameToDocumentURI("/src/internal/helpers/utils.ts") | ||
assert.Equal(t, string(expectedURI), string(definition.Location.Uri), "Should resolve to source .ts file, not .d.ts file") | ||
} else { | ||
t.Fatal("No definition found - expected to find definition") | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -144,7 +144,7 @@ func (fs *compilerFS) ReadFile(path string) (contents string, ok bool) { | |
if fh := fs.source.GetFile(path); fh != nil { | ||
return fh.Content(), true | ||
} | ||
return "", false | ||
return fs.source.FS().ReadFile(path) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This fixes issues with being able to get the source map files. It conforms to the fallback mechanism that TypeScript had which was going to the disk - https://github.com/microsoft/TypeScript/blob/release-5.9/src/services/sourcemaps.ts#L147-L151 |
||
} | ||
|
||
// Realpath implements vfs.FS. | ||
|
Uh oh!
There was an error while loading. Please reload this page.