Skip to content
pubscout

codemod

1.2.2, released 8 weeks ago by workiva.com

Write and run automated code modifications and refactors on a codebase.

$ flutter pub add codemod
Source repository

What it does

Provides utilities to write suggestor functions that analyze source files using package:analyzer, yield text patches, and run them via an interactive CLI. It supports regex, AST visitors, and test helpers to verify code refactoring logic.

AI-written summary of version 1.2.2, based on the package's README and metadata. Report an inaccuracy

Use it when

  • You need to automate large-scale refactoring or code updates across multiple files.
  • You are building a custom CLI tool to migrate Dart code or configuration files.
  • You want to write interactive codemods that prompt the user before applying patches.

Skip it when

  • You only need standard code generation during build time (use build_runner instead).
  • You are looking for a simple static analysis linter without modifying code.

Strengths

  • Leverages package:analyzer APIs for robust AST traversal and modification.
  • Interactive CLI handles user confirmation for patches.
  • Includes testing helpers like PackageContextForTest for verifying resolved AST suggestors.

Weaknesses

  • Requires familiarity with the analyzer package API for complex AST-based transforms.

Setup

Kind
Pure Dart
Effort
Low
Code generation
No
Direct dependencies
11

Add codemod to pubspec.yaml dependencies and create a main script that calls runInteractiveCodemod.

import 'dart:io';
import 'package:codemod/codemod.dart';

void main(List<String> args) async {
  exitCode = await runInteractiveCodemod(
    ['pubspec.yaml'],
    regexSubstituter,
    args: args,
  );
}

Alternatives

Packages that do the same job and could replace codemod.

  • lint_staged

    Run linters and custom commands on git staged files for Flutter and Dart projects

Works with

Packages commonly used together with codemod: ones it builds on, ones built on it, and its own family.