ABAP Cleaner

repository·main·Indexed 20 days ago

https://github.com/sap/abap-cleaner

A 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.

Tokens
118.1K
Snippets
162
Records
281
Agent score
70%

What's inside abap-cleaner

  1. What is ABAP Cleaner?

    main
    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.
  2. Set the default cleanup range

    main

    When 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 CLASS definition 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.

  3. Standardize empty lines within methods using EmptyLinesWithinMethodsRule

    main

    The EmptyLinesWithinMethodsRule enforces 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.
  4. Simplify a chain with one element rule

    main

    The ChainOfOneRule simplifies ABAP code by removing the colon (:) from chains that consist of only a single element. This applies to declarations (like DATA, CONSTANTS, FIELD-SYMBOLS) and non-declaration commands (like CHECK, 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).
  5. Align SELECT clauses with AlignSelectClausesRule

    main

    The AlignSelectClausesRule aligns keywords such as SELECT, FROM, FIELDS, WHERE, GROUP BY, etc., within the main or sub-query clauses of ABAP SQL statements (SELECT, WITH, and OPEN 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 ... JOIN
    • Align SELECT lists
    • Align logical expressions
  6. Align SELECT ... FROM ... JOIN statements

    main

    The AlignSelectFromRule aligns the FROM clause, including various JOIN operations, within ABAP SQL statements such as SELECT, WITH, and OPEN CURSOR.

    Behavioral Notes:

    • One-liners: Statements written on a single line are preserved and not modified.
    • Logical Expressions: Expressions following JOIN ... ON are not handled by this rule; they are instead managed by the Align logical expressions rule.
    • Hierarchies: HIERARCHY... ( ) structures are left unchanged.
    • Prerequisite: This rule assumes that the positions of keywords like FROM, FIELDS, and WHERE have already been established by the Align SELECT clauses rule.
  7. Align SELECT lists

    main

    The Align SELECT lists rule formats field lists within ABAP SQL statements to improve readability. It aligns the fields following specific keywords in SELECT, WITH, and OPEN CURSOR statements.

    Targeted Keywords:

    • SELECT
    • FIELDS
    • ORDER BY
    • GROUP BY
    • INTO ( ... )

    Note: GROUPING SETS ( ... ) is explicitly excluded from this rule and remains unchanged.

  8. Standardize spaces around brackets in DDL

    main

    The DdlSpacesAroundBracketsRule ensures 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 layout rule, not this one.

  9. Remove needless parentheses rule

    main

    The NeedlessParenthesesRule removes 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.

  10. Remove empty commands rule

    main

    The EmptyCommandRule identifies 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.
  11. Report unused parameters with UnusedParametersRule

    main

    The UnusedParametersRule identifies parameters in ABAP methods that are never used or assigned. When an unused parameter is detected, the tool adds a TODO comment to the method implementation to alert the developer.

    Key Behaviors

    • Suppression: You can suppress the TODO comment for a specific parameter by adding the ##NEEDED addition 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.
      • RETURNING parameters are never reported as unused because they are implicitly assigned by the RETURN statement.
    • 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

    OptionDefault BehaviorDescription
    Report IMPORTING parametersEnabled (in non-interface methods)Checks if IMPORTING parameters are used.
    Report EXPORTING parametersEnabled (in all methods)Checks if EXPORTING parameters are cleared or assigned.
    Report CHANGING parametersEnabled (in non-interface methods)Checks if CHANGING parameters are used or assigned.
    Report RETURNING parametersNeverAlways ignores RETURNING parameters.
    Only add TODO in methods with executable statementsEnabledPrevents TODO comments in empty or unimplemented methods.
    Do not add TODO for EXPORTING VALUE(...) parametersEnabledIgnores EXPORTING parameters 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.