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

Commit

Permalink
Syntax refactors
Browse files Browse the repository at this point in the history
  • Loading branch information
MichaReiser committed Sep 16, 2022
1 parent fdaf608 commit 5839e85
Show file tree
Hide file tree
Showing 256 changed files with 2,907 additions and 12,867 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,32 @@ impl FormatNodeRule<JsArrayAssignmentPattern> for FormatJsArrayAssignmentPattern
r_brack_token,
} = node.as_fields();

write!(
f,
[
format_delimited(&l_brack_token?, &elements.format(), &r_brack_token?,)
.soft_block_indent()
]
)
write!(f, [l_brack_token.format(),])?;

if elements.is_empty() {
write!(
f,
[format_dangling_comments(node.syntax()).with_block_indent()]
)?;
} else {
write!(f, [group(&soft_block_indent(&elements.format()))])?;
}

write!(f, [r_brack_token.format()])
}

fn needs_parentheses(&self, item: &JsArrayAssignmentPattern) -> bool {
item.needs_parentheses()
}

fn fmt_dangling_comments(
&self,
_: &JsArrayAssignmentPattern,
_: &mut JsFormatter,
) -> FormatResult<()> {
// Handled inside of `fmt_fields`
Ok(())
}
}

impl NeedsParentheses for JsArrayAssignmentPattern {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,15 @@ impl FormatNodeRule<JsObjectAssignmentPattern> for FormatJsObjectAssignmentPatte
fn needs_parentheses(&self, item: &JsObjectAssignmentPattern) -> bool {
item.needs_parentheses()
}

fn fmt_dangling_comments(
&self,
_: &JsObjectAssignmentPattern,
_: &mut JsFormatter,
) -> FormatResult<()> {
// Handled inside of `JsObjectPatternLike`
Ok(())
}
}

impl NeedsParentheses for JsObjectAssignmentPattern {
Expand Down
11 changes: 10 additions & 1 deletion crates/rome_js_formatter/src/js/auxiliary/default_clause.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,11 @@ impl FormatNodeRule<JsDefaultClause> for FormatJsDefaultClause {
Some(JsAnyStatement::JsBlockStatement(_))
);

write!(f, [default_token.format(), colon_token.format(), space()])?;
write!(f, [default_token.format(), colon_token.format()])?;

if f.comments().has_dangling_comments(node.syntax()) {
write!(f, [space(), format_dangling_comments(node.syntax())])?;
}

if consequent.is_empty() {
write!(f, [hard_line_break()])
Expand All @@ -37,4 +41,9 @@ impl FormatNodeRule<JsDefaultClause> for FormatJsDefaultClause {
)
}
}

fn fmt_dangling_comments(&self, _: &JsDefaultClause, _: &mut JsFormatter) -> FormatResult<()> {
// Handled inside of `fmt_fields`
Ok(())
}
}
33 changes: 25 additions & 8 deletions crates/rome_js_formatter/src/js/auxiliary/function_body.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,31 @@ impl FormatNodeRule<JsFunctionBody> for FormatJsFunctionBody {
r_curly_token,
} = node.as_fields();

write!(
f,
[format_delimited(
&l_curly_token?,
&format_args![directives.format(), statements.format()],
&r_curly_token?,
let r_curly_token = r_curly_token?;

if statements.is_empty() && directives.is_empty() {
write!(
f,
[
l_curly_token.format(),
format_dangling_comments(node.syntax()).with_block_indent(),
r_curly_token.format()
]
)
} else {
write!(
f,
[
l_curly_token.format(),
block_indent(&format_args![directives.format(), statements.format()]),
r_curly_token.format(),
]
)
.block_indent()]
)
}
}

fn fmt_dangling_comments(&self, _: &JsFunctionBody, _: &mut JsFormatter) -> FormatResult<()> {
// Formatted as part of `fmt_fields`
Ok(())
}
}
20 changes: 20 additions & 0 deletions crates/rome_js_formatter/src/js/auxiliary/module.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ impl FormatNodeRule<JsModule> for FormatJsModule {
f,
[
FormatInterpreterToken::new(interpreter_token.as_ref()),
format_leading_comments(node.syntax()),
directives.format()
]
]?;
Expand All @@ -30,9 +31,28 @@ impl FormatNodeRule<JsModule> for FormatJsModule {
f,
[
items.format(),
format_trailing_comments(node.syntax()),
format_removed(&eof_token?),
hard_line_break()
]
)
}

