From 269c7c3bfa8a92d4ad64ffbdc84370ba777ce4ec Mon Sep 17 00:00:00 2001 From: Nicolas Hedger Date: Tue, 6 Feb 2024 19:03:21 +0100 Subject: [PATCH 1/6] feat(project): initial partial support for vue files --- .../src/execute/process_file/format.rs | 19 ++- crates/biome_cli/tests/commands/format.rs | 107 ++++++++++++++++ .../format_empty_vue_files_write.snap | 15 +++ .../format_vue_files.snap | 48 ++++++++ .../format_vue_files_write.snap | 19 +++ crates/biome_service/src/file_handlers/mod.rs | 13 ++ crates/biome_service/src/file_handlers/vue.rs | 115 ++++++++++++++++++ .../@biomejs/backend-jsonrpc/src/workspace.ts | 3 +- 8 files changed, 337 insertions(+), 2 deletions(-) create mode 100644 crates/biome_cli/tests/snapshots/main_commands_format/format_empty_vue_files_write.snap create mode 100644 crates/biome_cli/tests/snapshots/main_commands_format/format_vue_files.snap create mode 100644 crates/biome_cli/tests/snapshots/main_commands_format/format_vue_files_write.snap create mode 100644 crates/biome_service/src/file_handlers/vue.rs diff --git a/crates/biome_cli/src/execute/process_file/format.rs b/crates/biome_cli/src/execute/process_file/format.rs index 733843c3a01f..563eafddfaa9 100644 --- a/crates/biome_cli/src/execute/process_file/format.rs +++ b/crates/biome_cli/src/execute/process_file/format.rs @@ -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; @@ -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)?; diff --git a/crates/biome_cli/tests/commands/format.rs b/crates/biome_cli/tests/commands/format.rs index b8296f51a68d..77355ea65c08 100644 --- a/crates/biome_cli/tests/commands/format.rs +++ b/crates/biome_cli/tests/commands/format.rs @@ -58,6 +58,18 @@ statement(); ---
"#; +const VUE_FILE_UNFORMATTED: &str = r#" +"#; + +const VUE_FILE_FORMATTED: &str = r#" +"#; + const APPLY_TRAILING_COMMA_BEFORE: &str = r#" const a = [ longlonglonglongItem1longlonglonglongItem1, @@ -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(), "".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, ""); + + assert_cli_snapshot(SnapshotPayload::new( + module_path!(), + "format_empty_vue_files_write", + fs, + console, + result, + )); +} diff --git a/crates/biome_cli/tests/snapshots/main_commands_format/format_empty_vue_files_write.snap b/crates/biome_cli/tests/snapshots/main_commands_format/format_empty_vue_files_write.snap new file mode 100644 index 000000000000..3803cc6a03db --- /dev/null +++ b/crates/biome_cli/tests/snapshots/main_commands_format/format_empty_vue_files_write.snap @@ -0,0 +1,15 @@ +--- +source: crates/biome_cli/tests/snap_test.rs +expression: content +--- +## `file.vue` + +```vue + +``` + +# Emitted Messages + +```block +Formatted 1 file(s) in