codemod/abap-complex-expression-mining
Annotate overly complex ABAP boolean expressions with metrics and remediation comments.
# @codemod/abap-complex-expression-mining
Annotate overly complex ABAP boolean expressions with inline remediation comments, complexity metrics, and guidance to help teams reduce maintainability issues.
## What It Rewrites
The codemod scans `.abap` and `.asprog` files for `IF`, `ELSEIF`, `CHECK`, `WHILE`, and `ASSERT` statements.
When a statement's complexity score is greater than the configured maximum, the codemod inserts three ABAP comment lines immediately above it:
- A TODO line with the measured score and configured maximum
- A metrics line with logical-operator count and maximum parenthesis depth
- A remediation line suggesting extraction into helper variables or methods
Complexity score is calculated as:
```text
logical operators (AND, OR, EQUIV, NOT) + maximum parenthesis depth
```
## Preserve Cases
- Statements at or below the configured maximum are unchanged
- Statements already preceded by this codemod's TODO annotation are left unchanged
- Non-target ABAP statements are unchanged
## Usage
```bash
# Run the packaged workflow locally
npx codemod@latest workflow run -w . --target /path/to/abap-repo
# Override the default complexity threshold (default: 3)
npx codemod@latest workflow run -w . --target /path/to/abap-repo --param maxComplexity=5
```
## Example
Before:
```abap
IF is_admin = abap_true
AND ( has_profile = abap_true OR has_override = abap_true )
AND account_locked = abap_false.
grant_access( ).
ENDIF.
```
After:
```abap
" TODO(codemod): simplify boolean expression (score 4 > max 3).
" Metrics: logical-operators=3, max-parenthesis-depth=1.
" Guidance: extract sub-conditions into named helper variables or methods.
IF is_admin = abap_true
AND ( has_profile = abap_true OR has_override = abap_true )
AND account_locked = abap_false.
grant_access( ).
ENDIF.
```
## Development
```bash
pnpm test
pnpm check-types
npx codemod@latest ai call validate_codemod_package --input '{"package_path":".","run_default_test":true,"run_check_types":true,"command_timeout_seconds":120}'
```