codemod 0.1.5

  • Readme
  • Changelog
  • Installing
  • 81

codemod for Dart #

Pub Build Status

A library that makes it easy to write and run automated code modifications on a codebase. Primarily geared towards updating/refactoring Dart code by leveraging the analyzer package's APIs for parsing and traversing the AST.

Inspired by and based on Facebook's codemod library.

Demo #


How It Works #

The end goal of this library is to enable you to easily and automatically apply code modifications and refactors via an interactive CLI. To that end, the following function is provided:

int runInteractiveCodemod(FileQuery query, Suggestor suggestor);

Calling this will tell codemod to scan for files using query, which will return a set of file paths that codemod will then read. Each file is then provided as the input to the suggestor, which will return a list of patches that should be suggested to the user. As patches are suggested and accepted by the user, codemod handles applying them to the files and writing the result to disk.

Writing a Suggestor #

This library provides Suggestor, but it is just an interface with two methods:

abstract class Suggestor {
  bool shouldSkip(String sourceFileContents);
  Iterable<Patch> generatePatches(SourceFile sourceFile);

Codemod will read the contents of each file returned from the query and first pass it to shouldSkip(). This provides a way to short-circuit the potentially expensive generatePatches() method if need be.

If not skipped, the file contents will be passed to generatePatches() in the form of a SourceFile from the source_span package. Operating on this model makes it easy to create patches at specific offsets within the file.

Suggestor Example: Insert License Headers #

The following suggestor checks each file for the expected license header, and if missing, yields a Patch that inserts it at the beginning of the file.

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

final String licenseHeader = '''
// Lorem ispum license.
// 2018-2019

class LicenseHeaderInserter implements Suggestor {
  bool shouldSkip(String sourceFileContents) =>

  Iterable<Patch> generatePatches(SourceFile sourceFile) sync* {
    yield Patch(
      // The span across which the patch should be applied.
        // Start offset.
        // 0 means "insert at the beginning of the file."
        // End offset.
        // Using the same offset as the start offset here means that the patch
        // is being inserted at this point instead of replacing a span of text.
      // Text to insert.

Suggestor Example: Regex Substitution #

Regex substitutions are also a common strategy for codemods and are sufficient for simple changes. The following suggestor updates a version constraint for the codemod package in a pubspec.yaml:

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

/// Pattern that matches a dependency version constraint line for the `codemod`
/// package, with the first capture group being the constraint.
final RegExp pattern = RegExp(
  multiLine: true,

/// The version constraint that `codemod` entries should be updated to.
const String targetConstraint = '^1.0.0';

class RegexSubstituter implements Suggestor {
  bool shouldSkip(String sourceFileContents) => false;

  Iterable<Patch> generatePatches(SourceFile sourceFile) sync* {
    final contents = sourceFile.getText(0);
    for (final match in pattern.allMatches(contents)) {
      final line =;
      final constraint =;
      final updated = line.replaceFirst(constraint, targetConstraint) + '\n';

      yield Patch(
        sourceFile.span(match.start, match.end),

Suggestor Example: AST Visitor #

Regexes and custom parsing can get you pretty far, but using the analyzer's visitor pattern to traverse the parsed AST is a much more robust option and allows for the creation of very powerful codemods with relatively little effort.

Consider the following suggestor that removes all deprecated declarations (i.e. classes, constructors, variables, methods, etc.):

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

class DeprecatedRemover extends GeneralizingAstVisitor
    with AstVisitingSuggestorMixin {
  static bool isDeprecated(AnnotatedNode node) =>
      node.metadata.any((m) => == 'deprecated');

  visitDeclaration(Declaration node) {
    if (isDeprecated(node)) {
      // Remove the node by replacing the span from its start offset to its end
      // offset with an empty string.
      yieldPatch(node.offset, node.end, '');

In this example, the suggestor extends the GeneralizingAstVisitor which allows it to target all nodes that could be deprecated with a single visit method. Then it's just a matter of checking for either the @Deprecated() or @deprecated() annotation and yielding a patch with an empty string across the entire node, which is effectively a deletion.

You may notice that in this example, the suggestor is no longer implementing generatePatches() – instead, we use the AstVisitingSuggestorMixin. This mixin handles parsing the AST for the given SourceFile and starting the visitor pattern so that all you have to do is override the applicable visit methods.

Additionally, although the GeneralizingAstVisitor was the appropriate choice for this suggestor, any AstVisitor will work. Choose whichever one fits the job.

If you're not familiar with the analyzer API, in particular the AstNode class hierarchy and the AstVisitor pattern, it may be a good opportunity to browse the analyzer source code or look at the AST visiting suggestor codemods that are linked below in the references section to see what is possible with this approach.

Running a Codemod #

All you need to run a codemod is:

  1. A FileQuery to determine the set of files to be read

  2. A Suggestor to suggest patches on each file

  3. A .dart file with a main() block that calls runInterativeCodemod()

If we were to run the 3 suggestor examples from above, it would like like so:

Regex Substituter:

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

void main(List<String> args) {
  exitCode = runInteractiveCodemod(
    args: args,

License Header Inserter:

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

void main(List<String> args) {
  exitCode = runInteractiveCodemod(
    FileQuery.dir(pathFilter: isDartFile),
    args: args,

Deprecated Remover:

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

void main(List<String> args) {
  exitCode = runInteractiveCodemod(
    FileQuery.dir(pathFilter: isDartFile),
    args: args,

Run the .dart file directly or package it up as an executable and publish it on pub!

Additional Options #

To facilitate the creation of more complex codemods, two additional pieces are provided by this library:

  • Aggregate multiple suggestors into a single suggestor with AggregateSuggestor:

      import 'dart:io';
      import 'package:codemod/codemod.dart';
      void main(List<String> args) {
        final query = ...;
        exitCode = runInteractiveCodemod(
  • Run multiple suggestors (or aggregate suggestors) sequentially:

      import 'dart:io';
      import 'package:codemod/codemod.dart';
      void main(List<String> args) {
        final query = ...;
        exitCode = runInteractiveCodemodSequence(
          args: args,

    This can be useful if a certain modification needs to happen prior to another, or if you need to use a "collector" pattern wherein the first suggestor collects information from the files that a second suggestor will then use to suggest patches.

Testing Suggestors #

Testing suggestors is relatively easy for two reasons:

  • The API surface area is small (most of the time you only need to test the generatePatches() method)

  • The list of patches returned by generatePatches() can be applied to the input SourceFile to obtain a String output, which is trivial to examine in order to assert correctness.

In other words, all you need to do is determine a sufficient set of inputs and their respective expected outputs.

To help out, this library exports the applyPatches(sourceFile, patches) function that it uses internally to make it easy to compare the result of a suggestor's patches to the expected output.

Let's use the DeprecatedRemover suggestor example from above to demonstrate testing:

import 'package:codemod/codemod.dart';
import 'package:source_span/source_span.dart';
import 'package:test/test.dart';

void main() {
  group('DeprecatedRemover', () {
    test('removes deprecated variable', () {
      final sourceFile = SourceFile.fromString('''
// Not deprecated.
var foo = 'foo';
var bar = 'bar';''');
      final expectedOutput = '''
// Not deprecated.
var foo = 'foo';

