Skip to content

Commit 11d5816

Browse files
lucasborinLucas Borin
andauthored
Chain Declaration for Complex Structures (#488)
* new test scope * fixes #466 * cleaning self-reference * cleaning chain declaration * cleaning chain declaration * cleaning chain declaration * fixing syntax error * changelog * documentation Co-authored-by: Lucas Borin <5233413+lucasborin@users.noreply.github.co>
1 parent 6a00533 commit 11d5816

14 files changed

+688
-187
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+
* Chain Declaration for Complex Structures (#488)
2122
* Empty Catch Alternative Pseudo Comment (#337)
2223
+ Alternative Pseudo Comment (#486)
2324
* line_exists does not support the operator IN (#484)

docs/checks/chain-declaration-usage.md

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ Change the chain up-front declarations to inline declarations.
1212

1313
### What to do in case of exception?
1414

15-
In exceptional cases, you can suppress this finding by using the pseudo comment `"#EC CHAIN_DECL_USAG` which should be placed after the `DATA:` statement:
15+
In exceptional cases, you can suppress this finding by using the pseudo comment `"#EC CHAIN_DECL_USAG` which should be placed after the declaration statement:
1616

1717
```abap
1818
DATA: "#EC CHAIN_DECL_USAG
@@ -21,6 +21,18 @@ In exceptional cases, you can suppress this finding by using the pseudo comment
2121
client LIKE sy-mandt.
2222
```
2323

24+
```abap
25+
TYPES: "#EC CHAIN_DECL_USAG
26+
name TYPE string,
27+
json TYPE REF TO cl_abap_json.
28+
```
29+
30+
```abap
31+
CONSTANTS: "#EC CHAIN_DECL_USAG
32+
min_age TYPE i VALUE 18,
33+
min_name_size TYPE i VALUE 3.
34+
```
35+
2436
### Example
2537

2638
Before the check:

src/checks/y_check_chain_decl_usage.clas.abap

Lines changed: 90 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -3,21 +3,23 @@ CLASS y_check_chain_decl_usage DEFINITION PUBLIC INHERITING FROM y_check_base CR
33
METHODS constructor.
44

55
PROTECTED SECTION.
6+
METHODS inspect_statements REDEFINITION.
67
METHODS inspect_tokens REDEFINITION.
78

89
PRIVATE SECTION.
9-
TYPES: BEGIN OF ty_raised_issues,
10-
include TYPE slevel-name,
11-
line_number_colon TYPE sstmnt-colonrow,
12-
END OF ty_raised_issues.
13-
14-
DATA raised_issues TYPE TABLE OF ty_raised_issues.
10+
METHODS get_chained_statements RETURNING VALUE(result) TYPE sstmnt_tab.
11+
METHODS get_chained_variables IMPORTING structure_index TYPE sy-tabix
12+
RETURNING VALUE(result) TYPE sstmnt_tab.
13+
METHODS get_chained_structures IMPORTING structure_index TYPE sy-tabix
14+
RETURNING VALUE(result) TYPE sstmnt_tab.
15+
METHODS is_chained_structure RETURNING VALUE(result) TYPE abap_bool.
16+
METHODS is_complex_structure RETURNING VALUE(result) TYPE abap_bool.
1517

1618
ENDCLASS.
1719

1820

1921

20-
CLASS Y_CHECK_CHAIN_DECL_USAGE IMPLEMENTATION.
22+
CLASS y_check_chain_decl_usage IMPLEMENTATION.
2123

2224

2325
METHOD constructor.
@@ -28,33 +30,98 @@ CLASS Y_CHECK_CHAIN_DECL_USAGE IMPLEMENTATION.
2830
settings-threshold = 0.
2931
settings-documentation = |{ c_docs_path-checks }chain-declaration-usage.md|.
3032

33+
relevant_statement_types = VALUE #( BASE relevant_statement_types
34+
( scan_struc_stmnt_type-public_section )
35+
( scan_struc_stmnt_type-protected_section )
36+
( scan_struc_stmnt_type-private_section ) ).
37+
3138
set_check_message( 'Do not chain up-front declarations!' ).
3239
ENDMETHOD.
3340

3441

42+
METHOD inspect_statements.
43+
DATA(statements) = get_chained_statements( ).
44+
45+
LOOP AT statements INTO statement_wa
46+
GROUP BY statement_wa-colonrow.
47+
IF keyword( ) <> if_kaizen_keywords_c=>gc_data
48+
AND keyword( ) <> if_kaizen_keywords_c=>gc_class_data
49+
AND keyword( ) <> if_kaizen_keywords_c=>gc_constants
50+
AND keyword( ) <> if_kaizen_keywords_c=>gc_types.
51+
CONTINUE.
52+
ENDIF.
53+
54+
DATA(count) = REDUCE i( INIT x = 0
55+
FOR row IN statements
56+
WHERE ( colonrow = statement_wa-colonrow )
57+
NEXT x = x + 1 ).
58+
59+
IF count = 1.
60+
CONTINUE.
61+
ENDIF.
62+
63+
DATA(configuration) = detect_check_configuration( statement_wa ).
64+
65+
raise_error( statement_level = statement_wa-level
66+
statement_index = statement_wa-number
67+
statement_from = statement_wa-from
68+
check_configuration = configuration ).
69+
ENDLOOP.
70+
ENDMETHOD.
71+
72+
3573
METHOD inspect_tokens.
36-
CHECK statement-colonrow IS NOT INITIAL.
37-
CHECK statement-terminator = ','.
38-
CHECK get_token_abs( statement-from ) = 'DATA'.
74+
RETURN.
75+
ENDMETHOD.
3976

40-
DATA(include) = get_include( p_level = statement-level ).
4177

42-
DATA(raised_issue) = VALUE ty_raised_issues( include = include
43-
line_number_colon = statement-colonrow ).
78+
METHOD get_chained_statements.
79+
DATA(index) = line_index( ref_scan->structures[ table_line = structure_wa ] ).
80+
APPEND LINES OF get_chained_variables( index ) TO result.
81+
APPEND LINES OF get_chained_structures( index ) TO result.
82+
ENDMETHOD.
4483

45-
IF line_exists( raised_issues[ include = raised_issue-include
46-
line_number_colon = raised_issue-line_number_colon ] ).
47-
RETURN.
48-
ENDIF.
4984

50-
APPEND raised_issue TO raised_issues.
85+
METHOD get_chained_variables.
86+
result = VALUE sstmnt_tab( FOR statement IN ref_scan->statements
87+
FROM structure_wa-stmnt_from TO structure_wa-stmnt_to
88+
WHERE ( struc = structure_index AND prefixlen > 0 )
89+
( statement ) ).
90+
ENDMETHOD.
91+
92+
93+
METHOD get_chained_structures.
94+
DATA(chained_structures) = VALUE sstmnt_tab( FOR statement IN ref_scan->statements
95+
FROM structure_wa-stmnt_from TO structure_wa-stmnt_to
96+
WHERE ( struc <> structure_index AND prefixlen > 0 )
97+
( statement ) ).
98+
99+
LOOP AT chained_structures ASSIGNING FIELD-SYMBOL(<chained_structure>)
100+
GROUP BY <chained_structure>-struc.
101+
structure_wa = ref_scan->structures[ <chained_structure>-struc ].
102+
103+
IF is_chained_structure( ) = abap_false
104+
OR is_complex_structure( ) = abap_true.
105+
CONTINUE.
106+
ENDIF.
107+
108+
APPEND <chained_structure> TO result.
109+
ENDLOOP.
110+
ENDMETHOD.
111+
112+
113+
METHOD is_chained_structure.
114+
result = xsdbool( structure_wa-stmnt_type = scan_struc_stmnt_type-data
115+
OR structure_wa-stmnt_type = scan_struc_stmnt_type-sequence
116+
OR structure_wa-stmnt_type = scan_struc_stmnt_type-types ).
117+
ENDMETHOD.
118+
51119

52-
DATA(configuration) = detect_check_configuration( statement ).
120+
METHOD is_complex_structure.
121+
DATA(statement_type) = ref_scan->structures[ structure_wa-back ]-stmnt_type.
53122

54-
raise_error( statement_level = statement-level
55-
statement_index = index
56-
statement_from = statement-from
57-
check_configuration = configuration ).
123+
result = xsdbool( statement_type = scan_struc_stmnt_type-data
124+
OR statement_type = scan_struc_stmnt_type-types ).
58125
ENDMETHOD.
59126

60127

0 commit comments

Comments
 (0)