Skip to content

Commit

Permalink
format with errors
Browse files Browse the repository at this point in the history
  • Loading branch information
ah-yu committed Jul 10, 2024
1 parent ff8a756 commit 4a325c1
Show file tree
Hide file tree
Showing 6 changed files with 66 additions and 23 deletions.
7 changes: 7 additions & 0 deletions crates/biome_js_formatter/tests/language.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use biome_css_formatter::context::CssFormatOptions;
use biome_css_parser::{parse_css, CssParserOptions};
use biome_formatter::FormatError;
use biome_formatter_test::TestFormatLanguage;
use biome_fs::BiomePath;
use biome_js_formatter::{context::JsFormatContext, JsForeignLanguageFormatter};
Expand All @@ -24,6 +25,7 @@ impl JsTestFormatLanguage {

#[derive(Debug, Clone)]
struct MultiLanguageFormatter {
format_with_errors: bool,
css_parse_options: CssParserOptions,
css_format_options: CssFormatOptions,
}
Expand All @@ -37,6 +39,9 @@ impl JsForeignLanguageFormatter for MultiLanguageFormatter {
match language {
JsForeignLanguage::Css => {
let parse = parse_css(source, self.css_parse_options);
if !self.format_with_errors && parse.has_errors() {
return Err(FormatError::SyntaxError);
}
biome_css_formatter::format_node(self.css_format_options.clone(), &parse.syntax())
.map(|formatted| formatted.into_document())
}
Expand Down Expand Up @@ -91,9 +96,11 @@ impl TestFormatLanguage for JsTestFormatLanguage {
.quote_style
.unwrap_or_default(),
);
let format_with_error = settings.formatter.format_with_errors;
let multi_language_formatter = MultiLanguageFormatter {
css_parse_options,
css_format_options,
format_with_errors: format_with_error,
};
JsFormatLanguage::new(js_formatter_options, multi_language_formatter)
}
Expand Down
5 changes: 4 additions & 1 deletion crates/biome_js_formatter/tests/prettier_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use std::{env, path::Path};

use biome_css_formatter::context::CssFormatOptions;
use biome_css_parser::CssParserOptions;
use biome_formatter::{prelude::Document, FormatResult, IndentStyle, IndentWidth};
use biome_formatter::{prelude::Document, FormatError, FormatResult, IndentStyle, IndentWidth};
use biome_formatter_test::test_prettier_snapshot::{PrettierSnapshot, PrettierTestFile};
use biome_js_formatter::{
context::JsFormatOptions, JsForeignLanguage, JsForeignLanguageFormatter, JsFormatLanguage,
Expand All @@ -23,6 +23,9 @@ impl JsForeignLanguageFormatter for MultiLanguageFormatter {
let parse_options = CssParserOptions::default().allow_grit_metavariables();
let format_options = CssFormatOptions::default();
let parse = biome_css_parser::parse_css(source, parse_options);
if parse.has_errors() {
return Err(FormatError::SyntaxError);
}
biome_css_formatter::format_node(format_options, &parse.syntax())
.map(|formatted| formatted.into_document())
}
Expand Down
9 changes: 8 additions & 1 deletion crates/biome_js_formatter/tests/quick_test.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
use biome_css_formatter::context::CssFormatOptions;
use biome_css_parser::{parse_css, CssParserOptions};
use biome_formatter::prelude::Document;
use biome_formatter::{AttributePosition, FormatResult, IndentStyle, LineWidth, QuoteStyle};
use biome_formatter::{
AttributePosition, FormatError, FormatResult, IndentStyle, LineWidth, QuoteStyle,
};
use biome_formatter_test::check_reformat::CheckReformat;
use biome_js_formatter::context::{ArrowParentheses, JsFormatOptions, Semicolons};
use biome_js_formatter::{
Expand All @@ -16,6 +18,7 @@ mod language {

#[derive(Debug, Clone)]
struct MultiLanguageFormatter {
format_with_errors: bool,
css_parse_options: CssParserOptions,
css_format_options: CssFormatOptions,
}
Expand All @@ -25,6 +28,9 @@ impl JsForeignLanguageFormatter for MultiLanguageFormatter {
match language {
JsForeignLanguage::Css => {
let parse = parse_css(source, self.css_parse_options);
if parse.has_errors() && !self.format_with_errors {
return Err(FormatError::SyntaxError);
}
biome_css_formatter::format_node(self.css_format_options.clone(), &parse.syntax())
.map(|formatted| formatted.into_document())
}
Expand Down Expand Up @@ -68,6 +74,7 @@ function outerFunctionToForceIndent() {
let multi_language_formatter = MultiLanguageFormatter {
css_parse_options,
css_format_options,
format_with_errors: false,
};

let doc = format_node(
Expand Down
23 changes: 10 additions & 13 deletions crates/biome_js_formatter/tests/spec_test.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use biome_css_formatter::context::CssFormatOptions;
use biome_css_parser::{parse_css, CssParserOptions};
use biome_formatter::FormatError;
use biome_formatter_test::spec::{SpecSnapshot, SpecTestFile};
use biome_js_formatter::{
context::JsFormatOptions, JsForeignLanguage, JsForeignLanguageFormatter, JsFormatLanguage,
Expand All @@ -12,21 +13,23 @@ mod language {
}

#[derive(Debug, Clone)]
struct MultiLanguageFormatter {
css_parse_options: CssParserOptions,
css_format_options: CssFormatOptions,
}
struct MultiLanguageFormatter;

impl JsForeignLanguageFormatter for MultiLanguageFormatter {
fn format(
&self,
language: biome_js_formatter::JsForeignLanguage,
source: &str,
) -> biome_formatter::FormatResult<biome_formatter::prelude::Document> {
let css_parse_options = CssParserOptions::default().allow_grit_metavariables();
let css_format_options = CssFormatOptions::default();
match language {
JsForeignLanguage::Css => {
let parse = parse_css(source, self.css_parse_options);
biome_css_formatter::format_node(self.css_format_options.clone(), &parse.syntax())
let parse = parse_css(source, css_parse_options);
if parse.has_errors() {
return Err(FormatError::SyntaxError);
}
biome_css_formatter::format_node(css_format_options, &parse.syntax())
.map(|formatted| formatted.into_document())
}
}
Expand Down Expand Up @@ -63,19 +66,13 @@ pub fn run(spec_input_file: &str, _expected_file: &str, test_directory: &str, fi
}

let options = JsFormatOptions::new(source_type);
let css_parse_options = CssParserOptions::default().allow_grit_metavariables();
let css_format_options = CssFormatOptions::default();
let multi_language_formatter = MultiLanguageFormatter {
css_parse_options,
css_format_options,
};
let language = language::JsTestFormatLanguage::new(source_type);

let snapshot = SpecSnapshot::new(
test_file,
test_directory,
language,
JsFormatLanguage::new(options, multi_language_formatter),
JsFormatLanguage::new(options, MultiLanguageFormatter),
);

snapshot.test()
Expand Down
29 changes: 29 additions & 0 deletions crates/biome_service/src/file_handlers/javascript.rs
Original file line number Diff line number Diff line change
Expand Up @@ -306,6 +306,7 @@ impl ExtensionHandler for JsFileHandler {

#[derive(Clone, Debug)]
struct MultiLanguageFormatter {
format_with_errors: bool,
css_parse_options: CssParserOptions,
css_format_options: CssFormatOptions,
}
Expand All @@ -319,6 +320,9 @@ impl JsForeignLanguageFormatter for MultiLanguageFormatter {
match language {
JsForeignLanguage::Css => {
let parse = parse_css(content, self.css_parse_options);
if parse.has_errors() && !self.format_with_errors {
return Err(FormatError::SyntaxError);
}
biome_css_formatter::format_node(self.css_format_options.clone(), &parse.syntax())
.map(|formatted| formatted.into_document())
}
Expand Down Expand Up @@ -428,9 +432,14 @@ fn debug_formatter_ir(
})
.unwrap_or_default();
let css_format_options = settings.format_options::<CssLanguage>(path, document_file_source);
let format_with_errors = settings
.settings()
.map(|settings| settings.formatter.format_with_errors)
.unwrap_or_default();
let multi_language_formatter = MultiLanguageFormatter {
css_parse_options,
css_format_options,
format_with_errors,
};

let tree = parse.syntax();
Expand Down Expand Up @@ -833,9 +842,14 @@ pub(crate) fn fix_all(params: FixAllParams) -> Result<FixFileResult, WorkspaceEr
.unwrap_or_default();
let css_format_options =
workspace.format_options::<CssLanguage>(biome_path, &document_file_source);
let format_with_errors = workspace
.settings()
.map(|settings| settings.formatter.format_with_errors)
.unwrap_or_default();
let multi_language_formatter = MultiLanguageFormatter {
css_parse_options,
css_format_options,
format_with_errors,
};
let code = if should_format {
format_node(
Expand Down Expand Up @@ -883,9 +897,14 @@ pub(crate) fn format(
.unwrap_or_default();
let css_format_options =
settings.format_options::<CssLanguage>(biome_path, document_file_source);
let format_with_errors = settings
.settings()
.map(|settings| settings.formatter.format_with_errors)
.unwrap_or_default();
let multi_language_formatter = MultiLanguageFormatter {
css_parse_options,
css_format_options,
format_with_errors,
};
let formatted = format_node(options, multi_language_formatter, &tree)?;
match formatted.print() {
Expand Down Expand Up @@ -919,9 +938,14 @@ pub(crate) fn format_range(
.unwrap_or_default();
let css_format_options =
settings.format_options::<CssLanguage>(biome_path, document_file_source);
let format_with_errors = settings
.settings()
.map(|settings| settings.formatter.format_with_errors)
.unwrap_or_default();
let multi_language_formatter = MultiLanguageFormatter {
css_parse_options,
css_format_options,
format_with_errors,
};
let printed =
biome_js_formatter::format_range(options, multi_language_formatter, &tree, range)?;
Expand Down Expand Up @@ -972,9 +996,14 @@ pub(crate) fn format_on_type(
})
.unwrap_or_default();
let css_format_options = settings.format_options::<CssLanguage>(path, document_file_source);
let format_with_errors = settings
.settings()
.map(|settings| settings.formatter.format_with_errors)
.unwrap_or_default();
let multi_language_formatter = MultiLanguageFormatter {
css_parse_options,
css_format_options,
format_with_errors,
};

let printed =
Expand Down
16 changes: 8 additions & 8 deletions xtask/bench/src/language.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@ use crate::test_case::TestCase;
use biome_analyze::options::JsxRuntime;
use biome_analyze::{AnalysisFilter, AnalyzerOptions, ControlFlow, Never, RuleCategoriesBuilder};
use biome_css_formatter::context::{CssFormatContext, CssFormatOptions};
use biome_css_parser::CssParserOptions;
use biome_css_parser::{parse_css, CssParserOptions};
use biome_css_syntax::{CssRoot, CssSyntaxNode};
use biome_formatter::{FormatResult, Formatted, PrintResult, Printed};
use biome_formatter::prelude::Document;
use biome_formatter::{FormatError, FormatResult, Formatted, PrintResult, Printed};
use biome_graphql_formatter::context::{GraphqlFormatContext, GraphqlFormatOptions};
use biome_graphql_syntax::GraphqlSyntaxNode;
use biome_js_formatter::context::{JsFormatContext, JsFormatOptions};
Expand Down Expand Up @@ -130,17 +131,16 @@ impl Parsed {
struct MultiLanguageFormatter;

impl JsForeignLanguageFormatter for MultiLanguageFormatter {
fn format(
&self,
language: biome_js_formatter::JsForeignLanguage,
source: &str,
) -> FormatResult<biome_formatter::prelude::Document> {
fn format(&self, language: JsForeignLanguage, source: &str) -> FormatResult<Document> {
match language {
JsForeignLanguage::Css => {
let parse = biome_css_parser::parse_css(
let parse = parse_css(
source,
CssParserOptions::default().allow_grit_metavariables(),
);
if parse.has_errors() {
return Err(FormatError::SyntaxError);
}
biome_css_formatter::format_node(CssFormatOptions::default(), &parse.syntax())
.map(|formatted| formatted.into_document())
}
Expand Down

0 comments on commit 4a325c1

Please sign in to comment.