Skip to content

Commit c2b78ea

Browse files
committed
Handles all the line breaks in calculation of line-character position
lineFeed = 0x0A, // \n carriageReturn = 0x0D, // \r lineSeparator = 0x2028, paragraphSeparator = 0x2029, nextLine = 0x0085, Fixes microsoft#131
1 parent 655039c commit c2b78ea

File tree

8 files changed

+412
-17
lines changed

8 files changed

+412
-17
lines changed

src/compiler/emitter.ts

+4-12
Original file line numberDiff line numberDiff line change
@@ -127,18 +127,10 @@ module ts {
127127
function writeLiteral(s: string) {
128128
if (s && s.length) {
129129
write(s);
130-
var pos = 0;
131-
while (pos < s.length) {
132-
switch (s.charCodeAt(pos++)) {
133-
case CharacterCodes.carriageReturn:
134-
if (pos < s.length && s.charCodeAt(pos) === CharacterCodes.lineFeed) {
135-
pos++;
136-
}
137-
case CharacterCodes.lineFeed:
138-
lineCount++;
139-
linePos = output.length - s.length + pos;
140-
break;
141-
}
130+
var lineStartsOfS = getLineStarts(s);
131+
if (lineStartsOfS.length > 1) {
132+
lineCount = lineCount + lineStartsOfS.length - 1;
133+
linePos = linePos = output.length - s.length + lineStartsOfS[lineStartsOfS.length - 1];
142134
}
143135
}
144136
}

src/compiler/scanner.ts

+9-2
Original file line numberDiff line numberDiff line change
@@ -249,7 +249,8 @@ module ts {
249249
var pos = 0;
250250
var lineStart = 0;
251251
while (pos < text.length) {
252-
switch (text.charCodeAt(pos++)) {
252+
var ch = text.charCodeAt(pos++);
253+
switch (ch) {
253254
case CharacterCodes.carriageReturn:
254255
if (text.charCodeAt(pos) === CharacterCodes.lineFeed) {
255256
pos++;
@@ -258,6 +259,12 @@ module ts {
258259
result.push(lineStart);
259260
lineStart = pos;
260261
break;
262+
default:
263+
if (ch > CharacterCodes.maxAsciiCharacter && isLineBreak(ch)) {
264+
result.push(lineStart);
265+
lineStart = pos;
266+
}
267+
break;
261268
}
262269
}
263270
result.push(lineStart);
@@ -298,7 +305,7 @@ module ts {
298305
}
299306

300307
export function isLineBreak(ch: number): boolean {
301-
return ch === CharacterCodes.lineFeed || ch === CharacterCodes.carriageReturn || ch === CharacterCodes.lineSeparator || ch === CharacterCodes.paragraphSeparator;
308+
return ch === CharacterCodes.lineFeed || ch === CharacterCodes.carriageReturn || ch === CharacterCodes.lineSeparator || ch === CharacterCodes.paragraphSeparator || ch === CharacterCodes.nextLine;
302309
}
303310

304311
function isDigit(ch: number): boolean {

src/compiler/types.ts

-3
Original file line numberDiff line numberDiff line change
@@ -1007,9 +1007,6 @@ module ts {
10071007
carriageReturn = 0x0D, // \r
10081008
lineSeparator = 0x2028,
10091009
paragraphSeparator = 0x2029,
1010-
1011-
// REVIEW: do we need to support this? The scanner doesn't, but our IText does. This seems
1012-
// like an odd disparity? (Or maybe it's completely fine for them to be different).
10131010
nextLine = 0x0085,
10141011

10151012
// Unicode 3.0 space characters

tests/baselines/reference/sourceMap-LineBreaks.js

+32
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tests/baselines/reference/sourceMap-LineBreaks.js.map

+2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)