Skip to content

Commit

Permalink
feat(project): initial partial support for vue files
Browse files Browse the repository at this point in the history
  • Loading branch information
nhedger committed Feb 6, 2024
1 parent 7b6b4d5 commit dcdb2be
Show file tree
Hide file tree
Showing 8 changed files with 337 additions and 2 deletions.
19 changes: 18 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,7 +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::file_handlers::{ASTRO_FENCE, VUE_FENCE};
use biome_service::workspace::RuleCategories;
use std::path::Path;
use std::sync::atomic::Ordering;
Expand Down Expand Up @@ -84,6 +84,23 @@ pub(crate) fn format_with_guard<'ctx>(
}
}

if workspace_file.as_extension() == Some("vue") {
if output.is_empty() {
return Ok(FileStatus::Ignored);
}
if let Some(script) = VUE_FENCE
.captures(&input)
.and_then(|captures| captures.name("script"))
{
output = format!(
"{}{}{}",
&input[..script.start()],
output.as_str(),
&input[script.end()..]
);
}
}

if output != input {
if should_write {
workspace_file.update_file(output)?;
Expand Down
107 changes: 107 additions & 0 deletions crates/biome_cli/tests/commands/format.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,18 @@ statement();
---
<div></div>"#;

const VUE_FILE_UNFORMATTED: &str = r#"<script>
import { something } from "file.vue";
statement ( ) ;
</script>
<template></template>"#;

const VUE_FILE_FORMATTED: &str = r#"<script>
import { something } from "file.vue";
statement();
</script>
<template></template>"#;

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

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

let vue_file_path = Path::new("file.vue");
fs.insert(vue_file_path.into(), VUE_FILE_UNFORMATTED.as_bytes());

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

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

assert_file_contents(&fs, vue_file_path, VUE_FILE_UNFORMATTED);

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

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

let vue_file_path = Path::new("file.vue");
fs.insert(vue_file_path.into(), VUE_FILE_UNFORMATTED.as_bytes());

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

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

assert_file_contents(&fs, vue_file_path, VUE_FILE_FORMATTED);

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

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

let vue_file_path = Path::new("file.vue");
fs.insert(vue_file_path.into(), "<template></template>".as_bytes());

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

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

assert_file_contents(&fs, vue_file_path, "<template></template>");

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

```vue
<template></template>
```

# Emitted Messages

```block
Formatted 1 file(s) in <TIME>
```
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
---
source: crates/biome_cli/tests/snap_test.rs
expression: content
---
## `file.vue`

```vue
<script>
import { something } from "file.vue";
statement ( ) ;
</script>
<template></template>
```

# Termination Message

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

# Emitted Messages

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

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


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

```vue
<script>
import { something } from "file.vue";
statement();
</script>
<template></template>
```

# Emitted Messages

```block
Formatted 1 file(s) in <TIME>
```
13 changes: 13 additions & 0 deletions crates/biome_service/src/file_handlers/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ use self::{
};
use crate::file_handlers::astro::AstroFileHandler;
pub use crate::file_handlers::astro::ASTRO_FENCE;
use crate::file_handlers::vue::VueFileHandler;
pub use crate::file_handlers::vue::VUE_FENCE;
use crate::workspace::{FixFileMode, OrganizeImportsResult};
use crate::{
settings::SettingsHandle,
Expand All @@ -29,6 +31,7 @@ mod css;
mod javascript;
mod json;
mod unknown;
mod vue;

/// Supported languages by Biome
#[derive(Clone, Copy, Debug, Eq, PartialEq, Default, serde::Serialize, serde::Deserialize)]
Expand All @@ -50,6 +53,8 @@ pub enum Language {
Css,
///
Astro,
///
Vue,
/// Any language that is not supported
#[default]
Unknown,
Expand Down Expand Up @@ -82,6 +87,7 @@ impl Language {
"json" => Language::Json,
"jsonc" => Language::Jsonc,
"astro" => Language::Astro,
"vue" => Language::Vue,
"css" => Language::Css,
_ => Language::Unknown,
}
Expand Down Expand Up @@ -134,6 +140,7 @@ impl Language {
"json" => Language::Json,
"jsonc" => Language::Jsonc,
"astro" => Language::Astro,
"vue" => Language::Vue,
// TODO: remove this when we are ready to handle CSS files
"css" => Language::Unknown,
_ => Language::Unknown,
Expand Down Expand Up @@ -195,6 +202,7 @@ impl Language {
Language::TypeScript => Some(JsFileSource::tsx()),
Language::TypeScriptReact => Some(JsFileSource::tsx()),
Language::Astro => Some(JsFileSource::ts()),
Language::Vue => Some(JsFileSource::ts()),
Language::Json | Language::Jsonc | Language::Css | Language::Unknown => None,
}
}
Expand All @@ -210,6 +218,7 @@ impl Language {
| Language::Css
| Language::Jsonc => true,
Language::Astro => ASTRO_FENCE.is_match(content),
Language::Vue => VUE_FENCE.is_match(content),
Language::Unknown => false,
}
}
Expand All @@ -226,6 +235,7 @@ impl biome_console::fmt::Display for Language {
Language::Jsonc => fmt.write_markup(markup! { "JSONC" }),
Language::Css => fmt.write_markup(markup! { "CSS" }),
Language::Astro => fmt.write_markup(markup! { "Astro" }),
Language::Vue => fmt.write_markup(markup! { "Vue" }),
Language::Unknown => fmt.write_markup(markup! { "Unknown" }),
}
}
Expand Down Expand Up @@ -385,6 +395,7 @@ pub(crate) struct Features {
#[allow(unused)]
css: CssFileHandler,
astro: AstroFileHandler,
vue: VueFileHandler,
unknown: UnknownFileHandler,
}

Expand All @@ -395,6 +406,7 @@ impl Features {
json: JsonFileHandler {},
css: CssFileHandler {},
astro: AstroFileHandler {},
vue: VueFileHandler {},
unknown: UnknownFileHandler::default(),
}
}
Expand All @@ -420,6 +432,7 @@ impl Features {
}
}
Language::Astro => self.astro.capabilities(),
Language::Vue => self.vue.capabilities(),
Language::Unknown => self.unknown.capabilities(),
}
}
Expand Down
Loading

0 comments on commit dcdb2be

Please sign in to comment.