summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGravatar Nate Moore <natemoo-re@users.noreply.github.com> 2023-01-30 11:37:13 -0600
committerGravatar GitHub <noreply@github.com> 2023-01-30 11:37:13 -0600
commite779c6242418d1d4102e683ca5b851b764c89688 (patch)
treed9003c0bd00ac4af61f8ab8935d2b1e53a50fe20
parentfbb34e1ef60a30de9f51b564bfd2ac6c57fa13b2 (diff)
downloadastro-e779c6242418d1d4102e683ca5b851b764c89688.tar.gz
astro-e779c6242418d1d4102e683ca5b851b764c89688.tar.zst
astro-e779c6242418d1d4102e683ca5b851b764c89688.zip
fix(#5922): improve top-level return error (#6036)
Co-authored-by: Nate Moore <nate@astro.build>
-rw-r--r--.changeset/neat-actors-switch.md5
-rw-r--r--packages/astro/src/vite-plugin-astro/compile.ts11
2 files changed, 14 insertions, 2 deletions
diff --git a/.changeset/neat-actors-switch.md b/.changeset/neat-actors-switch.md
new file mode 100644
index 000000000..208576317
--- /dev/null
+++ b/.changeset/neat-actors-switch.md
@@ -0,0 +1,5 @@
+---
+'astro': patch
+---
+
+Improve error handling when top-level `return` is present
diff --git a/packages/astro/src/vite-plugin-astro/compile.ts b/packages/astro/src/vite-plugin-astro/compile.ts
index b3c85373b..34fe645e5 100644
--- a/packages/astro/src/vite-plugin-astro/compile.ts
+++ b/packages/astro/src/vite-plugin-astro/compile.ts
@@ -93,7 +93,8 @@ async function enhanceCompileError({
source,
config,
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
// the user provided invalid JS/TS in the component frontmatter.
// 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.
const scannedFrontmatter = FRONTMATTER_PARSE_REGEXP.exec(source);
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 {
- await transformWithEsbuild(scannedFrontmatter[1], id, {
+ await transformWithEsbuild(frontmatter, id, {
loader: 'ts',
target: 'esnext',
sourcemap: false,