fix method

Future<void> fix(
  1. String filename, {
  2. bool? apply,
})

Implementation

Future<void> fix(String filename, {bool? apply}) async {
  var ast = parseFile(
    path: filename,
    featureSet: FeatureSet.latestLanguageVersion(),
  );

  var patcher = ImportsPatcher(this, filename: filename);

  patcher.visit(ast.unit);

  if (patcher.hasChanges && (apply ?? false)) {
    print("[pubtidy] ${p.relative(filename, from: pkgRoot.path)} fixed.");

    await File(filename).writeAsBytes(
      patcher.apply(ast.content.codeUnits),
    );
  }
}