Skip to content

fix(language-tools): syntax highlighting for script/style in expressions#15602

Merged
Princesseuh merged 7 commits into
withastro:mainfrom
0xRozier:fix/issue-15439-script-style-highlighting-in-expressions
Mar 16, 2026
Merged

fix(language-tools): syntax highlighting for script/style in expressions#15602
Princesseuh merged 7 commits into
withastro:mainfrom
0xRozier:fix/issue-15439-script-style-highlighting-in-expressions

Conversation

@0xRozier
Copy link
Copy Markdown
Contributor

Summary

Fixes #15439

<script> and <style> tags inside JSX expressions (e.g. {cond && <script>...</script>}) were losing syntax highlighting and IntelliSense because their content was scoped as plain TSX instead of JavaScript/CSS.

Changes to astro.tmLanguage.src.yaml:

  • New injection rule (L:meta.embedded.expression.astro - meta.embedded.block): Injects the existing #tags-lang pattern into expression scopes so <script>/<style> tags are properly recognized inside expressions and create the correct scope structure (meta.script.astro / meta.style.astro).
  • Narrowed injection exclusions: Changed - (meta source) to - (meta.embedded.block source) in all 12 language injection selectors. This allows them to fire inside expressions (where source.tsx is present from the interpolation contentName) while still preventing double-injection.

Before

Content inside <script> in expressions was scoped as source.tsx:

source.astro meta.embedded.expression.astro source.tsx

After

Content is now properly injected with source.js / source.css:

source.astro meta.embedded.expression.astro source.tsx meta.scope.tag.script.astro meta.script.astro meta.embedded.block.astro source.js

Test plan

  • All 20 existing grammar snapshot tests pass unchanged (zero regressions)
  • Added script/expression.astro test fixture covering <script> in ternary and && expressions
  • Added style/expression.astro test fixture covering <style> in ternary and && expressions
  • Verified snapshots show correct source.js / source.css scoping for script/style content inside expressions

🤖 Generated with Claude Code

Add support for proper syntax highlighting and language injection for
<script> and <style> tags inside JSX expressions ({...}) in .astro files.

Previously, script/style content inside expressions like
`{cond && <script>...</script>}` was treated as plain TSX, losing
JavaScript/CSS highlighting and IntelliSense.

The fix:
1. Adds a TextMate injection (`L:meta.embedded.expression.astro`) that
   injects the existing `tags-lang` pattern into expression scopes, so
   <script>/<style> tags are properly recognized inside expressions.
2. Narrows the exclusion in language injection selectors from
   `- (meta source)` to `- (meta.embedded.block source)` so they apply
   within expressions (which have `source.tsx` in the scope path) while
   still preventing double injection.

Closes #15439

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
@changeset-bot
Copy link
Copy Markdown

changeset-bot Bot commented Feb 20, 2026

🦋 Changeset detected

Latest commit: a8c9e9a

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

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

@0xRozier
Copy link
Copy Markdown
Contributor Author

@Princesseuh I know you have already a lot of thing to do but when you have time, can you take a quick look ?

@Princesseuh Princesseuh self-assigned this Feb 24, 2026
Copy link
Copy Markdown
Member

@Princesseuh Princesseuh left a comment

Choose a reason for hiding this comment

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

Apologies for the delay, finally got to try it in local and it seems to work, awesome work!

@Princesseuh Princesseuh merged commit 7832dfe into withastro:main Mar 16, 2026
23 of 24 checks passed
@astrobot-houston astrobot-houston mentioned this pull request Mar 16, 2026
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.

Astro VS Code extension loses syntax highlighting / IntelliSense for <script> when conditionally rendered

2 participants