Skip to content

Commit 9913672

Browse files
nyxst4ckbluwy
andauthored
fix: handle CRLF line endings in code frame positions (#23219)
Co-authored-by: nyxst4ck <[email protected]> Co-authored-by: Bjorn Lu <[email protected]>
1 parent c8f7b28 commit 9913672

3 files changed

Lines changed: 48 additions & 3 deletions

File tree

‎packages/vite/src/node/__tests__/__snapshots__/utils.spec.ts.snap‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -213,3 +213,16 @@ exports[`generateCodeFrames > works with CRLF 1`] = `
213213
| ^
214214
"
215215
`;
216+
217+
exports[`generateCodeFrames > works with CRLF given a range 1`] = `
218+
"
219+
1 | import foo from './foo'
220+
2 |
221+
3 | foo()
222+
| ^^^^^
223+
4 | // 1
224+
| ^^^^
225+
5 | // 2
226+
| ^^
227+
"
228+
`;

‎packages/vite/src/node/__tests__/utils.spec.ts‎

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -331,6 +331,10 @@ describe('posToNumber', () => {
331331
const actual = posToNumber('a\n\nb', { line: 3, column: 0 })
332332
expect(actual).toBe(3)
333333
})
334+
test('crlf', () => {
335+
const actual = posToNumber('a\r\nb', { line: 2, column: 0 })
336+
expect(actual).toBe(3)
337+
})
334338
test('out of range', () => {
335339
const actual = posToNumber('a\nb', { line: 4, column: 0 })
336340
expect(actual).toBe(4)
@@ -413,6 +417,25 @@ foo()
413417
expectSnapshot(generateCodeFrame(sourceCrLf, { line: 2, column: 0 }))
414418
})
415419

420+
test('works with CRLF given an offset', () => {
421+
const longSourceCrLf = longSource.replaceAll('\n', '\r\n')
422+
// the frame should point to the same location regardless of the line endings
423+
expect(
424+
generateCodeFrame(longSourceCrLf, longSourceCrLf.indexOf('// 3')),
425+
).toBe(generateCodeFrame(longSource, longSource.indexOf('// 3')))
426+
})
427+
428+
test('works with CRLF given a range', () => {
429+
const longSourceCrLf = longSource.replaceAll('\n', '\r\n')
430+
expectSnapshot(
431+
generateCodeFrame(
432+
longSourceCrLf,
433+
longSourceCrLf.indexOf('foo()'),
434+
longSourceCrLf.indexOf('// 2'),
435+
),
436+
)
437+
})
438+
416439
test('end', () => {
417440
expectSnapshot(generateCodeFrame(source, 0, 0))
418441
expectSnapshot(generateCodeFrame(source, 0, 23))

‎packages/vite/src/node/utils.ts‎

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -483,6 +483,14 @@ export const splitRE: RegExp = /\r?\n/g
483483

484484
const range: number = 2
485485

486+
/**
487+
* `splitRE` treats both LF and CRLF as a line terminator, so the terminator
488+
* following a line is 2 characters long when the source uses CRLF.
489+
*/
490+
function lineTerminatorLengthAt(source: string, index: number): number {
491+
return source[index] === '\r' ? 2 : 1
492+
}
493+
486494
export function pad(source: string, n = 2): string {
487495
const lines = source.split(splitRE)
488496
return lines.map((l) => ` `.repeat(n) + l).join(`\n`)
@@ -501,7 +509,8 @@ export function posToNumber(source: string, pos: number | Pos): number {
501509
const { line, column } = pos
502510
let start = 0
503511
for (let i = 0; i < line - 1 && i < lines.length; i++) {
504-
start += lines[i].length + 1
512+
start += lines[i].length
513+
start += lineTerminatorLengthAt(source, start)
505514
}
506515
return start + column
507516
}
@@ -594,12 +603,12 @@ export function generateCodeFrame(
594603
const underline = '^'.repeat(Math.min(length, MAX_DISPLAY_LEN))
595604
res.push(`${' '.repeat(lineNumberWidth)}| ` + underline)
596605
}
597-
count += lineLength + 1
606+
count += lineTerminatorLengthAt(source, count) + lineLength
598607
}
599608
}
600609
break
601610
}
602-
count++
611+
count += lineTerminatorLengthAt(source, count)
603612
}
604613
return res.join('\n')
605614
}

0 commit comments

Comments
 (0)