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
13 changes: 13 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 @@ -213,3 +213,16 @@ exports[`generateCodeFrames > works with CRLF 1`] = `
| ^
"
`;

exports[`generateCodeFrames > works with CRLF given a range 1`] = `
"
1 | import foo from './foo'
2 |
3 | foo()
| ^^^^^
4 | // 1
| ^^^^
5 | // 2
| ^^
"
`;
23 changes: 23 additions & 0 deletions packages/vite/src/node/__tests__/utils.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -279,6 +279,10 @@ describe('posToNumber', () => {
const actual = posToNumber('a\n\nb', { line: 3, column: 0 })
expect(actual).toBe(3)
})
test('crlf', () => {
const actual = posToNumber('a\r\nb', { line: 2, column: 0 })
expect(actual).toBe(3)
})
test('out of range', () => {
const actual = posToNumber('a\nb', { line: 4, column: 0 })
expect(actual).toBe(4)
Expand Down Expand Up @@ -361,6 +365,25 @@ foo()
expectSnapshot(generateCodeFrame(sourceCrLf, { line: 2, column: 0 }))
})

test('works with CRLF given an offset', () => {
const longSourceCrLf = longSource.replaceAll('\n', '\r\n')
// the frame should point to the same location regardless of the line endings
expect(
generateCodeFrame(longSourceCrLf, longSourceCrLf.indexOf('// 3')),
).toBe(generateCodeFrame(longSource, longSource.indexOf('// 3')))
})

test('works with CRLF given a range', () => {
const longSourceCrLf = longSource.replaceAll('\n', '\r\n')
expectSnapshot(
generateCodeFrame(
longSourceCrLf,
longSourceCrLf.indexOf('foo()'),
longSourceCrLf.indexOf('// 2'),
),
)
})

test('end', () => {
expectSnapshot(generateCodeFrame(source, 0, 0))
expectSnapshot(generateCodeFrame(source, 0, 23))
Expand Down
15 changes: 12 additions & 3 deletions packages/vite/src/node/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -481,6 +481,14 @@ export const splitRE: RegExp = /\r?\n/g

const range: number = 2

/**
* `splitRE` treats both LF and CRLF as a line terminator, so the terminator
* following a line is 2 characters long when the source uses CRLF.
*/
function lineTerminatorLengthAt(source: string, index: number): number {
return source[index] === '\r' ? 2 : 1
}

export function pad(source: string, n = 2): string {
const lines = source.split(splitRE)
return lines.map((l) => ` `.repeat(n) + l).join(`\n`)
Expand All @@ -499,7 +507,8 @@ export function posToNumber(source: string, pos: number | Pos): number {
const { line, column } = pos
let start = 0
for (let i = 0; i < line - 1 && i < lines.length; i++) {
start += lines[i].length + 1
start += lines[i].length
start += lineTerminatorLengthAt(source, start)
}
return start + column
}
Expand Down Expand Up @@ -592,12 +601,12 @@ export function generateCodeFrame(
const underline = '^'.repeat(Math.min(length, MAX_DISPLAY_LEN))
res.push(`${' '.repeat(lineNumberWidth)}| ` + underline)
}
count += lineLength + 1
count += lineTerminatorLengthAt(source, count) + lineLength
}
}
break
}
count++
count += lineTerminatorLengthAt(source, count)
}
return res.join('\n')
}
Expand Down
Loading