-
Notifications
You must be signed in to change notification settings - Fork 708
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
Closed
Closed
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) { | ||
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") | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -172,7 +172,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. | ||
|
12 changes: 6 additions & 6 deletions
12
testdata/baselines/reference/fourslash/goToDef/DeclarationMapGoToDefinition.baseline.jsonc
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
13 changes: 6 additions & 7 deletions
13
.../fourslash/goToDef/DeclarationMapsGoToDefinitionSameNameDifferentDirectory.baseline.jsonc
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
7 changes: 3 additions & 4 deletions
7
...data/baselines/reference/fourslash/goToDef/DeclarationMapsOutOfDateMapping.baseline.jsonc
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The 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.