pub repository
mirror.p-hatan.iranalyzer_buffer
0.3.2A package to help writing code-generators while supporting import prefixes and default values
Readme
Welcome to AnalyzerBuffer!
AnalyzerBuffer is a package designed for code-generator authors. It helps solving common complex tasks, such as:
- Gracefully supporting import prefixes (
import ... as prefix) - Copy-pasting default values to the generated code, whatever they are
- Writing types while preserving typedefs
- Automatically adding imports (if writing to a file where imports can be added)
All while offering a simple template-based syntax.
TL;DR:
// A typical "Generator" from source_gen
class MyBuilder extends GeneratorForAnnotation<AnnotationT> {
@override
Future<String> generate(
LibraryReader library,
BuildStep buildStep,
) async {
// We create a 'AnalyzerBuffer'
final buffer = AnalyzerBuffer.part(library.element);
// We write some code in the buffer
// We use #{{package:packageName/file.dart|Class}} inside the source
// to reference objects defined in other files
buffer.write('''
class Example {
// Dart package imports
final #{{dart:async|StreamController}} controller;
// Third-party package imports:
final #{{package:riverpod/riverpod.dart|Provider}} provider;
// Simplified syntax for packages:
final #{{riverpod|Provider}} provider;
}
''');
// We convert the buffer into a String
return buffer.toString();
}
}
Using this syntax, generated code will automatically use import prefixes as defined by the user.
See AnalyzerBuffer for the full list of methods available.
Dynamic content and loops
AnalyzerBuffer.write supports a custom String interpolation, for the sake of
passing non-static content.
The syntax is #{{name}} (without a package name), followed by passing
an args map:
buffer.write(args: {
'DOCS': () {
for (var i = 0; i < 10; i++)
buffer.write('// Hello $i\n');
}
}, '''
class Example {
#{{DOCS}}
}
''');
Writing DartTypes
AnalyzerBuffer offers a writeType method.
This method enables writing a DartType from analyzer directly into the generated code:
ClassElement element;
buffer.writeType(element.thisType);
Alternatively, you can convert any DartType into its #{{uri|name}} representation
using DartType.toCode:
ClassElement element;
buffer.write('''
${element.thisType.toCode()}
''');
Writing default values
A common pattern with code-generators is to have to "copy-paste" default values from user code into generated code.
For example, a user may define:
@riverpod
List<Item> fetch(Ref ref, {
int pageSize = 10,
}) {}
And generated code may want to generate copy-paste that = 10 somewhere
in the generated code, while preserving import prefixes but also
supporting enums and such.
To solve this, AnalyzerBuffer offers DartObject.toCode(), to convert
any constant value into its #{{uri|name}} representation.
A typical usage is:
ParameterElement parameter;
// Obtain the constant for the default value of a parameter
final obj = parameter.computeConstantValue();
// We generate code with the same default value
buffer.write('''
void example({
int pageSize = ${obj.toCode()}
}) {}
''')
Changelog
0.3.2 - 2026-04-23
Support analyzer 12.0.0
0.3.1 - 2026-02-03
Support analyzer 10.0.0
0.3.0 - 2026-01-06
- Removed deprecated
writeType #{{myPackage|MyClass}}now matches with classes defined in files such aspackage:myPackage/src/file.dart. If you don't want this, explicitly define the expected path in the code, such as#{{myPackage/src/another|MyClass}}
0.2.0
Support analyzer 9.0.0
0.1.12 - 2025-11-03
- Fixed a bug when two imports contain classes with the same name.
- Deprecated
writeTypein favour ofDartType.toCode+write
0.1.11 - 2025-09-09
Updated dependencies to support analyzer v8 This allows for compatibility with upgraded generators using analyzer v8 and source_gen v4 (thanks to @TekExplorer)
analyzer: '>=7.3.0 <9.0.0'source_gen: '>=3.0.0 <5.0.0'
0.1.10 - 2025-08-26
- Fix some issue with
functionType.toCodenot includingrequired(thanks to @TekExplorer)
0.1.9 - 2025-08-11
- Fix some bug with indirectly imported objects.
0.1.8 - 2025-08-11
- Fix
DartType.toCodewith nullable types
0.1.7 - 2025-08-11
- Fixed
DartType.toCodewith typedefs, records, and many other types.
0.1.6 - 2025-08-10
- Fixed a bug when importing nested files from other packages
0.1.5 - 2025-07-22
- Fix
DartType.toCodewith dynamic/invalid types.
0.1.4 - 2025-07-22
- Fix
writeTypewith dynamic/invalid types.
0.1.3 - 2025-07-21
- Fix a stackoverflow when using cyclic import/export
0.1.2 - 2025-07-21
- Fixed auto-import incorrectly using the same prefix for all added imports.
0.1.1 - 2025-07-21
Upgrade dependencies
0.1.0 - 2025-07-21
- Removed
fromLibraryin favour ofpart/part2. - Fixed various issues with prefixes
0.0.7 - 2025-07-09
chore: Downgraded meta
0.0.6 - 2025-07-09
fix: buffer.writeType no-longer imports the same package multiple times.
0.0.5 - 2025-07-07
breaking: AnalyzerBuffer constructors now take a mandatory sourcePath parameter.
It is necessary for certain edge-cases around types/defaults.
fix: Fixes an issue where AnalyzerBuffer could not be applied to test folders.
0.0.4
fix: correctly use prefix when an import is re-exporting an element used by generated code.
0.0.3
fix: buffer.write now correctly respects import prefixes if created using AnalyzerBuffer.fromLibrary
0.0.2
fix: buffer.toString now returns '' if the buffer is empty.
feat: added buffer.isEmpty
0.0.1
Initial release
Installing
export PUB_HOSTED_URL=https://pub.mirror.p-hatan.ir
flutter pub add analyzer_bufferpubspec.yaml
name: analyzer_buffer
version: 0.3.2
description: A package to help writing code-generators while supporting import prefixes and default values
repository: https://github.com/rrousselGit/analyzer_buffer
environment:
sdk: ^3.6.0
resolution: workspace
dependencies:
analyzer: ">=10.0.0 <13.0.0"
collection: ^1.19.1
meta: ^1.15.0
path: ^1.9.1
source_gen: ^4.0.0
dev_dependencies:
test: ^1.25.15
Version history
| Version | Published | Archive | SHA-256 |
|---|---|---|---|
| 0.0.1 | 2025-07-04T11:13:19.474987Z | Download | d32c7465406f78e9207b6d75844eaf302b33f29c502ddf96ecb4491640e8f91d |
| 0.0.2 | 2025-07-04T12:46:13.311452Z | Download | 24f734daf16f3b00fe2f95b87eab84d3f9fb0545611e0797aa518352d5185c8e |
| 0.0.3 | 2025-07-04T15:05:04.451765Z | Download | 0f13c41d9e17dbd3bf5168af46cba0fc6c26653291c1100a99e76df6075f8229 |
| 0.0.4 | 2025-07-04T16:59:16.133766Z | Download | 6ae60db0ac174e3789b1bb36a1ff5412edd3fe4d4466a3dcce22cd941bbe25b6 |
| 0.0.5 | 2025-07-07T09:36:27.027849Z | Download | f839e03fffcef1f305776863d2b7f50210cf82d7be6691b75b37bbd4a92495d9 |
| 0.0.6 | 2025-07-09T07:17:23.616580Z | Download | 9b97cc8d55c3fda0cee2b1314d2b53e142dd1bcaed3fb74104cb968e29b4b11b |
| 0.0.7 | 2025-07-09T07:40:33.178474Z | Download | 24af961dc96d980d284e3fa18dc9a277df7ab0cc5f8682a57937a45e12195d36 |
| 0.1.0 | 2025-07-21T11:18:24.550378Z | Download | 7b85859d761d926c6aab35aff6c3aadecbfd4b3a586b93b83adc554d08731179 |
| 0.1.1 | 2025-07-21T11:22:16.290676Z | Download | 721d411da309e401c1bf023a1230937f3a8be42c46d16f427d8b96358c682dbf |
| 0.1.2 | 2025-07-21T12:27:24.401259Z | Download | 37c3a450df2b95869001d8737181304db2e68f9ba831fc0bba884abb20547a99 |
| 0.1.3 | 2025-07-21T16:55:39.230420Z | Download | d39e2f427b68949654ae7233686fc7d1bf52987d24e12464c206b530f784c4ca |
| 0.1.4 | 2025-07-22T17:42:34.889489Z | Download | 84e2f45038087835309a406b3db92f7ee3fe4a40464b68b96c6f2954c9bfb591 |
| 0.1.5 | 2025-07-22T18:02:41.252425Z | Download | 6d9a3c970a9fbf673470cfa6ec76bbfd11bf5eb0bc2b8ab00fea304d6d985d8f |
| 0.1.6 | 2025-08-10T20:05:21.650677Z | Download | 946c01d21a7fc26a77f8cfb9fde6c521c82c7a7bf56cb6643fb858816b846025 |
| 0.1.7 | 2025-08-11T10:00:43.739860Z | Download | a83cacea4c88fbbefc331a58eee91393e87ca1debec1a910a29316b0f408d7f7 |
| 0.1.8 | 2025-08-11T11:49:33.993954Z | Download | e1b43c6b23bd8ea1ff9bb17a13f5cb20f5a891a4bd324492b9cd0434ffa9a4f5 |
| 0.1.9 | 2025-08-11T17:49:59.291546Z | Download | 5c99913875693c566e402384885ee43bb1b6a68a2f54dd5901d3d28c397e9ffe |
| 0.1.10 | 2025-08-26T13:30:25.719419Z | Download | f7833bee67c03c37241c67f8741b17cc501b69d9758df7a5a4a13ed6c947be43 |
| 0.1.11 | 2025-09-09T12:57:34.106711Z | Download | aba2f75e63b3135fd1efaa8b6abefe1aa6e41b6bd9806221620fa48f98156033 |
| 0.1.12 | 2025-11-03T14:31:10.050456Z | Download | bfc88ddd7b0c66c30149f2afedfae59dc3c209990e0800b7551470caa271c90a |
| 0.2.0 | 2025-12-30T19:28:52.276806Z | Download | bee9ac548ea67d4dbe5a13498c4a19365281adabf0a0b71ada19e6792c137178 |
| 0.3.0 | 2026-01-06T15:29:16.520549Z | Download | ff4bd291778c7417fe53fe24ee0d0a1f1ffe281a2d4ea887e7094f16e36eace7 |
| 0.3.1 | 2026-02-03T14:45:09.341969Z | Download | 5fcd06b0715ebeee99f03e3f437b3412249969d8d12b191ea8a1d76e42a4e4a1 |
| 0.3.2 | 2026-04-23T06:59:10.369590Z | Download | bf559bc54530827a92cc4d9ee340fc76b4f17f386218c2b9e7cd33ed468a7e4d |
| 0.3.3 | 2026-06-11T13:12:22.375425Z | Download | 445b77e2054fa3e8c8a8ef1b5e9e6b23bb8028fffd34b5e60eaef315b7750674 |
| 0.3.4 | 2026-08-29T17:39:56.729461Z | Download | f890a2e708e38fbf4238ad2f9fd3d506538acdddb0cd8eb2c3e8c7bd17784b57 |
Dependencies
analyzer "\u003e=10.0.0 \u003c13.0.0"
collection "^1.19.1"
meta "^1.15.0"
path "^1.9.1"
source_gen "^4.0.0"