Skip to content

Commit 3628313

Browse files
committed
fix(html/astro): don't indent Astro frontmatter
1 parent c09e45c commit 3628313

File tree

6 files changed

+118
-8
lines changed

6 files changed

+118
-8
lines changed

.changeset/vast-donuts-rescue.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+
`indentScriptAndStyle` no longer indents the frontmatter in Astro files.

crates/biome_cli/tests/cases/handle_astro_files.rs

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -667,3 +667,51 @@ fn format_astro_with_typescript_script_tag() {
667667
result,
668668
));
669669
}
670+
671+
#[test]
672+
fn dont_indent_frontmatter() {
673+
let fs = MemoryFileSystem::default();
674+
let mut console = BufferConsole::default();
675+
676+
fs.insert(
677+
"biome.json".into(),
678+
r#"{ "html": { "formatter": {"enabled": true, "indentScriptAndStyle": true}, "linter": {"enabled": true}, "experimentalFullSupportEnabled": true } }"#.as_bytes(),
679+
);
680+
681+
let astro_file_path = Utf8Path::new("file.astro");
682+
fs.insert(
683+
astro_file_path.into(),
684+
r#"---
685+
import Foo from "./Foo.astro"
686+
const bar = 123
687+
if (bar>1) {console.log(bar+1)}
688+
---
689+
<Foo>{bar}</Foo>
690+
691+
<style>
692+
#id { font-family: comic-sans } .class { background: red}
693+
</style>
694+
695+
<script>
696+
function foo(){console.log("Hello")}
697+
</script>
698+
"#
699+
.as_bytes(),
700+
);
701+
702+
let (fs, result) = run_cli(
703+
fs,
704+
&mut console,
705+
Args::from(["format", "--write", astro_file_path.as_str()].as_slice()),
706+
);
707+
708+
assert!(result.is_ok(), "run_cli returned {result:?}");
709+
710+
assert_cli_snapshot(SnapshotPayload::new(
711+
module_path!(),
712+
"dont_indent_frontmatter",
713+
fs,
714+
console,
715+
result,
716+
));
717+
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
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, "indentScriptAndStyle": true },
11+
"linter": { "enabled": true },
12+
"experimentalFullSupportEnabled": true
13+
}
14+
}
15+
```
16+
17+
## `file.astro`
18+
19+
```astro
20+
---
21+
import Foo from "./Foo.astro";
22+
const bar = 123;
23+
if (bar > 1) {
24+
console.log(bar + 1);
25+
}
26+
---
27+
<Foo>{bar}</Foo>
28+
29+
<style>
30+
#id {
31+
font-family: comic-sans;
32+
}
33+
.class {
34+
background: red;
35+
}
36+
</style>
37+
38+
<script>
39+
function foo() {
40+
console.log("Hello");
41+
}
42+
</script>
43+
44+
```
45+
46+
# Emitted Messages
47+
48+
```block
49+
Formatted 1 file in <TIME>. Fixed 1 file.
50+
```

crates/biome_js_syntax/src/file_source.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,9 @@ impl Language {
121121
)]
122122
pub enum EmbeddingKind {
123123
Astro,
124+
// This variant is currently only used to record whether it is an Astro frontmatter,
125+
// and in that case, the `indentScriptAndStyle` setting is ignored.
126+
AstroFrontmatter,
124127
Vue,
125128
Svelte,
126129
#[default]

crates/biome_service/src/file_handlers/html.rs

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -445,7 +445,7 @@ pub(crate) fn parse_astro_embedded_script(
445445
settings: &Settings,
446446
) -> Option<(EmbeddedSnippet<JsLanguage>, DocumentFileSource)> {
447447
let content = element.content_token()?;
448-
let file_source = JsFileSource::ts().with_embedding_kind(EmbeddingKind::Astro);
448+
let file_source = JsFileSource::ts().with_embedding_kind(EmbeddingKind::AstroFrontmatter);
449449
let document_file_source = DocumentFileSource::Js(file_source);
450450
let options = settings.parse_options::<JsLanguage>(path, &document_file_source);
451451
let parse = parse_js_with_offset_and_cache(
@@ -672,8 +672,8 @@ fn format_embedded(
672672
let mut iter = embedded_nodes.iter();
673673
let node = iter.find(|node| node.range == range)?;
674674

675-
let wrap_document = |document: Document| {
676-
if indent_script_and_style {
675+
let wrap_document = |document: Document, is_astro_frontmatter: bool| {
676+
if indent_script_and_style && !is_astro_frontmatter {
677677
let elements = vec![
678678
FormatElement::Line(LineMode::Hard),
679679
FormatElement::Tag(Tag::StartIndent),
@@ -693,27 +693,31 @@ fn format_embedded(
693693
};
694694

695695
match node.source {
696-
DocumentFileSource::Js(_) => {
696+
DocumentFileSource::Js(file_source) => {
697697
let js_options = settings.format_options::<JsLanguage>(biome_path, &node.source);
698698
let node = node.node.clone().embedded_syntax::<JsLanguage>().clone();
699699
let formatted =
700700
biome_js_formatter::format_node_with_offset(js_options, &node).ok()?;
701-
Some(wrap_document(formatted.into_document()))
701+
702+
Some(wrap_document(
703+
formatted.into_document(),
704+
file_source.as_embedding_kind() == &EmbeddingKind::AstroFrontmatter,
705+
))
702706
}
703707
DocumentFileSource::Json(_) => {
704708
let json_options =
705709
settings.format_options::<JsonLanguage>(biome_path, &node.source);
706710
let node = node.node.clone().embedded_syntax::<JsonLanguage>().clone();
707711
let formatted =
708712
biome_json_formatter::format_node_with_offset(json_options, &node).ok()?;
709-
Some(wrap_document(formatted.into_document()))
713+
Some(wrap_document(formatted.into_document(), false))
710714
}
711715
DocumentFileSource::Css(_) => {
712716
let css_options = settings.format_options::<CssLanguage>(biome_path, &node.source);
713717
let node = node.node.clone().embedded_syntax::<CssLanguage>();
714718
let formatted =
715719
biome_css_formatter::format_node_with_offset(css_options, &node).ok()?;
716-
Some(wrap_document(formatted.into_document()))
720+
Some(wrap_document(formatted.into_document(), false))
717721
}
718722
_ => None,
719723
}

crates/biome_service/src/file_handlers/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -814,7 +814,7 @@ impl Features {
814814
match language_hint {
815815
// TODO: remove match once we remove vue/astro/svelte handlers
816816
DocumentFileSource::Js(source) => match source.as_embedding_kind() {
817-
EmbeddingKind::Astro => self.astro.capabilities(),
817+
EmbeddingKind::Astro | EmbeddingKind::AstroFrontmatter => self.astro.capabilities(),
818818
EmbeddingKind::Vue => self.vue.capabilities(),
819819
EmbeddingKind::Svelte => self.svelte.capabilities(),
820820
EmbeddingKind::None => self.js.capabilities(),

0 commit comments

Comments
 (0)