analyzer_buffer
0.3.4, released 3 months ago by dash-overflow.net
Helper for code generators to write code with import prefixes and default values
What it does
AnalyzerBuffer provides a buffer that understands import prefixes, default values, and type writing for code generators. It offers a template‑based syntax to reference types and constants from other files and automatically manages imports.
AI-written summary of version 0.3.4, based on the package's README and metadata. Report an inaccuracy
Use it when
- Writing source_gen generators that need to reference types or constants from other packages
- Generating code that must preserve import prefixes or copy default values
- Creating boilerplate code that requires type or constant conversion to source
- Automating import management in generated files
Skip it when
- Runtime code that does not involve code generation
- Scenarios where manual string concatenation is simpler than a buffer
- Projects that do not use the analyzer or source_gen
Strengths
- Supports import prefixes and automatic import insertion
- Copies default values and constants preserving prefixes
- Provides writeType and DartType.toCode for type conversion
- Custom string interpolation for dynamic content
Weaknesses
- Only useful for code generation, not runtime
- Depends on analyzer and source_gen, so not suitable for pure Dart projects
- Requires understanding of analyzer APIs
Setup
- Kind
- Pure Dart
- Effort
- Low
- Code generation
- Yes (build_runner)
- Direct dependencies
- 5
Add analyzer_buffer to pubspec, import it in your generator, and use AnalyzerBuffer.part(library.element) to start writing.
```dart
final buffer = AnalyzerBuffer.part(library.element);
buffer.write('''
class Example {
final #{{dart:async|StreamController}} controller;
final #{{riverpod|Provider}} provider;
}
''');
return buffer.toString();
```Alternatives
Packages that do the same job and could replace analyzer_buffer.
- build
Package for authoring build_runner compatible code generators.
87Compare - flutter_gen_runner
Generates strongly-typed Dart classes for Flutter assets, fonts, and colors to avoid string-based APIs.
87Compare - flutter_gen
A code generator for your assets, fonts, and colors to eliminate string-based APIs.
87Compare - source_helper
Utilities to help with Dart source code generation and string literal formatting.
81Compare
Works with
Packages commonly used together with analyzer_buffer: ones it builds on, ones built on it, and its own family.
- analyzeranalyzer_buffer depends on itPerforms static analysis of Dart code for tool integration and embedding.
- source_genanalyzer_buffer depends on itSource code generation builders and utilities for the Dart build system.
- build_runnerUsed togetherA build system for Dart code generation and modular compilation.
- freezedBuilt on analyzer_bufferCode generation for immutable classes, tagged unions, and cloning without boilerplate.
- riverpod_generatorBuilt on analyzer_bufferGenerates Riverpod providers from annotated functions using build_runner.