From 4ccc4331d6d1e89dd27e9ecdc38d709749861d4f Mon Sep 17 00:00:00 2001 From: sapphi-red <49056869+sapphi-red@users.noreply.github.com> Date: Wed, 30 Jul 2025 21:21:45 +0900 Subject: [PATCH 1/3] feat: support files with more than 1000 lines by `generateCodeFrame` --- .../__tests__/__snapshots__/utils.spec.ts.snap | 11 +++++++++++ packages/vite/src/node/__tests__/utils.spec.ts | 7 +++++++ packages/vite/src/node/utils.ts | 15 ++++++++++++--- 3 files changed, 30 insertions(+), 3 deletions(-) diff --git a/packages/vite/src/node/__tests__/__snapshots__/utils.spec.ts.snap b/packages/vite/src/node/__tests__/__snapshots__/utils.spec.ts.snap index 7692ad1d7b223f..9d6679a37e09c7 100644 --- a/packages/vite/src/node/__tests__/__snapshots__/utils.spec.ts.snap +++ b/packages/vite/src/node/__tests__/__snapshots__/utils.spec.ts.snap @@ -139,6 +139,17 @@ exports[`generateCodeFrames > start with position 3`] = ` " `; +exports[`generateCodeFrames > supports more than 1000 lines 1`] = ` +" +1198| // 1197 +1199| // 1198 +1200| // 1199 + | ^ +1201| // 1200 +1202| // 1201 +" +`; + exports[`generateCodeFrames > works with CRLF 1`] = ` " 1 | import foo from './foo' diff --git a/packages/vite/src/node/__tests__/utils.spec.ts b/packages/vite/src/node/__tests__/utils.spec.ts index 0996b9e712d8b9..9dbf6784994f07 100644 --- a/packages/vite/src/node/__tests__/utils.spec.ts +++ b/packages/vite/src/node/__tests__/utils.spec.ts @@ -288,6 +288,9 @@ foo() // 2 // 3 `.trim() + const veryLongSource = Array.from({ length: 2000 }, (_, i) => `// ${i}`).join( + '\n', + ) const expectSnapshot = (value: string) => { try { @@ -340,6 +343,10 @@ foo() test('invalid start > end', () => { expectSnapshot(generateCodeFrame(source, 2, 0)) }) + + test('supports more than 1000 lines', () => { + expectSnapshot(generateCodeFrame(veryLongSource, { line: 1200, column: 0 })) + }) }) describe('getHash', () => { diff --git a/packages/vite/src/node/utils.ts b/packages/vite/src/node/utils.ts index 882aa00e03f170..3931e71669a90a 100644 --- a/packages/vite/src/node/utils.ts +++ b/packages/vite/src/node/utils.ts @@ -511,6 +511,11 @@ export function generateCodeFrame( end !== undefined ? posToNumber(source, end) : start, source.length, ) + const lastPosLine = + end !== undefined + ? numberToPos(source, end).line + : numberToPos(source, start).line + range + const lineNumberWidth = Math.max(3, Math.floor(Math.log10(lastPosLine)) + 1) const lines = source.split(splitRE) let count = 0 const res: string[] = [] @@ -521,7 +526,7 @@ export function generateCodeFrame( if (j < 0 || j >= lines.length) continue const line = j + 1 res.push( - `${line}${' '.repeat(Math.max(3 - String(line).length, 0))}| ${ + `${line}${' '.repeat(lineNumberWidth - String(line).length)}| ${ lines[j] }`, ) @@ -533,11 +538,15 @@ export function generateCodeFrame( 1, end > count ? lineLength - pad : end - start, ) - res.push(` | ` + ' '.repeat(pad) + '^'.repeat(length)) + res.push( + `${' '.repeat(lineNumberWidth)}| ` + + ' '.repeat(pad) + + '^'.repeat(length), + ) } else if (j > i) { if (end > count) { const length = Math.max(Math.min(end - count, lineLength), 1) - res.push(` | ` + '^'.repeat(length)) + res.push(`${' '.repeat(lineNumberWidth)}| ` + '^'.repeat(length)) } count += lineLength + 1 } From e6799fcc1c3527e60e02bc2e0bb2a0000a5d4d6b Mon Sep 17 00:00:00 2001 From: sapphi-red <49056869+sapphi-red@users.noreply.github.com> Date: Tue, 5 Aug 2025 13:21:48 +0900 Subject: [PATCH 2/3] refactor: use `String().length` --- packages/vite/src/node/utils.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/vite/src/node/utils.ts b/packages/vite/src/node/utils.ts index 3931e71669a90a..a48d60c3966163 100644 --- a/packages/vite/src/node/utils.ts +++ b/packages/vite/src/node/utils.ts @@ -515,7 +515,7 @@ export function generateCodeFrame( end !== undefined ? numberToPos(source, end).line : numberToPos(source, start).line + range - const lineNumberWidth = Math.max(3, Math.floor(Math.log10(lastPosLine)) + 1) + const lineNumberWidth = Math.max(3, String(lastPosLine).length) const lines = source.split(splitRE) let count = 0 const res: string[] = [] From 86120246d1a4f3a6b4124678c89a1a78a9875497 Mon Sep 17 00:00:00 2001 From: sapphi-red <49056869+sapphi-red@users.noreply.github.com> Date: Tue, 5 Aug 2025 13:23:24 +0900 Subject: [PATCH 3/3] feat: add a space after the number --- .../node/__tests__/__snapshots__/utils.spec.ts.snap | 12 ++++++------ packages/vite/src/node/utils.ts | 2 +- 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/packages/vite/src/node/__tests__/__snapshots__/utils.spec.ts.snap b/packages/vite/src/node/__tests__/__snapshots__/utils.spec.ts.snap index 9d6679a37e09c7..806e9eba2be8ce 100644 --- a/packages/vite/src/node/__tests__/__snapshots__/utils.spec.ts.snap +++ b/packages/vite/src/node/__tests__/__snapshots__/utils.spec.ts.snap @@ -141,12 +141,12 @@ exports[`generateCodeFrames > start with position 3`] = ` exports[`generateCodeFrames > supports more than 1000 lines 1`] = ` " -1198| // 1197 -1199| // 1198 -1200| // 1199 - | ^ -1201| // 1200 -1202| // 1201 +1198 | // 1197 +1199 | // 1198 +1200 | // 1199 + | ^ +1201 | // 1200 +1202 | // 1201 " `; diff --git a/packages/vite/src/node/utils.ts b/packages/vite/src/node/utils.ts index a48d60c3966163..4b0094aff46133 100644 --- a/packages/vite/src/node/utils.ts +++ b/packages/vite/src/node/utils.ts @@ -515,7 +515,7 @@ export function generateCodeFrame( end !== undefined ? numberToPos(source, end).line : numberToPos(source, start).line + range - const lineNumberWidth = Math.max(3, String(lastPosLine).length) + const lineNumberWidth = Math.max(3, String(lastPosLine).length + 1) const lines = source.split(splitRE) let count = 0 const res: string[] = []