      final patches = DeprecatedRemover().generatePatches(sourceFile);
      expect(patches, hasLength(1));
      expect(applyPatches(sourceFile, patches), expectedOutput);

References #

Credits #

Contributing #

  • Run tests: pub run test

  • Format code: pub run dart_dev format

  • Run static analysis: dartanalyzer .

0.1.4 #

  • Prompts the user to either skip overlapping patches or quit when they are found.

0.1.3 #

  • Codemod authors can now augment the help output and the changes required output via runInteractiveCodemod() and runInteractiveCodemodSequence() using the optional additionalHelpOutput and changesRequiredOutput params.

    • If additionalHelpOutput is given, it will be printed to stderr after the default help output when the codemod is run with the -h|--help flag.

    • If changesRequiredOutput is given, it will be printed to stderr after the default output when the codemod is run with the --fail-on-changes flag and changes are in fact required.

0.1.2 #

  • Fix a typing issue with the AggregateSuggestor's constructor param.

  • Add tests for AggregateSuggestor and AstVisitingSuggestorMixin

0.1.1 #

  • Update pubspec.yaml for initial OSS release.

0.1.0 #

  • Initial tag.

Use this package as a library

1. Depend on it

Add this to your package's pubspec.yaml file:

  codemod: ^0.1.5

2. Install it

You can install packages from the command line:

with pub:

$ pub get

with Flutter:

$ flutter pub get

Alternatively, your editor might support pub get or flutter pub get. Check the docs for your editor to learn more.

3. Import it

Now in your Dart code, you can use:

import 'package:codemod/codemod.dart';
Describes how popular the package is relative to other packages. [more]
Code health derived from static analysis. [more]
Reflects how tidy and up-to-date the package is. [more]
Weighted score of the above. [more]
Learn more about scoring.

We analyzed this package on Mar 27, 2020, and provided a score, details, and suggestions below. Analysis was completed with status completed using:

