Skip to content

Commit

Permalink
fix(biome_service): add Astro object to globals for .astro files (#3006)
Browse files Browse the repository at this point in the history
  • Loading branch information
minht11 committed May 27, 2024
1 parent 48f36ac commit 6184c8d
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 13 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ our [guidelines for writing a good changelog entry](https://github.com/biomejs/b

#### Enhancements

- Assume `Astro` object is always a global when processing `.astro` files. Contributed by @minht11
- Assume Vue compiler macros are globals when processing `.vue` files. ([#2771](https://github.com/biomejs/biome/pull/2771)) Contributed by @dyc3

### CLI
Expand Down
35 changes: 35 additions & 0 deletions crates/biome_cli/tests/cases/handle_astro_files.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,11 @@ const foo = "";
---
<div></div>"#;

const ASTRO_FILE_ASTRO_GLOBAL_OBJECT: &str = r#"---
const { some } = Astro.props
---
<div>{some}</div>"#;

#[test]
fn format_astro_files() {
let mut fs = MemoryFileSystem::default();
Expand Down Expand Up @@ -685,3 +690,33 @@ fn check_stdin_write_unsafe_successfully() {
result,
));
}

#[test]
fn astro_global_object() {
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_ASTRO_GLOBAL_OBJECT.as_bytes(),
);

let result = run_cli(
DynRef::Borrowed(&mut fs),
&mut console,
Args::from([("lint"), 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_ASTRO_GLOBAL_OBJECT);

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

```astro
---
const { some } = Astro.props
---
<div>{some}</div>
```

# Emitted Messages

```block
Checked 1 file in <TIME>. No fixes needed.
```
33 changes: 20 additions & 13 deletions crates/biome_service/src/file_handlers/javascript.rs
Original file line number Diff line number Diff line change
Expand Up @@ -201,19 +201,26 @@ impl ServiceLanguage for JsLanguage {
.override_js_globals(path, &global.languages.javascript.globals)
.into_iter()
.collect();
if path.extension().and_then(OsStr::to_str) == Some("vue") {
globals.extend(
[
"defineEmits",
"defineProps",
"defineExpose",
"defineModel",
"defineOptions",
"defineSlots",
]
.map(ToOwned::to_owned),
);
}

match path.extension().and_then(OsStr::to_str) {
Some("vue") => {
globals.extend(
[
"defineEmits",
"defineProps",
"defineExpose",
"defineModel",
"defineOptions",
"defineSlots",
]
.map(ToOwned::to_owned),
);
}
Some("astro") => {
globals.extend(["Astro"].map(ToOwned::to_owned));
}
_ => {}
};

let configuration = AnalyzerConfiguration {
rules: to_analyzer_rules(global, path),
Expand Down

0 comments on commit 6184c8d

Please sign in to comment.