forked from biomejs/biome
-
Notifications
You must be signed in to change notification settings - Fork 0
/
array_expression.rs
135 lines (118 loc) · 4.27 KB
/
array_expression.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
use crate::prelude::*;
use crate::parentheses::NeedsParentheses;
use biome_formatter::{write, FormatRuleWithOptions};
use biome_js_syntax::{
AnyJsArrayElement, AnyJsExpression, JsArrayElementList, JsArrayExpressionFields,
};
use biome_js_syntax::{JsArrayExpression, JsSyntaxNode};
use biome_rowan::SyntaxResult;
#[derive(Debug, Clone, Default)]
pub(crate) 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<()> {
let JsArrayExpressionFields {
l_brack_token,
elements,
r_brack_token,
} = node.as_fields();
let r_brack_token = r_brack_token?;
if elements.is_empty() {
write!(
f,
[
l_brack_token.format(),
format_dangling_comments(node.syntax()).with_block_indent(),
r_brack_token.format(),
]
)
} else {
let group_id = f.group_id("array");
let should_expand = !self.options.is_force_flat_mode && should_break(&elements)?;
let elements = elements.format().with_options(Some(group_id));
write!(
f,
[
l_brack_token.format(),
group(&soft_block_indent(&elements))
.with_group_id(Some(group_id))
.should_expand(should_expand),
r_brack_token.format()
]
)
}
}
fn needs_parentheses(&self, item: &JsArrayExpression) -> bool {
item.needs_parentheses()
}
fn fmt_dangling_comments(
&self,
_: &JsArrayExpression,
_: &mut JsFormatter,
) -> FormatResult<()> {
// Formatted inside of `fmt_fields`
Ok(())
}
}
#[derive(Debug, Copy, Clone, Default)]
pub(crate) 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.
fn should_break(elements: &JsArrayElementList) -> SyntaxResult<bool> {
if elements.len() < 2 {
Ok(false)
} else {
let mut elements = elements.iter().peekable();
while let Some(element) = elements.next() {
match element? {
AnyJsArrayElement::AnyJsExpression(AnyJsExpression::JsArrayExpression(array)) => {
let next_is_array_or_end = matches!(
elements.peek(),
None | Some(Ok(AnyJsArrayElement::AnyJsExpression(
AnyJsExpression::JsArrayExpression(_)
)))
);
if array.elements().len() < 2 || !next_is_array_or_end {
return Ok(false);
}
}
AnyJsArrayElement::AnyJsExpression(AnyJsExpression::JsObjectExpression(object)) => {
let next_is_object_or_empty = matches!(
elements.peek(),
None | Some(Ok(AnyJsArrayElement::AnyJsExpression(
AnyJsExpression::JsObjectExpression(_)
)))
);
if object.members().len() < 2 || !next_is_object_or_empty {
return Ok(false);
}
}
_ => {
return Ok(false);
}
}
}
Ok(true)
}
}
impl NeedsParentheses for JsArrayExpression {
#[inline(always)]
fn needs_parentheses(&self) -> bool {
false
}
#[inline(always)]
fn needs_parentheses_with_parent(&self, _parent: &JsSyntaxNode) -> bool {
false
}
}