Skip to content

Conversation

@NattyPluz
Copy link
Contributor

This PR introduces an automated Quick Fix for the existing catch_parameter_names lint rule.

Previously, the linter would warn if catch parameters (exception or stack trace) did not match the configured standard (default err, st), but users had to manually rename them. This update adds a DartFix that automatically renames the parameter and all its usages within the catch block.

Changes

  1. New Fix Class (RenameCatchParameter):

    • Analyzes the specific parameter causing the lint error.
    • Determines the correct replacement name based on the CatchParameterNamesConfig.
    • Implements a RecursiveAstVisitor (_UsageFinder) to safely find all usages of that parameter within the catch block scope.
    • Handles Shadowing: Correctly detects and skips nested scopes (nested catch clauses, function declarations, or function expressions) where the parameter name might be shadowed, ensuring safe refactoring.
  2. Updated CatchParameterNames Rule:

    • Registered RenameCatchParameter in getFixes().
  3. Documentation:

    • Updated README.md to indicate that catch_parameter_names now supports a Quick Fix.

Example

Configuration:

custom_lint:
  rules:
    - catch_parameter_names:
      exception: error
      stack_trace: stackTrace

Input (Before Fix):

try {
  // ...
} catch (e, s) { // LINT: Parameter name for exception/stacktrace is non-standard.
  print(e);
  log(s);
}

Output (After Fix):

try {
  // ...
} catch (error, stackTrace) {
  print(error);
  log(stackTrace);
}

Implementation Details

The fix logic is sturdy against scope shadowing. For example, in the following case, the inner e is not renamed when fixing the outer catch block:

try {
  // ...
} catch (e) { // Fix triggered here
  print(e); // Renamed

  try { ... } catch (e) { // Shadowed variable
     print(e); // Ignored/Not renamed
  }
}

Checklist

  • The code compiles correctly.
  • Lint rules passed.
  • README.md updated.
  • Tested with custom configuration options.

This commit introduces a new quick-fix for the `catch_parameter_names` lint.

The `RenameCatchParameter` fix automatically renames incorrectly named exception and stack trace parameters in `catch` clauses to conform to the names configured for the lint. The fix also renames all usages of the parameter within the catch block's scope, properly handling shadowing from nested scopes.
…bles

This commit introduces a quick fix for the `prefer_const_constructors_in_immutables` lint rule. The fix automatically renames incorrectly named catch clause parameters (`exception` and `stackTrace`) and updates all their usages within the catch block.

The `README.md` has also been updated to document this new quick fix.
@NattyPluz NattyPluz requested a review from mchudy as a code owner December 31, 2025 22:48
@github-actions github-actions bot added the p: leancode_lint Related to the leancode_lint package label Dec 31, 2025
@NattyPluz NattyPluz changed the title [leancode_lint] Add Quick Fix for catch_parameter_names rule Feat: Add Quick Fix for catch_parameter_names rule Dec 31, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

p: leancode_lint Related to the leancode_lint package

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant