Skip to content

Commit e7d0b2a

Browse files
authored
feat: support files with more than 1000 lines by generateCodeFrame (vitejs#20508)
1 parent bac9f3e commit e7d0b2a

File tree

3 files changed

+30
-3
lines changed

3 files changed

+30
-3
lines changed

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

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,17 @@ exports[`generateCodeFrames > start with position 3`] = `
139139
"
140140
`;
141141

142+
exports[`generateCodeFrames > supports more than 1000 lines 1`] = `
143+
"
144+
1198 | // 1197
145+
1199 | // 1198
146+
1200 | // 1199
147+
| ^
148+
1201 | // 1200
149+
1202 | // 1201
150+
"
151+
`;
152+
142153
exports[`generateCodeFrames > works with CRLF 1`] = `
143154
"
144155
1 | import foo from './foo'

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -288,6 +288,9 @@ foo()
288288
// 2
289289
// 3
290290
`.trim()
291+
const veryLongSource = Array.from({ length: 2000 }, (_, i) => `// ${i}`).join(
292+
'\n',
293+
)
291294

292295
const expectSnapshot = (value: string) => {
293296
try {
@@ -340,6 +343,10 @@ foo()
340343
test('invalid start > end', () => {
341344
expectSnapshot(generateCodeFrame(source, 2, 0))
342345
})
346+
347+
test('supports more than 1000 lines', () => {
348+
expectSnapshot(generateCodeFrame(veryLongSource, { line: 1200, column: 0 }))
349+
})
343350
})
344351

345352
describe('getHash', () => {

packages/vite/src/node/utils.ts

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -511,6 +511,11 @@ export function generateCodeFrame(
511511
end !== undefined ? posToNumber(source, end) : start,
512512
source.length,
513513
)
514+
const lastPosLine =
515+
end !== undefined
516+
? numberToPos(source, end).line
517+
: numberToPos(source, start).line + range
518+
const lineNumberWidth = Math.max(3, String(lastPosLine).length + 1)
514519
const lines = source.split(splitRE)
515520
let count = 0
516521
const res: string[] = []
@@ -521,7 +526,7 @@ export function generateCodeFrame(
521526
if (j < 0 || j >= lines.length) continue
522527
const line = j + 1
523528
res.push(
524-
`${line}${' '.repeat(Math.max(3 - String(line).length, 0))}| ${
529+
`${line}${' '.repeat(lineNumberWidth - String(line).length)}| ${
525530
lines[j]
526531
}`,
527532
)
@@ -533,11 +538,15 @@ export function generateCodeFrame(
533538
1,
534539
end > count ? lineLength - pad : end - start,
535540
)
536-
res.push(` | ` + ' '.repeat(pad) + '^'.repeat(length))
541+
res.push(
542+
`${' '.repeat(lineNumberWidth)}| ` +
543+
' '.repeat(pad) +
544+
'^'.repeat(length),
545+
)
537546
} else if (j > i) {
538547
if (end > count) {
539548
const length = Math.max(Math.min(end - count, lineLength), 1)
540-
res.push(` | ` + '^'.repeat(length))
549+
res.push(`${' '.repeat(lineNumberWidth)}| ` + '^'.repeat(length))
541550
}
542551
count += lineLength + 1
543552
}

0 commit comments

Comments
 (0)