0
Fork 0
mirror of https://github.com/withastro/astro.git synced 2024-12-23 21:53:55 -05:00

fix(#5922): improve top-level return error (#6036)

Co-authored-by: Nate Moore <nate@astro.build>
This commit is contained in:
Nate Moore 2023-01-30 11:37:13 -06:00 committed by GitHub
parent fbb34e1ef6
commit e779c62424
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 14 additions and 2 deletions

View file

@ -0,0 +1,5 @@
---
'astro': patch
---
Improve error handling when top-level `return` is present

View file

@ -93,7 +93,8 @@ async function enhanceCompileError({
source, source,
config, config,
logging, logging,
}: EnhanceCompilerErrorOptions): Promise<never> { }: EnhanceCompilerErrorOptions): Promise<void> {
const lineText = (err as any).loc?.lineText;
// Verify frontmatter: a common reason that this plugin fails is that // Verify frontmatter: a common reason that this plugin fails is that
// the user provided invalid JS/TS in the component frontmatter. // the user provided invalid JS/TS in the component frontmatter.
// If the frontmatter is invalid, the `err` object may be a compiler // If the frontmatter is invalid, the `err` object may be a compiler
@ -104,8 +105,14 @@ async function enhanceCompileError({
// If frontmatter is valid or cannot be parsed, then continue. // If frontmatter is valid or cannot be parsed, then continue.
const scannedFrontmatter = FRONTMATTER_PARSE_REGEXP.exec(source); const scannedFrontmatter = FRONTMATTER_PARSE_REGEXP.exec(source);
if (scannedFrontmatter) { if (scannedFrontmatter) {
// Top-level return is not supported, so replace `return` with throw
const frontmatter = scannedFrontmatter[1].replace(/\breturn\b/g, 'throw');
// If frontmatter does not actually include the offending line, skip
if (lineText && !frontmatter.includes(lineText)) throw err;
try { try {
await transformWithEsbuild(scannedFrontmatter[1], id, { await transformWithEsbuild(frontmatter, id, {
loader: 'ts', loader: 'ts',
target: 'esnext', target: 'esnext',
sourcemap: false, sourcemap: false,