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

Prettier support for .astro files #106

Merged
merged 22 commits into from
Apr 21, 2021
Merged

Prettier support for .astro files #106

merged 22 commits into from
Apr 21, 2021

Conversation

natemoo-re
Copy link
Member

@natemoo-re natemoo-re commented Apr 16, 2021

Changes

Closes #103. Adds prettier-plugin-astro to format .astro files.

We'll probably want to revisit this prior to launch, as this is not the most elegant solution in the world. But it works with minimal development effort!

This implementation attempts to leverage built-in prettier behavior as much as possible. It does this by doing a parse of the .astro file, then converting any expressions inside of HTML to a <script> to allow Prettier to do it's thing. Then we walk the generated HTML doc to make a few modifications to the output (like replacing the <script> tags with { and } and dedenting the content.)

Todo

  • Handle comments
  • Track Node start and end within document
  • Split AstroContent into HTML fragments and JSX expressions

Testing

Tests have been added to tests/astro-prettier.test.js. You can run just these tests using npm test -- prettier.

  • Tests are passing
  • Tests updated where necessary

Docs

  • Docs / READMEs updated
  • Code comments added where helpful

@natemoo-re natemoo-re force-pushed the feat/prettier branch 2 times, most recently from 9c64f7a to d3db440 Compare April 16, 2021 17:52
<h1>
Hello world!</h1>
</body>
</html>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

”Why would these files be so poorly formatted— Oh. Oh. I see. Carry on!”

Copy link
Member

@drwpow drwpow left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I’m not sure how to test this, but I see tests added. This looks great for a first cut; I don’t have any feedback.

@natemoo-re
Copy link
Member Author

I’m not sure how to test this, but I see tests added.

npm test -- prettier from the root!

@drwpow
Copy link
Member

drwpow commented Apr 16, 2021

Did you mean to remove the social banner in this branch?

Copy link
Contributor

@mxmul mxmul left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was playing around with this locally, and there seem to be some edge cases with interpolations where running Prettier breaks the syntax.

E.g.

Input:

---
---
<p>
  {"hello world! this is a really long string. I sure hope it survives the formatter"}
</p>

Output:

---
---

<p>
  {"hello world! this is a really long string. I sure hope it survives the
  formatter"}
</p>

@natemoo-re
Copy link
Member Author

Did you mean to remove the social banner in this branch?

No, those commits were on main but got overwritten. Rebase seems to have fixed it appearing on this branch.

@natemoo-re
Copy link
Member Author

I was playing around with this locally, and there seem to be some edge cases with interpolations where running Prettier breaks the syntax.

Oh no! Good catch. I'm really hoping we don's have to implement much custom printing logic, but it may be the only way around stuff like this.

I'll have to check how the official JSX formatting handles expressions!

"node": "~14.0.0",
"npm": ">=6.14.0 <7.0.0"
"node": ">=14.0.0",
"npm": ">=6.14.0"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Glad you made this change. Was thinking the same

@natemoo-re natemoo-re changed the title WIP: Prettier support for .astro files Prettier support for .astro files Apr 20, 2021
@natemoo-re natemoo-re merged commit 54409a0 into main Apr 21, 2021
@natemoo-re natemoo-re deleted the feat/prettier branch April 21, 2021 16:14
@joshlangner
Copy link

Sorry for digging up an old conversation, but this might be related. I'm experiencing an issue where I will write the following in a helloworld.astro file, and prettier decides to rename elements to their lower cased versions, thinking it's in an html context. Example:

---
import { Button } from 'somewhere'
---

<Button>Hello world</Button>

I write the above, but when I save / run prettier on it, it turns into

<button>Hello world</button>

Notice now the <button> is lowercased. I assume it's because prettier is normalizing what it thinks is HTML, but I have in my project configured to be JSX.

Any pointers?

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

Successfully merging this pull request may close these issues.

prettier support for .astro files
4 participants