@@ -4,6 +4,7 @@ use crate::analysis::parsing::parser::Token;
44use crate :: analysis:: parsing:: structure:: MethodContent ;
55use crate :: analysis:: parsing:: tree:: { ZeroRange , TreeElementTokenIterator , TreeElement } ;
66use crate :: analysis:: parsing:: expression:: { CastContent , FunctionCallContent ,
7+ BinaryExpressionContent ,
78 ParenExpressionContent , TertiaryExpressionContent } ;
89use crate :: lint:: { DMLStyleError , RuleType } ;
910use super :: indentation:: IndentParenExprArgs ;
@@ -216,6 +217,60 @@ impl FuncCallBreakOnOpenParenRule {
216217 }
217218}
218219
220+ pub struct BreakBeforeBinaryOpRule {
221+ pub enabled : bool ,
222+ }
223+
224+ #[ derive( Clone , Debug , Serialize , Deserialize , PartialEq ) ]
225+ pub struct BreakBeforeBinaryOpOptions { }
226+
227+ pub struct BreakBeforeBinaryOpArgs {
228+ pub left : ZeroRange ,
229+ pub operation : ZeroRange ,
230+ pub right : ZeroRange
231+ }
232+
233+ impl BreakBeforeBinaryOpArgs {
234+ pub fn from_binary_expression ( node : & BinaryExpressionContent )
235+ -> Option < BreakBeforeBinaryOpArgs > {
236+ Some ( BreakBeforeBinaryOpArgs {
237+ left : node. left . range ( ) ,
238+ operation : node. operation . range ( ) ,
239+ right : node. right . range ( ) ,
240+ } )
241+ }
242+ }
243+
244+ impl BreakBeforeBinaryOpRule {
245+ pub fn from_options ( options : & Option < BreakBeforeBinaryOpOptions > ) -> BreakBeforeBinaryOpRule {
246+ BreakBeforeBinaryOpRule {
247+ enabled : options. is_some ( ) ,
248+ }
249+ }
250+
251+ pub fn check ( & self , args : Option < BreakBeforeBinaryOpArgs > , acc : & mut Vec < DMLStyleError > ) {
252+ if !self . enabled { return ; }
253+ let Some ( args) = args else { return ; } ;
254+ let binop_is_broken = args. left . row_start . 0 != args. right . row_end . 0 ;
255+ let has_break_after_operator = args. operation . row_end . 0 != args. right . row_start . 0 ;
256+ if binop_is_broken && has_break_after_operator {
257+ acc. push ( self . create_err ( args. operation ) ) ;
258+ }
259+ }
260+ }
261+
262+ impl Rule for BreakBeforeBinaryOpRule {
263+ fn name ( ) -> & ' static str {
264+ "break_before_binary_op"
265+ }
266+ fn description ( ) -> & ' static str {
267+ "Break binary expressions before the operator, not after."
268+ }
269+ fn get_rule_type ( ) -> RuleType {
270+ RuleType :: LL2
271+ }
272+ }
273+
219274pub struct ConditionalExpressionBreakBeforeOperatorRule {
220275 pub enabled : bool ,
221276}
0 commit comments