  • Dart: 2.7.1
  • pana: 0.13.6

Health suggestions

Fix lib/src/suggestors.dart. (-1.49 points)

Analysis of lib/src/suggestors.dart reported 3 hints:

line 15 col 1: 'package:analyzer/analyzer.dart' is deprecated and shouldn't be used.

line 190 col 29: 'parseCompilationUnit' is deprecated and shouldn't be used. Please use parseString instead.

line 199 col 13: Unnecessary new keyword.

Fix lib/src/util.dart. (-1 points)

Analysis of lib/src/util.dart reported 2 hints:

line 37 col 13: Unnecessary new keyword.

line 70 col 11: Unnecessary new keyword.

Fix lib/src/file_query.dart. (-0.50 points)

Analysis of lib/src/file_query.dart reported 1 hint:

line 54 col 48: Unnecessary new keyword.

Fix lib/src/run_interactive_codemod.dart. (-0.50 points)

Analysis of lib/src/run_interactive_codemod.dart reported 1 hint:

line 238 col 11: Unnecessary new keyword.

Maintenance issues and suggestions

Support latest dependencies. (-10 points)

The version constraint in pubspec.yaml does not support the latest published versions for 1 dependency (analyzer).

Maintain an example.

None of the files in the package's example/ directory matches known example patterns.

Common filename patterns include main.dart, example.dart, and codemod.dart. Packages with multiple examples should provide example/

For more information see the pub package layout conventions.


Package Constraint Resolved Available
Direct dependencies
Dart SDK >=2.2.1 <3.0.0
analyzer >=0.37.0 <0.39.0 0.38.5 0.39.4
args ^1.5.1 1.6.0
io ^0.3.3 0.3.3
logging ^0.11.3+2 0.11.4
meta ^1.1.7 1.1.8
path ^1.6.2 1.6.4
source_span ^1.4.1 1.7.0
stack_trace ^1.9.3 1.9.3
Transitive dependencies
async 2.4.1
charcode 1.1.3
collection 1.14.12
convert 2.1.1
crypto 2.1.4
csslib 0.16.1
front_end 0.1.27 0.1.29
glob 1.2.0
html 0.14.0+3
js 0.6.1+1
kernel 0.3.27 0.3.29
node_interop 1.0.3
node_io 1.0.1+2
package_config 1.9.3
pub_semver 1.4.4
string_scanner 1.0.5
term_glyph 1.1.0
typed_data 1.1.6
watcher 0.9.7+14
yaml 2.2.0
Dev dependencies
build_runner ^1.0.0
build_test ^0.10.8
dart_dev ^2.0.1
dart_style ^1.2.0
dependency_validator ^1.2.3
mockito ^4.0.0
pedantic ^1.4.0 1.9.0
test ^1.5.1+1