Skip to content

Commit 0d1bee8

Browse files
authored
refactor(formatter): remove spacing options for operators (#315)
Following the simplification work in #310, this PR continues to strengthen Mago's role as an opinionated formatter by removing nearly all options related to spacing around operators. The configuration surface has been significantly reduced by removing granular settings for spacing around assignment, logical, equality, comparison, and other binary operators. The formatter now enforces a single, consistent style: **all binary and assignment operators will have spaces around them.** This removes ambiguity and ensures a uniform codebase style. This change further simplifies the formatter's internal logic, reduces maintenance overhead, and makes the tool easier to configure and use. Signed-off-by: azjezz <[email protected]>
1 parent 6138756 commit 0d1bee8

File tree

6 files changed

+66
-236
lines changed

6 files changed

+66
-236
lines changed

crates/formatter/src/internal/format/assignment.rs

Lines changed: 15 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -69,63 +69,57 @@ pub(super) fn print_assignment<'arena>(
6969
operator: Document<'arena>,
7070
rhs_expression: &'arena Expression<'arena>,
7171
) -> Document<'arena> {
72-
let needs_spacing = if matches!(assignment_node, AssignmentLikeNode::AssignmentOperation(_)) {
73-
f.settings.space_around_assignment_operators
74-
} else {
75-
true
76-
};
77-
7872
let layout = choose_layout(f, &lhs, &assignment_node, rhs_expression);
7973
let rhs = rhs_expression.format(f);
8074

