From b8a0ff3372c6cbdec370962f21a2b161b060d302 Mon Sep 17 00:00:00 2001 From: Zheyu Zhang Date: Fri, 14 Jun 2024 11:10:15 +0800 Subject: [PATCH 1/4] tweak testFormatLanguage trait --- Cargo.lock | 3 ++ crates/biome_css_formatter/Cargo.toml | 1 + crates/biome_css_formatter/tests/language.rs | 38 ++++++++---------- .../tests/prettier_tests.rs | 4 +- .../biome_css_formatter/tests/quick_test.rs | 11 +++++- crates/biome_css_formatter/tests/spec_test.rs | 9 ++++- .../src/check_reformat.rs | 11 +++--- crates/biome_formatter_test/src/lib.rs | 34 +++++++--------- crates/biome_formatter_test/src/spec.rs | 37 ++++++++++-------- .../src/test_prettier_snapshot.rs | 18 ++++----- crates/biome_js_formatter/Cargo.toml | 1 + crates/biome_js_formatter/tests/language.rs | 39 ++++++++----------- .../tests/prettier_tests.rs | 4 +- crates/biome_js_formatter/tests/quick_test.rs | 4 +- crates/biome_js_formatter/tests/spec_test.rs | 9 ++++- crates/biome_json_formatter/Cargo.toml | 1 + crates/biome_json_formatter/tests/language.rs | 39 ++++++++----------- .../tests/prettier_tests.rs | 4 +- .../biome_json_formatter/tests/quick_test.rs | 11 ++++-- .../biome_json_formatter/tests/spec_test.rs | 9 ++++- 20 files changed, 147 insertions(+), 140 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 260c936f1655..f8646e68073c 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -287,6 +287,7 @@ dependencies = [ "biome_diagnostics", "biome_formatter", "biome_formatter_test", + "biome_fs", "biome_parser", "biome_rowan", "biome_service", @@ -694,6 +695,7 @@ dependencies = [ "biome_diagnostics_categories", "biome_formatter", "biome_formatter_test", + "biome_fs", "biome_js_factory", "biome_js_parser", "biome_js_syntax", @@ -819,6 +821,7 @@ dependencies = [ "biome_diagnostics", "biome_formatter", "biome_formatter_test", + "biome_fs", "biome_json_parser", "biome_json_syntax", "biome_parser", diff --git a/crates/biome_css_formatter/Cargo.toml b/crates/biome_css_formatter/Cargo.toml index c05a544a7017..8f7a95fcffdf 100644 --- a/crates/biome_css_formatter/Cargo.toml +++ b/crates/biome_css_formatter/Cargo.toml @@ -23,6 +23,7 @@ biome_suppression = { workspace = true } biome_configuration = { path = "../biome_configuration" } biome_css_parser = { path = "../biome_css_parser" } biome_formatter_test = { path = "../biome_formatter_test" } +biome_fs = { path = "../biome_fs" } biome_parser = { path = "../biome_parser" } biome_service = { path = "../biome_service" } countme = { workspace = true, features = ["enable"] } diff --git a/crates/biome_css_formatter/tests/language.rs b/crates/biome_css_formatter/tests/language.rs index c378aab61c39..e4967fe1f0e9 100644 --- a/crates/biome_css_formatter/tests/language.rs +++ b/crates/biome_css_formatter/tests/language.rs @@ -1,11 +1,10 @@ -use biome_css_formatter::context::{CssFormatContext, CssFormatOptions}; -use biome_css_formatter::{format_node, format_range, CssFormatLanguage}; +use biome_css_formatter::context::CssFormatContext; +use biome_css_formatter::CssFormatLanguage; use biome_css_parser::{parse_css, CssParserOptions}; use biome_css_syntax::{CssFileSource, CssLanguage}; -use biome_formatter::{FormatResult, Formatted, Printed}; use biome_formatter_test::TestFormatLanguage; +use biome_fs::BiomePath; use biome_parser::AnyParse; -use biome_rowan::{SyntaxNode, TextRange}; use biome_service::settings::{ServiceLanguage, Settings}; #[derive(Default)] @@ -33,24 +32,19 @@ impl TestFormatLanguage for CssTestFormatLanguage { &settings.languages.css.formatter } - fn format_node( + fn to_format_language( &self, - options: ::FormatOptions, - node: &SyntaxNode, - ) -> FormatResult> { - format_node(options, node) - } - - fn format_range( - &self, - options: ::FormatOptions, - node: &SyntaxNode, - range: TextRange, - ) -> FormatResult { - format_range(options, node, range) - } - - fn default_options(&self) -> ::FormatOptions { - CssFormatOptions::default() + settings: &Settings, + file_source: &biome_service::workspace::DocumentFileSource, + ) -> Self::FormatLanguage { + let language_settings = self.to_language_settings(settings); + let options = Self::ServiceLanguage::resolve_format_options( + Some(&settings.formatter), + Some(&settings.override_settings), + Some(language_settings), + &BiomePath::new(""), + file_source, + ); + CssFormatLanguage::new(options) } } diff --git a/crates/biome_css_formatter/tests/prettier_tests.rs b/crates/biome_css_formatter/tests/prettier_tests.rs index ed5df82438ef..ae1f1ef107a0 100644 --- a/crates/biome_css_formatter/tests/prettier_tests.rs +++ b/crates/biome_css_formatter/tests/prettier_tests.rs @@ -1,6 +1,6 @@ use std::{env, path::Path}; -use biome_css_formatter::context::CssFormatOptions; +use biome_css_formatter::{context::CssFormatOptions, CssFormatLanguage}; use biome_formatter::{IndentStyle, IndentWidth}; use biome_formatter_test::test_prettier_snapshot::{PrettierSnapshot, PrettierTestFile}; @@ -21,7 +21,7 @@ fn test_snapshot(input: &'static str, _: &str, _: &str, _: &str) { .with_indent_style(IndentStyle::Space) .with_indent_width(IndentWidth::default()); let language = language::CssTestFormatLanguage::default(); - let snapshot = PrettierSnapshot::new(test_file, language, options); + let snapshot = PrettierSnapshot::new(test_file, language, CssFormatLanguage::new(options)); snapshot.test() } diff --git a/crates/biome_css_formatter/tests/quick_test.rs b/crates/biome_css_formatter/tests/quick_test.rs index 827714d22da8..495e773e2895 100644 --- a/crates/biome_css_formatter/tests/quick_test.rs +++ b/crates/biome_css_formatter/tests/quick_test.rs @@ -1,5 +1,5 @@ -use biome_css_formatter::context::CssFormatOptions; use biome_css_formatter::format_node; +use biome_css_formatter::{context::CssFormatOptions, CssFormatLanguage}; use biome_css_parser::{parse_css, CssParserOptions}; use biome_formatter::{IndentStyle, LineWidth}; use biome_formatter_test::check_reformat::CheckReformat; @@ -34,5 +34,12 @@ fn quick_test() { println!("{}", doc.into_document()); eprintln!("{}", result.as_code()); - CheckReformat::new(root, result.as_code(), "quick_test", &language, options).check_reformat(); + CheckReformat::new( + root, + result.as_code(), + "quick_test", + &language, + CssFormatLanguage::new(options), + ) + .check_reformat(); } diff --git a/crates/biome_css_formatter/tests/spec_test.rs b/crates/biome_css_formatter/tests/spec_test.rs index b5da1aea24f9..bb79a8e678ab 100644 --- a/crates/biome_css_formatter/tests/spec_test.rs +++ b/crates/biome_css_formatter/tests/spec_test.rs @@ -1,5 +1,5 @@ use biome_configuration::{PartialConfiguration, PartialCssConfiguration, PartialCssFormatter}; -use biome_css_formatter::context::CssFormatOptions; +use biome_css_formatter::{context::CssFormatOptions, CssFormatLanguage}; use biome_formatter_test::spec::{SpecSnapshot, SpecTestFile}; use biome_service::workspace::UpdateSettingsParams; use std::path::Path; @@ -51,7 +51,12 @@ pub fn run(spec_input_file: &str, _expected_file: &str, test_directory: &str, _f let options = CssFormatOptions::default(); let language = language::CssTestFormatLanguage::default(); - let snapshot = SpecSnapshot::new(test_file, test_directory, language, options); + let snapshot = SpecSnapshot::new( + test_file, + test_directory, + language, + CssFormatLanguage::new(options), + ); snapshot.test() } diff --git a/crates/biome_formatter_test/src/check_reformat.rs b/crates/biome_formatter_test/src/check_reformat.rs index 8ea27859695f..d51e1fc66dfb 100644 --- a/crates/biome_formatter_test/src/check_reformat.rs +++ b/crates/biome_formatter_test/src/check_reformat.rs @@ -4,7 +4,6 @@ use biome_diagnostics::console::markup; use biome_diagnostics::termcolor; use biome_diagnostics::{DiagnosticExt, PrintDiagnostic}; use biome_rowan::SyntaxNode; -use biome_service::settings::ServiceLanguage; /// Perform a second pass of formatting on a file, printing a diff if the /// output doesn't match the input @@ -18,7 +17,7 @@ where file_name: &'a str, language: &'a L, - options: ::FormatOptions, + format_language: L::FormatLanguage, } impl<'a, L> CheckReformat<'a, L> @@ -31,7 +30,7 @@ where file_name: &'a str, language: &'a L, - options: ::FormatOptions, + format_language: L::FormatLanguage, ) -> Self { CheckReformat { root, @@ -39,7 +38,7 @@ where file_name, language, - options, + format_language, } } @@ -70,7 +69,7 @@ where let formatted = match self .language - .format_node(self.options.clone(), &re_parse.syntax()) + .format_node(self.format_language.clone(), &re_parse.syntax()) { Ok(formatted) => formatted, Err(err) => { @@ -83,7 +82,7 @@ where if self.text != printed.as_code() { let input_format_element = self .language - .format_node(self.options.clone(), self.root) + .format_node(self.format_language.clone(), self.root) .unwrap(); let pretty_input_ir = format!("{}", formatted.into_document()); let pretty_reformat_ir = format!("{}", input_format_element.into_document()); diff --git a/crates/biome_formatter_test/src/lib.rs b/crates/biome_formatter_test/src/lib.rs index b24220ed2f8c..c5fb7d257f75 100644 --- a/crates/biome_formatter_test/src/lib.rs +++ b/crates/biome_formatter_test/src/lib.rs @@ -1,10 +1,8 @@ use biome_formatter::{CstFormatContext, FormatLanguage, FormatResult, Formatted, Printed}; -use biome_fs::BiomePath; use biome_parser::AnyParse; use biome_rowan::{SyntaxNode, TextRange}; use biome_service::file_handlers::DocumentFileSource; -use biome_service::settings::ServiceLanguage; -use biome_service::settings::Settings; +use biome_service::settings::{ServiceLanguage, Settings}; pub mod check_reformat; pub mod diff_report; @@ -19,7 +17,8 @@ pub trait TestFormatLanguage { Options = ::FormatOptions, >; type FormatLanguage: FormatLanguage - + 'static; + + 'static + + Clone; fn parse(&self, text: &str) -> AnyParse; @@ -30,31 +29,24 @@ pub trait TestFormatLanguage { fn format_node( &self, - options: ::FormatOptions, + language: Self::FormatLanguage, node: &SyntaxNode, - ) -> FormatResult>; + ) -> FormatResult> { + biome_formatter::format_node(node, language) + } fn format_range( &self, - options: ::FormatOptions, + language: Self::FormatLanguage, node: &SyntaxNode, range: TextRange, - ) -> FormatResult; - - fn default_options(&self) -> ::FormatOptions; + ) -> FormatResult { + biome_formatter::format_range(node, range, language) + } - fn to_options( + fn to_format_language( &self, settings: &Settings, file_source: &DocumentFileSource, - ) -> ::FormatOptions { - let language_settings = self.to_language_settings(settings); - Self::ServiceLanguage::resolve_format_options( - Some(&settings.formatter), - Some(&settings.override_settings), - Some(language_settings), - &BiomePath::new(""), - file_source, - ) - } + ) -> Self::FormatLanguage; } diff --git a/crates/biome_formatter_test/src/spec.rs b/crates/biome_formatter_test/src/spec.rs index 61c44c653a33..da0bc0eb9dd0 100644 --- a/crates/biome_formatter_test/src/spec.rs +++ b/crates/biome_formatter_test/src/spec.rs @@ -6,11 +6,11 @@ use biome_configuration::PartialConfiguration; use biome_console::EnvConsole; use biome_deserialize::json::deserialize_from_str; use biome_diagnostics::print_diagnostic_to_string; -use biome_formatter::{FormatOptions, Printed}; +use biome_formatter::{FormatLanguage, FormatOptions, Printed}; use biome_fs::BiomePath; use biome_parser::AnyParse; use biome_rowan::{TextRange, TextSize}; -use biome_service::settings::{ServiceLanguage, Settings}; +use biome_service::settings::Settings; use biome_service::workspace::{ DocumentFileSource, FeaturesBuilder, RegisterProjectFolderParams, SupportsFeatureParams, UpdateSettingsParams, @@ -122,7 +122,7 @@ where test_file: SpecTestFile<'a>, test_directory: PathBuf, language: L, - options: ::FormatOptions, + format_language: L::FormatLanguage, } impl<'a, L> SpecSnapshot<'a, L> @@ -133,7 +133,7 @@ where test_file: SpecTestFile<'a>, test_directory: &str, language: L, - options: ::FormatOptions, + format_language: L::FormatLanguage, ) -> Self { let test_directory = PathBuf::from(test_directory); @@ -141,14 +141,14 @@ where test_file, test_directory, language, - options, + format_language, } } fn formatted( &self, parsed: &AnyParse, - options: ::FormatOptions, + format_language: L::FormatLanguage, ) -> (String, Printed) { let has_errors = parsed.has_errors(); let syntax = parsed.syntax(); @@ -157,7 +157,7 @@ where let result = match range { (Some(start), Some(end)) => self.language.format_range( - options.clone(), + format_language.clone(), &syntax, TextRange::new( TextSize::try_from(start).unwrap(), @@ -166,7 +166,7 @@ where ), _ => self .language - .format_node(options.clone(), &syntax) + .format_node(format_language.clone(), &syntax) .map(|formatted| formatted.print().unwrap()), }; let formatted = result.expect("formatting failed"); @@ -201,7 +201,7 @@ where output_code, self.test_file.file_name(), &self.language, - options, + format_language, ); check_reformat.check_reformat(); } @@ -223,14 +223,14 @@ where let parsed = self.language.parse(self.test_file.input_code()); - let (output_code, printed) = self.formatted(&parsed, self.options.clone()); + let (output_code, printed) = self.formatted(&parsed, self.format_language.clone()); - let max_width = self.options.line_width().value() as usize; + let max_width = self.format_language.options().line_width().value() as usize; snapshot_builder = snapshot_builder .with_output_and_options( SnapshotOutput::new(&output_code).with_index(1), - self.options.clone(), + self.format_language.options().clone(), ) .with_unimplemented(&printed) .with_lines_exceeding_max_width(&output_code, max_width); @@ -257,13 +257,13 @@ where panic!("Configuration is invalid"); } - let options = self + let format_language = self .language - .to_options(&settings, &DocumentFileSource::from_path(input_file)); + .to_format_language(&settings, &DocumentFileSource::from_path(input_file)); - let (mut output_code, printed) = self.formatted(&parsed, options.clone()); + let (mut output_code, printed) = self.formatted(&parsed, format_language.clone()); - let max_width = options.line_width().value() as usize; + let max_width = format_language.options().line_width().value() as usize; // There are some logs that print different line endings, and we can't snapshot those // otherwise we risk automatically having them replaced with LF by git. @@ -276,7 +276,10 @@ where .replace(CR_PATTERN, "\n"); snapshot_builder = snapshot_builder - .with_output_and_options(SnapshotOutput::new(&output_code).with_index(1), options) + .with_output_and_options( + SnapshotOutput::new(&output_code).with_index(1), + format_language.options(), + ) .with_unimplemented(&printed) .with_lines_exceeding_max_width(&output_code, max_width); } diff --git a/crates/biome_formatter_test/src/test_prettier_snapshot.rs b/crates/biome_formatter_test/src/test_prettier_snapshot.rs index 66a04d8d3571..9f226a6f7c3a 100644 --- a/crates/biome_formatter_test/src/test_prettier_snapshot.rs +++ b/crates/biome_formatter_test/src/test_prettier_snapshot.rs @@ -5,9 +5,8 @@ use crate::check_reformat::CheckReformat; use crate::snapshot_builder::{SnapshotBuilder, SnapshotOutput}; use crate::utils::{get_prettier_diff, strip_prettier_placeholders, PrettierDiff}; use crate::TestFormatLanguage; -use biome_formatter::FormatOptions; +use biome_formatter::{FormatLanguage, FormatOptions}; use biome_parser::AnyParse; -use biome_service::settings::ServiceLanguage; const PRETTIER_IGNORE: &str = "prettier-ignore"; const BIOME_IGNORE: &str = "biome-ignore format: prettier ignore"; @@ -96,7 +95,8 @@ where { test_file: PrettierTestFile<'a>, language: L, - options: ::FormatOptions, + // options: ::FormatOptions, + format_language: L::FormatLanguage, } impl<'a, L> PrettierSnapshot<'a, L> @@ -106,12 +106,12 @@ where pub fn new( test_file: PrettierTestFile<'a>, language: L, - options: ::FormatOptions, + format_language: L::FormatLanguage, ) -> Self { PrettierSnapshot { test_file, language, - options, + format_language, } } @@ -130,7 +130,7 @@ where } self.language.format_range( - self.options.clone(), + self.format_language.clone(), &syntax, TextRange::new( TextSize::try_from(start).unwrap(), @@ -140,7 +140,7 @@ where } _ => self .language - .format_node(self.options.clone(), &syntax) + .format_node(self.format_language.clone(), &syntax) .map(|formatted| formatted.print().unwrap()), }; @@ -165,7 +165,7 @@ where &formatted, self.test_file.file_name(), &self.language, - self.options.clone(), + self.format_language.clone(), ); check_reformat.check_reformat(); } @@ -203,7 +203,7 @@ where .with_output(SnapshotOutput::new(&formatted)) .with_errors(&parsed, &self.test_file().parse_input); - let max_width = self.options.line_width().value() as usize; + let max_width = self.format_language.options().line_width().value() as usize; builder = builder.with_lines_exceeding_max_width(&formatted, max_width); builder.finish(relative_file_name); diff --git a/crates/biome_js_formatter/Cargo.toml b/crates/biome_js_formatter/Cargo.toml index 718f4738f826..dd63e08425b4 100644 --- a/crates/biome_js_formatter/Cargo.toml +++ b/crates/biome_js_formatter/Cargo.toml @@ -30,6 +30,7 @@ unicode-width = { workspace = true } [dev-dependencies] biome_formatter_test = { path = "../biome_formatter_test" } +biome_fs = { path = "../biome_fs" } biome_js_factory = { path = "../biome_js_factory" } biome_js_parser = { path = "../biome_js_parser" } biome_parser = { path = "../biome_parser" } diff --git a/crates/biome_js_formatter/tests/language.rs b/crates/biome_js_formatter/tests/language.rs index 4346cd8660e7..0a9d8cb49347 100644 --- a/crates/biome_js_formatter/tests/language.rs +++ b/crates/biome_js_formatter/tests/language.rs @@ -1,13 +1,11 @@ -use biome_formatter::{FormatResult, Formatted, Printed}; use biome_formatter_test::TestFormatLanguage; -use biome_js_formatter::context::{JsFormatContext, JsFormatOptions}; -use biome_js_formatter::{format_node, format_range, JsFormatLanguage}; +use biome_fs::BiomePath; +use biome_js_formatter::context::JsFormatContext; +use biome_js_formatter::JsFormatLanguage; use biome_js_parser::{parse, JsParserOptions}; use biome_js_syntax::{JsFileSource, JsLanguage}; use biome_parser::AnyParse; -use biome_rowan::SyntaxNode; use biome_service::settings::{ServiceLanguage, Settings}; -use biome_text_size::TextRange; pub struct JsTestFormatLanguage { source_type: JsFileSource, @@ -37,24 +35,19 @@ impl TestFormatLanguage for JsTestFormatLanguage { &settings.languages.javascript.formatter } - fn format_node( + fn to_format_language( &self, - options: ::FormatOptions, - node: &SyntaxNode, - ) -> FormatResult> { - format_node(options, node) - } - - fn format_range( - &self, - options: ::FormatOptions, - node: &SyntaxNode, - range: TextRange, - ) -> FormatResult { - format_range(options, node, range) - } - - fn default_options(&self) -> ::FormatOptions { - JsFormatOptions::new(self.source_type) + settings: &Settings, + file_source: &biome_service::workspace::DocumentFileSource, + ) -> Self::FormatLanguage { + let language_settings = self.to_language_settings(settings); + let options = Self::ServiceLanguage::resolve_format_options( + Some(&settings.formatter), + Some(&settings.override_settings), + Some(language_settings), + &BiomePath::new(""), + file_source, + ); + JsFormatLanguage::new(options) } } diff --git a/crates/biome_js_formatter/tests/prettier_tests.rs b/crates/biome_js_formatter/tests/prettier_tests.rs index c296e1dcb574..59175a3ecea2 100644 --- a/crates/biome_js_formatter/tests/prettier_tests.rs +++ b/crates/biome_js_formatter/tests/prettier_tests.rs @@ -2,7 +2,7 @@ use std::{env, path::Path}; use biome_formatter::{IndentStyle, IndentWidth}; use biome_formatter_test::test_prettier_snapshot::{PrettierSnapshot, PrettierTestFile}; -use biome_js_formatter::context::JsFormatOptions; +use biome_js_formatter::{context::JsFormatOptions, JsFormatLanguage}; use biome_js_syntax::{JsFileSource, LanguageVariant, ModuleKind}; mod language; @@ -45,7 +45,7 @@ fn test_snapshot(input: &'static str, _: &str, _: &str, _: &str) { let language = language::JsTestFormatLanguage::new(source_type); - let snapshot = PrettierSnapshot::new(test_file, language, options); + let snapshot = PrettierSnapshot::new(test_file, language, JsFormatLanguage::new(options)); snapshot.test() } diff --git a/crates/biome_js_formatter/tests/quick_test.rs b/crates/biome_js_formatter/tests/quick_test.rs index 934cf0bcdf29..e2b38d871768 100644 --- a/crates/biome_js_formatter/tests/quick_test.rs +++ b/crates/biome_js_formatter/tests/quick_test.rs @@ -1,7 +1,7 @@ use biome_formatter::{AttributePosition, IndentStyle, LineWidth, QuoteStyle}; use biome_formatter_test::check_reformat::CheckReformat; use biome_js_formatter::context::{ArrowParentheses, JsFormatOptions, Semicolons}; -use biome_js_formatter::format_node; +use biome_js_formatter::{format_node, JsFormatLanguage}; use biome_js_parser::{parse, JsParserOptions}; use biome_js_syntax::JsFileSource; @@ -51,7 +51,7 @@ function outerFunctionToForceIndent() { result.as_code(), "testing", &language::JsTestFormatLanguage::new(source_type), - options, + JsFormatLanguage::new(options), ) .check_reformat(); } diff --git a/crates/biome_js_formatter/tests/spec_test.rs b/crates/biome_js_formatter/tests/spec_test.rs index e3009bae7c6f..c21eff69f01c 100644 --- a/crates/biome_js_formatter/tests/spec_test.rs +++ b/crates/biome_js_formatter/tests/spec_test.rs @@ -1,5 +1,5 @@ use biome_formatter_test::spec::{SpecSnapshot, SpecTestFile}; -use biome_js_formatter::context::JsFormatOptions; +use biome_js_formatter::{context::JsFormatOptions, JsFormatLanguage}; use biome_js_syntax::{JsFileSource, ModuleKind}; use std::path::Path; @@ -39,7 +39,12 @@ pub fn run(spec_input_file: &str, _expected_file: &str, test_directory: &str, fi let options = JsFormatOptions::new(source_type); let language = language::JsTestFormatLanguage::new(source_type); - let snapshot = SpecSnapshot::new(test_file, test_directory, language, options); + let snapshot = SpecSnapshot::new( + test_file, + test_directory, + language, + JsFormatLanguage::new(options), + ); snapshot.test() } diff --git a/crates/biome_json_formatter/Cargo.toml b/crates/biome_json_formatter/Cargo.toml index e6e570b2e0d9..be80ef65c9ed 100644 --- a/crates/biome_json_formatter/Cargo.toml +++ b/crates/biome_json_formatter/Cargo.toml @@ -26,6 +26,7 @@ serde = { workspace = true, features = ["derive"], optional = [dev-dependencies] biome_formatter_test = { path = "../biome_formatter_test" } +biome_fs = { path = "../biome_fs" } biome_json_parser = { path = "../biome_json_parser" } biome_parser = { path = "../biome_parser" } biome_service = { path = "../biome_service" } diff --git a/crates/biome_json_formatter/tests/language.rs b/crates/biome_json_formatter/tests/language.rs index 498f167379e1..2dcebc7f8d5c 100644 --- a/crates/biome_json_formatter/tests/language.rs +++ b/crates/biome_json_formatter/tests/language.rs @@ -1,13 +1,11 @@ -use biome_formatter::{ - FormatResult, Formatted, IndentStyle, IndentWidth, LineEnding, LineWidth, Printed, -}; +use biome_formatter::{IndentStyle, IndentWidth, LineEnding, LineWidth}; use biome_formatter_test::TestFormatLanguage; +use biome_fs::BiomePath; use biome_json_formatter::context::{JsonFormatContext, JsonFormatOptions}; -use biome_json_formatter::{format_node, format_range, JsonFormatLanguage}; +use biome_json_formatter::JsonFormatLanguage; use biome_json_parser::{parse_json, JsonParserOptions}; use biome_json_syntax::{JsonFileSource, JsonLanguage}; use biome_parser::AnyParse; -use biome_rowan::{SyntaxNode, TextRange}; use biome_service::settings::{ServiceLanguage, Settings}; use serde::{Deserialize, Serialize}; @@ -32,25 +30,20 @@ impl TestFormatLanguage for JsonTestFormatLanguage { &settings.languages.json.formatter } - fn format_node( + fn to_format_language( &self, - options: ::FormatOptions, - node: &SyntaxNode, - ) -> FormatResult> { - format_node(options, node) - } - - fn format_range( - &self, - options: ::FormatOptions, - node: &SyntaxNode, - range: TextRange, - ) -> FormatResult { - format_range(options, node, range) - } - - fn default_options(&self) -> ::FormatOptions { - JsonFormatOptions::default() + settings: &Settings, + file_source: &biome_service::workspace::DocumentFileSource, + ) -> Self::FormatLanguage { + let language_settings = self.to_language_settings(settings); + let options = Self::ServiceLanguage::resolve_format_options( + Some(&settings.formatter), + Some(&settings.override_settings), + Some(language_settings), + &BiomePath::new(""), + file_source, + ); + JsonFormatLanguage::new(options) } } diff --git a/crates/biome_json_formatter/tests/prettier_tests.rs b/crates/biome_json_formatter/tests/prettier_tests.rs index 484e3a2b8817..d5992617fcb6 100644 --- a/crates/biome_json_formatter/tests/prettier_tests.rs +++ b/crates/biome_json_formatter/tests/prettier_tests.rs @@ -2,7 +2,7 @@ use std::{env, path::Path}; use biome_formatter::{IndentStyle, IndentWidth}; use biome_formatter_test::test_prettier_snapshot::{PrettierSnapshot, PrettierTestFile}; -use biome_json_formatter::context::JsonFormatOptions; +use biome_json_formatter::{context::JsonFormatOptions, JsonFormatLanguage}; mod language; @@ -21,7 +21,7 @@ fn test_snapshot(input: &'static str, _: &str, _: &str, _: &str) { .with_indent_style(IndentStyle::Space) .with_indent_width(IndentWidth::default()); let language = language::JsonTestFormatLanguage::default(); - let snapshot = PrettierSnapshot::new(test_file, language, options); + let snapshot = PrettierSnapshot::new(test_file, language, JsonFormatLanguage::new(options)); snapshot.test() } diff --git a/crates/biome_json_formatter/tests/quick_test.rs b/crates/biome_json_formatter/tests/quick_test.rs index 8fcccb82d14d..6b1253f4539a 100644 --- a/crates/biome_json_formatter/tests/quick_test.rs +++ b/crates/biome_json_formatter/tests/quick_test.rs @@ -1,6 +1,6 @@ use biome_formatter_test::check_reformat::CheckReformat; -use biome_json_formatter::context::JsonFormatOptions; use biome_json_formatter::format_node; +use biome_json_formatter::{context::JsonFormatOptions, JsonFormatLanguage}; use biome_json_parser::{parse_json, JsonParserOptions}; mod language { @@ -25,8 +25,13 @@ fn quick_test() { let root = &parse.syntax(); let language = language::JsonTestFormatLanguage::default(); - let check_reformat = - CheckReformat::new(root, result.as_code(), "quick_test", &language, options); + let check_reformat = CheckReformat::new( + root, + result.as_code(), + "quick_test", + &language, + JsonFormatLanguage::new(options), + ); check_reformat.check_reformat(); assert_eq!( diff --git a/crates/biome_json_formatter/tests/spec_test.rs b/crates/biome_json_formatter/tests/spec_test.rs index 8bd77f680a0c..25e283e4101b 100644 --- a/crates/biome_json_formatter/tests/spec_test.rs +++ b/crates/biome_json_formatter/tests/spec_test.rs @@ -1,5 +1,5 @@ use biome_formatter_test::spec::{SpecSnapshot, SpecTestFile}; -use biome_json_formatter::context::JsonFormatOptions; +use biome_json_formatter::{context::JsonFormatOptions, JsonFormatLanguage}; use std::path::Path; mod language { @@ -33,7 +33,12 @@ pub fn run(spec_input_file: &str, _expected_file: &str, test_directory: &str, _f let options = JsonFormatOptions::default(); let language = language::JsonTestFormatLanguage::default(); - let snapshot = SpecSnapshot::new(test_file, test_directory, language, options); + let snapshot = SpecSnapshot::new( + test_file, + test_directory, + language, + JsonFormatLanguage::new(options), + ); snapshot.test() } From e0d26b9f1cc799679b187dd84917448ccdde848d Mon Sep 17 00:00:00 2001 From: Zheyu Zhang Date: Wed, 3 Jul 2024 11:38:06 +0800 Subject: [PATCH 2/4] remove to_language_settings --- crates/biome_css_formatter/tests/language.rs | 9 +-------- crates/biome_formatter_test/src/lib.rs | 5 ----- crates/biome_js_formatter/tests/language.rs | 9 +-------- crates/biome_json_formatter/tests/language.rs | 9 +-------- 4 files changed, 3 insertions(+), 29 deletions(-) diff --git a/crates/biome_css_formatter/tests/language.rs b/crates/biome_css_formatter/tests/language.rs index e4967fe1f0e9..b7724f384e43 100644 --- a/crates/biome_css_formatter/tests/language.rs +++ b/crates/biome_css_formatter/tests/language.rs @@ -25,19 +25,12 @@ impl TestFormatLanguage for CssTestFormatLanguage { parse_css(text, options).into() } - fn to_language_settings<'a>( - &self, - settings: &'a Settings, - ) -> &'a ::FormatterSettings { - &settings.languages.css.formatter - } - fn to_format_language( &self, settings: &Settings, file_source: &biome_service::workspace::DocumentFileSource, ) -> Self::FormatLanguage { - let language_settings = self.to_language_settings(settings); + let language_settings = &settings.languages.css.formatter; let options = Self::ServiceLanguage::resolve_format_options( Some(&settings.formatter), Some(&settings.override_settings), diff --git a/crates/biome_formatter_test/src/lib.rs b/crates/biome_formatter_test/src/lib.rs index c5fb7d257f75..4804c28d47ab 100644 --- a/crates/biome_formatter_test/src/lib.rs +++ b/crates/biome_formatter_test/src/lib.rs @@ -22,11 +22,6 @@ pub trait TestFormatLanguage { fn parse(&self, text: &str) -> AnyParse; - fn to_language_settings<'a>( - &self, - settings: &'a Settings, - ) -> &'a ::FormatterSettings; - fn format_node( &self, language: Self::FormatLanguage, diff --git a/crates/biome_js_formatter/tests/language.rs b/crates/biome_js_formatter/tests/language.rs index 0a9d8cb49347..c3c925ad7708 100644 --- a/crates/biome_js_formatter/tests/language.rs +++ b/crates/biome_js_formatter/tests/language.rs @@ -28,19 +28,12 @@ impl TestFormatLanguage for JsTestFormatLanguage { parse(text, self.source_type, options).into() } - fn to_language_settings<'a>( - &self, - settings: &'a Settings, - ) -> &'a ::FormatterSettings { - &settings.languages.javascript.formatter - } - fn to_format_language( &self, settings: &Settings, file_source: &biome_service::workspace::DocumentFileSource, ) -> Self::FormatLanguage { - let language_settings = self.to_language_settings(settings); + let language_settings = &settings.languages.javascript.formatter; let options = Self::ServiceLanguage::resolve_format_options( Some(&settings.formatter), Some(&settings.override_settings), diff --git a/crates/biome_json_formatter/tests/language.rs b/crates/biome_json_formatter/tests/language.rs index 2dcebc7f8d5c..76acf2fb398e 100644 --- a/crates/biome_json_formatter/tests/language.rs +++ b/crates/biome_json_formatter/tests/language.rs @@ -23,19 +23,12 @@ impl TestFormatLanguage for JsonTestFormatLanguage { parse_json(text, JsonParserOptions::default().with_allow_comments()).into() } - fn to_language_settings<'a>( - &self, - settings: &'a Settings, - ) -> &'a ::FormatterSettings { - &settings.languages.json.formatter - } - fn to_format_language( &self, settings: &Settings, file_source: &biome_service::workspace::DocumentFileSource, ) -> Self::FormatLanguage { - let language_settings = self.to_language_settings(settings); + let language_settings = &settings.languages.json.formatter; let options = Self::ServiceLanguage::resolve_format_options( Some(&settings.formatter), Some(&settings.override_settings), From e225450bf54dd378d06a9d99f2a153628428a615 Mon Sep 17 00:00:00 2001 From: Zheyu Zhang Date: Wed, 3 Jul 2024 12:01:20 +0800 Subject: [PATCH 3/4] adapt to changes for gql formatter --- Cargo.lock | 1 + crates/biome_css_formatter/tests/language.rs | 7 ++- crates/biome_graphql_formatter/Cargo.toml | 1 + .../biome_graphql_formatter/tests/language.rs | 50 ++++++++----------- .../tests/prettier_tests.rs | 4 +- .../tests/quick_test.rs | 11 +++- .../tests/spec_test.rs | 9 +++- 7 files changed, 45 insertions(+), 38 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index f8646e68073c..00de52263857 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -502,6 +502,7 @@ dependencies = [ "biome_diagnostics", "biome_formatter", "biome_formatter_test", + "biome_fs", "biome_graphql_parser", "biome_graphql_syntax", "biome_parser", diff --git a/crates/biome_css_formatter/tests/language.rs b/crates/biome_css_formatter/tests/language.rs index b7724f384e43..bdacd11c84f4 100644 --- a/crates/biome_css_formatter/tests/language.rs +++ b/crates/biome_css_formatter/tests/language.rs @@ -5,7 +5,10 @@ use biome_css_syntax::{CssFileSource, CssLanguage}; use biome_formatter_test::TestFormatLanguage; use biome_fs::BiomePath; use biome_parser::AnyParse; -use biome_service::settings::{ServiceLanguage, Settings}; +use biome_service::{ + settings::{ServiceLanguage, Settings}, + workspace::DocumentFileSource, +}; #[derive(Default)] pub struct CssTestFormatLanguage { @@ -28,7 +31,7 @@ impl TestFormatLanguage for CssTestFormatLanguage { fn to_format_language( &self, settings: &Settings, - file_source: &biome_service::workspace::DocumentFileSource, + file_source: &DocumentFileSource, ) -> Self::FormatLanguage { let language_settings = &settings.languages.css.formatter; let options = Self::ServiceLanguage::resolve_format_options( diff --git a/crates/biome_graphql_formatter/Cargo.toml b/crates/biome_graphql_formatter/Cargo.toml index a436fcde4ff9..3c281e18a2e4 100644 --- a/crates/biome_graphql_formatter/Cargo.toml +++ b/crates/biome_graphql_formatter/Cargo.toml @@ -22,6 +22,7 @@ biome_suppression = { workspace = true } [dev-dependencies] biome_configuration = { path = "../biome_configuration" } biome_formatter_test = { path = "../biome_formatter_test" } +biome_fs = { path = "../biome_fs" } biome_graphql_parser = { path = "../biome_graphql_parser" } biome_parser = { path = "../biome_parser" } biome_service = { path = "../biome_service" } diff --git a/crates/biome_graphql_formatter/tests/language.rs b/crates/biome_graphql_formatter/tests/language.rs index c22918f81158..26493e04295a 100644 --- a/crates/biome_graphql_formatter/tests/language.rs +++ b/crates/biome_graphql_formatter/tests/language.rs @@ -1,12 +1,14 @@ -use biome_formatter::{FormatResult, Formatted, Printed}; use biome_formatter_test::TestFormatLanguage; -use biome_graphql_formatter::context::{GraphqlFormatContext, GraphqlFormatOptions}; -use biome_graphql_formatter::{format_node, format_range, GraphqlFormatLanguage}; +use biome_fs::BiomePath; +use biome_graphql_formatter::context::GraphqlFormatContext; +use biome_graphql_formatter::GraphqlFormatLanguage; use biome_graphql_parser::parse_graphql; use biome_graphql_syntax::{GraphqlFileSource, GraphqlLanguage}; use biome_parser::AnyParse; -use biome_rowan::{SyntaxNode, TextRange}; -use biome_service::settings::{ServiceLanguage, Settings}; +use biome_service::{ + settings::{ServiceLanguage, Settings}, + workspace::DocumentFileSource, +}; #[derive(Default)] pub struct GraphqlTestFormatLanguage { @@ -24,31 +26,19 @@ impl TestFormatLanguage for GraphqlTestFormatLanguage { AnyParse::new(parse.syntax().as_send().unwrap(), parse.into_diagnostics()) } - fn to_language_settings<'a>( + fn to_format_language( &self, - settings: &'a Settings, - ) -> &'a ::FormatterSettings { - &settings.languages.graphql.formatter - } - - fn format_node( - &self, - options: ::FormatOptions, - node: &SyntaxNode, - ) -> FormatResult> { - format_node(options, node) - } - - fn format_range( - &self, - options: ::FormatOptions, - node: &SyntaxNode, - range: TextRange, - ) -> FormatResult { - format_range(options, node, range) - } - - fn default_options(&self) -> ::FormatOptions { - GraphqlFormatOptions::default() + settings: &Settings, + file_source: &DocumentFileSource, + ) -> Self::FormatLanguage { + let language_settings = &settings.languages.graphql.formatter; + let options = Self::ServiceLanguage::resolve_format_options( + Some(&settings.formatter), + Some(&settings.override_settings), + Some(language_settings), + &BiomePath::new(""), + file_source, + ); + GraphqlFormatLanguage::new(options) } } diff --git a/crates/biome_graphql_formatter/tests/prettier_tests.rs b/crates/biome_graphql_formatter/tests/prettier_tests.rs index 7bab51e87f49..07412b7e77f7 100644 --- a/crates/biome_graphql_formatter/tests/prettier_tests.rs +++ b/crates/biome_graphql_formatter/tests/prettier_tests.rs @@ -2,7 +2,7 @@ use std::{env, path::Path}; use biome_formatter::{IndentStyle, IndentWidth}; use biome_formatter_test::test_prettier_snapshot::{PrettierSnapshot, PrettierTestFile}; -use biome_graphql_formatter::context::GraphqlFormatOptions; +use biome_graphql_formatter::{context::GraphqlFormatOptions, GraphqlFormatLanguage}; mod language; @@ -22,7 +22,7 @@ fn test_snapshot(input: &'static str, _: &str, _: &str, _: &str) { .with_indent_style(IndentStyle::Space) .with_indent_width(IndentWidth::default()); let language = language::GraphqlTestFormatLanguage::default(); - let snapshot = PrettierSnapshot::new(test_file, language, options); + let snapshot = PrettierSnapshot::new(test_file, language, GraphqlFormatLanguage::new(options)); snapshot.test() } diff --git a/crates/biome_graphql_formatter/tests/quick_test.rs b/crates/biome_graphql_formatter/tests/quick_test.rs index 47ed4c8bacb6..9cdb0b3d0756 100644 --- a/crates/biome_graphql_formatter/tests/quick_test.rs +++ b/crates/biome_graphql_formatter/tests/quick_test.rs @@ -1,7 +1,7 @@ use biome_formatter::{IndentStyle, LineWidth}; use biome_formatter_test::check_reformat::CheckReformat; use biome_graphql_formatter::context::GraphqlFormatOptions; -use biome_graphql_formatter::format_node; +use biome_graphql_formatter::{format_node, GraphqlFormatLanguage}; use biome_graphql_parser::parse_graphql; mod language { @@ -38,5 +38,12 @@ E { println!("{}", doc.into_document()); eprintln!("{}", result.as_code()); - CheckReformat::new(root, result.as_code(), "quick_test", &language, options).check_reformat(); + CheckReformat::new( + root, + result.as_code(), + "quick_test", + &language, + GraphqlFormatLanguage::new(options), + ) + .check_reformat(); } diff --git a/crates/biome_graphql_formatter/tests/spec_test.rs b/crates/biome_graphql_formatter/tests/spec_test.rs index ddcb7350dff2..55905cf1c4bf 100644 --- a/crates/biome_graphql_formatter/tests/spec_test.rs +++ b/crates/biome_graphql_formatter/tests/spec_test.rs @@ -2,7 +2,7 @@ use biome_configuration::{ PartialConfiguration, PartialGraphqlConfiguration, PartialGraphqlFormatter, }; use biome_formatter_test::spec::{SpecSnapshot, SpecTestFile}; -use biome_graphql_formatter::context::GraphqlFormatOptions; +use biome_graphql_formatter::{context::GraphqlFormatOptions, GraphqlFormatLanguage}; use biome_service::workspace::UpdateSettingsParams; use std::path::Path; @@ -53,7 +53,12 @@ pub fn run(spec_input_file: &str, _expected_file: &str, test_directory: &str, _f let options = GraphqlFormatOptions::default(); let language = language::GraphqlTestFormatLanguage::default(); - let snapshot = SpecSnapshot::new(test_file, test_directory, language, options); + let snapshot = SpecSnapshot::new( + test_file, + test_directory, + language, + GraphqlFormatLanguage::new(options), + ); snapshot.test() } From 19a4f25cb6d6cc5e6de6d66f601235324d2941f4 Mon Sep 17 00:00:00 2001 From: Zheyu Zhang Date: Wed, 3 Jul 2024 12:01:31 +0800 Subject: [PATCH 4/4] cleanup --- crates/biome_js_formatter/tests/language.rs | 7 +++++-- crates/biome_json_formatter/tests/language.rs | 3 ++- 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/crates/biome_js_formatter/tests/language.rs b/crates/biome_js_formatter/tests/language.rs index c3c925ad7708..a94792bea95b 100644 --- a/crates/biome_js_formatter/tests/language.rs +++ b/crates/biome_js_formatter/tests/language.rs @@ -5,7 +5,10 @@ use biome_js_formatter::JsFormatLanguage; use biome_js_parser::{parse, JsParserOptions}; use biome_js_syntax::{JsFileSource, JsLanguage}; use biome_parser::AnyParse; -use biome_service::settings::{ServiceLanguage, Settings}; +use biome_service::{ + settings::{ServiceLanguage, Settings}, + workspace::DocumentFileSource, +}; pub struct JsTestFormatLanguage { source_type: JsFileSource, @@ -31,7 +34,7 @@ impl TestFormatLanguage for JsTestFormatLanguage { fn to_format_language( &self, settings: &Settings, - file_source: &biome_service::workspace::DocumentFileSource, + file_source: &DocumentFileSource, ) -> Self::FormatLanguage { let language_settings = &settings.languages.javascript.formatter; let options = Self::ServiceLanguage::resolve_format_options( diff --git a/crates/biome_json_formatter/tests/language.rs b/crates/biome_json_formatter/tests/language.rs index 76acf2fb398e..970a7c4d278f 100644 --- a/crates/biome_json_formatter/tests/language.rs +++ b/crates/biome_json_formatter/tests/language.rs @@ -7,6 +7,7 @@ use biome_json_parser::{parse_json, JsonParserOptions}; use biome_json_syntax::{JsonFileSource, JsonLanguage}; use biome_parser::AnyParse; use biome_service::settings::{ServiceLanguage, Settings}; +use biome_service::workspace::DocumentFileSource; use serde::{Deserialize, Serialize}; #[derive(Default)] @@ -26,7 +27,7 @@ impl TestFormatLanguage for JsonTestFormatLanguage { fn to_format_language( &self, settings: &Settings, - file_source: &biome_service::workspace::DocumentFileSource, + file_source: &DocumentFileSource, ) -> Self::FormatLanguage { let language_settings = &settings.languages.json.formatter; let options = Self::ServiceLanguage::resolve_format_options(