From 931e74e9ecf932be77728bcb78531a24edec9541 Mon Sep 17 00:00:00 2001 From: Tomas Votruba Date: Fri, 30 Oct 2026 17:36:39 +0100 Subject: [PATCH] Space compound-assignment operators in blink binary_operator_spaces --- .../fixer/rules/binary_operator_spaces.go | 16 +++++++++++++++- .../rules/binary_operator_spaces_config_test.go | 2 ++ 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/blink/internal/fixer/rules/binary_operator_spaces.go b/blink/internal/fixer/rules/binary_operator_spaces.go index f0da086189..574ccb2a61 100644 --- a/blink/internal/fixer/rules/binary_operator_spaces.go +++ b/blink/internal/fixer/rules/binary_operator_spaces.go @@ -191,7 +191,7 @@ func (f BinaryOperatorSpaces) modeAt(s *tokens.Stream, i int) string { } return "" } - if !legacy { + if !legacy && !binaryOperatorSpacesCompoundAssign(t.Value) { return "" } if f.Default == "" { @@ -200,6 +200,17 @@ func (f BinaryOperatorSpaces) modeAt(s *tokens.Stream, i int) string { return f.Default } +// binaryOperatorSpacesCompoundAssign reports the compound assignment operators. +// They are always binary, so they follow the default mode like "=", even without +// an explicit operators override (matching php-cs-fixer). +func binaryOperatorSpacesCompoundAssign(op string) bool { + switch op { + case "+=", "-=", "*=", "/=", ".=", "%=", "**=", "??=", "&=", "|=", "^=", "<<=", ">>=": + return true + } + return false +} + func binaryOperatorSpacesDefaultTarget(s *tokens.Stream, i int) bool { t := s.At(i) if t.Kind != token.Punct { @@ -217,6 +228,9 @@ func binaryOperatorSpacesDefaultTarget(s *tokens.Stream, i int) bool { // may only be spaced when explicitly configured through the operators option. func binaryOperatorSpacesExtraTarget(s *tokens.Stream, i int) bool { t := s.At(i) + if binaryOperatorSpacesCompoundAssign(t.Value) { + return true + } switch t.Value { case "**", "/", "%", "^", "<<", ">>": return true diff --git a/blink/internal/fixer/rules/binary_operator_spaces_config_test.go b/blink/internal/fixer/rules/binary_operator_spaces_config_test.go index f2017c45a8..632a90394b 100644 --- a/blink/internal/fixer/rules/binary_operator_spaces_config_test.go +++ b/blink/internal/fixer/rules/binary_operator_spaces_config_test.go @@ -22,6 +22,8 @@ func TestBinaryOperatorSpacesConfigCases(t *testing.T) { {"collapse aligned arrow inside attribute", BinaryOperatorSpaces{}, " 1,\n 'bb' => 2,\n])]\nclass C {}", " 1,\n 'bb' => 2,\n])]\nclass C {}"}, {"arrow inside attribute string left alone", BinaryOperatorSpaces{}, " b')]\nclass C {}", " b')]\nclass C {}"}, {"reference assignment is spaced around =", BinaryOperatorSpaces{}, "": "single_space", "=": "single_space"}}).(fixerRule), "