Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(js_formatter): explore embedded language formatting #3228

Closed
wants to merge 15 commits into from
  •  
  •  
  •  
8 changes: 8 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions crates/biome_cli/src/commands/rage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,7 @@ impl Display for RageConfiguration<'_, '_> {
{KeyValuePair("Line ending", markup!({DebugDisplayOption(javascript_formatter_configuration.line_ending)}))}
{KeyValuePair("Line width", markup!({DebugDisplayOption(javascript_formatter_configuration.line_width.map(|lw| lw.value()))}))}
{KeyValuePair("Attribute position", markup!({DebugDisplay(javascript_formatter_configuration.attribute_position)}))}
{KeyValuePair("Embedded language formatting", markup!({DebugDisplay(javascript_formatter_configuration.embedded_language_formatting)}))}
)
.fmt(fmt)?;

Expand Down
30 changes: 29 additions & 1 deletion crates/biome_cli/src/execute/migrate/prettier.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@ use biome_formatter::{
AttributePosition, IndentWidth, LineEnding, LineWidth, ParseFormatNumberError, QuoteStyle,
};
use biome_fs::{FileSystem, OpenOptions};
use biome_js_formatter::context::{ArrowParentheses, QuoteProperties, Semicolons, TrailingCommas};
use biome_js_formatter::context::{
ArrowParentheses, EmbeddedLanguageFormatting, QuoteProperties, Semicolons, TrailingCommas,
};
use biome_json_parser::JsonParserOptions;
use biome_service::DynRef;
use std::path::Path;
Expand Down Expand Up @@ -58,6 +60,8 @@ pub(crate) struct PrettierConfiguration {
end_of_line: EndOfLine,
/// https://prettier.io/docs/en/configuration.html#configuration-overrides
overrides: Vec<Override>,
/// https://prettier.io/docs/en/options#embedded-language-formatting
embedded_language_formatting: PrettierEmbeddedLanguageFormatting,
}

impl Default for PrettierConfiguration {
Expand All @@ -76,6 +80,7 @@ impl Default for PrettierConfiguration {
arrow_parens: ArrowParens::default(),
end_of_line: EndOfLine::default(),
overrides: vec![],
embedded_language_formatting: PrettierEmbeddedLanguageFormatting::default(),
}
}
}
Expand Down Expand Up @@ -113,6 +118,8 @@ pub(crate) struct OverrideOptions {
arrow_parens: Option<ArrowParens>,
/// https://prettier.io/docs/en/options#end-of-line
end_of_line: Option<EndOfLine>,
/// https://prettier.io/docs/en/options#embedded-language-formatting
embedded_language_formatting: Option<PrettierEmbeddedLanguageFormatting>,
}

#[derive(Clone, Debug, Default, Deserializable, Eq, PartialEq)]
Expand Down Expand Up @@ -147,6 +154,13 @@ enum QuoteProps {
Preserve,
}

#[derive(Clone, Debug, Default, Deserializable, Eq, PartialEq)]
enum PrettierEmbeddedLanguageFormatting {
#[default]
Auto,
Off,
}

