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’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Illegal return statement outside of a function when used with Biome and Astro #52

Open
jaredLunde opened this issue Sep 10, 2024 · 9 comments

Comments

@jaredLunde
Copy link

jaredLunde commented Sep 10, 2024

This error is encountered when formatting JavaScript in Astro frontmatter with Biome as the external formatter.

Message: failed to format code with external formatter:
parse ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

  × Illegal return statement outside of a function

Biome itself can handle parsing frontmatter return statements provided that the language is "astro": https://github.com/biomejs/biome/pull/2273/files

Edit:
I see this is known:

// dprint-plugin-biome doesn't support top-level return (but Biome supports),
// so skip it.

Is there a workaround or ignore statement that can be used?


Biome version: 1.8.4
markup_fmt version: 0.12.0

@g-plane
Copy link
Owner

g-plane commented Sep 10, 2024

There may be a bug in Biome or dprint-plugin-biome. It's a known issue and I even ignore this case in tests: https://github.com/g-plane/markup_fmt/blob/main/dprint_plugin/tests/integration.rs#L157-L161 .

@jaredLunde
Copy link
Author

I think the issue is this:

  1. You're choosing the the extension "tsx" for frontmatter in Astro (for good reason that will follow)

    if matches!(ctx.language, Language::Astro) {
    let formatted = ctx.format_script(self.raw, "tsx", self.start);
    Doc::text("---")
    .append(Doc::hard_line())
    .concat(reflow_with_indent(formatted.trim()))
    .append(Doc::hard_line())
    .append(Doc::text("---"))
    } else {

  2. dprint-plugin-biome does not support the astro extension. Only these:
    https://github.com/dprint/dprint-plugin-biome/blob/main/src/format_text.rs#L58-L76

  3. For Biome to correctly parse the syntax for Astro frontmatter scripts, it requires the astro extension.

Here is the exception they've carved out for Astro:
https://github.com/biomejs/biome/blob/6a54b27ec740e6a286fd88518e21bc661194857d/crates/biome_js_parser/src/syntax/stmt.rs#L728-L729

When dprint-plugin-biome checks the syntax it calls JsFileSource::try_from(file_path) and winds up here.
https://github.com/biomejs/biome/blob/7ffc53fab4160a8851348c1d59f80ed2382ad6b0/crates/biome_js_syntax/src/file_source.rs#L311


So for this to work, dprint-plugin-biome would need to support astro, vue, and svelte extensions and markup_fmt would have to pass the correct file extension to dprint-plugin-biome instead of "tsx".

@jaredLunde
Copy link
Author

I submitted a PR in dprint-plugin-biome that would allow you to use those file extensions: dprint/dprint-plugin-biome#16

@g-plane
Copy link
Owner

g-plane commented Sep 11, 2024

There's no way to know whether user is using Biome or not for choosing file extension.

@jaredLunde
Copy link
Author

jaredLunde commented Sep 11, 2024

Could you maybe add an option to your dprint config? You wouldn't know for certain obviously, but it wouldn't be the plugin's fault if someone opted into something they weren't using.

Alternatively, we could add something to your plugin documentation that calls out this core Astro functionality can't be supported when using the Biome plugin.

@g-plane
Copy link
Owner

g-plane commented Sep 11, 2024

Updating documentation sounds good, while adding an option just for "fixing some problems" isn't an ideal solution.

Also, are there anything can do in dprint-plugin-biome?

@jaredLunde
Copy link
Author

I don't think so. It receives the file path from your plugin and ultimately that determines which syntax is used in Biome.

@jaredLunde
Copy link
Author

Here is what the Astro prettier plugin does: https://github.com/withastro/prettier-plugin-astro/?tab=readme-ov-file#astro-skip-frontmatter

This seems reasonable to me as a general configuration option, since Biome can run on Astro files by itself.

@g-plane
Copy link
Owner

g-plane commented Sep 11, 2024

No, they are different cases.

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

No branches or pull requests

2 participants