ABAP Cleaner
repository·main·Indexed 20 days ago
https://github.com/sap/abap-cleanerA configurable tool for automating ABAP code style cleanup, applying over 100 rules for formatting, alignment, and the replacement of obsolete commands to help developers adhere to guidelines like the Clean ABAP Styleguide. It is available as a plug-in for ABAP Development Tools (ADT) in Eclipse, an extension for Visual Studio Code, and as a stand-alone application. It supports object-oriented ABAP, reports, and functions, utilizing profiles (default, essential, or custom) to manage rule activation and strictness.
What's inside abap-cleaner
- ABAP Cleaner is a configurable tool designed to automate ABAP code style cleanup. It applies over 100 different cleanup rules (covering formatting, alignment, replacing obsolete commands, and reducing nesting depth) to code sections or entire documents. It is designed to work with object-oriented ABAP, but also supports reports and functions. Note that EML (Entity Manipulation Language) statements are currently not supported and will remain unchanged.
Set the default cleanup range
mainWhen opening the interactive ABAP cleaner from ADT without a prior selection, you can define a 'Default cleanup range':
- Current command: The command at the cursor position.
- Current method / declaration section / FORM etc.: The logical block containing the cursor.
- Current class: The entire
CLASSdefinition or implementation. - Entire code document: The whole file.
Important: If you have already selected code in the editor before opening the tool, ABAP cleaner will ignore the default range and only clean the selected statements.
Standardize empty lines within methods using EmptyLinesWithinMethodsRule
mainThe
EmptyLinesWithinMethodsRuleenforces consistent spacing within ABAP methods by restricting consecutive empty lines and ensuring a single empty line separates declarations from the first executable statement (or the comments preceding it).This rule is part of the essential profile and is designed to comply with the Clean ABAP Styleguide.
For function modules, the rule preserves additional empty lines at the beginning to maintain alignment with ADT and SE37 behavior.
METHOD empty_lines_within_methods. DATA lv_any_integer TYPE i. DATA lv_any_string TYPE string. do_something( ). do_something_else( ). finish_doing_something( ). ENDMETHOD.Simplify a chain with one element rule
mainThe
ChainOfOneRulesimplifies ABAP code by removing the colon (:) from chains that consist of only a single element. This applies to declarations (likeDATA,CONSTANTS,FIELD-SYMBOLS) and non-declaration commands (likeCHECK,CLEAR).Configuration Options
You can configure where this rule is applied:
- Execute on declarations in CLASS ... DEFINITION sections: Applies the simplification within class definition blocks.
- Execute on declarations in methods etc.: Applies the simplification within method implementations and other local blocks.
- Execute on non-declaration commands: Applies the simplification to commands that are not part of a declaration (e.g.,
CHECK,CLEAR).
Align SELECT clauses with AlignSelectClausesRule
mainThe
AlignSelectClausesRulealigns keywords such asSELECT,FROM,FIELDS,WHERE,GROUP BY, etc., within the main or sub-query clauses of ABAP SQL statements (SELECT,WITH, andOPEN CURSOR).Note on related rules: This rule focuses on the alignment of the clause keywords themselves. It does not handle the alignment of field lists, joins, or logical expressions. For those, use the following dedicated rules:
Align SELECT ... FROM ... JOINAlign SELECT listsAlign logical expressions
Align SELECT ... FROM ... JOIN statements
mainThe
AlignSelectFromRulealigns theFROMclause, including variousJOINoperations, within ABAP SQL statements such asSELECT,WITH, andOPEN CURSOR.Behavioral Notes:
- One-liners: Statements written on a single line are preserved and not modified.
- Logical Expressions: Expressions following
JOIN ... ONare not handled by this rule; they are instead managed by theAlign logical expressionsrule. - Hierarchies:
HIERARCHY... ( )structures are left unchanged. - Prerequisite: This rule assumes that the positions of keywords like
FROM,FIELDS, andWHEREhave already been established by theAlign SELECT clausesrule.
Align SELECT lists
mainThe
Align SELECT listsrule formats field lists within ABAP SQL statements to improve readability. It aligns the fields following specific keywords inSELECT,WITH, andOPEN CURSORstatements.Targeted Keywords:
SELECTFIELDSORDER BYGROUP BYINTO ( ... )
Note:
GROUPING SETS ( ... )is explicitly excluded from this rule and remains unchanged.Standardize spaces around brackets in DDL
mainThe
DdlSpacesAroundBracketsRuleensures consistent spacing around brackets[...]and parentheses(...)within Data Definition Language (DDL) files. It targets cardinality in associations, path expressions, built-in functions, ABAP types, and arithmetic expressions.Note: Spacing within annotations is managed by the
Standardize annotation layoutrule, not this one.Remove needless parentheses rule
mainThe
NeedlessParenthesesRuleremoves unnecessary parentheses from logical expressions in ABAP code.Side Effect: If parentheses are removed, the 'Align logical expressions' rule is automatically executed, even if it is otherwise deactivated.
Caution: While removing parentheses does not change the logic, it may occasionally reduce code readability. Use the available options to balance cleanliness and clarity.
Remove empty commands rule
mainThe
EmptyCommandRuleidentifies and removes ABAP commands that consist solely of punctuation marks such as.or: , ..Crucially, this rule preserves comments that may be contained within or alongside these empty commands. It is used to clean up syntactically redundant punctuation that does not contribute to the logic of the code.
METHOD remove_empty_commands. " although this method contains syntactically correct ABAP code, " we hope you'll never see anything like this in real life! DATA lv_primary TYPE i... lv_primary = 2. . . . lv_primary = 3. . lv_primary = 5. .. lv_primary = 7.. :,. lv_primary = 11. ::::. lv_primary = 13. ::::. .:.:,.:,.lv_primary = 17.:.:,.:,. ... lv_primary = 19... lv_primary = 23... lv_primary = 29... : " comment 1 * comment 2 , " comment 3 . " comment 4 ENDMETHOD.Resulting code:
METHOD remove_empty_commands. " although this method contains syntactically correct ABAP code, " we hope you'll never see anything like this in real life! DATA lv_primary TYPE i. lv_primary = 2. lv_primary = 3. lv_primary = 5. lv_primary = 7. lv_primary = 11. lv_primary = 13. lv_primary = 17. lv_primary = 19. lv_primary = 23. lv_primary = 29. " comment 1 * comment 2 " comment 3 " comment 4 ENDMETHOD.Remove space before commas and period
mainThis rule automates the removal of spaces before chain commas and before the period at the end of a statement. It is part of the essential profile and is required to comply with the Clean ABAP Styleguide.Report unused parameters with UnusedParametersRule
mainThe
UnusedParametersRuleidentifies parameters in ABAP methods that are never used or assigned. When an unused parameter is detected, the tool adds aTODOcomment to the method implementation to alert the developer.Key Behaviors
- Suppression: You can suppress the
TODOcomment for a specific parameter by adding the##NEEDEDaddition to its declaration in the method signature. - Detection Logic:
- Parameters used only in commented-out ABAP code are counted as used.
- Parameters mentioned only in text comments are not counted as used.
RETURNINGparameters are never reported as unused because they are implicitly assigned by theRETURNstatement.
- Limitations:
- The method signature must be located in the same code document as the implementation for the check to work.
- Methods that use any macros are skipped.
Configuration Options
Option Default Behavior Description Report IMPORTING parameters Enabled (in non-interface methods) Checks if IMPORTINGparameters are used.Report EXPORTING parameters Enabled (in all methods) Checks if EXPORTINGparameters are cleared or assigned.Report CHANGING parameters Enabled (in non-interface methods) Checks if CHANGINGparameters are used or assigned.Report RETURNING parameters Never Always ignores RETURNINGparameters.Only add TODO in methods with executable statements Enabled Prevents TODOcomments in empty or unimplemented methods.Do not add TODO for EXPORTING VALUE(...) parameters Enabled Ignores EXPORTINGparameters passed by value, as they are automatically cleared.CLASS cl_report_unused_parameters DEFINITION. PUBLIC SECTION. METHODS any_method IMPORTING its_any_table TYPE ty_ts_table iv_unused_but_needed TYPE string ##NEEDED " suppresses a TODO comment on this parameter EXPORTING ev_count TYPE i CHANGING cs_any_struc TYPE ty_s_struc RETURNING VALUE(rv_result) TYPE i. ... ENDCLASS. CLASS cl_report_unused_parameters IMPLEMENTATION. METHOD any_method. " TODO: parameter ITS_ANY_TABLE is only used in commented-out code (ABAP cleaner) " TODO: parameter CS_ANY_STRUC is never used or assigned (ABAP cleaner) CLEAR ev_count. ENDMETHOD. ENDCLASS.- Suppression: You can suppress the