Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: indicator position of error message #3855

Merged
merged 7 commits into from Aug 3, 2023
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 3 additions & 2 deletions packages/vitest/src/node/error.ts
Expand Up @@ -251,6 +251,7 @@ export function generateCodeFrame(
const start = positionToOffset(source, lineNumber, columnNumber)
const end = start
const lines = source.split(lineSplitRE)
const nl = /\r\n/.test(source) ? 2 : 1
let count = 0
let res: string[] = []

Expand All @@ -261,7 +262,7 @@ export function generateCodeFrame(
}

for (let i = 0; i < lines.length; i++) {
count += lines[i].length + 1
count += lines[i].length + nl
if (count >= start) {
for (let j = i - range; j <= i + range || end > count; j++) {
if (j < 0 || j >= lines.length)
Expand All @@ -277,7 +278,7 @@ export function generateCodeFrame(

if (j === i) {
// push underline
const pad = start - (count - lineLength)
const pad = start - (count - lineLength) + (nl - 1)
const length = Math.max(1, end > count ? lineLength - pad : end - start)
res.push(lineNo() + ' '.repeat(pad) + c.red('^'.repeat(length)))
}
Expand Down