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

feat(rome_js_formatter): number literals, number literal types, bigint literal types #3554

Merged
merged 5 commits into from
Nov 3, 2022
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use crate::prelude::*;
use crate::utils::number_utils::CleanedNumberLiteralText;

use crate::parentheses::{is_member_object, NeedsParentheses};
use rome_formatter::write;
use rome_js_syntax::JsNumberLiteralExpression;
use rome_js_syntax::{JsNumberLiteralExpressionFields, JsSyntaxNode};

Expand All @@ -15,9 +15,7 @@ impl FormatNodeRule<JsNumberLiteralExpression> for FormatJsNumberLiteralExpressi
f: &mut JsFormatter,
) -> FormatResult<()> {
let JsNumberLiteralExpressionFields { value_token } = node.as_fields();
let value_token = value_token?;

write![f, [value_token.format()]]
CleanedNumberLiteralText::from_number_literal_token(&value_token?).fmt(f)
}

fn needs_parentheses(&self, item: &JsNumberLiteralExpression) -> bool {
Expand Down
19 changes: 18 additions & 1 deletion crates/rome_js_formatter/src/ts/types/big_int_literal_type.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
use std::borrow::Cow;

use crate::prelude::*;

use crate::parentheses::NeedsParentheses;
use crate::utils::string_utils::ToAsciiLowercaseCow;
use rome_formatter::write;
use rome_js_syntax::{JsSyntaxNode, TsBigIntLiteralType, TsBigIntLiteralTypeFields};

Expand All @@ -13,8 +16,22 @@ impl FormatNodeRule<TsBigIntLiteralType> for FormatTsBigIntLiteralType {
minus_token,
literal_token,
} = node.as_fields();
write![f, [minus_token.format()]]?;
jeysal marked this conversation as resolved.
Show resolved Hide resolved
let literal_token = literal_token?;

write![f, [minus_token.format(), literal_token.format()]]
let original = literal_token.text_trimmed();
match original.to_ascii_lowercase_cow() {
Cow::Borrowed(_) => write![f, [literal_token.format()]],
Cow::Owned(lowercase) => {
write!(
f,
[format_replaced(
&literal_token,
&dynamic_text(&lowercase, literal_token.text_trimmed_range().start())
)]
)
}
}
}

fn needs_parentheses(&self, item: &TsBigIntLiteralType) -> bool {
Expand Down
9 changes: 8 additions & 1 deletion crates/rome_js_formatter/src/ts/types/number_literal_type.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use crate::prelude::*;

use crate::parentheses::NeedsParentheses;
use crate::utils::number_utils::CleanedNumberLiteralText;
use rome_formatter::write;
use rome_js_syntax::{JsSyntaxNode, TsNumberLiteralType, TsNumberLiteralTypeFields};

Expand All @@ -13,7 +14,13 @@ impl FormatNodeRule<TsNumberLiteralType> for FormatTsNumberLiteralType {
minus_token,
literal_token,
} = node.as_fields();
write![f, [minus_token.format(), literal_token.format()]]
write![
f,
[
minus_token.format(),
CleanedNumberLiteralText::from_number_literal_token(&literal_token?)
]
]
}

fn needs_parentheses(&self, item: &TsNumberLiteralType) -> bool {
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 @@ -2,6 +2,7 @@ pub(crate) mod array;
mod assignment_like;
mod binary_like_expression;
mod conditional;
pub mod number_utils;
pub mod string_utils;

pub(crate) mod format_class;
Expand Down
Loading