-
Notifications
You must be signed in to change notification settings - Fork 59
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(tsx): Transform top level returns into throws (#1028)
* fix(tsx): Transform top level returns into throws * fix: logic * nit: use return elngth * Create bright-plums-suffer.md
- Loading branch information
1 parent
2e95e17
commit 7fa6577
Showing
4 changed files
with
125 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
"@astrojs/compiler": patch | ||
--- | ||
|
||
Transform top level returns into throws in the TSX output |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
import { convertToTSX } from '@astrojs/compiler'; | ||
import { test } from 'uvu'; | ||
import * as assert from 'uvu/assert'; | ||
import { TSXPrefix } from '../utils.js'; | ||
|
||
test('transforms top-level returns to throw statements', async () => { | ||
const input = `--- | ||
if (something) { | ||
return Astro.redirect(); | ||
} | ||
function thatDoesSomething() { | ||
return "Hey"; | ||
} | ||
class Component { | ||
render() { | ||
return "wow"! | ||
} | ||
} | ||
---`; | ||
const output = `${TSXPrefix} | ||
if (something) { | ||
throw Astro.redirect(); | ||
} | ||
function thatDoesSomething() { | ||
return "Hey"; | ||
} | ||
class Component { | ||
render() { | ||
return "wow"! | ||
} | ||
} | ||
export default function __AstroComponent_(_props: Record<string, any>): any {} | ||
`; | ||
const { code } = await convertToTSX(input, { sourcemap: 'external' }); | ||
assert.snapshot(code, output, 'expected code to match snapshot'); | ||
}); | ||
|
||
test.run(); |