-
Notifications
You must be signed in to change notification settings - Fork 784
Fix: Handle Negative VLQ Offsets in Source Maps for VSCode Jump to Definition #2419
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
Open
sethconvex
wants to merge
3
commits into
microsoft:main
Choose a base branch
from
sethconvex:fixNegativeVLQ
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 2 commits
Commits
Show all changes
3 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,219 @@ | ||
| package ls | ||
|
|
||
| import ( | ||
| "testing" | ||
|
|
||
| "github.com/microsoft/typescript-go/internal/format" | ||
| "github.com/microsoft/typescript-go/internal/ls/lsconv" | ||
| "github.com/microsoft/typescript-go/internal/ls/lsutil" | ||
| "github.com/microsoft/typescript-go/internal/lsp/lsproto" | ||
| "github.com/microsoft/typescript-go/internal/sourcemap" | ||
| "gotest.tools/v3/assert" | ||
| ) | ||
|
|
||
| // mockHost is a minimal implementation of Host for testing | ||
| type mockHost struct { | ||
| files map[string]string | ||
| } | ||
|
|
||
| func (m *mockHost) UseCaseSensitiveFileNames() bool { | ||
| return false | ||
| } | ||
|
|
||
| func (m *mockHost) ReadFile(path string) (contents string, ok bool) { | ||
| contents, ok = m.files[path] | ||
| return | ||
| } | ||
|
|
||
| func (m *mockHost) Converters() *lsconv.Converters { | ||
| return lsconv.NewConverters(lsproto.PositionEncodingKindUTF16, func(fileName string) *lsconv.LSPLineMap { | ||
| return nil | ||
| }) | ||
| } | ||
|
|
||
| func (m *mockHost) UserPreferences() *lsutil.UserPreferences { | ||
| return lsutil.NewDefaultUserPreferences() | ||
| } | ||
|
|
||
| func (m *mockHost) FormatOptions() *format.FormatCodeSettings { | ||
| return nil | ||
| } | ||
|
|
||
| func (m *mockHost) GetECMALineInfo(fileName string) *sourcemap.ECMALineInfo { | ||
| return nil | ||
| } | ||
|
|
||
| // mockDocumentPositionMapper simulates a source map with non-monotonic mappings | ||
| type mockDocumentPositionMapper struct { | ||
| // Maps from .d.ts position to source position | ||
| // In this test, we simulate non-monotonic mappings where | ||
| // position 100 in .d.ts maps to position 800 in source | ||
| // position 200 in .d.ts maps to position 200 in source (goes backwards!) | ||
| mappings map[int]int | ||
| sourceFile string | ||
| } | ||
|
|
||
| func (m *mockDocumentPositionMapper) GetSourcePosition(pos *sourcemap.DocumentPosition) *sourcemap.DocumentPosition { | ||
| if mappedPos, ok := m.mappings[pos.Pos]; ok { | ||
| return &sourcemap.DocumentPosition{ | ||
| FileName: m.sourceFile, | ||
| Pos: mappedPos, | ||
| } | ||
| } | ||
| return nil | ||
| } | ||
|
|
||
| func (m *mockDocumentPositionMapper) GetGeneratedPosition(pos *sourcemap.DocumentPosition) *sourcemap.DocumentPosition { | ||
| // Reverse lookup - not needed for this test | ||
| return nil | ||
| } | ||
|
|
||
| // TestGetMappedLocation_NonMonotonicMappings tests that getMappedLocation | ||
| // correctly handles non-monotonic source map mappings by clamping inverted ranges. | ||
| // This test verifies the fix logic that prevents inverted ranges when source maps | ||
| // have non-monotonic mappings (where declaration order differs from source order). | ||
| func TestGetMappedLocation_NonMonotonicMappings(t *testing.T) { | ||
| t.Parallel() | ||
|
|
||
| t.Run("inverted range should be clamped", func(t *testing.T) { | ||
| // This test verifies the fix logic: | ||
| // If endPos.Pos < startPos.Pos, endPosValue should be clamped to startPos.Pos | ||
|
|
||
| startPos := &sourcemap.DocumentPosition{ | ||
| FileName: "/source/index.ts", | ||
| Pos: 800, // getAllTools position | ||
| } | ||
|
|
||
| endPos := &sourcemap.DocumentPosition{ | ||
| FileName: "/source/index.ts", | ||
| Pos: 200, // getAvailableTools position (before start!) | ||
| } | ||
|
|
||
| // Simulate the fix logic | ||
| endPosValue := endPos.Pos | ||
| if endPos.FileName != startPos.FileName || endPos.Pos < startPos.Pos { | ||
| endPosValue = startPos.Pos | ||
| } | ||
|
|
||
| // Verify the range is clamped | ||
| assert.Equal(t, endPosValue, startPos.Pos, "endPos should be clamped to startPos when inverted") | ||
| assert.Equal(t, endPosValue, 800, "clamped value should be 800") | ||
|
|
||
| // Verify the range is valid (not inverted) | ||
| assert.Assert(t, endPosValue >= startPos.Pos, "endPosValue should be >= startPos.Pos") | ||
| }) | ||
|
|
||
| t.Run("valid range should not be clamped", func(t *testing.T) { | ||
| startPos := &sourcemap.DocumentPosition{ | ||
| FileName: "/source/index.ts", | ||
| Pos: 200, | ||
| } | ||
|
|
||
| endPos := &sourcemap.DocumentPosition{ | ||
| FileName: "/source/index.ts", | ||
| Pos: 800, // After start - valid | ||
| } | ||
|
|
||
| // Simulate the fix logic | ||
| endPosValue := endPos.Pos | ||
| if endPos.FileName != startPos.FileName || endPos.Pos < startPos.Pos { | ||
| endPosValue = startPos.Pos | ||
| } | ||
|
|
||
| // Verify the range is not clamped | ||
| assert.Equal(t, endPosValue, endPos.Pos, "endPos should not be clamped when valid") | ||
| assert.Equal(t, endPosValue, 800, "value should remain 800") | ||
| }) | ||
|
|
||
| t.Run("different files should be clamped", func(t *testing.T) { | ||
| startPos := &sourcemap.DocumentPosition{ | ||
| FileName: "/source/index.ts", | ||
| Pos: 200, | ||
| } | ||
|
|
||
| endPos := &sourcemap.DocumentPosition{ | ||
| FileName: "/source/other.ts", // Different file | ||
| Pos: 800, | ||
| } | ||
|
|
||
| // Simulate the fix logic | ||
| endPosValue := endPos.Pos | ||
| if endPos.FileName != startPos.FileName || endPos.Pos < startPos.Pos { | ||
| endPosValue = startPos.Pos | ||
| } | ||
|
|
||
| // Verify the range is clamped due to different files | ||
| assert.Equal(t, endPosValue, startPos.Pos, "endPos should be clamped when files differ") | ||
| }) | ||
| } | ||
|
|
||
| // TestGetMappedLocation_ZeroLengthRange tests that zero-length ranges | ||
| // (clamped ranges) are handled correctly. | ||
| func TestGetMappedLocation_ZeroLengthRange(t *testing.T) { | ||
| t.Parallel() | ||
|
|
||
| t.Run("zero-length range is valid", func(t *testing.T) { | ||
| startPos := &sourcemap.DocumentPosition{ | ||
| FileName: "/source/index.ts", | ||
| Pos: 800, | ||
| } | ||
|
|
||
| // Clamped end position (same as start) | ||
| endPosValue := startPos.Pos | ||
|
|
||
| // Create a zero-length range | ||
| rangeLen := endPosValue - startPos.Pos | ||
| assert.Equal(t, rangeLen, 0, "clamped range should be zero-length") | ||
| assert.Assert(t, rangeLen >= 0, "range length should never be negative") | ||
| }) | ||
| } | ||
|
|
||
| // TestValidateAndClampSpanLogic tests the core validation logic used in the fix. | ||
| // This directly tests the logic that prevents inverted spans. | ||
| func TestValidateAndClampSpanLogic(t *testing.T) { | ||
| t.Parallel() | ||
|
|
||
| // This is the exact validation logic from getMappedLocation | ||
| validateAndClamp := func(startPos, endPos *sourcemap.DocumentPosition) int { | ||
| endPosValue := endPos.Pos | ||
| if endPos.FileName != startPos.FileName || endPos.Pos < startPos.Pos { | ||
| endPosValue = startPos.Pos | ||
| } | ||
| return endPosValue | ||
| } | ||
|
|
||
| t.Run("inverted span is clamped", func(t *testing.T) { | ||
| start := &sourcemap.DocumentPosition{FileName: "/file.ts", Pos: 800} | ||
| end := &sourcemap.DocumentPosition{FileName: "/file.ts", Pos: 200} // Before start! | ||
|
|
||
| clamped := validateAndClamp(start, end) | ||
| assert.Equal(t, clamped, 800, "inverted span should be clamped to start") | ||
| assert.Assert(t, clamped >= start.Pos, "clamped value must be >= start") | ||
| }) | ||
|
|
||
| t.Run("valid span is not clamped", func(t *testing.T) { | ||
| start := &sourcemap.DocumentPosition{FileName: "/file.ts", Pos: 200} | ||
| end := &sourcemap.DocumentPosition{FileName: "/file.ts", Pos: 800} // After start | ||
|
|
||
| clamped := validateAndClamp(start, end) | ||
| assert.Equal(t, clamped, 800, "valid span should not be clamped") | ||
| }) | ||
|
|
||
| t.Run("different files are clamped", func(t *testing.T) { | ||
| start := &sourcemap.DocumentPosition{FileName: "/file1.ts", Pos: 200} | ||
| end := &sourcemap.DocumentPosition{FileName: "/file2.ts", Pos: 800} // Different file | ||
|
|
||
| clamped := validateAndClamp(start, end) | ||
| assert.Equal(t, clamped, 200, "different files should be clamped to start") | ||
| }) | ||
|
|
||
| t.Run("zero-length span is valid", func(t *testing.T) { | ||
| start := &sourcemap.DocumentPosition{FileName: "/file.ts", Pos: 800} | ||
| end := &sourcemap.DocumentPosition{FileName: "/file.ts", Pos: 800} // Same as start | ||
|
|
||
| clamped := validateAndClamp(start, end) | ||
| assert.Equal(t, clamped, 800, "zero-length span should be valid") | ||
| assert.Equal(t, clamped-start.Pos, 0, "range length should be zero") | ||
| }) | ||
| } | ||
|
|
Oops, something went wrong.
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 don't really like that this introduces a whole new system just to test this one thing.
Can we not test this with an actual fourslash test containing a file with a source map inline? We have similar tests for that already.
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.
Makes tons of sense! So glad you got a vacation too :-)
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've switched to fourslash, but haven't used it before. I've verified the test fails without this patch, and succeeds afterwards.