Skip to content

Commit

Permalink
CHECK_PREFER_IS_NOT: Add case for NEW before predicative method
Browse files Browse the repository at this point in the history
  • Loading branch information
ConjuringCoffee authored Aug 14, 2023
1 parent 42203f6 commit 36c2403
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 9 deletions.
25 changes: 16 additions & 9 deletions src/checks/y_check_prefer_is_not.clas.abap
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,11 @@ CLASS y_check_prefer_is_not DEFINITION PUBLIC INHERITING FROM y_check_base CREAT
PRIVATE SECTION.
METHODS get_position_closing_bracket
IMPORTING
tokens TYPE stokesx_tab
statement TYPE sstmnt
position_of_not TYPE syst_tabix
tokens TYPE stokesx_tab
statement TYPE sstmnt
position_of_open_bracket TYPE syst_tabix
RETURNING
VALUE(result) TYPE i.
VALUE(result) TYPE i.

ENDCLASS.

Expand Down Expand Up @@ -98,14 +98,21 @@ CLASS y_check_prefer_is_not IMPLEMENTATION.
ENDMETHOD.

METHOD is_predicative_method.
IF tokens[ position_of_not + 1 ]-str NP '*('.
DATA(position_to_check) = position_of_not + 1.

IF tokens[ position_to_check ]-str = 'NEW'.
position_to_check = position_to_check + 1.
ENDIF.

IF tokens[ position_to_check ]-str NP '*('.
" This is not the start of a method call
RETURN.
ENDIF.

DATA(position_closing_bracket) = get_position_closing_bracket( tokens = tokens
statement = statement
position_of_not = position_of_not ).
DATA(position_closing_bracket) = get_position_closing_bracket(
tokens = tokens
statement = statement
position_of_open_bracket = position_to_check ).

IF position_closing_bracket = statement-to.
" Nothing follows the closing bracket
Expand All @@ -124,7 +131,7 @@ CLASS y_check_prefer_is_not IMPLEMENTATION.

METHOD get_position_closing_bracket.
DATA(open_brackets) = 1.
result = position_of_not + 1.
result = position_of_open_bracket.

WHILE open_brackets <> 0 AND result <= statement-to.
result = result + 1.
Expand Down
28 changes: 28 additions & 0 deletions src/checks/y_check_prefer_is_not.clas.testclasses.abap
Original file line number Diff line number Diff line change
Expand Up @@ -616,6 +616,34 @@ CLASS ltc_predicative_nested IMPLEMENTATION.

ENDCLASS.

CLASS ltc_new_predicative DEFINITION INHERITING FROM ltc_not_value FOR TESTING RISK LEVEL HARMLESS DURATION SHORT.
PROTECTED SECTION.
METHODS get_code_without_issue REDEFINITION.
ENDCLASS.

CLASS ltc_new_predicative IMPLEMENTATION.

METHOD get_code_without_issue.
result = VALUE #(
( 'REPORT y_example. ' )
( ' CLASS y_example_class DEFINITION. ' )
( ' PUBLIC SECTION. ' )
( ' METHODS is_active RETURNING VALUE(result) TYPE abap_bool. ' )
( ' ENDCLASS. ' )
( ' CLASS y_example_class IMPLEMENTATION. ' )
( ' METHOD is_active. ' )
( ' ENDMETHOD. ' )
( ' ENDCLASS. ' )
( ' START-OF-SELECTION. ' )
( ' DATA(count) = 0. ' )
( ' IF NOT NEW y_example_class( )->is_active( ).' )
( ' count = 1. ' )
( ' ENDIF. ' )
).
ENDMETHOD.

ENDCLASS.

CLASS ltc_method_return_structure DEFINITION INHERITING FROM ltc_not_value FOR TESTING RISK LEVEL HARMLESS DURATION SHORT.
PROTECTED SECTION.
METHODS get_code_with_issue REDEFINITION.
Expand Down

0 comments on commit 36c2403

Please sign in to comment.