Skip to content

Commit 5061b5f

Browse files
ematipicocoderabbitai[bot]
authored andcommitted
fix(core): parse astro script as TS (biomejs#7878)
Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com>
1 parent 0fcd30b commit 5061b5f

File tree

4 files changed

+94
-0
lines changed

4 files changed

+94
-0
lines changed

.changeset/ten-hoops-unite.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 [#7857](https://github.com/biomejs/biome/issues/7857): Biome now parses `<script>` tags as TypeScript when analysing `.astro` files.

crates/biome_cli/tests/cases/handle_astro_files.rs

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,20 @@ const { some } = Astro.props
5656
---
5757
<div>{some}</div>"#;
5858

59+
const ASTRO_FILE_WITH_TS_SCRIPT_TAG: &str = r#"---
60+
const title = "My Page";
61+
---
62+
<html>
63+
<body>
64+
<script>
65+
const message: string = "Hello TypeScript";
66+
function greet(name: string): void {
67+
console.log( message + ", " + name );
68+
}
69+
</script>
70+
</body>
71+
</html>"#;
72+
5973
#[test]
6074
fn format_astro_files() {
6175
let fs = MemoryFileSystem::default();
@@ -620,3 +634,36 @@ fn astro_global_object() {
620634
result,
621635
));
622636
}
637+
638+
#[test]
639+
fn format_astro_with_typescript_script_tag() {
640+
let fs = MemoryFileSystem::default();
641+
let mut console = BufferConsole::default();
642+
643+
fs.insert(
644+
"biome.json".into(),
645+
r#"{ "html": { "formatter": {"enabled": true}, "linter": {"enabled": true}, "experimentalFullSupportEnabled": true } }"#.as_bytes(),
646+
);
647+
648+
let astro_file_path = Utf8Path::new("file.astro");
649+
fs.insert(
650+
astro_file_path.into(),
651+
ASTRO_FILE_WITH_TS_SCRIPT_TAG.as_bytes(),
652+
);
653+
654+
let (fs, result) = run_cli(
655+
fs,
656+
&mut console,
657+
Args::from(["format", "--write", astro_file_path.as_str()].as_slice()),
658+
);
659+
660+
assert!(result.is_ok(), "run_cli returned {result:?}");
661+
662+
assert_cli_snapshot(SnapshotPayload::new(
663+
module_path!(),
664+
"format_astro_with_typescript_script_tag",
665+
fs,
666+
console,
667+
result,
668+
));
669+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
---
2+
source: crates/biome_cli/tests/snap_test.rs
3+
expression: redactor(content)
4+
---
5+
## `biome.json`
6+
7+
```json
8+
{
9+
"html": {
10+
"formatter": { "enabled": true },
11+
"linter": { "enabled": true },
12+
"experimentalFullSupportEnabled": true
13+
}
14+
}
15+
```
16+
17+
## `file.astro`
18+
19+
```astro
20+
---
21+
const title = "My Page";
22+
---
23+
<html>
24+
<body>
25+
<script>
26+
const message: string = "Hello TypeScript";
27+
function greet(name: string): void {
28+
console.log(message + ", " + name);
29+
}
30+
</script>
31+
</body>
32+
</html>
33+
34+
```
35+
36+
# Emitted Messages
37+
38+
```block
39+
Formatted 1 file in <TIME>. Fixed 1 file.
40+
```

crates/biome_service/src/file_handlers/html.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -488,6 +488,8 @@ pub(crate) fn parse_embedded_script(
488488
file_source = file_source.with_embedding_kind(EmbeddingKind::Vue);
489489
}
490490
file_source
491+
} else if html_file_source.is_astro() {
492+
JsFileSource::ts().with_embedding_kind(EmbeddingKind::Astro)
491493
} else {
492494
let is_module = element.is_javascript_module().unwrap_or_default();
493495
if is_module {

0 commit comments

Comments
 (0)