impl From<PrettierTrailingComma> for TrailingCommas {
fn from(value: PrettierTrailingComma) -> Self {
match value {
Expand Down Expand Up @@ -186,6 +200,15 @@ impl From<QuoteProps> for QuoteProperties {
}
}

impl From<PrettierEmbeddedLanguageFormatting> for EmbeddedLanguageFormatting {
fn from(value: PrettierEmbeddedLanguageFormatting) -> Self {
match value {
PrettierEmbeddedLanguageFormatting::Auto => Self::Auto,
PrettierEmbeddedLanguageFormatting::Off => Self::Off,
}
}
}

impl TryFrom<PrettierConfiguration> for biome_configuration::PartialConfiguration {
type Error = ParseFormatNumberError;
fn try_from(value: PrettierConfiguration) -> Result<Self, Self::Error> {
Expand Down Expand Up @@ -249,6 +272,7 @@ impl TryFrom<PrettierConfiguration> for biome_configuration::PartialConfiguratio
bracket_spacing: Some(value.bracket_spacing),
jsx_quote_style: Some(jsx_quote_style),
attribute_position: Some(AttributePosition::default()),
embedded_language_formatting: Some(value.embedded_language_formatting.into()),
};
let js_config = biome_configuration::PartialJavascriptConfiguration {
formatter: Some(js_formatter),
Expand Down Expand Up @@ -314,6 +338,7 @@ impl TryFrom<Override> for biome_configuration::OverridePattern {
&& options.trailing_comma.is_none()
&& options.quote_props.is_none()
&& options.bracket_spacing.is_none()
&& options.embedded_language_formatting.is_none()
{
// no js option are set
return Ok(result);
Expand Down Expand Up @@ -351,6 +376,9 @@ impl TryFrom<Override> for biome_configuration::OverridePattern {
quote_properties: options.quote_props.map(|quote_props| quote_props.into()),
bracket_spacing: options.bracket_spacing,
jsx_quote_style,
embedded_language_formatting: options
.embedded_language_formatting
.map(|embedded_language_formatting| embedded_language_formatting.into()),
..Default::default()
};
let js_config = biome_configuration::PartialJavascriptConfiguration {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ The configuration that is contained inside the file `biome.json`
--bracket-same-line=<true|false> Whether to hug the closing bracket of multiline HTML/JSX tags
to the end of the last line, rather than being alone on the following line.
Defaults to false.
--embedded-language-formatting=<auto|off> Whether to format quoted code embedded in the file.
Defaults to "off".
--javascript-formatter-enabled=<true|false> Control the formatter for JavaScript (and its super
languages) files.
--javascript-formatter-indent-style=<tab|space> The indent style applied to JavaScript (and
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@ The configuration that is contained inside the file `biome.json`
--bracket-same-line=<true|false> Whether to hug the closing bracket of multiline HTML/JSX tags
to the end of the last line, rather than being alone on the following line.
Defaults to false.
--embedded-language-formatting=<auto|off> Whether to format quoted code embedded in the file.
Defaults to "off".
--javascript-formatter-enabled=<true|false> Control the formatter for JavaScript (and its super
languages) files.
--javascript-formatter-indent-style=<tab|space> The indent style applied to JavaScript (and
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ Formatting options specific to the JavaScript files
--bracket-same-line=<true|false> Whether to hug the closing bracket of multiline HTML/JSX tags
to the end of the last line, rather than being alone on the following line.
Defaults to false.
--embedded-language-formatting=<auto|off> Whether to format quoted code embedded in the file.
Defaults to "off".
--javascript-formatter-enabled=<true|false> Control the formatter for JavaScript (and its super
languages) files.
--javascript-formatter-indent-style=<tab|space> The indent style applied to JavaScript (and
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,13 @@ biome.json migrate ━━━━━━━━━━━━━━━━━━━━
18 │ + → → → "arrowParentheses":·"always",
19 │ + → → → "bracketSpacing":·true,
20 │ + → → → "bracketSameLine":·false,
21 │ + → → → "quoteStyle":·"single",
22 │ + → → → "attributePosition":·"auto"
23 │ + → → }
24 │ + → }
25 │ + }
26 │ +
21 │ + → → → "embeddedLanguageFormatting":·"auto",
22 │ + → → → "quoteStyle":·"single",
23 │ + → → → "attributePosition":·"auto"
24 │ + → → }
25 │ + → }
26 │ + }
27 │ +


```
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,12 +45,13 @@ biome.json migrate ━━━━━━━━━━━━━━━━━━━━
17 │ + → → → "arrowParentheses":·"always",
18 │ + → → → "bracketSpacing":·true,
19 │ + → → → "bracketSameLine":·false,
20 │ + → → → "quoteStyle":·"single",
21 │ + → → → "attributePosition":·"auto"
22 │ + → → }
23 │ + → }
24 │ + }
25 │ +
20 │ + → → → "embeddedLanguageFormatting":·"auto",
21 │ + → → → "quoteStyle":·"single",
22 │ + → → → "attributePosition":·"auto"
23 │ + → → }
24 │ + → }
25 │ + }
26 │ +


```
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ expression: content
"arrowParentheses": "always",
"bracketSpacing": true,
"bracketSameLine": false,
"embeddedLanguageFormatting": "auto",
"quoteStyle": "single",
"attributePosition": "auto"
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,13 @@ biome.jsonc migrate ━━━━━━━━━━━━━━━━━━━━
18 │ + → → → "arrowParentheses":·"always",
19 │ + → → → "bracketSpacing":·true,
20 │ + → → → "bracketSameLine":·false,
21 │ + → → → "quoteStyle":·"single",
22 │ + → → → "attributePosition":·"auto"
23 │ + → → }
24 │ + → }
25 │ + }
26 │ +
21 │ + → → → "embeddedLanguageFormatting":·"auto",
22 │ + → → → "quoteStyle":·"single",
23 │ + → → → "attributePosition":·"auto"
24 │ + → → }
25 │ + → }
26 │ + }
27 │ +


