Skip to content

fix: strip newlines at the end of component files #406

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

Merged
merged 4 commits into from
May 23, 2022

Conversation

virchau13
Copy link
Contributor

Changes

This makes it so that newlines at the end of files are not considered. This is because it can lead to confusing behaviour; defining an Astro component with a newline at the end (which can be implicitly placed by most editors) adds an implicit newline to the final output, resulting in strange spaces in the final HTML.

For example:

<!-- src/components/Link.astro -->
<a href={Astro.props.href}><slot /></a>
---
/* src/pages/index.astro */
import Link from '../components/Link.astro';
---
<p>this is a <Link href="example.com">test</Link>!</p>

results in a strange space between the link text and the exclamation mark:
strange space example

This change simply makes it so that the three exposed JS functions all strip newlines from the end of the source text before attempting to use it.

Testing

This was manually tested. Does this need automated testing?

Docs

Bug fix only. (Does this need documentation?)

@changeset-bot
Copy link

changeset-bot bot commented May 23, 2022

🦋 Changeset detected

Latest commit: 6a71cad

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
@astrojs/compiler Minor

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@matthewp
Copy link
Contributor

It looks like some tests are expecting this. I think it's likely that they really don't care, so you might want to take a look and update the tests.

Copy link
Member

@natemoo-re natemoo-re left a comment

Choose a reason for hiding this comment

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

This is great! Thanks for providing a really clear use case where this behavior breaks expectations.

I updated the implementation to trim trailing space nodes without updating the source text (which could throw off source maps).

I also added a test suite to check for this behavior explicitly.

@natemoo-re natemoo-re merged commit 702e848 into withastro:main May 23, 2022
natemoo-re added a commit that referenced this pull request Jul 20, 2022
* fix: strip newlines at the end of component files

* refactor: trim trailing space in transform

* test: add explicit space preservation test

* Update chilly-chairs-enjoy.md

Co-authored-by: Nate Moore <[email protected]>
Co-authored-by: Nate Moore <[email protected]>
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.

3 participants