-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support for return in Astro component script (#176)
Addresses #96 (comment) In Astro, it's possible to [return a Response](https://docs.astro.build/en/guides/server-side-rendering/#return-a-response-object) from inside of a component script, without wrapping it in a function. This doesn't look like valid JavaScript, so the babel parser choked on it. This change checks to see if the file was parsed with the astro plugin, and if so, enables the `allowReturnOutsideFunction` parser option. This is admittedly a bit of a band-aid. I think the more correct approach is to parse the code into AST using the configured parser, rather than always using our version of babel. This is what some other prettier plugins do. Some, like tailwindcss, goes so far as to load a list of compatible other prettier plugins to enable cross-compatibility. But, that's a much bigger change that will potentially require a breaking change version bump, so I didn't tackle it here. The other thing I did do here, is update the README a bit. I added `Astro` to our supported frameworks as experimental (there may be other parser options we need to set), and also included support for Svelte, which we've always supported, as far as I know, but don't really test for (it would be good to make some tests sometime).
- Loading branch information
Showing
9 changed files
with
109 additions
and
27 deletions.
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
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
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,29 @@ | ||
// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html | ||
|
||
exports[`return-outside-function.astro - astro-verify > return-outside-function.astro 1`] = ` | ||
--- | ||
import Layout from '../layouts/Layout.astro'; | ||
import type {LayoutType} from '../layouts.ts' | ||
import x from 'y'; | ||
return new Response('This is 404', { status: 404 }); | ||
--- | ||
<Layout title="Hello, world!"> | ||
<main> | ||
Hello, world!</main> | ||
</Layout> | ||
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | ||
--- | ||
import type { LayoutType } from "../layouts.ts"; | ||
import x from "y"; | ||
import Layout from "../layouts/Layout.astro"; | ||
return new Response("This is 404", { status: 404 }); | ||
--- | ||
<Layout title="Hello, world!"> | ||
<main>Hello, world!</main> | ||
</Layout> | ||
`; |
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,12 @@ | ||
import * as plugin from '../../src'; | ||
import { run_spec } from '../../test-setup/run_spec'; | ||
|
||
run_spec(__dirname, ['astro'], { | ||
plugins: ['prettier-plugin-astro', plugin], | ||
importOrder: [ | ||
'<TYPES>', | ||
'<BUILT_IN_MODULES>', | ||
'<THIRD_PARTY_MODULES>', | ||
'^[./]', | ||
], | ||
}); |
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,12 @@ | ||
--- | ||
import Layout from '../layouts/Layout.astro'; | ||
import type {LayoutType} from '../layouts.ts' | ||
import x from 'y'; | ||
return new Response('This is 404', { status: 404 }); | ||
--- | ||
|
||
<Layout title="Hello, world!"> | ||
<main> | ||
Hello, world!</main> | ||
</Layout> |
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