Skip to content
This repository has been archived by the owner on Aug 31, 2023. It is now read-only.

Commit

Permalink
feat(rome_js_formatter): jestEach template literals #3308
Browse files Browse the repository at this point in the history
  • Loading branch information
denbezrukov committed Nov 14, 2022
1 parent 99db51f commit 30cb7b9
Show file tree
Hide file tree
Showing 14 changed files with 998 additions and 336 deletions.
1 change: 1 addition & 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/rome_js_formatter/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ rome_js_syntax = { path = "../rome_js_syntax" }
rome_js_factory = { path = "../rome_js_factory" }
rome_formatter = { path = "../rome_formatter" }
rome_rowan = { path = "../rome_rowan" }
rome_text_size = { path = "../rome_text_size"}
tracing = { workspace = true }
unicode-width = "0.1.9"
serde = { version = "1.0.136", features = ["derive"], optional = true }
Expand Down
22 changes: 19 additions & 3 deletions crates/rome_js_formatter/src/js/expressions/array_expression.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,26 @@
use crate::prelude::*;

use crate::parentheses::NeedsParentheses;
use rome_formatter::write;
use rome_formatter::{write, FormatRuleWithOptions};
use rome_js_syntax::{
JsAnyArrayElement, JsAnyExpression, JsArrayElementList, JsArrayExpressionFields,
};
use rome_js_syntax::{JsArrayExpression, JsSyntaxNode};
use rome_rowan::SyntaxResult;

#[derive(Debug, Clone, Default)]
pub struct FormatJsArrayExpression;
pub struct FormatJsArrayExpression {
options: FormatJsArrayExpressionOptions,
}

impl FormatRuleWithOptions<JsArrayExpression> for FormatJsArrayExpression {
type Options = FormatJsArrayExpressionOptions;

fn with_options(mut self, options: Self::Options) -> Self {
self.options = options;
self
}
}

