Skip to content

Commit

Permalink
challenge(formatter): Attach comments to labels (#730)
Browse files Browse the repository at this point in the history
  • Loading branch information
Conaclos committed Nov 18, 2023
1 parent 52e990e commit 3b0659a
Show file tree
Hide file tree
Showing 40 changed files with 453 additions and 471 deletions.
32 changes: 20 additions & 12 deletions crates/biome_js_factory/src/generated/node_factory.rs

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

22 changes: 19 additions & 3 deletions crates/biome_js_factory/src/generated/syntax_factory.rs

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

16 changes: 16 additions & 0 deletions crates/biome_js_formatter/src/comments.rs
Original file line number Diff line number Diff line change
Expand Up @@ -208,8 +208,24 @@ fn handle_call_expression_comment(
fn handle_continue_break_comment(
comment: DecoratedComment<JsLanguage>,
) -> CommentPlacement<JsLanguage> {
// Make comments between `continue`/`break` and the label, leading comments of the label.
// ```javascript
// continue /* comment */ a;
// ```
if let Some(next) = comment.following_node() {
if next.kind() == JsSyntaxKind::JS_LABEL {
return CommentPlacement::leading(next.clone(), comment);
}
}

let enclosing = comment.enclosing_node();

if let Some(preceding) = comment.preceding_node() {
if preceding.kind() == JsSyntaxKind::JS_LABEL {
return CommentPlacement::trailing(preceding.clone(), comment);
}
}

// Make comments between the `continue` and label token trailing comments
// ```javascript
// continue /* comment */ a;
Expand Down
23 changes: 23 additions & 0 deletions crates/biome_js_formatter/src/generated.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1510,6 +1510,29 @@ impl IntoFormat<JsFormatContext> for biome_js_syntax::JsVariableDeclarator {
)
}
}
impl FormatRule<biome_js_syntax::JsLabel> for crate::js::auxiliary::label::FormatJsLabel {
type Context = JsFormatContext;
#[inline(always)]
fn fmt(&self, node: &biome_js_syntax::JsLabel, f: &mut JsFormatter) -> FormatResult<()> {
FormatNodeRule::<biome_js_syntax::JsLabel>::fmt(self, node, f)
}
}
impl AsFormat<JsFormatContext> for biome_js_syntax::JsLabel {
type Format<'a> =
FormatRefWithRule<'a, biome_js_syntax::JsLabel, crate::js::auxiliary::label::FormatJsLabel>;
fn format(&self) -> Self::Format<'_> {
#![allow(clippy::default_constructed_unit_structs)]
FormatRefWithRule::new(self, crate::js::auxiliary::label::FormatJsLabel::default())
}
}
impl IntoFormat<JsFormatContext> for biome_js_syntax::JsLabel {
type Format =
FormatOwnedWithRule<biome_js_syntax::JsLabel, crate::js::auxiliary::label::FormatJsLabel>;
fn into_format(self) -> Self::Format {
#![allow(clippy::default_constructed_unit_structs)]
FormatOwnedWithRule::new(self, crate::js::auxiliary::label::FormatJsLabel::default())
}
}
impl FormatRule<biome_js_syntax::JsCaseClause>
for crate::js::auxiliary::case_clause::FormatJsCaseClause
{
Expand Down
12 changes: 12 additions & 0 deletions crates/biome_js_formatter/src/js/any/label.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
//! This is a generated file. Don't modify it by hand! Run 'cargo codegen formatter' to re-generate the file.

use crate::prelude::*;
use biome_js_syntax::JsLabel;
#[derive(Debug, Clone, Default)]
pub(crate) struct FormatAnyJsLabel;
impl FormatRule<AnyJsBinding> for FormatAnyJsLabel {
type Context = JsFormatContext;
fn fmt(&self, node: &JsLabel, f: &mut JsFormatter) -> FormatResult<()> {
node.format().fmt(f)
}
}
11 changes: 11 additions & 0 deletions crates/biome_js_formatter/src/js/auxiliary/label.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
use crate::prelude::*;
use biome_formatter::write;
use biome_js_syntax::{JsLabel, JsLabelFields};
#[derive(Debug, Clone, Default)]
pub(crate) struct FormatJsLabel;
impl FormatNodeRule<JsLabel> for FormatJsLabel {
fn fmt_fields(&self, node: &JsLabel, f: &mut JsFormatter) -> FormatResult<()> {
let JsLabelFields { value_token } = node.as_fields();
write![f, [value_token.format()]]
}
}
1 change: 1 addition & 0 deletions crates/biome_js_formatter/src/js/auxiliary/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ pub(crate) mod expression_snipped;
pub(crate) mod finally_clause;
pub(crate) mod function_body;
pub(crate) mod initializer_clause;
pub(crate) mod label;
pub(crate) mod module;
pub(crate) mod name;
pub(crate) mod private_name;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,13 @@ impl FormatNodeRule<JsBreakStatement> for FormatJsBreakStatement {
fn fmt_fields(&self, node: &JsBreakStatement, f: &mut JsFormatter) -> FormatResult<()> {
let JsBreakStatementFields {
break_token,
label_token,
label,
semicolon_token,
} = node.as_fields();

write!(f, [break_token.format()])?;

if let Some(label) = &label_token {
if let Some(label) = &label {
write!(f, [space(), label.format()])?;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,13 @@ impl FormatNodeRule<JsContinueStatement> for FormatJsContinueStatement {
fn fmt_fields(&self, node: &JsContinueStatement, f: &mut JsFormatter) -> FormatResult<()> {
let JsContinueStatementFields {
continue_token,
label_token,
label,
semicolon_token,
} = node.as_fields();

write!(f, [continue_token.format()])?;

if let Some(label) = &label_token {
if let Some(label) = &label {
write!(f, [space(), label.format()])?;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@ pub(crate) struct FormatJsLabeledStatement;
impl FormatNodeRule<JsLabeledStatement> for FormatJsLabeledStatement {
fn fmt_fields(&self, node: &JsLabeledStatement, f: &mut JsFormatter) -> FormatResult<()> {
let JsLabeledStatementFields {
label_token,
label,
colon_token,
body,
} = node.as_fields();

write!(f, [label_token.format(), colon_token.format()])?;
write!(f, [label.format(), colon_token.format()])?;

match body? {
AnyJsStatement::JsEmptyStatement(empty) => {
Expand Down

This file was deleted.

0 comments on commit 3b0659a

Please sign in to comment.