Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions packages/vite/src/node/__tests__/__snapshots__/utils.spec.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,24 @@ exports[`generateCodeFrames > end 6`] = `
"
`;

exports[`generateCodeFrames > end with CRLF 1`] = `
"
1 | import foo from './foo'
| ^^^^^^^^^^^^^^^^^^^^^^^
2 | foo()
| ^^^^^
"
`;

exports[`generateCodeFrames > end with CRLF 2`] = `
"
1 | import foo from './foo'
| ^^^^^^^^^^^^^^^^^^^^^^^
2 | foo()
| ^^^^^
"
`;

exports[`generateCodeFrames > invalid start > end 1`] = `
"
1 | import foo from './foo'
Expand Down
5 changes: 5 additions & 0 deletions packages/vite/src/node/__tests__/utils.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -361,6 +361,11 @@ foo()
expectSnapshot(generateCodeFrame(sourceCrLf, { line: 2, column: 0 }))
})

test('end with CRLF', () => {
expectSnapshot(generateCodeFrame(sourceCrLf, 0, sourceCrLf.length))
expectSnapshot(generateCodeFrame(sourceCrLf, 0, sourceCrLf.length + 100))
})

test('end', () => {
expectSnapshot(generateCodeFrame(source, 0, 0))
expectSnapshot(generateCodeFrame(source, 0, 23))
Expand Down
12 changes: 11 additions & 1 deletion packages/vite/src/node/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -543,7 +543,17 @@ export function generateCodeFrame(
for (let i = 0; i < lines.length; i++) {
count += lines[i].length
if (count >= start) {
for (let j = i - range; j <= i + range || end > count; j++) {
// `j < lines.length` bounds the second disjunct. Without it, once `j`
// runs past the last line the `continue` below skips the `count`
// update, so `count` freezes while `end` stays larger and the loop
// never terminates. `count` advances by one per line break, but `end`
// is clamped to `source.length`, which counts two characters per CRLF
// -- so on a CRLF source `end` can exceed anything `count` reaches.
for (
let j = i - range;
j <= i + range || (end > count && j < lines.length);
j++
) {
if (j < 0 || j >= lines.length) continue
const line = j + 1
const lineLength = lines[j].length
Expand Down
Loading