Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions changelog.txt
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ Legend

2021-08-XX v.1.16.0
------------------
* Chain Declaration for Complex Structures (#488)
* Empty Catch Alternative Pseudo Comment (#337)
+ Alternative Pseudo Comment (#486)
* line_exists does not support the operator IN (#484)
Expand Down
14 changes: 13 additions & 1 deletion docs/checks/chain-declaration-usage.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ Change the chain up-front declarations to inline declarations.

### What to do in case of exception?

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:
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:

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

```abap
TYPES: "#EC CHAIN_DECL_USAG
name TYPE string,
json TYPE REF TO cl_abap_json.
```

```abap
CONSTANTS: "#EC CHAIN_DECL_USAG
min_age TYPE i VALUE 18,
min_name_size TYPE i VALUE 3.
```

### Example

Before the check:
Expand Down
113 changes: 90 additions & 23 deletions src/checks/y_check_chain_decl_usage.clas.abap
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,23 @@ CLASS y_check_chain_decl_usage DEFINITION PUBLIC INHERITING FROM y_check_base CR
METHODS constructor.

PROTECTED SECTION.
METHODS inspect_statements REDEFINITION.
METHODS inspect_tokens REDEFINITION.

PRIVATE SECTION.
TYPES: BEGIN OF ty_raised_issues,
include TYPE slevel-name,
line_number_colon TYPE sstmnt-colonrow,
END OF ty_raised_issues.

DATA raised_issues TYPE TABLE OF ty_raised_issues.
METHODS get_chained_statements RETURNING VALUE(result) TYPE sstmnt_tab.
METHODS get_chained_variables IMPORTING structure_index TYPE sy-tabix
RETURNING VALUE(result) TYPE sstmnt_tab.
METHODS get_chained_structures IMPORTING structure_index TYPE sy-tabix
RETURNING VALUE(result) TYPE sstmnt_tab.
METHODS is_chained_structure RETURNING VALUE(result) TYPE abap_bool.
METHODS is_complex_structure RETURNING VALUE(result) TYPE abap_bool.

ENDCLASS.



CLASS Y_CHECK_CHAIN_DECL_USAGE IMPLEMENTATION.
CLASS y_check_chain_decl_usage IMPLEMENTATION.


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

relevant_statement_types = VALUE #( BASE relevant_statement_types
( scan_struc_stmnt_type-public_section )
( scan_struc_stmnt_type-protected_section )
( scan_struc_stmnt_type-private_section ) ).

set_check_message( 'Do not chain up-front declarations!' ).
ENDMETHOD.


METHOD inspect_statements.
DATA(statements) = get_chained_statements( ).

LOOP AT statements INTO statement_wa
GROUP BY statement_wa-colonrow.
IF keyword( ) <> if_kaizen_keywords_c=>gc_data
AND keyword( ) <> if_kaizen_keywords_c=>gc_class_data
AND keyword( ) <> if_kaizen_keywords_c=>gc_constants
AND keyword( ) <> if_kaizen_keywords_c=>gc_types.
CONTINUE.
ENDIF.

DATA(count) = REDUCE i( INIT x = 0
FOR row IN statements
WHERE ( colonrow = statement_wa-colonrow )
NEXT x = x + 1 ).

IF count = 1.
CONTINUE.
ENDIF.

DATA(configuration) = detect_check_configuration( statement_wa ).

raise_error( statement_level = statement_wa-level
statement_index = statement_wa-number
statement_from = statement_wa-from
check_configuration = configuration ).
ENDLOOP.
ENDMETHOD.


METHOD inspect_tokens.
CHECK statement-colonrow IS NOT INITIAL.
CHECK statement-terminator = ','.
CHECK get_token_abs( statement-from ) = 'DATA'.
RETURN.
ENDMETHOD.

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

DATA(raised_issue) = VALUE ty_raised_issues( include = include
line_number_colon = statement-colonrow ).
METHOD get_chained_statements.
DATA(index) = line_index( ref_scan->structures[ table_line = structure_wa ] ).
APPEND LINES OF get_chained_variables( index ) TO result.
APPEND LINES OF get_chained_structures( index ) TO result.
ENDMETHOD.

IF line_exists( raised_issues[ include = raised_issue-include
line_number_colon = raised_issue-line_number_colon ] ).
RETURN.
ENDIF.

APPEND raised_issue TO raised_issues.
METHOD get_chained_variables.
result = VALUE sstmnt_tab( FOR statement IN ref_scan->statements
FROM structure_wa-stmnt_from TO structure_wa-stmnt_to
WHERE ( struc = structure_index AND prefixlen > 0 )
( statement ) ).
ENDMETHOD.


METHOD get_chained_structures.
DATA(chained_structures) = VALUE sstmnt_tab( FOR statement IN ref_scan->statements
FROM structure_wa-stmnt_from TO structure_wa-stmnt_to
WHERE ( struc <> structure_index AND prefixlen > 0 )
( statement ) ).

LOOP AT chained_structures ASSIGNING FIELD-SYMBOL(<chained_structure>)
GROUP BY <chained_structure>-struc.
structure_wa = ref_scan->structures[ <chained_structure>-struc ].

IF is_chained_structure( ) = abap_false
OR is_complex_structure( ) = abap_true.
CONTINUE.
ENDIF.

APPEND <chained_structure> TO result.
ENDLOOP.
ENDMETHOD.


METHOD is_chained_structure.
result = xsdbool( structure_wa-stmnt_type = scan_struc_stmnt_type-data
OR structure_wa-stmnt_type = scan_struc_stmnt_type-sequence
OR structure_wa-stmnt_type = scan_struc_stmnt_type-types ).
ENDMETHOD.


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

raise_error( statement_level = statement-level
statement_index = index
statement_from = statement-from
check_configuration = configuration ).
result = xsdbool( statement_type = scan_struc_stmnt_type-data
OR statement_type = scan_struc_stmnt_type-types ).
ENDMETHOD.


Expand Down
Loading