Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

馃悰 BUG: Parsing error: 'return' outside of function. #326

Closed
jsun969 opened this issue Feb 3, 2023 · 13 comments 路 Fixed by #336
Closed

馃悰 BUG: Parsing error: 'return' outside of function. #326

jsun969 opened this issue Feb 3, 2023 · 13 comments 路 Fixed by #336

Comments

@jsun969
Copy link

jsun969 commented Feb 3, 2023

Describe the Bug

when I write this in astro

if (!post) {
  return Astro.redirect('/404');
}

Prettier give me an error that I cannot format (eslint just for showing prettier's error):
image

Steps to Reproduce

https://github.com/jsun969/jsundotlol/blob/c901168568b93fd5e7038e1699948bb288869287/src/pages/blog/%5Bslug%5D.astro#L10-L13

@Princesseuh
Copy link
Member

Hello, this error is coming from ESLint, not Prettier. The Prettier plugin is able to format this snippet with no errors.

I suspect that this is a similar issue to #315, where the ESLint plugin does not pass the correct information to the Prettier plugin, or something similar.

Please create an issue on the ESLint plugin: https://github.com/ota-meshi/eslint-plugin-astro

@jsun969
Copy link
Author

jsun969 commented Feb 6, 2023

I think it must be a prettier error. When I run prettier --write

image
image

@Princesseuh
Copy link
Member

Princesseuh commented Feb 6, 2023

Hmm, weird, this successfully format for me 馃 I'll investigate more!

@Princesseuh Princesseuh reopened this Feb 6, 2023
@Princesseuh
Copy link
Member

Tried a bunch of things, including your link and I cannot reproduce, sorry

@jsun969
Copy link
Author

jsun969 commented Feb 6, 2023

Tried a bunch of things, including your link and I cannot reproduce, sorry

Gosh.. Maybe it's just my problem. I feel guilty for wasting your time 馃槗

I'll close this issue. Contact you again after I find a solution. Thanks a lot!

@jsun969 jsun969 closed this as completed Feb 6, 2023
@Princesseuh
Copy link
Member

Princesseuh commented Feb 6, 2023

I feel guilty for wasting your time 馃槗

No worries, I'm paid by the hour 馃槃

@FugiTech
Copy link
Contributor

Sorry to re-open this but I was having the same issue. I've narrowed it down to an interaction between Astro and @trivago/prettier-plugin-sort-imports. It seems like there was an issue filed with prettier-plugin-sort-imports but no progress has been made trivago/prettier-plugin-sort-imports#210

I don't know enough about Prettier to understand if this is an issue entirely in prettier-plugin-sort-imports or in an interaction with it and prettier-plugin-astro.

@jasikpark
Copy link
Collaborator

mind creating an https://astro.new reproduction?

@FugiTech
Copy link
Contributor

https://stackblitz.com/edit/github-pjbsr1?file=src/pages/index.astro

This is based on the "Just the basics" template. All I've done is run npm add prettier prettier-plugin-astro @trivago/prettier-plugin-sort-imports, added a basic .prettierrc.cjs that loads the two plugins, and added if (false) { return Astro.redirect('/404') } to src/pages/index.astro

If you run npx prettier --write src/pages/index.astro you'll see the "return outside of function" error. If you comment out the return line then it'll run as expected. Likewise if you leave the return uncommented but remove the @trivago/prettier-plugin-sort-imports plugin then prettier will run as expected.

@jsun969
Copy link
Author

jsun969 commented Mar 22, 2023

https://stackblitz.com/edit/github-pjbsr1?file=src/pages/index.astro

This is based on the "Just the basics" template. All I've done is run npm add prettier prettier-plugin-astro @trivago/prettier-plugin-sort-imports, added a basic .prettierrc.cjs that loads the two plugins, and added if (false) { return Astro.redirect('/404') } to src/pages/index.astro

If you run npx prettier --write src/pages/index.astro you'll see the "return outside of function" error. If you comment out the return line then it'll run as expected. Likewise if you leave the return uncommented but remove the @trivago/prettier-plugin-sort-imports plugin then prettier will run as expected.

Awesome! Thx a lot!

@jasikpark
Copy link
Collaborator

Thanks for the reproduction, I'll re-open this issue as I can reproduce the bug myself with the stackblitz link 馃憤

@jasikpark jasikpark reopened this Mar 22, 2023
@Princesseuh
Copy link
Member

I am not too sure what the solution can be.. Parsing a top level return always results in an error, it's a design flaw of Astro to have one at the top level (there's a reason other frameworks "throw" their returns, as weird as it looks).

We could fix this in our parser, maybe, but then other plugins would need to be after our parser but before our printer which is wonky in Prettier

@FugiTech
Copy link
Contributor

If Astro wants to deprecate return Astro.redirect(...) and move to throw Astro.redirect(...) then I see no problem with that.

Otherwise it seems like src/printer/embed.ts already has hacks in place for expressions in the html template, so maybe some more hacks could be added for the frontmatter? Might also require changes to the astro compiler as it looks like all of the frontmatter is stored as an individual string.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
4 participants