Skip to content

Commit c90b89e

Browse files
authored
Prefer Pragmas to Pseudo Comments (#446)
* new check * new check * documentation * exempting SLIN * Adding example
1 parent 19c5893 commit c90b89e

8 files changed

+206
-1
lines changed

.cspell.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,8 @@
5151
"msgid",
5252
"msgty",
5353
"msgno",
54-
"UNDETEC"
54+
"UNDETEC",
55+
"SLIN"
5556
],
5657
// flagWords - list of words to be always considered incorrect
5758
// This is useful for offensive words and common spelling errors.

changelog.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ Whenever you upgrade code pal for ABAP, it is highly recommended to execute the
1414

1515
2021-08-XX v.1.16.0
1616
------------------
17+
+ Prefer Pragmas to Pseudo Comments (#421)
1718
+ COLLECT restriction (#441)
1819
* RAP needs CREATE OBJECT ... FOR TESTING (#444)
1920

docs/check_documentation.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@
4747
- [Prefer IS NOT to NOT IS](checks/prefer-is-not-to-not-is.md)
4848
- [Prefer LINE_EXISTS or LINE_INDEX to READ TABLE or LOOP AT](checks/prefer-line-exists.md)
4949
- [Prefer NEW to CREATE OBJECT](checks/prefer-new-to-create-object.md)
50+
- [Prefer Pragma to Pseudo Comment](checks/prefer-pragmas-to-pseudo-comments.md)
5051
- [Pseudo Comment Usage](checks/pseudo-comment-usage.md)
5152
- [Omit Optional EXPORTING](checks/omit-optional-exporting.md)
5253
- [Optional Parameters](checks/optional-parameters.md)
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
[code pal for ABAP](../../README.md) > [Documentation](../check_documentation.md) > [Prefer Pragmas to Pseudo Comments](prefer-pragmas-to-pseudo-comments.md)
2+
3+
## Prefer Pragmas to Pseudo Comments
4+
5+
### What is the Intent of the Check?
6+
7+
Based on the [Clean ABAP](https://github.com/SAP/styleguides/blob/main/clean-abap/CleanABAP.md#prefer-pragmas-to-pseudo-comments):
8+
> Prefer pragmas to pseudo comments to suppress irrelevant warnings and errors identified by the ATC. Pseudo comments have mostly become obsolete and have been replaced by pragmas
9+
10+
:bulb: Only the Pseudo Comments and Pragmas available in the `SLIN_DESC` table are in scope.
11+
12+
:wan: Code Pal does not support Pragmas.
13+
14+
### How to solve the issue?
15+
16+
Change the `"#EC ` (Pseudo Comment) to `##` (Pragma).
17+
18+
### What to do in case of exception?
19+
20+
This Check cannot be exempt.
21+
22+
### Example
23+
24+
Before the check:
25+
26+
```abap
27+
DATA a TYPE string. "#EC NEEDED
28+
```
29+
30+
After the check:
31+
32+
```abap
33+
DATA a TYPE string. ##NEEDED
34+
```
35+
36+
### Further Readings & Knowledge
37+
38+
* [Clean ABAP: Prefer pragmas to pseudo comments](https://github.com/SAP/styleguides/blob/main/clean-abap/CleanABAP.md#prefer-pragmas-to-pseudo-comments)
39+
* [ABAP - Keyword Documentation: Pseudo Comments for the Extended Program Check](https://help.sap.com/doc/abapdocu_752_index_htm/7.52/en-US/abenpseudo_comment_slin.htm)
Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
CLASS y_check_prefer_pragmas DEFINITION PUBLIC INHERITING FROM y_check_base CREATE PUBLIC .
2+
PUBLIC SECTION.
3+
METHODS constructor.
4+
5+
PROTECTED SECTION.
6+
METHODS inspect_tokens REDEFINITION.
7+
8+
PRIVATE SECTION.
9+
TYPES: BEGIN OF mapping,
10+
pragma TYPE slin_desc-pragma,
11+
pseudo_com TYPE slin_desc-pseudo_com,
12+
END OF mapping.
13+
14+
CLASS-DATA mappings TYPE TABLE OF mapping.
15+
16+
METHODS extract_pseudo_comment IMPORTING statement TYPE sstmnt
17+
RETURNING value(result) TYPE string.
18+
19+
METHODS to_pragma IMPORTING pseudo_comment TYPE string
20+
RETURNING value(result) TYPE string.
21+
22+
ENDCLASS.
23+
24+
25+
26+
CLASS y_check_prefer_pragmas IMPLEMENTATION.
27+
28+
29+
METHOD constructor.
30+
super->constructor( ).
31+
32+
settings-ignore_pseudo_comments = abap_true.
33+
settings-disable_threshold_selection = abap_true.
34+
settings-threshold = 0.
35+
settings-documentation = |{ c_docs_path-checks }prefer-pragmas-to-pseudo-comments.md|.
36+
37+
IF mappings IS INITIAL.
38+
SELECT pragma, pseudo_com FROM slin_desc INTO CORRESPONDING FIELDS OF TABLE @mappings.
39+
ENDIF.
40+
41+
set_check_message( 'Change the &1 to &2' ).
42+
ENDMETHOD.
43+
44+
45+
METHOD inspect_tokens.
46+
DATA(pseudo_comment) = extract_pseudo_comment( statement ).
47+
48+
IF pseudo_comment IS INITIAL.
49+
RETURN.
50+
ENDIF.
51+
52+
DATA(pragma) = to_pragma( pseudo_comment ).
53+
54+
IF pragma IS INITIAL.
55+
RETURN.
56+
ENDIF.
57+
58+
DATA(configuration) = detect_check_configuration( statement ).
59+
60+
IF configuration IS INITIAL.
61+
RETURN.
62+
ENDIF.
63+
64+
pragma = |##{ pragma }|.
65+
66+
raise_error( statement_level = statement-level
67+
statement_index = index
68+
statement_from = statement-from
69+
error_priority = configuration-prio
70+
parameter_01 = pseudo_comment
71+
parameter_02 = pragma ).
72+
ENDMETHOD.
73+
74+
75+
METHOD extract_pseudo_comment.
76+
LOOP AT ref_scan_manager->tokens ASSIGNING FIELD-SYMBOL(<token>)
77+
FROM statement-from TO statement-to
78+
WHERE type = scan_token_type-comment.
79+
IF <token>-str CS '"#EC'.
80+
result = <token>-str.
81+
RETURN.
82+
ENDIF.
83+
ENDLOOP.
84+
ENDMETHOD.
85+
86+
87+
METHOD to_pragma.
88+
TRY.
89+
DATA(text) = pseudo_comment.
90+
REPLACE '"#EC' IN text WITH ''.
91+
CONDENSE text.
92+
result = mappings[ pseudo_com = text ]-pragma.
93+
CATCH cx_sy_itab_line_not_found.
94+
RETURN.
95+
ENDTRY.
96+
ENDMETHOD.
97+
98+
ENDCLASS.
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
CLASS ltc_pseudo_comment DEFINITION INHERITING FROM y_unit_test_base FOR TESTING RISK LEVEL HARMLESS DURATION SHORT.
2+
PROTECTED SECTION.
3+
METHODS get_cut REDEFINITION.
4+
METHODS get_code_with_issue REDEFINITION.
5+
METHODS get_code_without_issue REDEFINITION.
6+
METHODS get_code_with_exemption REDEFINITION.
7+
ENDCLASS.
8+
9+
CLASS ltc_pseudo_comment IMPLEMENTATION.
10+
11+
METHOD get_cut.
12+
result ?= NEW y_check_prefer_pragmas( ).
13+
ENDMETHOD.
14+
15+
METHOD get_code_with_issue.
16+
result = VALUE #(
17+
( ' REPORT y_example. ' )
18+
( ' START-OF-SELECTION. ' )
19+
( ' DATA a TYPE string. "#EC NEEDED ' )
20+
( ' DATA b TYPE string. ' )
21+
( ' a = b. ' )
22+
).
23+
ENDMETHOD.
24+
25+
METHOD get_code_without_issue.
26+
result = VALUE #(
27+
( ' REPORT y_example. ' )
28+
( ' START-OF-SELECTION. ' )
29+
( ' DATA a TYPE string. ##needed ' )
30+
( ' DATA b TYPE string. ' )
31+
( ' a = b. ' )
32+
).
33+
ENDMETHOD.
34+
35+
METHOD get_code_with_exemption.
36+
result = VALUE #( ).
37+
ENDMETHOD.
38+
39+
ENDCLASS.
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<abapGit version="v1.0.0" serializer="LCL_OBJECT_CLAS" serializer_version="v1.0.0">
3+
<asx:abap xmlns:asx="http://www.sap.com/abapxml" version="1.0">
4+
<asx:values>
5+
<VSEOCLASS>
6+
<CLSNAME>Y_CHECK_PREFER_PRAGMAS</CLSNAME>
7+
<LANGU>E</LANGU>
8+
<DESCRIPT>Prefer Pragmas to Pseudo Comments</DESCRIPT>
9+
<STATE>1</STATE>
10+
<CLSCCINCL>X</CLSCCINCL>
11+
<FIXPT>X</FIXPT>
12+
<UNICODE>X</UNICODE>
13+
<WITH_UNIT_TESTS>X</WITH_UNIT_TESTS>
14+
</VSEOCLASS>
15+
</asx:values>
16+
</asx:abap>
17+
</abapGit>

src/examples/y_demo_failures.clas.abap

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,7 @@ CLASS y_demo_failures DEFINITION PUBLIC FINAL CREATE PUBLIC.
122122
METHODS prefer_returning EXPORTING result TYPE string.
123123
METHODS text_assembly.
124124
METHODS collect.
125+
METHODS prefer_pragmas.
125126

126127
PRIVATE SECTION.
127128
DATA attribute_7 TYPE string.
@@ -511,4 +512,12 @@ CLASS Y_DEMO_FAILURES IMPLEMENTATION.
511512
COLLECT seats INTO seats_tab.
512513
ENDMETHOD.
513514

515+
516+
METHOD prefer_pragmas.
517+
TRY.
518+
DATA(div_by_zero) = 1 / 0.
519+
CATCH cx_root. "#EC CATCH_ALL
520+
ENDTRY.
521+
ENDMETHOD.
522+
514523
ENDCLASS.

0 commit comments

Comments
 (0)