fn fmt_leading_comments(&self, _: &JsModule, _: &mut JsFormatter) -> FormatResult<()> {
// Formatted as part of `fmt_fields`
Ok(())
}

fn fmt_dangling_comments(&self, module: &JsModule, f: &mut JsFormatter) -> FormatResult<()> {
debug_assert!(
!f.comments().has_dangling_comments(module.syntax()),
"Module should never have dangling comments."
);
Ok(())
}

fn fmt_trailing_comments(&self, _: &JsModule, _: &mut JsFormatter) -> FormatResult<()> {
// Formatted as part of `fmt_fields`
Ok(())
}
}
20 changes: 20 additions & 0 deletions crates/rome_js_formatter/src/js/auxiliary/script.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ impl FormatNodeRule<JsScript> for FormatJsScript {
f,
[
FormatInterpreterToken::new(interpreter_token.as_ref()),
format_leading_comments(node.syntax()),
directives.format(),
]
]?;
Expand All @@ -29,9 +30,28 @@ impl FormatNodeRule<JsScript> for FormatJsScript {
f,
[
statements.format(),
format_trailing_comments(node.syntax()),
format_removed(&eof_token?),
hard_line_break()
]
]
}

fn fmt_leading_comments(&self, _: &JsScript, _: &mut JsFormatter) -> FormatResult<()> {
// Formatted as part of `fmt_fields`
Ok(())
}

fn fmt_dangling_comments(&self, node: &JsScript, f: &mut JsFormatter) -> FormatResult<()> {
debug_assert!(
!f.comments().has_dangling_comments(node.syntax()),
"Scrip should never have dangling comments."
);
Ok(())
}

fn fmt_trailing_comments(&self, _: &JsScript, _: &mut JsFormatter) -> FormatResult<()> {
// Formatted as part of `fmt_fields`
Ok(())
}
}
28 changes: 21 additions & 7 deletions crates/rome_js_formatter/src/js/bindings/array_binding_pattern.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,26 @@ impl FormatNodeRule<JsArrayBindingPattern> for FormatJsArrayBindingPattern {
r_brack_token,
} = node.as_fields();

write!(
f,
[
format_delimited(&l_brack_token?, &elements.format(), &r_brack_token?,)
.soft_block_indent()
]
)
write!(f, [l_brack_token.format(),])?;

if elements.is_empty() {
write!(
f,
[format_dangling_comments(node.syntax()).with_block_indent()]
)?;
} else {
write!(f, [group(&soft_block_indent(&elements.format()))])?;
}

write!(f, [r_brack_token.format()])
}