```
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,28 +52,29 @@ biome.json migrate ━━━━━━━━━━━━━━━━━━━━
17 │ + → → → "arrowParentheses":·"always",
18 │ + → → → "bracketSpacing":·true,
19 │ + → → → "bracketSameLine":·false,
20 │ + → → → "quoteStyle":·"single",
21 │ + → → → "attributePosition":·"auto"
22 │ + → → }
23 │ + → },
24 │ + → "overrides":·[
25 │ + → → {·"include":·["**/*.test.js"],·"formatter":·{·"indentStyle":·"space"·}·},
26 │ + → → {
27 │ + → → → "include":·["**/*.spec.js"],
28 │ + → → → "javascript":·{
29 │ + → → → → "formatter":·{·"semicolons":·"always",·"quoteStyle":·"single"·}
30 │ + → → → }
31 │ + → → },
32 │ + → → {
33 │ + → → → "include":·["**/*.ts"],
34 │ + → → → "javascript":·{
35 │ + → → → → "formatter":·{·"semicolons":·"always",·"quoteStyle":·"single"·}
36 │ + → → → },
37 │ + → → → "formatter":·{·"indentStyle":·"space"·}
38 │ + → → }
39 │ + → ]
40 │ + }
41 │ +
20 │ + → → → "embeddedLanguageFormatting":·"auto",
21 │ + → → → "quoteStyle":·"single",
22 │ + → → → "attributePosition":·"auto"
23 │ + → → }
24 │ + → },
25 │ + → "overrides":·[
26 │ + → → {·"include":·["**/*.test.js"],·"formatter":·{·"indentStyle":·"space"·}·},
27 │ + → → {
28 │ + → → → "include":·["**/*.spec.js"],
29 │ + → → → "javascript":·{
30 │ + → → → → "formatter":·{·"semicolons":·"always",·"quoteStyle":·"single"·}
31 │ + → → → }
32 │ + → → },
33 │ + → → {
34 │ + → → → "include":·["**/*.ts"],
35 │ + → → → "javascript":·{
36 │ + → → → → "formatter":·{·"semicolons":·"always",·"quoteStyle":·"single"·}
37 │ + → → → },
38 │ + → → → "formatter":·{·"indentStyle":·"space"·}
39 │ + → → }
40 │ + → ]
41 │ + }
42 │ +


```
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,12 +56,13 @@ biome.json migrate ━━━━━━━━━━━━━━━━━━━━
19 │ + → → → "arrowParentheses":·"always",
20 │ + → → → "bracketSpacing":·true,
21 │ + → → → "bracketSameLine":·false,
22 │ + → → → "quoteStyle":·"single",
23 │ + → → → "attributePosition":·"auto"
24 │ + → → }
25 │ + → }
26 │ + }
27 │ +
22 │ + → → → "embeddedLanguageFormatting":·"auto",
23 │ + → → → "quoteStyle":·"single",
24 │ + → → → "attributePosition":·"auto"
25 │ + → → }
26 │ + → }
27 │ + }
28 │ +


```
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ expression: content
"arrowParentheses": "always",
"bracketSpacing": true,
"bracketSameLine": false,
"embeddedLanguageFormatting": "auto",
"quoteStyle": "single",
"attributePosition": "auto"
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ expression: content
"arrowParentheses": "always",
"bracketSpacing": true,
"bracketSameLine": false,
"embeddedLanguageFormatting": "auto",
"quoteStyle": "single",
"attributePosition": "auto"
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ expression: content
"arrowParentheses": "always",
"bracketSpacing": true,
"bracketSameLine": false,
"embeddedLanguageFormatting": "auto",
"quoteStyle": "single",
"attributePosition": "auto"
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ expression: content
"arrowParentheses": "always",
"bracketSpacing": true,
"bracketSameLine": false,
"embeddedLanguageFormatting": "auto",
"quoteStyle": "single",
"attributePosition": "auto"
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ expression: content
"arrowParentheses": "always",
"bracketSpacing": true,
"bracketSameLine": false,
"embeddedLanguageFormatting": "auto",
"quoteStyle": "single",
"attributePosition": "auto"
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ JavaScript Formatter:
Line ending: Lf
Line width: 100
Attribute position: Auto
Embedded language formatting: Off

JSON Formatter:
Enabled: true
Expand Down
Loading
Loading