Skip to content

Commit

Permalink
fix: generateCodeFrame infinite loop (#15093)
Browse files Browse the repository at this point in the history
  • Loading branch information
sapphi-red authored Nov 22, 2023
1 parent fd31412 commit 6619de7
Show file tree
Hide file tree
Showing 3 changed files with 85 additions and 12 deletions.
67 changes: 61 additions & 6 deletions packages/vite/src/node/__tests__/__snapshots__/utils.spec.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,61 @@ 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 > end 6`] = `
"
1 | import foo from './foo'
| ^^^^^^^^^^^^^^^^^^^^^^^
2 | foo()
| ^^^^^
"
`;

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

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

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

Expand All @@ -47,13 +94,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
23 changes: 19 additions & 4 deletions packages/vite/src/node/__tests__/utils.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -187,10 +187,10 @@ foo()
import foo from './foo'
foo()
// bar
// baz
`
// 1
// 2
// 3
`.trim()

const expectSnapshot = (value: string) => {
try {
Expand All @@ -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,10 +224,24 @@ 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))
expectSnapshot(generateCodeFrame(source, 0, source.length + 100))
})

test('range', () => {
expectSnapshot(generateCodeFrame(longSource, { line: 3, column: 0 }))
expectSnapshot(
generateCodeFrame(
longSource,
{ line: 3, column: 0 },
{ line: 4, column: 0 },
),
)
})

test('invalid start > end', () => {
expectSnapshot(generateCodeFrame(source, 2, 0))
})
})

Expand Down
7 changes: 5 additions & 2 deletions packages/vite/src/node/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -505,8 +505,11 @@ export function generateCodeFrame(
start: number | Pos = 0,
end?: number | Pos,
): string {
start = posToNumber(source, start)
end = end !== undefined ? posToNumber(source, end) : start
start = Math.max(posToNumber(source, start), 0)
end = Math.min(
end !== undefined ? posToNumber(source, end) : start,
source.length,
)
const lines = source.split(splitRE)
let count = 0
const res: string[] = []
Expand Down

0 comments on commit 6619de7

Please sign in to comment.