File tree Expand file tree Collapse file tree
astro/src/vite-plugin-astro
test/fixtures/top-level-return/src/pages Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1+ ---
2+ ' @astrojs/cloudflare ' : patch
3+ ' astro ' : patch
4+ ---
5+
6+ Fixes ` return; ` syntax not working in the frontmatter correctly in certain contexts
Original file line number Diff line number Diff line change @@ -110,7 +110,9 @@ async function enhanceCompileError({
110110 const scannedFrontmatter = frontmatterRE . exec ( source ) ;
111111 if ( scannedFrontmatter ) {
112112 // Top-level return is not supported, so replace `return` with throw
113- const frontmatter = scannedFrontmatter [ 1 ] . replace ( / \b r e t u r n \b / g, 'throw' ) ;
113+ const frontmatter = scannedFrontmatter [ 1 ]
114+ . replace ( / \b r e t u r n \s * ; / g, 'throw 0;' )
115+ . replace ( / \b r e t u r n \b / g, 'throw ' ) ;
114116
115117 // If frontmatter does not actually include the offending line, skip
116118 if ( lineText && ! frontmatter . includes ( lineText ) ) throw err ;
Original file line number Diff line number Diff line change @@ -30,7 +30,9 @@ export function astroFrontmatterScanPlugin(): ESBuildPlugin {
3030 //
3131 // Known Limitation: Using regex /\breturn\b/ will incorrectly match
3232 // identifiers like `$return` or aliases like `import { return as ret }`.
33- const contents = frontmatterMatch [ 1 ] . replace ( / \b r e t u r n \b / g, 'throw ' ) ;
33+ const contents = frontmatterMatch [ 1 ]
34+ . replace ( / \b r e t u r n \s * ; / g, 'throw 0;' )
35+ . replace ( / \b r e t u r n \b / g, 'throw ' ) ;
3436
3537 return {
3638 contents,
Original file line number Diff line number Diff line change @@ -13,6 +13,10 @@ if (guard()) {
1313 return Astro .redirect (" /404" )
1414}
1515
16+ // Bare return (no value) — must not produce invalid `throw ;` syntax during dep scanning
17+ const source = " hello" ;
18+ if (! source ) return ;
19+
1620---
1721
1822<html >
You can’t perform that action at this time.
0 commit comments