feat(js-embed): detect embedded GraphQL/CSS via content comments#9581
feat(js-embed): detect embedded GraphQL/CSS via content comments#9581praetoros wants to merge 2 commits intobiomejs:nextfrom
Conversation
Support `#graphql` and `#css` comments on the first line of untagged template literals, and `/* GraphQL */` / `/* CSS */` block comments before template literals, as markers for embedded language detection. This enables embedded formatting and linting for codebases that use comment-based markers instead of tagged templates (e.g. for `as const` type inference compatibility with tools like graphql-codegen). Closes biomejs#9511 > This PR was written primarily by Claude Code.
🦋 Changeset detectedLatest commit: c482f26 The changes in this PR will be included in the next version bump. This PR includes changesets to release 14 packages
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 |
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info⚙️ Run configurationConfiguration used: Path: .coderabbit.yaml Review profile: CHILL Plan: Pro Run ID: 📒 Files selected for processing (1)
✅ Files skipped from review due to trivial changes (1)
WalkthroughThis PR adds comment-based detection for embedded GraphQL and CSS in JavaScript template literals. It introduces Possibly related PRs
Suggested labels
Suggested reviewers
🚥 Pre-merge checks | ✅ 2✅ Passed checks (2 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment Tip You can make CodeRabbit's review stricter and more nitpicky using the `assertive` profile, if that's what you prefer.Change the |
There was a problem hiding this comment.
Actionable comments posted: 1
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.
Inline comments:
In `@crates/biome_service/src/file_handlers/javascript.rs`:
- Around line 670-679: The detection logic in detect_content_comment_tag is
case-sensitive while detect_block_comment_tag uses eq_ignore_ascii_case, causing
mismatched behavior (e.g., "#GraphQL" not detected but "/* graphql */" is);
update detect_content_comment_tag to perform case-insensitive comparisons (e.g.,
use language_id.eq_ignore_ascii_case(...)) for the same set of languages
(graphql, css) or, if the difference is intentional, add a concise comment in
detect_content_comment_tag explaining why inline tags are case-sensitive to
avoid confusion; reference detect_content_comment_tag and
detect_block_comment_tag when making the change.
ℹ️ Review info
⚙️ Run configuration
Configuration used: Path: .coderabbit.yaml
Review profile: CHILL
Plan: Pro
Run ID: 0a20d6ed-d201-4652-ad5f-516327b65d9a
⛔ Files ignored due to path filters (2)
crates/biome_cli/tests/snapshots/main_cases_javascript/should_detect_graphql_with_content_comment.snapis excluded by!**/*.snapand included by**crates/biome_js_formatter/tests/specs/prettier/js/multiparser-graphql/inline-comment-tag.js.snapis excluded by!**/*.snapand included by**
📒 Files selected for processing (7)
crates/biome_cli/tests/cases/javascript.rscrates/biome_js_formatter/tests/specs/prettier/js/multiparser-graphql/inline-comment-tag.jscrates/biome_js_formatter/tests/specs/prettier/js/multiparser-graphql/inline-comment-tag.js.prettier-snapcrates/biome_service/src/embed/detector.rscrates/biome_service/src/embed/registry.rscrates/biome_service/src/embed/types.rscrates/biome_service/src/file_handlers/javascript.rs
| fn detect_content_comment_tag(text: &str) -> Option<TemplateTagKind> { | ||
| let trimmed = text.trim_start_matches(['\n', '\r', ' ', '\t']); | ||
| let rest = trimmed.strip_prefix('#')?; | ||
| let language_id = rest.split(|c: char| c.is_whitespace()).next()?; | ||
| match language_id { | ||
| "graphql" => Some(TemplateTagKind::ContentComment { language: "graphql" }), | ||
| "css" => Some(TemplateTagKind::ContentComment { language: "css" }), | ||
| _ => None, | ||
| } | ||
| } |
There was a problem hiding this comment.
Inconsistent case sensitivity between inline and block comment detection.
detect_content_comment_tag uses case-sensitive matching ("graphql", "css"), whilst detect_block_comment_tag uses eq_ignore_ascii_case. This means:
`#GraphQL ...`→ not detected/* graphql */→ detected
Is this intentional? If so, consider adding a brief comment explaining the difference. If not, you may want to align them.
🔧 Suggested fix for consistent case-insensitive matching
fn detect_content_comment_tag(text: &str) -> Option<TemplateTagKind> {
let trimmed = text.trim_start_matches(['\n', '\r', ' ', '\t']);
let rest = trimmed.strip_prefix('#')?;
let language_id = rest.split(|c: char| c.is_whitespace()).next()?;
- match language_id {
- "graphql" => Some(TemplateTagKind::ContentComment { language: "graphql" }),
- "css" => Some(TemplateTagKind::ContentComment { language: "css" }),
- _ => None,
+ if language_id.eq_ignore_ascii_case("graphql") {
+ Some(TemplateTagKind::ContentComment { language: "graphql" })
+ } else if language_id.eq_ignore_ascii_case("css") {
+ Some(TemplateTagKind::ContentComment { language: "css" })
+ } else {
+ None
}
}Also applies to: 710-713
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.
In `@crates/biome_service/src/file_handlers/javascript.rs` around lines 670 - 679,
The detection logic in detect_content_comment_tag is case-sensitive while
detect_block_comment_tag uses eq_ignore_ascii_case, causing mismatched behavior
(e.g., "#GraphQL" not detected but "/* graphql */" is); update
detect_content_comment_tag to perform case-insensitive comparisons (e.g., use
language_id.eq_ignore_ascii_case(...)) for the same set of languages (graphql,
css) or, if the difference is intentional, add a concise comment in
detect_content_comment_tag explaining why inline tags are case-sensitive to
avoid confusion; reference detect_content_comment_tag and
detect_block_comment_tag when making the change.
There was a problem hiding this comment.
Thank you for the PR. I'm not totally sure this is the best way to propose something. I see that your discussion didn't gain any feedback, and maybe the PR could help. Nothing wrong with that.
Here's what I think you should improve:
- the original discussion assumes we know what you're talking about. That's not the case.
- the PR fails to provide the minimal business requirements that should help reviewers to understand the validity of the feature
- the PR description it's just AI slop of technicalities that don't play any role in the review. In fact even the AI bot doesn't know what's correct or what's wrong.
- still, there are zero links to the source of this feature. I spent 5min looking for something, and I couldn't find anything. I don't want to do that. If you propose something, the least you can do is to send us directly to the proper docs
|
Hey, firstly, sorry for the slop, this was still WIP (I'm testing it at the moment), but I figured I would put it here since then I'm at least contributing a little rather than asking others to do it on my behalf. When I have some more time, I'll do another pass over to fill in the holes you have pointed out in how I have suggested/submitted this. When it comes to the body of the PR, I do not have any rust experience, so I have clearly outlined everything here was done by AI as the contributing.md requests. To my understanding the case (embedded detection based off comments) and the technical description of the changes present line up. Sources:
|
Summary
#graphqland#csscomments on the first line of untagged template literals as embedded language markers./* GraphQL */and/* CSS */block comments before template literals as embedded language markers.as consttype inference compatibility with tools like graphql-codegen).Closes #9511
AI assistance notice
Details
The implementation extends the existing embed detection system with a new
TemplateCommentdetector andContentCommenttag kind, following the same patterns asTemplateTag/TemplateExpression. Detection is zero-cost for tagged templates (early return) and lightweight for untagged templates (string prefix checks, no allocations). Gated behindexperimentalEmbeddedSnippetsEnabled.Supported patterns:
Test plan
should_detect_graphql_with_content_commentcovering both#graphqland/* GraphQL */patterns.inline-comment-tag.jsfor formatter comparison.