@@ -2,8 +2,9 @@ use serde::{Deserialize, Serialize};
22use crate :: analysis:: parsing:: lexer:: TokenKind ;
33use crate :: analysis:: parsing:: parser:: Token ;
44use crate :: analysis:: parsing:: structure:: MethodContent ;
5- use crate :: analysis:: parsing:: tree:: { TreeElement , TreeElementTokenIterator , ZeroRange } ;
6- use crate :: analysis:: parsing:: expression:: { CastContent , FunctionCallContent , ParenExpressionContent } ;
5+ use crate :: analysis:: parsing:: tree:: { ZeroRange , TreeElementTokenIterator , TreeElement } ;
6+ use crate :: analysis:: parsing:: expression:: { CastContent , FunctionCallContent ,
7+ ParenExpressionContent , TertiaryExpressionContent } ;
78use crate :: lint:: { DMLStyleError , RuleType } ;
89use super :: indentation:: IndentParenExprArgs ;
910use super :: Rule ;
@@ -214,3 +215,74 @@ impl FuncCallBreakOnOpenParenRule {
214215 }
215216 }
216217}
218+
219+ pub struct ConditionalExpressionBreakBeforeOperatorRule {
220+ pub enabled : bool ,
221+ }
222+
223+ pub struct ConditionalExpressionBreakBeforeOperatorArgs {
224+ pub left : ZeroRange ,
225+ pub left_operation : ZeroRange ,
226+ pub middle : ZeroRange ,
227+ pub right_operation : ZeroRange ,
228+ pub right : ZeroRange
229+ }
230+
231+
232+ impl ConditionalExpressionBreakBeforeOperatorArgs {
233+ pub fn from_tertiary_expression ( node : & TertiaryExpressionContent )
234+ -> Option < ConditionalExpressionBreakBeforeOperatorArgs > {
235+ Some ( ConditionalExpressionBreakBeforeOperatorArgs {
236+ left : node. left . range ( ) ,
237+ left_operation : node. left_operation . range ( ) ,
238+ middle : node. middle . range ( ) ,
239+ right_operation : node. right_operation . range ( ) ,
240+ right : node. right . range ( ) ,
241+ } )
242+ }
243+ }
244+
245+ impl ConditionalExpressionBreakBeforeOperatorRule {
246+ pub fn from_options ( options : & Option < ConditionalExpressionBreakBeforeOperatorOptions > ) -> ConditionalExpressionBreakBeforeOperatorRule {
247+ match options {
248+ Some ( _options) => ConditionalExpressionBreakBeforeOperatorRule {
249+ enabled : true ,
250+ } ,
251+ None => ConditionalExpressionBreakBeforeOperatorRule {
252+ enabled : false ,
253+ }
254+ }
255+ }
256+
257+ pub fn check ( & self , args : Option < ConditionalExpressionBreakBeforeOperatorArgs > , acc : & mut Vec < DMLStyleError > ) {
258+ if !self . enabled { return ; }
259+ let Some ( args) = args else { return ; } ;
260+ let has_break_before_question_operator = args. left . row_end . 0 != args. left_operation . row_start . 0 ;
261+ let has_break_after_question_operator = args. left_operation . row_end . 0 != args. middle . row_start . 0 ;
262+ let has_break_before_colon_operator = args. middle . row_end . 0 != args. right_operation . row_start . 0 ;
263+ let has_break_after_colon_operator = args. right_operation . row_end . 0 != args. right . row_start . 0 ;
264+ if has_break_after_question_operator {
265+ acc. push ( self . create_err ( args. left_operation ) ) ;
266+ }
267+ if has_break_after_colon_operator || ( has_break_before_colon_operator && !has_break_before_question_operator ) {
268+ acc. push ( self . create_err ( args. right_operation ) ) ;
269+ }
270+ }
271+ }
272+
273+
274+ #[ derive( Clone , Debug , Serialize , Deserialize , PartialEq ) ]
275+ pub struct ConditionalExpressionBreakBeforeOperatorOptions {
276+ }
277+
278+ impl Rule for ConditionalExpressionBreakBeforeOperatorRule {
279+ fn name ( ) -> & ' static str {
280+ "COND_EXPRESSION_BREAK_BEFORE_OPERATOR"
281+ }
282+ fn description ( ) -> & ' static str {
283+ "Break conditional expressions before the ?, or both before the ? and before the :."
284+ }
285+ fn get_rule_type ( ) -> RuleType {
286+ RuleType :: LL3
287+ }
288+ }
0 commit comments