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

feat(tsx): Extract script and styles from TSX for language-server consumption #1019

Merged
merged 15 commits into from
Jul 16, 2024

Conversation

Princesseuh
Copy link
Member

@Princesseuh Princesseuh commented Jul 11, 2024

Changes

Required by withastro/language-tools#900

Previously, the language server would extract scripts and styles manually using both the compiler AST and the HTML AST. Not only is this kinda inefficient, it also lead to sometimes incorrect results due to wrong AST positions, HTML not understanding expressions, etc.

This PR makes it so the compiler instead provide scripts and styles in the TSXResult. Additionally, this PR adds the ability to remove the scripts and styles from the output, for multiple reasons:

  • The script tags produce invalid code, as no processing (apart from wrapping in an arrow function) was done on the content
  • The style tags were simply text, they provide no actual value to the TSX. Actually, they end up often causing issues as in the language server, TSX seeing text there would override the symbol / selection range information of the actual content

These two changes were put under flags, so there's no breaking changes (certain consumer might still need this, for instance, our TS plugin does want the script tags content. Albeit, fixed, but that's for another PR, ha)

Testing

Updated tests

Docs

N/A

Copy link

changeset-bot bot commented Jul 11, 2024

🦋 Changeset detected

Latest commit: bc2e8b2

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

@@ -36,6 +36,9 @@ type printer struct {

// Optional, used only for TSX output
ranges TSXRanges
// Keep track of how many multi-byte characters we've printed so that they can be skipped whenever we need a character-based index
// This could be directly in the token / node information, however this would require a fairly large refactor
bytesToSkip int
Copy link
Member Author

@Princesseuh Princesseuh Jul 12, 2024

Choose a reason for hiding this comment

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

So, this not really optimal, because it confuse the printed bytes with the source bytes. It's fine in 99.999% of cases, because we're not printing any multi-bytes ourselves that were not part of the source. But it's something we could do and would have to take into account then. (and is trivial to do, but still)

The real fix, but it'd require a large amount of work would be to have this information directly on the tokens / nodes. I first tried to do that, but I quickly realised that I was changing everything and ran into the risk of breaking things everywhere.

If this prove to be an issue, I'd definitely be willing to make the proper change, but in my testing this honestly proved to work just fine.

@Princesseuh Princesseuh merged commit 3e25858 into main Jul 16, 2024
5 checks passed
@Princesseuh Princesseuh deleted the fix/tsx-extract-scripts branch July 16, 2024 09:20
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.

2 participants