Skip to content

Commit 4d42823

Browse files
fix(embeds): track attributes text expressions (#9526)
Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com>
1 parent 61451ef commit 4d42823

File tree

5 files changed

+52
-4
lines changed

5 files changed

+52
-4
lines changed

.changeset/poor-spies-switch.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@biomejs/biome": patch
3+
---
4+
5+
Fixed [#9358](https://github.com/biomejs/biome/issues/9358) and [#9375](https://github.com/biomejs/biome/issues/9375). Now attributes that have text expressions such as `class={buttonClass()}` are correctly tracked in Svelte files.
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<!-- should not generate diagnostics -->
2+
<script lang="ts">
3+
import { buttonClass } from "./buttonClass.ts";
4+
export let variant: Parameters<typeof buttonClass>[0] = "default";
5+
</script>
6+
7+
<button class={buttonClass(variant)}>Test</button>
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
---
2+
source: crates/biome_js_analyze/tests/spec_tests.rs
3+
expression: valid-issue-9358.svelte
4+
---
5+
# Input
6+
```svelte
7+
<!-- should not generate diagnostics -->
8+
<script lang="ts">
9+
import { buttonClass } from "./buttonClass.ts";
10+
export let variant: Parameters<typeof buttonClass>[0] = "default";
11+
</script>
12+
13+
<button class={buttonClass(variant)}>Test</button>
14+
15+
```

crates/biome_service/src/file_handlers/html.rs

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -878,7 +878,7 @@ fn parse_embedded_nodes(
878878
}
879879
}
880880

881-
// Pass 4: directive attributes via registry using captured embedded_file_source
881+
// Pass 4: directive attributes and attributes which initializer is a text expression
882882
for element in html_root.syntax().descendants() {
883883
// Handle special Svelte directives (bind:, class:, etc.)
884884
if let Some(directive) = AnySvelteDirective::cast_ref(&element)
@@ -898,6 +898,24 @@ fn parse_embedded_nodes(
898898
{
899899
nodes.push(parsed.node);
900900
}
901+
902+
if let Some(attr) = HtmlAttribute::cast_ref(&element)
903+
&& let Some(initializer) = attr.initializer()
904+
&& let Some(candidate) = build_attribute_expression_candidate(&initializer)
905+
&& let Some(embed_match) = EmbedDetectorsRegistry::detect_match(
906+
HostLanguage::Html,
907+
&candidate,
908+
&doc_file_source,
909+
)
910+
&& let Some(parsed) = parse_matched_embed(
911+
&candidate,
912+
&embed_match,
913+
&mut ctx,
914+
Some(embedded_file_source),
915+
)
916+
{
917+
nodes.push(parsed.node);
918+
}
901919
}
902920
}
903921
}

crates/biome_test_utils/src/lib.rs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -898,7 +898,7 @@ pub fn analyze_with_workspace(input_file: &Utf8Path, group: &str, rule: &str) ->
898898
configuration: config,
899899
workspace_directory: Some(BiomePath::new(&project_root)),
900900
extended_configurations: vec![],
901-
module_graph_resolution_kind: ModuleGraphResolutionKind::default(),
901+
module_graph_resolution_kind: ModuleGraphResolutionKind::None,
902902
})
903903
.expect("failed to update settings");
904904

@@ -908,7 +908,7 @@ pub fn analyze_with_workspace(input_file: &Utf8Path, group: &str, rule: &str) ->
908908
project_key,
909909
watch: false,
910910
force: false,
911-
scan_kind: ScanKind::Project,
911+
scan_kind: ScanKind::NoScanner,
912912
verbose: false,
913913
})
914914
.expect("failed to scan project");
@@ -918,7 +918,10 @@ pub fn analyze_with_workspace(input_file: &Utf8Path, group: &str, rule: &str) ->
918918
.open_file(OpenFileParams {
919919
project_key,
920920
path: BiomePath::new(&virtual_file_path),
921-
content: FileContent::FromServer,
921+
content: FileContent::FromClient {
922+
content: input_code.clone(),
923+
version: 0,
924+
},
922925
document_file_source: None,
923926
persist_node_cache: false,
924927
inline_config: None,

0 commit comments

Comments
 (0)