Skip to content

Commit 5079483

Browse files
lucasborinLucas Borin
andauthored
Prefer Pragmas to Pseudo Comments (#495)
* fixes #489 * changelog Co-authored-by: Lucas Borin <5233413+lucasborin@users.noreply.github.co>
1 parent 1fa7fc6 commit 5079483

File tree

3 files changed

+85
-33
lines changed

3 files changed

+85
-33
lines changed

changelog.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ Legend
1818

1919
2021-08-XX v.1.16.0
2020
------------------
21+
* Prefer Pragmas to Pseudo Comments: Multiple Pseudo Comments (#489)
2122
* Y_CHECK_MESSAGE_EASY_TO_FIND dump (#492)
2223
* Chain Declaration for Complex Structures (#488)
2324
* Empty Catch Alternative Pseudo Comment (#337)

src/checks/y_check_prefer_pragmas.clas.abap

Lines changed: 35 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,15 @@ CLASS y_check_prefer_pragmas DEFINITION PUBLIC INHERITING FROM y_check_base CREA
1111
pseudo_com TYPE slin_desc-pseudo_com,
1212
END OF mapping.
1313

14+
TYPES: pseudo_comments TYPE SORTED TABLE OF mapping-pseudo_com WITH UNIQUE KEY table_line.
15+
1416
CLASS-DATA mappings TYPE TABLE OF mapping.
1517

16-
METHODS extract_pseudo_comment IMPORTING statement TYPE sstmnt
17-
RETURNING value(result) TYPE string.
18+
METHODS get_pseudo_comments IMPORTING statement TYPE sstmnt
19+
RETURNING VALUE(result) TYPE pseudo_comments.
1820

19-
METHODS to_pragma IMPORTING pseudo_comment TYPE string
20-
RETURNING value(result) TYPE string.
21+
METHODS get_mapping IMPORTING pseudo_comments TYPE pseudo_comments
22+
RETURNING VALUE(result) LIKE mappings.
2123

2224
ENDCLASS.
2325

@@ -35,60 +37,60 @@ CLASS y_check_prefer_pragmas IMPLEMENTATION.
3537
settings-documentation = |{ c_docs_path-checks }prefer-pragmas-to-pseudo-comments.md|.
3638

3739
IF mappings IS INITIAL.
38-
SELECT pragma, pseudo_com FROM slin_desc INTO CORRESPONDING FIELDS OF TABLE @mappings.
40+
SELECT DISTINCT pragma, pseudo_com FROM slin_desc INTO CORRESPONDING FIELDS OF TABLE @mappings.
3941
ENDIF.
4042

4143
set_check_message( 'Change the &1 to &2' ).
4244
ENDMETHOD.
4345

4446

4547
METHOD inspect_tokens.
46-
DATA(pseudo_comment) = extract_pseudo_comment( statement ).
48+
DATA(pseudo_comments) = get_pseudo_comments( statement ).
4749

48-
IF pseudo_comment IS INITIAL.
50+
IF lines( pseudo_comments ) = 0.
4951
RETURN.
5052
ENDIF.
5153

52-
DATA(pragma) = to_pragma( pseudo_comment ).
54+
DATA(mapping_to_pragmas) = get_mapping( pseudo_comments ).
5355

54-
IF pragma IS INITIAL.
56+
IF lines( mapping_to_pragmas ) = 0.
5557
RETURN.
5658
ENDIF.
5759

5860
DATA(check_configuration) = detect_check_configuration( statement ).
5961

60-
pragma = |##{ pragma }|.
61-
62-
raise_error( statement_level = statement-level
63-
statement_index = index
64-
statement_from = statement-from
65-
check_configuration = check_configuration
66-
parameter_01 = pseudo_comment
67-
parameter_02 = pragma ).
62+
LOOP AT mapping_to_pragmas ASSIGNING FIELD-SYMBOL(<mapping>).
63+
raise_error( statement_level = statement-level
64+
statement_index = index
65+
statement_from = statement-from
66+
check_configuration = check_configuration
67+
parameter_01 = |#EC { <mapping>-pseudo_com }|
68+
parameter_02 = |##{ <mapping>-pragma }| ).
69+
ENDLOOP.
6870
ENDMETHOD.
6971

7072

71-
METHOD extract_pseudo_comment.
72-
LOOP AT ref_scan->tokens ASSIGNING FIELD-SYMBOL(<token>)
73+
METHOD get_pseudo_comments.
74+
LOOP AT ref_scan->tokens INTO token_wa
7375
FROM statement-from TO statement-to
74-
WHERE type = scan_token_type-comment.
75-
IF <token>-str CS '"#EC'.
76-
result = <token>-str.
77-
RETURN.
78-
ENDIF.
76+
WHERE type = scan_token_type-comment
77+
AND str CS '#EC '.
78+
REPLACE ALL OCCURRENCES OF `"#EC ` IN token_wa-str WITH `#EC `.
79+
REPLACE ALL OCCURRENCES OF ` #EC` IN token_wa-str WITH '#EC'.
80+
REPLACE ALL OCCURRENCES OF `#EC ` IN token_wa-str WITH '#EC'.
81+
SPLIT token_wa-str AT space INTO TABLE DATA(pseudo_comments).
82+
LOOP AT pseudo_comments ASSIGNING FIELD-SYMBOL(<pseudo_comment>)
83+
WHERE table_line CS '#EC'.
84+
REPLACE ALL OCCURRENCES OF '#EC' IN <pseudo_comment> WITH ''.
85+
APPEND <pseudo_comment> TO result.
86+
ENDLOOP.
7987
ENDLOOP.
8088
ENDMETHOD.
8189

8290

83-
METHOD to_pragma.
84-
TRY.
85-
DATA(text) = pseudo_comment.
86-
REPLACE '"#EC' IN text WITH ''.
87-
CONDENSE text.
88-
result = mappings[ pseudo_com = text ]-pragma.
89-
CATCH cx_sy_itab_line_not_found.
90-
RETURN.
91-
ENDTRY.
91+
METHOD get_mapping.
92+
result = FILTER #( mappings IN pseudo_comments WHERE pseudo_com = table_line ).
9293
ENDMETHOD.
9394

95+
9496
ENDCLASS.

src/checks/y_check_prefer_pragmas.clas.testclasses.abap

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,3 +37,52 @@ CLASS ltc_pseudo_comment IMPLEMENTATION.
3737
ENDMETHOD.
3838

3939
ENDCLASS.
40+
41+
42+
43+
CLASS ltc_multiple_pseudo_comments DEFINITION INHERITING FROM y_unit_test_base FOR TESTING RISK LEVEL HARMLESS DURATION SHORT.
44+
PROTECTED SECTION.
45+
METHODS get_cut REDEFINITION.
46+
METHODS get_expected_count REDEFINITION.
47+
METHODS get_code_with_issue REDEFINITION.
48+
METHODS get_code_without_issue REDEFINITION.
49+
METHODS get_code_with_exemption REDEFINITION.
50+
ENDCLASS.
51+
52+
CLASS ltc_multiple_pseudo_comments IMPLEMENTATION.
53+
54+
METHOD get_cut.
55+
result ?= NEW y_check_prefer_pragmas( ).
56+
ENDMETHOD.
57+
58+
METHOD get_expected_count.
59+
result = 2.
60+
ENDMETHOD.
61+
62+
METHOD get_code_with_issue.
63+
result = VALUE #(
64+
( ' REPORT y_example. ' )
65+
( ' START-OF-SELECTION. ' )
66+
( ' TRY. ' )
67+
( ' DATA(div_by_zero) = 1 / 0. ' )
68+
( ' CATCH cx_root. "#EC NEEDED "#EC NO_HANDLER' )
69+
( ' ENDTRY. ' )
70+
).
71+
ENDMETHOD.
72+
73+
METHOD get_code_without_issue.
74+
result = VALUE #(
75+
( ' REPORT y_example. ' )
76+
( ' START-OF-SELECTION. ' )
77+
( ' TRY. ' )
78+
( ' DATA(div_by_zero) = 1 / 0. ' )
79+
( ' CATCH cx_root. ##NEEDED ##NO_HANDLER' )
80+
( ' ENDTRY. ' )
81+
).
82+
ENDMETHOD.
83+
84+
METHOD get_code_with_exemption.
85+
result = VALUE #( ).
86+
ENDMETHOD.
87+
88+
ENDCLASS.

0 commit comments

Comments
 (0)