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

challenge(formatter): fix typescript declarations and soft wrap index signatures #794

Merged
merged 7 commits into from
Nov 19, 2023
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
72 changes: 71 additions & 1 deletion crates/biome_js_formatter/src/comments.rs
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,8 @@ impl CommentStyle for JsCommentStyle {
.or_else(handle_array_hole_comment)
.or_else(handle_call_expression_comment)
.or_else(handle_continue_break_comment)
.or_else(handle_class_comment),
.or_else(handle_class_comment)
.or_else(handle_declare_comment),
}
}
}
Expand Down Expand Up @@ -301,6 +302,75 @@ fn handle_labelled_statement_comment(
}
}

/// Moves comments in declaration statements to behind the identifier
///
/// ```javascript
/// declare module /* comment */ A {}
/// declare /* module */ global {}
/// ```
fn handle_declare_comment(comment: DecoratedComment<JsLanguage>) -> CommentPlacement<JsLanguage> {
match (comment.enclosing_node().kind(), comment.following_node()) {
// Check if it is a declare statement
(JsSyntaxKind::TS_DECLARE_STATEMENT, Some(following)) => {
println!("following: {:?}", following);
Yash-Singh1 marked this conversation as resolved.
Show resolved Hide resolved
following.children().for_each(|child| {
println!("\tchild: {:?}", child);
Yash-Singh1 marked this conversation as resolved.
Show resolved Hide resolved
child.children().for_each(|grandchild| {
println!("\t\tgrandchild: {:?}", grandchild);
Yash-Singh1 marked this conversation as resolved.
Show resolved Hide resolved
});
});
match following.kind() {
JsSyntaxKind::TS_GLOBAL_DECLARATION => {
// Global declarations have no identifier, so keep at default
CommentPlacement::Default(comment)
}
JsSyntaxKind::TS_MODULE_DECLARATION
| JsSyntaxKind::TS_ENUM_DECLARATION
| JsSyntaxKind::TS_INTERFACE_DECLARATION
| JsSyntaxKind::TS_DECLARE_FUNCTION_DECLARATION
| JsSyntaxKind::TS_TYPE_ALIAS_DECLARATION => {
// Move comment after the module keyword
// This is the first child of the module declaration which is the identifier
if following.first_child().is_some() {
return CommentPlacement::leading(
following.first_child().unwrap().clone(),
comment,
);
}
Yash-Singh1 marked this conversation as resolved.
Show resolved Hide resolved
CommentPlacement::Default(comment)
}
JsSyntaxKind::JS_CLASS_DECLARATION => {
if following.first_child().is_some()
&& following.first_child().unwrap().next_sibling().is_some()
{
return CommentPlacement::leading(
following
.first_child()
.unwrap()
.next_sibling()
.unwrap()
.clone(),
comment,
);
}
CommentPlacement::Default(comment)
Yash-Singh1 marked this conversation as resolved.
Show resolved Hide resolved
}
JsSyntaxKind::JS_VARIABLE_DECLARATION_CLAUSE => {
let first_identifier = following
.descendants()
.find(|node| node.kind() == JsSyntaxKind::JS_IDENTIFIER_BINDING);
if let Some(first_identifier_exists) = first_identifier {
return CommentPlacement::leading(first_identifier_exists.clone(), comment);
}
CommentPlacement::Default(comment)
}
_ => CommentPlacement::Default(comment),
}
}
_ => CommentPlacement::Default(comment),
}
}

fn handle_class_comment(comment: DecoratedComment<JsLanguage>) -> CommentPlacement<JsLanguage> {
// Make comments following after the `extends` or `implements` keyword trailing comments
// of the preceding extends, type parameters, or id.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use crate::prelude::*;
use crate::utils::FormatTypeMemberSeparator;

use biome_formatter::write;
use biome_formatter::{format_args, write};
use biome_js_syntax::{TsIndexSignatureTypeMember, TsIndexSignatureTypeMemberFields};

#[derive(Debug, Clone, Default)]
Expand Down Expand Up @@ -29,9 +29,11 @@ impl FormatNodeRule<TsIndexSignatureTypeMember> for FormatTsIndexSignatureTypeMe
write![
f,
[
l_brack_token.format(),
parameter.format(),
r_brack_token.format(),
group(&format_args![
l_brack_token.format(),
soft_block_indent(&format_args![parameter.format()]),
r_brack_token.format(),
]),
type_annotation.format(),
FormatTypeMemberSeparator::new(separator_token.as_ref()),
]
Expand Down
Original file line number Diff line number Diff line change
@@ -1 +1,9 @@
declare /* module */ namespace A {}
declare /* module */ function B()
declare /* module */ class C {}
declare /* module */ enum D {}
declare /* module */ interface E {}
declare /* module */ type F = {}
declare /* module */ var G: number;
declare /* module */ let H: number;
declare /* module */ const I = 1;
Original file line number Diff line number Diff line change
@@ -1 +1,9 @@
declare namespace /* module */ A {}
declare function /* module */ B();
declare class /* module */ C {}
declare enum /* module */ D {}
declare interface /* module */ E {}
declare type /* module */ F = {};
declare var /* module */ G: number;
declare let /* module */ H: number;
declare const /* module */ I = 1;

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ type TooLongSingleParam = {
```diff
--- Prettier
+++ Biome
@@ -2,30 +2,22 @@
@@ -2,26 +2,20 @@

type TwoParams = {
[a: string, b: string]: string;
Expand Down Expand Up @@ -71,11 +71,6 @@ type TooLongSingleParam = {

// note lack of trailing comma in the index signature
type TooLongSingleParam = {
- [
- looooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooong: string
- ]: string;
+ [looooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooong: string]: string;
};
```

# Output
Expand All @@ -102,7 +97,9 @@ type TooLong80 =

// note lack of trailing comma in the index signature
type TooLongSingleParam = {
[looooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooong: string]: string;
[
looooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooong: string
]: string;
};
```

Expand Down Expand Up @@ -447,7 +444,7 @@ index-signature.ts:14:99 parse ━━━━━━━━━━━━━━━━
11: [loooooooooooooooooooooooooong: string, looooooooooooooooooooooooooooooooooooooong: string]: string;
14: { [loooooooooooooooooooooooooong: string, loooooooooooooooooong: string]: string;
17: { [loooooooooooooooooooooooooong: string, looooooooooooooooong: string]: string;
22: [looooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooong: string]: string;
23: looooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooong: string
```