-
Notifications
You must be signed in to change notification settings - Fork 73
Fix SingleMapping.spanFor
to use previous line
#2205
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
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
23a6d33
Fix `SingleMapping.spanFor` to use previous line
rakudrama 7bd8d0e
fix lints
rakudrama e58f14b
Merge branch 'dart-lang:main' into GetOriginalPositions
rakudrama f33078f
xxx
rakudrama 6b3d38d
xxx
rakudrama 614df72
add test
rakudrama a344b6f
fix typo
rakudrama 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,110 @@ | ||
// Copyright (c) 2025, the Dart project authors. Please see the AUTHORS file | ||
// for details. All rights reserved. Use of this source code is governed by a | ||
// BSD-style license that can be found in the LICENSE file. | ||
|
||
import 'package:source_maps/source_maps.dart'; | ||
import 'package:source_span/source_span.dart'; | ||
import 'package:test/test.dart'; | ||
|
||
void main() { | ||
/// This is a test for spans of the generated file that continue over several | ||
/// lines. | ||
/// | ||
/// In a sourcemap, a span continues from the start encoded position until the | ||
/// next position, regardless of whether the second position in on the same | ||
/// line in the generated file or a subsequent line. | ||
void testSpans(int lineA, int columnA, int lineB, int columnB) { | ||
// Create a sourcemap describing a 'rectangular' generated file with three | ||
// spans, each potentially over several lines: (1) an initial span that is | ||
// unmapped, (2) a span that maps to file 'A', the span continuing until (3) | ||
// a span that maps to file 'B'. | ||
// | ||
// We can describe the mapping by an 'image' of the generated file, where | ||
// the positions marked as 'A' in the 'image' correspond to locations in the | ||
// generated file that map to locations in source file 'A'. Lines and | ||
// columns are zero-based. | ||
// | ||
// 0123456789 | ||
// 0: ---------- | ||
// 1: ----AAAAAA lineA: 1, columnA: 4, i.e. locationA | ||
// 2: AABBBBBBBB lineB: 2, columnB: 2, i.e. locationB | ||
// 3: BBBBBBBBBB | ||
// | ||
// Once we have the mapping, we probe every position in a 8x10 rectangle to | ||
// validate that it maps to the intended original source file. | ||
|
||
expect(isBefore(lineB, columnB, lineA, columnA), isFalse, | ||
reason: 'Test valid only for ordered positions'); | ||
|
||
SourceLocation location(Uri? uri, int line, int column) { | ||
final offset = line * 10 + column; | ||
return SourceLocation(offset, sourceUrl: uri, line: line, column: column); | ||
} | ||
|
||
// Locations in the generated file. | ||
final uriMap = Uri.parse('output.js.map'); | ||
final locationA = location(uriMap, lineA, columnA); | ||
final locationB = location(uriMap, lineB, columnB); | ||
|
||
// Original source locations. | ||
final sourceA = location(Uri.parse('A'), 0, 0); | ||
final sourceB = location(Uri.parse('B'), 0, 0); | ||
|
||
final json = (SourceMapBuilder() | ||
..addLocation(sourceA, locationA, null) | ||
..addLocation(sourceB, locationB, null)) | ||
.build(uriMap.toString()); | ||
|
||
final mapping = parseJson(json); | ||
|
||
// Validate by comparing 'images' of the generate file. | ||
final expectedImage = StringBuffer(); | ||
final actualImage = StringBuffer(); | ||
|
||
for (var line = 0; line < 8; line++) { | ||
for (var column = 0; column < 10; column++) { | ||
final span = mapping.spanFor(line, column); | ||
final expected = isBefore(line, column, lineA, columnA) | ||
? '-' | ||
: isBefore(line, column, lineB, columnB) | ||
? 'A' | ||
: 'B'; | ||
final actual = span?.start.sourceUrl?.path ?? '-'; // Unmapped -> '-'. | ||
|
||
expectedImage.write(expected); | ||
actualImage.write(actual); | ||
} | ||
expectedImage.writeln(); | ||
actualImage.writeln(); | ||
} | ||
expect(actualImage.toString(), expectedImage.toString()); | ||
} | ||
|
||
test('continued span, same position', () { | ||
testSpans(2, 4, 2, 4); | ||
}); | ||
|
||
test('continued span, same line', () { | ||
testSpans(2, 4, 2, 7); | ||
}); | ||
|
||
test('continued span, next line, earlier column', () { | ||
testSpans(2, 4, 3, 2); | ||
}); | ||
|
||
test('continued span, next line, later column', () { | ||
testSpans(2, 4, 3, 6); | ||
}); | ||
|
||
test('continued span, later line, earlier column', () { | ||
testSpans(2, 4, 5, 2); | ||
}); | ||
|
||
test('continued span, later line, later column', () { | ||
testSpans(2, 4, 5, 6); | ||
}); | ||
} | ||
|
||
bool isBefore(int line1, int column1, int line2, int column2) { | ||
return line1 < line2 || line1 == line2 && column1 < column2; | ||
} |
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.
Uh oh!
There was an error while loading. Please reload this page.