Skip to content

fix(ssr): format ssrTransform parse error #18644

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

Merged
merged 10 commits into from
Nov 14, 2024
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,17 @@ Expected ";" but found "code"

exports[`parse error 2`] = `
{
"frame": "",
"id": "",
"loc": undefined,
"message": "Expected ';', '}' or <eof>",
"frame": "1 | invalid code
| ^
2 | ",
"id": "/fixtures/errors/syntax-error.js",
"loc": {
"column": 9,
"file": "/fixtures/errors/syntax-error.js",
"line": 1,
},
"message": "Parse failure: Expected ';', '}' or <eof>
At file: /fixtures/errors/syntax-error.js:1:9",
}
`;

Expand All @@ -49,9 +56,16 @@ Expected ";" but found "code"

exports[`parse error 4`] = `
{
"frame": "",
"id": "",
"loc": undefined,
"message": "Expected ';', '}' or <eof>",
"frame": "1 | invalid code
| ^
2 | ",
"id": "/fixtures/errors/syntax-error.js",
"loc": {
"column": 9,
"file": "/fixtures/errors/syntax-error.js",
"line": 1,
},
"message": "Parse failure: Expected ';', '}' or <eof>
At file: /fixtures/errors/syntax-error.js:1:9",
}
`;
31 changes: 21 additions & 10 deletions packages/vite/src/node/ssr/ssrTransform.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,12 @@ import { walk as eswalk } from 'estree-walker'
import type { RawSourceMap } from '@ampproject/remapping'
import { parseAstAsync as rollupParseAstAsync } from 'rollup/parseAst'
import type { TransformResult } from '../server/transformRequest'
import { combineSourcemaps, isDefined } from '../utils'
import {
combineSourcemaps,
generateCodeFrame,
isDefined,
numberToPos,
} from '../utils'
import { isJSONRequest } from '../plugins/json'
import type { DefineImportMetadata } from '../../shared/ssrTransform'

Expand Down Expand Up @@ -81,15 +86,21 @@ async function ssrTransformScript(
try {
ast = await rollupParseAstAsync(code)
} catch (err) {
if (!err.loc || !err.loc.line) throw err
const line = err.loc.line
throw new Error(
`Parse failure: ${
err.message
}\nAt file: ${url}\nContents of line ${line}: ${
code.split('\n')[line - 1]
}`,
)
// enhance known rollup errors
// https://github.com/rollup/rollup/blob/42e587e0e37bc0661aa39fe7ad6f1d7fd33f825c/src/utils/bufferToAst.ts#L17-L22
if (err.code === 'PARSE_ERROR') {
err.message = `Parse failure: ${err.message}\n`
err.id = url
if (typeof err.pos === 'number') {
err.loc = numberToPos(code, err.pos)
err.loc.file = url
err.frame = generateCodeFrame(code, err.pos)
err.message += `At file: ${url}:${err.loc.line}:${err.loc.column}`
} else {
err.message += `At file: ${url}`
}
}
throw err
}

let uid = 0
Expand Down