fn fmt_dangling_comments(
&self,
_: &JsArrayBindingPattern,
_: &mut JsFormatter,
) -> FormatResult<()> {
// Handled inside of `fmt_fields`
Ok(())
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,13 @@ impl FormatNodeRule<JsConstructorParameters> for FormatJsConstructorParameters {
fn fmt_fields(&self, node: &JsConstructorParameters, f: &mut JsFormatter) -> FormatResult<()> {
FormatJsAnyParameters::from(node.clone()).fmt(f)
}

fn fmt_dangling_comments(
&self,
_: &JsConstructorParameters,
_: &mut JsFormatter,
) -> FormatResult<()> {
// Formatted inside of `FormatJsAnyParameters
Ok(())
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,13 @@ impl FormatNodeRule<JsObjectBindingPattern> for FormatJsObjectBindingPattern {
fn fmt_fields(&self, node: &JsObjectBindingPattern, f: &mut JsFormatter) -> FormatResult<()> {
write!(f, [JsObjectPatternLike::from(node.clone())])
}

fn fmt_dangling_comments(
&self,
_: &JsObjectBindingPattern,
_: &mut JsFormatter,
) -> FormatResult<()> {
// Handled in `JsObjectPatternLike`
Ok(())
}
}
61 changes: 44 additions & 17 deletions crates/rome_js_formatter/src/js/bindings/parameters.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::prelude::*;
use rome_formatter::write;
use rome_formatter::{write, CstFormatContext};

use crate::js::expressions::call_arguments::is_test_call_expression;
use crate::js::lists::parameter_list::{
Expand All @@ -19,6 +19,11 @@ impl FormatNodeRule<JsParameters> for FormatJsParameters {
fn fmt_fields(&self, node: &JsParameters, f: &mut JsFormatter) -> FormatResult<()> {
FormatJsAnyParameters::from(node.clone()).fmt(f)
}

fn fmt_dangling_comments(&self, _: &JsParameters, _: &mut JsFormatter) -> FormatResult<()> {
// Formatted inside of `FormatJsAnyParameters
Ok(())
}
}

declare_node_union! {
Expand All @@ -39,9 +44,12 @@ impl Format<JsFormatContext> for FormatJsAnyParameters {
Err(_) => false,
});

let can_hug = should_hug_function_parameters(self)? && !has_any_decorated_parameter;
let can_hug = should_hug_function_parameters(self, f.context().comments())?
&& !has_any_decorated_parameter;

let layout = if can_hug || self.is_in_test_call()? {
let layout = if list.is_empty() {
ParameterLayout::NoParameters
} else if can_hug || self.is_in_test_call()? {
ParameterLayout::Hug
} else {
ParameterLayout::Default
Expand All @@ -51,6 +59,16 @@ impl Format<JsFormatContext> for FormatJsAnyParameters {
let r_paren_token = self.r_paren_token()?;

match layout {
ParameterLayout::NoParameters => {
write!(
f,
[
l_paren_token.format(),
format_dangling_comments(self.syntax()).with_soft_block_indent(),
r_paren_token.format()
]
)
}
ParameterLayout::Hug => {
write!(
f,
Expand All @@ -61,14 +79,19 @@ impl Format<JsFormatContext> for FormatJsAnyParameters {
]
)
}
ParameterLayout::Default => format_delimited(
&l_paren_token,
&FormatJsAnyParameterList::with_layout(&list, ParameterLayout::Default),
&r_paren_token,
)
.soft_block_indent()
.ungrouped()
.fmt(f),
ParameterLayout::Default => {
write!(
f,
[
l_paren_token.format(),
soft_block_indent(&FormatJsAnyParameterList::with_layout(
&list,
ParameterLayout::Default
)),
r_paren_token.format()
]
)
}
}
}
}
Expand Down Expand Up @@ -138,6 +161,11 @@ impl FormatJsAnyParameters {

#[derive(Copy, Debug, Clone, Eq, PartialEq)]
pub enum ParameterLayout {
/// ```javascript
/// function test() {}
/// ```
NoParameters,

/// Enforce that the opening and closing parentheses aren't separated from the first token of the parameter.
/// For example, to enforce that the `{` and `}` of an object expression are formatted on the same line
/// as the `(` and `)` tokens even IF the object expression itself breaks across multiple lines.
Expand All @@ -163,7 +191,10 @@ pub enum ParameterLayout {
Default,
}

fn should_hug_function_parameters(parameters: &FormatJsAnyParameters) -> FormatResult<bool> {
fn should_hug_function_parameters(
parameters: &FormatJsAnyParameters,
comments: &JsComments,
) -> FormatResult<bool> {
use rome_js_syntax::{
JsAnyBinding::*, JsAnyBindingPattern::*, JsAnyExpression::*, JsAnyFormalParameter::*,
JsAnyParameter::*,
Expand All @@ -175,14 +206,10 @@ fn should_hug_function_parameters(parameters: &FormatJsAnyParameters) -> FormatR
return Ok(false);
}

if parameters.r_paren_token()?.has_leading_comments() {
return Ok(false);
}

// SAFETY: Safe because of the length check above
let only_parameter = list.first().unwrap()?;

if only_parameter.syntax().has_comments_direct() {
if comments.has_comments(only_parameter.syntax()) {
return Ok(false);
}

Expand Down
Loading

0 comments on commit 5839e85

Please sign in to comment.