Skip to content

Commit b865b32

Browse files
petebacondarwinjosephperrott
authored andcommitted
fix(compiler-cli): handle \r\n line-endings correctly in source-mapping (#40187)
Previously `\r\n` was being treated as a single character in source-map line start positions, which caused segment positions to become offset. Now the `\r` is ignored when splitting, leaving it at the end of the previous line, which solves the offsetting problem, and does not affect source-mappings. Fixes #40169 Fixes #39654 PR Close #40187
1 parent 632fe60 commit b865b32

File tree

3 files changed

+18
-16
lines changed

3 files changed

+18
-16
lines changed

packages/compiler-cli/src/ngtsc/sourcemaps/src/source_file.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -446,5 +446,5 @@ export function computeStartOfLinePositions(str: string) {
446446
}
447447

448448
function computeLineLengths(str: string): number[] {
449-
return (str.split(/\r?\n/)).map(s => s.length);
449+
return (str.split(/\n/)).map(s => s.length);
450450
}

packages/compiler-cli/src/ngtsc/sourcemaps/test/segment_marker_spec.ts

+14-13
Original file line numberDiff line numberDiff line change
@@ -79,31 +79,32 @@ describe('SegmentMarker utils', () => {
7979
it('should return a new marker offset by the given chars', () => {
8080
const startOfLinePositions =
8181
computeStartOfLinePositions('012345\n0123456789\r\n012*4567\n0123456');
82-
const marker = {line: 2, column: 3, position: 21, next: undefined};
82+
const marker = {line: 2, column: 3, position: 22, next: undefined};
83+
8384
expect(offsetSegment(startOfLinePositions, marker, 1))
84-
.toEqual({line: 2, column: 4, position: 22, next: undefined});
85+
.toEqual({line: 2, column: 4, position: 23, next: undefined});
8586
expect(offsetSegment(startOfLinePositions, marker, 2))
86-
.toEqual({line: 2, column: 5, position: 23, next: undefined});
87+
.toEqual({line: 2, column: 5, position: 24, next: undefined});
8788
expect(offsetSegment(startOfLinePositions, marker, 4))
88-
.toEqual({line: 2, column: 7, position: 25, next: undefined});
89+
.toEqual({line: 2, column: 7, position: 26, next: undefined});
8990
expect(offsetSegment(startOfLinePositions, marker, 6))
90-
.toEqual({line: 3, column: 0, position: 27, next: undefined});
91+
.toEqual({line: 3, column: 0, position: 28, next: undefined});
9192
expect(offsetSegment(startOfLinePositions, marker, 8))
92-
.toEqual({line: 3, column: 2, position: 29, next: undefined});
93+
.toEqual({line: 3, column: 2, position: 30, next: undefined});
9394
expect(offsetSegment(startOfLinePositions, marker, 20))
94-
.toEqual({line: 3, column: 14, position: 41, next: undefined});
95+
.toEqual({line: 3, column: 14, position: 42, next: undefined});
9596
expect(offsetSegment(startOfLinePositions, marker, -1))
96-
.toEqual({line: 2, column: 2, position: 20, next: undefined});
97+
.toEqual({line: 2, column: 2, position: 21, next: undefined});
9798
expect(offsetSegment(startOfLinePositions, marker, -2))
98-
.toEqual({line: 2, column: 1, position: 19, next: undefined});
99+
.toEqual({line: 2, column: 1, position: 20, next: undefined});
99100
expect(offsetSegment(startOfLinePositions, marker, -3))
100-
.toEqual({line: 2, column: 0, position: 18, next: undefined});
101+
.toEqual({line: 2, column: 0, position: 19, next: undefined});
101102
expect(offsetSegment(startOfLinePositions, marker, -4))
102-
.toEqual({line: 1, column: 10, position: 17, next: undefined});
103+
.toEqual({line: 1, column: 11, position: 18, next: undefined});
103104
expect(offsetSegment(startOfLinePositions, marker, -6))
104-
.toEqual({line: 1, column: 8, position: 15, next: undefined});
105+
.toEqual({line: 1, column: 9, position: 16, next: undefined});
105106
expect(offsetSegment(startOfLinePositions, marker, -16))
106-
.toEqual({line: 0, column: 5, position: 5, next: undefined});
107+
.toEqual({line: 0, column: 6, position: 6, next: undefined});
107108
});
108109
});
109110
});

packages/compiler-cli/src/ngtsc/sourcemaps/test/source_file_spec.ts

+3-2
Original file line numberDiff line numberDiff line change
@@ -677,8 +677,9 @@ runInEachFileSystem(() => {
677677
expect(computeStartOfLinePositions('abc\n')).toEqual([0, 4]);
678678
expect(computeStartOfLinePositions('\nabc')).toEqual([0, 1]);
679679
expect(computeStartOfLinePositions('abc\ndefg')).toEqual([0, 4]);
680-
expect(computeStartOfLinePositions('abc\r\n')).toEqual([0, 4]);
681-
expect(computeStartOfLinePositions('abc\r\ndefg')).toEqual([0, 4]);
680+
expect(computeStartOfLinePositions('abc\r\n')).toEqual([0, 5]);
681+
expect(computeStartOfLinePositions('abc\r\ndefg')).toEqual([0, 5]);
682+
expect(computeStartOfLinePositions('abc\uD83D\uDE80\ndef🚀\r\n')).toEqual([0, 6, 13]);
682683
});
683684
});
684685
});

0 commit comments

Comments
 (0)