Skip to content

Commit

Permalink
fix: generateCodeFrame infinite loop
Browse files Browse the repository at this point in the history
  • Loading branch information
sapphi-red committed Nov 22, 2023
1 parent bcc4a61 commit 1fa00e7
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,24 @@ exports[`generateCodeFrames > end 3`] = `
"
`;

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

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

exports[`generateCodeFrames > range 1`] = `
"
1 |
Expand All @@ -47,13 +65,21 @@ exports[`generateCodeFrames > start with number 1`] = `
exports[`generateCodeFrames > start with number 2`] = `
"
1 | import foo from './foo'
| ^
| ^
2 | foo()
"
`;

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

exports[`generateCodeFrames > start with number 4`] = `
"
1 | import foo from './foo'
2 | foo()
| ^
Expand Down
3 changes: 3 additions & 0 deletions packages/vite/src/node/__tests__/utils.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,7 @@ foo()
}

test('start with number', () => {
expectSnapshot(generateCodeFrame(source, -1))
expectSnapshot(generateCodeFrame(source, 0))
expectSnapshot(generateCodeFrame(source, 1))
expectSnapshot(generateCodeFrame(source, 24))
Expand All @@ -223,6 +224,8 @@ foo()
expectSnapshot(generateCodeFrame(source, 0, 0))
expectSnapshot(generateCodeFrame(source, 0, 23))
expectSnapshot(generateCodeFrame(source, 0, 29))
expectSnapshot(generateCodeFrame(source, 0, source.length))
expectSnapshot(generateCodeFrame(source, 0, source.length + 1))
})

test('range', () => {
Expand Down
1 change: 1 addition & 0 deletions packages/vite/src/node/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -530,6 +530,7 @@ export function generateCodeFrame(
end > count ? lineLength - pad : end - start,
)
res.push(` | ` + ' '.repeat(pad) + '^'.repeat(length))
count++
} else if (j > i) {
if (end > count) {
const length = Math.max(Math.min(end - count, lineLength), 1)
Expand Down

0 comments on commit 1fa00e7

Please sign in to comment.