Skip to content

Commit

Permalink
feat(project): initial partial support for astro files (#1718)
Browse files Browse the repository at this point in the history
  • Loading branch information
ematipico authored Feb 6, 2024
1 parent 9047bba commit 5fa1fa4
Show file tree
Hide file tree
Showing 28 changed files with 527 additions and 142 deletions.
10 changes: 10 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,16 @@ Read our [guidelines for writing a good changelog entry](https://github.com/biom
- Biome now allows to format the `package.json` file. This is now the default behaviour and users can remove their workarounds.
If you rely on other tools to format `package.json`, you'll have to ignore it via configuration. Contributed by @pattrickrice
- New formatter option `attributePosition` that have similar behavior as Prettier `singleAttributePerLine` [#1706](https://github.com/biomejs/biome/issues/1706). Contributed by @octoshikari
- Add partial for `.astro` files. Biome is able to format the frontmatter of the Astro files. Contributed by @ematipico

```diff
---
- statement ( );
+ statement();
---

<div></div>
```

#### Bug fixes

Expand Down
15 changes: 8 additions & 7 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,7 @@ lazy_static = "1.4.0"
quickcheck = "1.0.3"
quickcheck_macros = "1.0.0"
quote = { version = "1.0.28" }
regex = "1.10.3"
rustc-hash = "1.1.0"
schemars = { version = "0.8.12" }
serde = { version = "1.0.163", features = ["derive"] }
Expand Down
18 changes: 17 additions & 1 deletion crates/biome_cli/src/execute/process_file/format.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use crate::execute::process_file::{
};
use crate::execute::TraversalMode;
use biome_diagnostics::{category, DiagnosticExt};
use biome_service::file_handlers::ASTRO_FENCE;
use biome_service::workspace::RuleCategories;
use std::path::Path;
use std::sync::atomic::Ordering;
Expand Down Expand Up @@ -61,13 +62,28 @@ pub(crate) fn format_with_guard<'ctx>(
category!("format"),
)?;

let output = printed.into_code();
let mut output = printed.into_code();

// NOTE: ignoring the
if ignore_errors {
return Ok(FileStatus::Ignored);
}

if workspace_file.as_extension() == Some("astro") {
if output.is_empty() {
return Ok(FileStatus::Ignored);
}
let mut matches = ASTRO_FENCE.find_iter(&input);
if let (Some(start), Some(end)) = (matches.next(), matches.next()) {
output = format!(
"{}{}{}",
&input[..start.end() + 1],
output.as_str(),
&input[end.start()..]
);
}
}

if output != input {
if should_write {
workspace_file.update_file(output)?;
Expand Down
5 changes: 5 additions & 0 deletions crates/biome_cli/src/execute/process_file/workspace_file.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,9 +60,14 @@ impl<'ctx, 'app> WorkspaceFile<'ctx, 'app> {
self.guard().get_file_content()
}

pub(crate) fn as_extension(&self) -> Option<&str> {
self.path.extension().and_then(|s| s.to_str())
}

/// It updates the workspace file with `new_content`
pub(crate) fn update_file(&mut self, new_content: impl Into<String>) -> Result<(), Error> {
let new_content = new_content.into();

self.file
.set_content(new_content.as_bytes())
.with_file_path(self.path.display().to_string())?;
Expand Down
110 changes: 110 additions & 0 deletions crates/biome_cli/tests/commands/format.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,21 @@ const APPLY_CSS_QUOTE_STYLE_BEFORE: &str =
const APPLY_CSS_QUOTE_STYLE_AFTER: &str =
"[class='foo'] {\n\tbackground-image: url('/path/to/file.jpg');\n}\n";

const ASTRO_FILE_UNFORMATTED: &str = r#"---
import { something } from "file.astro";
statement ( ) ;
---
<div></div>"#;

const ASTRO_FILE_FORMATTED: &str = r#"---
import { something } from "file.astro";
statement();
---
<div></div>"#;

const APPLY_TRAILING_COMMA_BEFORE: &str = r#"
const a = [
longlonglonglongItem1longlonglonglongItem1,
Expand Down Expand Up @@ -2881,3 +2896,98 @@ fn format_package_json() {
result,
));
}

#[test]
fn format_astro_files() {
let mut fs = MemoryFileSystem::default();
let mut console = BufferConsole::default();

let astro_file_path = Path::new("file.astro");
fs.insert(astro_file_path.into(), ASTRO_FILE_UNFORMATTED.as_bytes());

let result = run_cli(
DynRef::Borrowed(&mut fs),
&mut console,
Args::from([("format"), astro_file_path.as_os_str().to_str().unwrap()].as_slice()),
);

assert!(result.is_err(), "run_cli returned {result:?}");

assert_file_contents(&fs, astro_file_path, ASTRO_FILE_UNFORMATTED);

assert_cli_snapshot(SnapshotPayload::new(
module_path!(),
"format_astro_files",
fs,
console,
result,
));
}

#[test]
fn format_astro_files_write() {
let mut fs = MemoryFileSystem::default();
let mut console = BufferConsole::default();

let astro_file_path = Path::new("file.astro");
fs.insert(astro_file_path.into(), ASTRO_FILE_UNFORMATTED.as_bytes());

let result = run_cli(
DynRef::Borrowed(&mut fs),
&mut console,
Args::from(
[
"format",
"--write",
astro_file_path.as_os_str().to_str().unwrap(),
]
.as_slice(),
),
);

assert!(result.is_ok(), "run_cli returned {result:?}");

assert_file_contents(&fs, astro_file_path, ASTRO_FILE_FORMATTED);

assert_cli_snapshot(SnapshotPayload::new(
module_path!(),
"format_astro_files_write",
fs,
console,
result,
));
}

#[test]
fn format_empty_astro_files_write() {
let mut fs = MemoryFileSystem::default();
let mut console = BufferConsole::default();

let astro_file_path = Path::new("file.astro");
fs.insert(astro_file_path.into(), "<div></div>".as_bytes());

let result = run_cli(
DynRef::Borrowed(&mut fs),
&mut console,
Args::from(
[
"format",
"--write",
astro_file_path.as_os_str().to_str().unwrap(),
]
.as_slice(),
),
);

assert!(result.is_ok(), "run_cli returned {result:?}");

assert_file_contents(&fs, astro_file_path, "<div></div>");

assert_cli_snapshot(SnapshotPayload::new(
module_path!(),
"format_empty_astro_files_write",
fs,
console,
result,
));
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
---
source: crates/biome_cli/tests/snap_test.rs
expression: content
---
## `file.astro`

```astro
---
import { something } from "file.astro";
statement ( ) ;
---
<div></div>
```

# Termination Message

```block
format ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
× Some errors were emitted while running checks.
```

# Emitted Messages

```block
file.astro format ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
i Formatter would have printed the following content:
1 1 │ ---
2 │ - import·{····something·}·from·"file.astro";
2 │ + import·{·something·}·from·"file.astro";
3 3 │
4 │ - statement·(·)·;
5 │ -
4 │ + statement();
6 5 │ ---
7 6 │ <div></div>
```

```block
Compared 1 file(s) in <TIME>
```


Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
---
source: crates/biome_cli/tests/snap_test.rs
expression: content
---
## `file.astro`

```astro
---
import { something } from "file.astro";
statement();
---
<div></div>
```

# Emitted Messages

```block
Formatted 1 file(s) in <TIME>
```


Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
---
source: crates/biome_cli/tests/snap_test.rs
expression: content
---
## `file.astro`

```astro
<div></div>
```

# Emitted Messages

```block
Formatted 1 file(s) in <TIME>
```


1 change: 1 addition & 0 deletions crates/biome_service/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ dashmap = { workspace = true }
ignore = { workspace = true }
indexmap = { workspace = true, features = ["serde"] }
lazy_static = { workspace = true }
regex = { workspace = true }
rustc-hash = { workspace = true }
schemars = { workspace = true, features = ["indexmap1"], optional = true }
serde = { workspace = true, features = ["derive"] }
Expand Down
2 changes: 1 addition & 1 deletion crates/biome_service/src/diagnostics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -428,7 +428,7 @@ pub struct CantReadFile {
message("The file "{self.path}" was ignored."),
description = "The file {path} was ignored."
),
severity = Warning
severity = Warning,
)]
pub struct FileIgnored {
#[location(resource)]
Expand Down
Loading

0 comments on commit 5fa1fa4

Please sign in to comment.