8175
match layout {
8276
Layout::Chain => Document::Array(vec![
8377
in f.arena;
8478
Document::Group(Group::new(vec![in f.arena; lhs])),
85-
if needs_spacing { Document::space() } else { Document::empty() },
79+
Document::space(),
8680
operator,
87-
if needs_spacing { Document::Line(Line::default()) } else { Document::Line(Line::soft()) },
81+
Document::Line(Line::default()),
8882
rhs,
8983
]),
9084
Layout::ChainTailArrowChain => Document::Array(vec![
9185
in f.arena;
9286
Document::Group(Group::new(vec![in f.arena; lhs])),
93-
if needs_spacing { Document::space() } else { Document::empty() },
87+
Document::space(),
9488
operator,
9589
rhs,
9690
]),
9791
Layout::ChainTail => Document::Group(Group::new(vec![
9892
in f.arena;
9993
lhs,
100-
if needs_spacing { Document::space() } else { Document::empty() },
94+
Document::space(),
10195
operator,
10296
Document::Indent(vec![in f.arena; Document::Line(Line::hard()), rhs]),
10397
])),
10498
Layout::BreakAfterOperator => Document::Group(Group::new(vec![
10599
in f.arena;
106100
Document::Group(Group::new(vec![in f.arena; lhs])),
107-
if needs_spacing { Document::space() } else { Document::empty() },
101+
Document::space(),
108102
operator,
109103
Document::Group(Group::new(vec![in f.arena; Document::IndentIfBreak(IndentIfBreak::new(vec![
110104
in f.arena;
111-
if needs_spacing { Document::Line(Line::default()) } else { Document::Line(Line::soft()) },
105+
Document::Line(Line::default()),
112106
rhs,
113107
]))])),
114108
])),
115109
Layout::NeverBreakAfterOperator => Document::Group(Group::new(vec![
116110
in f.arena;
117111
Document::Group(Group::new(vec![in f.arena; lhs])),
118-
if needs_spacing { Document::space() } else { Document::empty() },
112+
Document::space(),
119113
operator,
120-
if needs_spacing { Document::space() } else { Document::empty() },
114+
Document::space(),
121115
Document::Group(Group::new(vec![in f.arena; rhs])),
122116
])),
123117
Layout::BreakLhs => Document::Group(Group::new(vec![
124118
in f.arena;
125119
lhs,
126-
if needs_spacing { Document::space() } else { Document::empty() },
120+
Document::space(),
127121
operator,
128-
if needs_spacing { Document::space() } else { Document::empty() },
122+
Document::space(),
129123
Document::Group(Group::new(vec![in f.arena; rhs])),
130124
])),
131125
Layout::Fluid => {
@@ -134,15 +128,13 @@ pub(super) fn print_assignment<'arena>(
134128
Document::Group(Group::new(vec![
135129
in f.arena;
136130
lhs,
137-
if needs_spacing { Document::space() } else { Document::empty() },
131+
Document::space(),
138132
operator,
139133
Document::Group(
140-
Group::new(vec![in f.arena; Document::Indent(vec![in f.arena; if needs_spacing {
141-
Document::Line(Line::default())
142-
} else {
143-
Document::Line(Line::soft())
144-
}])])
145-
.with_id(assignment_id),
134+
Group::new(vec![
135+
in f.arena;
136+
Document::Indent(vec![in f.arena; Document::Line(Line::default())])
137+
]).with_id(assignment_id),
146138
),
147139
Document::IndentIfBreak(IndentIfBreak::new(vec![in f.arena; rhs]).with_id(assignment_id)),
148140
]))

crates/formatter/src/internal/format/binaryish.rs

Lines changed: 3 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -140,36 +140,8 @@ pub(super) fn print_binaryish_expressions<'arena>(
140140
let should_inline = should_inline_binary_rhs_expression(right, operator);
141141

142142
let has_space_around = match operator {
143-
BinaryOperator::And(_)
144-
| BinaryOperator::Or(_)
145-
| BinaryOperator::LowAnd(_)
146-
| BinaryOperator::LowOr(_)
147-
| BinaryOperator::LowXor(_) => f.settings.space_around_logical_binary_operators,
148-
BinaryOperator::Equal(_)
149-
| BinaryOperator::NotEqual(_)
150-
| BinaryOperator::Identical(_)
151-
| BinaryOperator::NotIdentical(_)
152-
| BinaryOperator::AngledNotEqual(_)
153-
| BinaryOperator::Spaceship(_) => f.settings.space_around_equality_binary_operators,
154-
BinaryOperator::LessThan(_)
155-
| BinaryOperator::LessThanOrEqual(_)
156-
| BinaryOperator::GreaterThan(_)
157-
| BinaryOperator::GreaterThanOrEqual(_) => f.settings.space_around_comparison_binary_operators,
158-
BinaryOperator::BitwiseAnd(_) | BinaryOperator::BitwiseOr(_) | BinaryOperator::BitwiseXor(_) => {
159-
f.settings.space_around_bitwise_binary_operators
160-
}
161-
BinaryOperator::Multiplication(_) | BinaryOperator::Division(_) | BinaryOperator::Modulo(_) => {
162-
f.settings.space_around_multiplicative_binary_operators
163-
}
164-
BinaryOperator::Exponentiation(_) => f.settings.space_around_exponentiation_binary_operators,
165-
BinaryOperator::Addition(_) | BinaryOperator::Subtraction(_) => {
166-
f.settings.space_around_additive_binary_operators
167-
}
168-
BinaryOperator::LeftShift(_) | BinaryOperator::RightShift(_) => f.settings.space_around_shift_binary_operators,
169143
BinaryOperator::StringConcat(_) => f.settings.space_around_concatenation_binary_operator,
170-
BinaryOperator::Elvis(_) => f.settings.space_around_elvis_binary_operator,
171-
BinaryOperator::NullCoalesce(_) => f.settings.space_around_null_coalescing_binary_operator,
172-
BinaryOperator::Instanceof(_) => true,
144+
_ => true,
173145
};
174146

175147
let line_before_operator = f.settings.line_before_binary_operator && !f.has_leading_own_line_comment(right.span());
@@ -178,12 +150,10 @@ pub(super) fn print_binaryish_expressions<'arena>(
178150
in f.arena;
179151
if line_before_operator && !should_inline {
180152
Document::Line(if has_space_around { Line::default() } else { Line::soft() })
181-
} else {
182-
Document::String(if has_space_around { " " } else { "" })
183-
},
153+
} else if has_space_around { Document::space() } else { Document::empty() },
184154
format_token(f, operator.span(), operator.as_str()),
185155
if line_before_operator || should_inline {
186-
Document::String(if has_space_around { " " } else { "" })
156+
if has_space_around { Document::space() } else { Document::empty() }
187157
} else {
188158
Document::Line(if has_space_around { Line::default() } else { Line::soft() })
189159
},

crates/formatter/src/settings.rs

Lines changed: 0 additions & 110 deletions
Original file line numberDiff line numberDiff line change
@@ -699,87 +699,6 @@ pub struct FormatSettings {
699699
#[serde(default = "default_false")]
700700
pub space_after_additive_unary_prefix_operator: bool,
701701

702-
/// Whether to add spaces around assignment operators.
703-
///
704-
/// When enabled: `$a = $b`
705-
/// When disabled: `$a=$b`
706-
///
707-
/// Default: true
708-
#[serde(default = "default_true")]
709-
pub space_around_assignment_operators: bool,
710-
711-
/// Whether to add spaces around logical operators (&&, ||, and, or, xor).
712-
///
713-
/// When enabled: `$a && $b`
714-
/// When disabled: `$a&&$b`
715-
///
716-
/// Default: true
717-
#[serde(default = "default_true")]
718-
pub space_around_logical_binary_operators: bool,
719-
720-
/// Whether to add spaces around equality operators (==, ===, !=, !==).
721-
///
722-
/// When enabled: `$a === $b`
723-
/// When disabled: `$a===$b`
724-
///
725-
/// Default: true
726-
#[serde(default = "default_true")]
727-
pub space_around_equality_binary_operators: bool,
728-
729-
/// Whether to add spaces around comparison operators (<, >, <=, >=, <=>).
730-
///
731-
/// When enabled: `$a < $b`
732-
/// When disabled: `$a<$b`
733-
///
734-
/// Default: true
735-
#[serde(default = "default_true")]
736-
pub space_around_comparison_binary_operators: bool,
737-
738-
/// Whether to add spaces around bitwise operators (&, |, ^).
739-
///
740-
/// When enabled: `$a | $b`
741-
/// When disabled: `$a|$b`
742-
///
743-
/// Default: true
744-
#[serde(default = "default_true")]
745-
pub space_around_bitwise_binary_operators: bool,
746-
747-
/// Whether to add spaces around additive operators (+ and -).
748-
///
749-
/// When enabled: `$a + $b`
750-
/// When disabled: `$a+$b`
751-
///
752-
/// Default: true
753-
#[serde(default = "default_true")]
754-
pub space_around_additive_binary_operators: bool,
755-
756-
/// Whether to add spaces around multiplicative operators (*, /, %).
757-
///
758-
/// When enabled: `$a * $b`
759-
/// When disabled: `$a*$b`
760-
///
761-
/// Default: true
762-
#[serde(default = "default_true")]
763-
pub space_around_multiplicative_binary_operators: bool,
764-
765-
/// Whether to add spaces around exponentiation operator (**).
766-
///
767-
/// When enabled: `$a ** $b`
768-
/// When disabled: `$a**$b`
769-
///
770-
/// Default: true
771-
#[serde(default = "default_true")]
772-
pub space_around_exponentiation_binary_operators: bool,
773-
774-
/// Whether to add spaces around shift operators (<<, >>).
775-
///
776-
/// When enabled: `$a << $b`
777-
/// When disabled: `$a<<$b`
778-
///
779-
/// Default: true
780-
#[serde(default = "default_true")]
781-
pub space_around_shift_binary_operators: bool,
782-
783702
/// Whether to add spaces around the concatenation operator (.)
784703
///
785704
/// When enabled: `$a . $b`
@@ -789,24 +708,6 @@ pub struct FormatSettings {
789708
#[serde(default = "default_true")]
790709
pub space_around_concatenation_binary_operator: bool,
791710

792-
/// Whether to add spaces around the null coalescing operator (??).
793-
///
794-
/// When enabled: `$a ?? $b`
795-
/// When disabled: `$a??$b`
796-
///
797-
/// Default: true
798-
#[serde(default = "default_true")]
799-
pub space_around_null_coalescing_binary_operator: bool,
800-
801-
/// Whether to add spaces around the elvis operator (?:).
802-
///
803-
/// When enabled: `$a ?: $b`
804-
/// When disabled: `$a?:$b`
805-
///
806-
/// Default: true
807-
#[serde(default = "default_true")]
808-
pub space_around_elvis_binary_operator: bool,
809-
810711
/// Whether to add spaces around the assignment in declare statements.
811712
///
812713
/// When enabled: `declare(strict_types = 1)`
@@ -992,18 +893,7 @@ impl Default for FormatSettings {
992893
space_around_assignment_in_declare: false,
993894
space_within_grouping_parenthesis: false,
994895
space_before_hook_parameter_list_parenthesis: false,
995-
space_around_assignment_operators: true,
996-
space_around_logical_binary_operators: true,
997-
space_around_equality_binary_operators: true,
998-
space_around_comparison_binary_operators: true,
999-
space_around_bitwise_binary_operators: true,
1000-
space_around_additive_binary_operators: true,
1001-
space_around_multiplicative_binary_operators: true,
1002-
space_around_exponentiation_binary_operators: true,
1003-
space_around_shift_binary_operators: true,
1004896
space_around_concatenation_binary_operator: true,
1005-
space_around_null_coalescing_binary_operator: true,
1006-
space_around_elvis_binary_operator: true,
1007897
space_after_cast_unary_prefix_operators: true,
1008898
space_after_reference_unary_prefix_operator: false,
1009899
space_after_error_control_unary_prefix_operator: false,

0 commit comments

Comments
 (0)