impl FormatNodeRule<JsArrayExpression> for FormatJsArrayExpression {
fn fmt_fields(&self, node: &JsArrayExpression, f: &mut JsFormatter) -> FormatResult<()> {
Expand All @@ -33,7 +44,7 @@ impl FormatNodeRule<JsArrayExpression> for FormatJsArrayExpression {
} else {
let group_id = f.group_id("array");

let should_expand = should_break(&elements)?;
let should_expand = !self.options.is_force_flat_mode && should_break(&elements)?;
let elements = elements.format().with_options(Some(group_id));

write!(
Expand Down Expand Up @@ -63,6 +74,11 @@ impl FormatNodeRule<JsArrayExpression> for FormatJsArrayExpression {
}
}

#[derive(Debug, Copy, Clone, Default)]
pub struct FormatJsArrayExpressionOptions {
pub(crate) is_force_flat_mode: bool,
}

/// Returns `true` for arrays containing at least two elements if:
/// * all elements are either object or array expressions
/// * each child array expression has at least two elements, or each child object expression has at least two members.
Expand Down
9 changes: 8 additions & 1 deletion crates/rome_js_formatter/src/js/expressions/template.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ use crate::prelude::*;
use rome_formatter::write;

use crate::js::expressions::static_member_expression::member_chain_callee_needs_parens;
use crate::js::lists::template_element_list::FormatJsTemplateElementListOptions;
use crate::parentheses::NeedsParentheses;
use crate::utils::test_call::is_test_each_pattern;
use rome_js_syntax::{JsAnyExpression, JsSyntaxNode, JsTemplate, TsTemplateLiteralType};
use rome_js_syntax::{JsSyntaxToken, TsTypeArguments};
use rome_rowan::{declare_node_union, SyntaxResult};
Expand Down Expand Up @@ -67,7 +69,12 @@ impl JsAnyTemplate {
fn write_elements(&self, f: &mut JsFormatter) -> FormatResult<()> {
match self {
JsAnyTemplate::JsTemplate(template) => {
write!(f, [template.elements().format()])
let is_test_each_pattern = is_test_each_pattern(template);
let options = FormatJsTemplateElementListOptions {
is_test_each_pattern,
};

write!(f, [template.elements().format().with_options(options)])
}
JsAnyTemplate::TsTemplateLiteralType(template) => {
write!(f, [template.elements().format()])
Expand Down
14 changes: 11 additions & 3 deletions crates/rome_js_formatter/src/js/expressions/template_element.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use rome_formatter::{
};

use crate::context::TabWidth;
use crate::js::expressions::array_expression::FormatJsArrayExpressionOptions;
use crate::js::lists::template_element_list::{TemplateElementIndention, TemplateElementLayout};
use rome_js_syntax::{
JsAnyExpression, JsSyntaxNode, JsSyntaxToken, JsTemplateElement, TsTemplateElement,
Expand Down Expand Up @@ -66,9 +67,16 @@ impl FormatTemplateElement {
impl Format<JsFormatContext> for FormatTemplateElement {
fn fmt(&self, f: &mut JsFormatter) -> FormatResult<()> {
let format_expression = format_with(|f| match &self.element {
AnyTemplateElement::JsTemplateElement(template) => {
write!(f, [template.expression().format()])
}
AnyTemplateElement::JsTemplateElement(template) => match template.expression()? {
JsAnyExpression::JsArrayExpression(expression) => {
let option = FormatJsArrayExpressionOptions {
is_force_flat_mode: true,
};

write!(f, [expression.format().with_options(option)])
}
expression => write!(f, [expression.format()]),
},
AnyTemplateElement::TsTemplateElement(template) => {
write!(f, [template.ty().format()])
}
Expand Down
30 changes: 25 additions & 5 deletions crates/rome_js_formatter/src/js/lists/template_element_list.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
use crate::context::TabWidth;
use crate::js::expressions::template_chunk_element::AnyTemplateChunkElement;
use crate::js::expressions::template_element::{AnyTemplateElement, TemplateElementOptions};

use crate::context::TabWidth;
use crate::prelude::*;

use crate::utils::test_each_template::EachTemplateTable;
use rome_formatter::FormatRuleWithOptions;
use rome_js_syntax::{
JsAnyExpression, JsAnyLiteralExpression, JsAnyTemplateElement, JsLanguage,
JsTemplateElementList, TsAnyTemplateElement, TsTemplateElementList,
Expand All @@ -12,16 +12,36 @@ use rome_rowan::{declare_node_union, AstNodeListIterator, SyntaxResult};
use std::iter::FusedIterator;

#[derive(Debug, Clone, Default)]
pub struct FormatJsTemplateElementList;
pub struct FormatJsTemplateElementList {
options: FormatJsTemplateElementListOptions,
}

impl FormatRuleWithOptions<JsTemplateElementList> for FormatJsTemplateElementList {
type Options = FormatJsTemplateElementListOptions;

fn with_options(mut self, options: Self::Options) -> Self {
self.options = options;
self
}
}

impl FormatRule<JsTemplateElementList> for FormatJsTemplateElementList {
type Context = JsFormatContext;

fn fmt(&self, node: &JsTemplateElementList, f: &mut JsFormatter) -> FormatResult<()> {
AnyTemplateElementList::JsTemplateElementList(node.clone()).fmt(f)
if self.options.is_test_each_pattern {
EachTemplateTable::from(node, f)?.fmt(f)
} else {
AnyTemplateElementList::JsTemplateElementList(node.clone()).fmt(f)
}
}
}

#[derive(Debug, Copy, Clone, Default)]
pub struct FormatJsTemplateElementListOptions {
pub(crate) is_test_each_pattern: bool,
}

pub(crate) enum AnyTemplateElementList {
JsTemplateElementList(JsTemplateElementList),
TsTemplateElementList(TsTemplateElementList),
Expand Down
10 changes: 5 additions & 5 deletions crates/rome_js_formatter/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -787,11 +787,11 @@ function() {
// use this test check if your snippet prints as you wish, without using a snapshot
fn quick_test() {
let src = r#"
<OtherComponent
value={
new Set(veryLongConditionZzzzzzzzzzzzzzzzzveryLongConditionZzzzzzzzzzzzzzzzzveryLongConditionZzzzzzzzzzzzzzzzz)
}
/>
describe.each`a | b | expected
${11111111111} | ${a().b(x => x).c().d()} | ${2}
${1} | ${2} | ${3}
${2} | ${1} | ${3}`
"#;
let syntax = SourceType::tsx();
Expand Down
1 change: 1 addition & 0 deletions crates/rome_js_formatter/src/utils/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ mod object_pattern_like;
#[cfg(test)]
mod quickcheck_utils;
pub(crate) mod test_call;
pub(crate) mod test_each_template;
mod typescript;

use crate::context::trailing_comma::FormatTrailingComma;
Expand Down
Loading

0 comments on commit 30cb7b9

Please sign in to comment.