|
| 1 | +// Copyright (c) 2025, the Dart project authors. Please see the AUTHORS file |
| 2 | +// for details. All rights reserved. Use of this source code is governed by a |
| 3 | +// BSD-style license that can be found in the LICENSE file. |
| 4 | + |
| 5 | +import 'package:source_maps/source_maps.dart'; |
| 6 | +import 'package:source_span/source_span.dart'; |
| 7 | +import 'package:test/test.dart'; |
| 8 | + |
| 9 | +void main() { |
| 10 | + /// This is a test for spans of the generated file that continue over several |
| 11 | + /// lines. |
| 12 | + /// |
| 13 | + /// In a sourcemap, a span continues from the start encoded position until the |
| 14 | + /// next position, regardless of whether the second position in on the same |
| 15 | + /// line in the generated file or a subsequent line. |
| 16 | + void testSpans(int lineA, int columnA, int lineB, int columnB) { |
| 17 | + // Create a sourcemap describing a 'rectangular' generated file with three |
| 18 | + // spans, each potentially over several lines: (1) an initial span that is |
| 19 | + // unmapped, (2) a span that maps to file 'A', the span continuing until (3) |
| 20 | + // a span that maps to file 'B'. |
| 21 | + // |
| 22 | + // We can describe the mapping by an 'image' of the generated file, where |
| 23 | + // the positions marked as 'A' in the 'image' correspond to locations in the |
| 24 | + // generated file that map to locations in source file 'A'. Lines and |
| 25 | + // columns are zero-based. |
| 26 | + // |
| 27 | + // 0123456789 |
| 28 | + // 0: ---------- |
| 29 | + // 1: ----AAAAAA lineA: 1, columnA: 4, i.e. locationA |
| 30 | + // 2: AABBBBBBBB lineB: 2, columnB: 2, i.e. locationB |
| 31 | + // 3: BBBBBBBBBB |
| 32 | + // |
| 33 | + // Once we have the mapping, we probe every position in a 8x10 rectangle to |
| 34 | + // validate that it maps to the intended original source file. |
| 35 | + |
| 36 | + expect(isBefore(lineB, columnB, lineA, columnA), isFalse, |
| 37 | + reason: 'Test valid only for ordered positions'); |
| 38 | + |
| 39 | + SourceLocation location(Uri? uri, int line, int column) { |
| 40 | + final offset = line * 10 + column; |
| 41 | + return SourceLocation(offset, sourceUrl: uri, line: line, column: column); |
| 42 | + } |
| 43 | + |
| 44 | + // Locations in the generated file. |
| 45 | + final uriMap = Uri.parse('output.js.map'); |
| 46 | + final locationA = location(uriMap, lineA, columnA); |
| 47 | + final locationB = location(uriMap, lineB, columnB); |
| 48 | + |
| 49 | + // Original source locations. |
| 50 | + final sourceA = location(Uri.parse('A'), 0, 0); |
| 51 | + final sourceB = location(Uri.parse('B'), 0, 0); |
| 52 | + |
| 53 | + final json = (SourceMapBuilder() |
| 54 | + ..addLocation(sourceA, locationA, null) |
| 55 | + ..addLocation(sourceB, locationB, null)) |
| 56 | + .build(uriMap.toString()); |
| 57 | + |
| 58 | + final mapping = parseJson(json); |
| 59 | + |
| 60 | + // Validate by comparing 'images' of the generate file. |
| 61 | + final expectedImage = StringBuffer(); |
| 62 | + final actualImage = StringBuffer(); |
| 63 | + |
| 64 | + for (var line = 0; line < 8; line++) { |
| 65 | + for (var column = 0; column < 10; column++) { |
| 66 | + final span = mapping.spanFor(line, column); |
| 67 | + final expected = isBefore(line, column, lineA, columnA) |
| 68 | + ? '-' |
| 69 | + : isBefore(line, column, lineB, columnB) |
| 70 | + ? 'A' |
| 71 | + : 'B'; |
| 72 | + final actual = span?.start.sourceUrl?.path ?? '-'; // Unmapped -> '-'. |
| 73 | + |
| 74 | + expectedImage.write(expected); |
| 75 | + actualImage.write(actual); |
| 76 | + } |
| 77 | + expectedImage.writeln(); |
| 78 | + actualImage.writeln(); |
| 79 | + } |
| 80 | + expect(actualImage.toString(), expectedImage.toString()); |
| 81 | + } |
| 82 | + |
| 83 | + test('continued span, same position', () { |
| 84 | + testSpans(2, 4, 2, 4); |
| 85 | + }); |
| 86 | + |
| 87 | + test('continued span, same line', () { |
| 88 | + testSpans(2, 4, 2, 7); |
| 89 | + }); |
| 90 | + |
| 91 | + test('continued span, next line, earlier column', () { |
| 92 | + testSpans(2, 4, 3, 2); |
| 93 | + }); |
| 94 | + |
| 95 | + test('continued span, next line, later column', () { |
| 96 | + testSpans(2, 4, 3, 6); |
| 97 | + }); |
| 98 | + |
| 99 | + test('continued span, later line, earlier column', () { |
| 100 | + testSpans(2, 4, 5, 2); |
| 101 | + }); |
| 102 | + |
| 103 | + test('continued span, later line, later column', () { |
| 104 | + testSpans(2, 4, 5, 6); |
| 105 | + }); |
| 106 | +} |
| 107 | + |
| 108 | +bool isBefore(int line1, int column1, int line2, int column2) { |
| 109 | + return line1 < line2 || line1 == line2 && column1 < column2; |
| 110 | +} |
0 commit comments