r/dartlang • u/raman4183 • 5h ago
Tools An update on analyzer_kit
About nine months ago, I put out analyzer_kit 1.0 as an experiment to see how well Dart's analysis_server_plugin API could fill in some of the language's missing data-class ergonomics (copyWith, ==/hashCode, toString, toMap/fromMap) without build_runner or generated part files.
The development was basically put on hold because i was waiting for augmented class support to land but after all this time, I decided to revive it and update it again to cover some use-cases, fix bugs and basically modernize the package for 3.14 release.
What's in 2.0.0
• CompositeAnnotation replaces @DataClass: Instead of a single fixed annotation with boolean flags, you can compose any combination of CopyWith, OverrideEquality, OverrideToString, Serialize, and Deserialize into reusable const annotations.
• Primary constructor support: Works with positional and named primary constructors (including empty bodies), alongside standard and inherited fields.
• Out-of-sync warnings and in-place fixes: A single-pass AnalyzerKitRule checks existing methods against your fields and reports *_out_of_sync warnings for missing/extra fields, nullability changes, or type argument drift. Quick fixes replace out-of-sync methods in-place, and there are 6 force-regenerate IDE assists.
• @Ignore and @Redact: Exclude fields globally or for specific generated methods (like @Ignore({.overrideToString, .serialize})), or mask sensitive values in toString/serialization while keeping them in copyWith, ==, and fromMap.
• FieldRename casing: @Serialize and @Deserialize support none, snake, kebab, pascal, and screamingSnake field renaming.
• More Dart types and options: Generics (Box<T>), @CopyWith(nullable: true) using T? Function()?, @OverrideToString(multiline: true), deep collection equality by default, and serialization for DateTime, Uri, Duration, BigInt, Enum, List/Set/Iterable/Map, and nested annotated classes.
An Example
import 'package:analyzer_kit_annotation/analyzer_kit_annotation.dart';
const dataClass = CompositeAnnotation([
CopyWith(),
OverrideEquality(),
OverrideToString(),
Serialize(name: .toJson(), fieldRename: .snake),
Deserialize(name: .fromJson(), fieldRename: .snake),
]);
@dataClass
final class User(final String userId, @redact final String apiKey);
// Quick fix generates copyWith, ==, hashCode, toString (with [REDACTED]), toJson, and fromJson by automatically creating a class body and directly injects the generated code inside `User`.
Since the plugin generates code directly into the source file via quick fixes rather than maintaining .g.dart files, the trade-off is that you trigger a quick fix (or force regenerate the methods) when fields change, and in return you get zero build step and plain readable Dart in your class. Curious to hear what you think or if there are patterns you'd want supported next.