diff --git a/changelog.txt b/changelog.txt index 16ab1fb4..1dc38fc3 100644 --- a/changelog.txt +++ b/changelog.txt @@ -18,6 +18,7 @@ Legend 2021-08-XX v.1.16.0 ------------------ +* Pseudo Comment Usage: Multiple Inline Entries (#494) * Prefer Pragmas to Pseudo Comments: Multiple Pseudo Comments (#489) * Y_CHECK_MESSAGE_EASY_TO_FIND dump (#492) * Chain Declaration for Complex Structures (#488) diff --git a/src/checks/y_check_pseudo_comment_usage.clas.abap b/src/checks/y_check_pseudo_comment_usage.clas.abap index 860cb0b4..ceffce61 100644 --- a/src/checks/y_check_pseudo_comment_usage.clas.abap +++ b/src/checks/y_check_pseudo_comment_usage.clas.abap @@ -3,16 +3,21 @@ CLASS y_check_pseudo_comment_usage DEFINITION PUBLIC INHERITING FROM y_check_bas METHODS constructor. PROTECTED SECTION. - METHODS inspect_structures REDEFINITION. + METHODS execute_check REDEFINITION. METHODS inspect_tokens REDEFINITION. PRIVATE SECTION. - CLASS-DATA pseudo_comments LIKE TABLE OF settings-pseudo_comment. - DATA pseudo_comment_counter TYPE i. + TYPES ty_pseudo_comments LIKE SORTED TABLE OF settings-pseudo_comment WITH NON-UNIQUE KEY table_line. + TYPES ty_string_table TYPE TABLE OF string. - METHODS get_pseudo_comments RETURNING VALUE(result) LIKE pseudo_comments. - METHODS count_pseudo_comments IMPORTING token TYPE stokesx. - METHODS check_result. + CLASS-DATA relevant_pseudo_comments TYPE ty_pseudo_comments. + + DATA counter TYPE i. + + METHODS get_relevant_pseudo_comments RETURNING VALUE(result) TYPE ty_pseudo_comments. + + METHODS get_count IMPORTING pseudo_comments TYPE ty_string_table + RETURNING VALUE(result) TYPE i. METHODS create_check IMPORTING name TYPE tadir-obj_name RETURNING VALUE(result) TYPE REF TO y_check_base. @@ -45,30 +50,56 @@ CLASS y_check_pseudo_comment_usage IMPLEMENTATION. ENDMETHOD. - METHOD inspect_structures. - pseudo_comment_counter = 0. + METHOD execute_check. + CLEAR counter. - IF pseudo_comments IS INITIAL. - pseudo_comments = get_pseudo_comments( ). + super->execute_check( ). + + IF counter = 0. + RETURN. ENDIF. - super->inspect_structures( ). + DATA(statement) = ref_scan->statements[ 1 ]. - check_result( ). + DATA(check_configuration) = detect_check_configuration( statement ). + + raise_error( statement_level = statement-level + statement_index = statement-from + statement_from = statement-from + check_configuration = check_configuration + parameter_01 = |{ counter }| ). ENDMETHOD. METHOD inspect_tokens. - LOOP AT ref_scan->tokens ASSIGNING FIELD-SYMBOL() + LOOP AT ref_scan->tokens INTO token_wa FROM statement-from TO statement-to - WHERE type = 'C' - OR type = 'P'. - count_pseudo_comments( ). + WHERE type = scan_token_type-comment + AND str CS '#EC '. + REPLACE ALL OCCURRENCES OF `"#EC` IN token_wa-str WITH '#EC'. + REPLACE ALL OCCURRENCES OF `#EC ` IN token_wa-str WITH '#EC'. + SPLIT token_wa-str AT space INTO TABLE DATA(pseudo_comments). + counter = counter + get_count( pseudo_comments ). + ENDLOOP. + ENDMETHOD. + + + METHOD get_count. + IF relevant_pseudo_comments IS INITIAL. + relevant_pseudo_comments = get_relevant_pseudo_comments( ). + ENDIF. + + LOOP AT pseudo_comments INTO DATA(pseudo_comment) + WHERE table_line CS '#EC'. + REPLACE ALL OCCURRENCES OF '#EC' IN pseudo_comment WITH ''. + IF line_exists( relevant_pseudo_comments[ table_line = pseudo_comment ] ). + result = result + 1. + ENDIF. ENDLOOP. ENDMETHOD. - METHOD get_pseudo_comments. + METHOD get_relevant_pseudo_comments. LOOP AT y_profile_manager=>get_checks_from_db( ) ASSIGNING FIELD-SYMBOL() WHERE object = 'CLAS'. DATA(check) = create_check( -obj_name ). @@ -76,10 +107,12 @@ CLASS y_check_pseudo_comment_usage IMPLEMENTATION. CONTINUE. ENDIF. IF check->settings-pseudo_comment IS NOT INITIAL. - APPEND check->settings-pseudo_comment TO result. + REPLACE ALL OCCURRENCES OF `"#EC ` IN check->settings-pseudo_comment WITH ''. + INSERT check->settings-pseudo_comment INTO TABLE result. ENDIF. IF check->settings-alternative_pseudo_comment IS NOT INITIAL. - APPEND check->settings-alternative_pseudo_comment TO result. + REPLACE ALL OCCURRENCES OF `"#EC ` IN check->settings-alternative_pseudo_comment WITH ''. + INSERT check->settings-alternative_pseudo_comment INTO TABLE result. ENDIF. ENDLOOP. ENDMETHOD. @@ -90,28 +123,4 @@ CLASS y_check_pseudo_comment_usage IMPLEMENTATION. ENDMETHOD. - METHOD count_pseudo_comments. - LOOP AT pseudo_comments ASSIGNING FIELD-SYMBOL(). - IF token-str CS . - pseudo_comment_counter = pseudo_comment_counter + 1. - ENDIF. - ENDLOOP. - ENDMETHOD. - - - METHOD check_result. - CHECK pseudo_comment_counter > 0. - - DATA(statement) = ref_scan->statements[ 1 ]. - - DATA(check_configuration) = detect_check_configuration( statement ). - - raise_error( statement_level = statement-level - statement_index = statement-from - statement_from = statement-from - check_configuration = check_configuration - parameter_01 = |{ pseudo_comment_counter }| ). - ENDMETHOD. - - ENDCLASS. diff --git a/src/checks/y_check_pseudo_comment_usage.clas.testclasses.abap b/src/checks/y_check_pseudo_comment_usage.clas.testclasses.abap index 5dc3c5f5..db5980e6 100644 --- a/src/checks/y_check_pseudo_comment_usage.clas.testclasses.abap +++ b/src/checks/y_check_pseudo_comment_usage.clas.testclasses.abap @@ -39,6 +39,7 @@ CLASS ltc_pseudo_comment IMPLEMENTATION. ENDCLASS. + CLASS ltc_pragma DEFINITION INHERITING FROM ltc_pseudo_comment FOR TESTING RISK LEVEL HARMLESS DURATION SHORT. PROTECTED SECTION. METHODS get_code_without_issue REDEFINITION. @@ -59,6 +60,7 @@ CLASS ltc_pragma IMPLEMENTATION. ENDCLASS. + CLASS ltc_alternative_pseudo_comment DEFINITION INHERITING FROM ltc_pseudo_comment FOR TESTING RISK LEVEL HARMLESS DURATION SHORT. PROTECTED SECTION. METHODS get_code_with_issue REDEFINITION. @@ -77,3 +79,45 @@ CLASS ltc_alternative_pseudo_comment IMPLEMENTATION. ENDMETHOD. ENDCLASS. + + + +CLASS ltc_multiple_pseudo_comments DEFINITION INHERITING FROM y_unit_test_base FOR TESTING RISK LEVEL HARMLESS DURATION SHORT. + PROTECTED SECTION. + METHODS get_cut REDEFINITION. + METHODS get_code_with_issue REDEFINITION. + METHODS get_code_without_issue REDEFINITION. + METHODS get_code_with_exemption REDEFINITION. +ENDCLASS. + +CLASS ltc_multiple_pseudo_comments IMPLEMENTATION. + + METHOD get_cut. + result ?= NEW y_check_pseudo_comment_usage( ). + ENDMETHOD. + + METHOD get_code_with_issue. + result = VALUE #( + ( 'REPORT y_example. ' ) + ( ' START-OF-SELECTION. ' ) + ( ' TRY. ' ) + ( ' CATCH cx_sy_arg_out_of_domain. "#EC EMPTY_CATCH #EC NO_HANDLER' ) + ( ' ENDTRY. ' ) + ). + ENDMETHOD. + + METHOD get_code_without_issue. + result = VALUE #( + ( 'REPORT y_example. ' ) + ( ' START-OF-SELECTION. ' ) + ( ' TRY. ' ) + ( ' CATCH cx_sy_arg_out_of_domain. "#EC NEEDED ' ) + ( ' ENDTRY. ' ) + ). + ENDMETHOD. + + METHOD get_code_with_exemption. + result = VALUE #( ). + ENDMETHOD. + +ENDCLASS.