Releases: dart-lang/source_gen
v0.7.4+1
- Removed a
log.finest
with the output source of each generator. This allows
a verbose option (-v
) for tools like bazel or build_runner to be much more
readable and debuggable. Files are emitted to disk for inspection in any
case.
v0.7.4
-
Added
typeNameOf
, which is a safe way to get the name of aDartType
,
even when the type is aFunctionType
, which has anull
name in newer
versions of the Dart analyzer. -
Added
LibraryReader.pathToUrl(Uri|String)
, which computes theimport
or
export
path necessary to reach the provided URL from the current library.
Also addedpathToAsset
andpathToElement
as convenience functions. -
Expanded
package:build
support to allow version0.12.0
.
v0.7.0
-
Breaking changes: See the wiki
for help upgrading.Generator.generate
now operates on aLibraryReader
rather than being
called for everyElement
within a library. Generators can iterate over
elements usingLibraryReader.allElements
.GeneratorForAnnotation
will
continue to callgenerateForAnnotatedElement
repeatedly for each element.GeneratorForAnnotation
passes in aConstantReader
for the annotation
instance rather than re-creating it using mirrors.GeneratorBuilder
is replaced withPartBuilder
andLibraryBuilder
depending on whether the output is meant to be included in apart
file.- Removed
JsonSerializable
and related classes. These are moved to
package:json_serializable
. - Removed
lib/builder.dart
. Import throughsource_gen.dart
instead. - Removed
OutputFormatter
typedef.
-
Add
LibraryReader.allElements
- a utility to iterate across allElement
instances contained in Dart library. -
Add
LibraryReader.element
to get back to theLibraryElement
instance. -
Add
ConstantReader.objectValue
to get back to theDartObject
instance. -
Add
ConstantReader.peek
to read a value that returnsnull
if not found:
// Tries to read the field "token" first, then "_token".
findTokenField(DartObject o) {
final reader = new ConstantReader(o);
final token = o.peek('token') ?? o.read('_token');
}
- Add
throwOnUnresolved
optional parameter toTypeChecker.annotationsOf
,
TypeChecker.annotationsOfExact
,TypeChecker.firstAnnotationOf
, and
TypeChecker.firstAnnotationOfExact
. Setting this tofalse
will enable you
to check for matching annotations with incomplete type information (at your
own risk). - Builder logs now log the primary inputs
AssetId
instead of thelibrary
,
which is more useful for tracking down the actual files.
v0.6.1+1
v0.6.0
-
Breaking change:
TypeChecker#annotationsOf|firstAnnotationOf
now
returns annotations that are assignable to theTypeChecker
's type. As a
result we've added#annotationsOfExact|firstAnnotationOfExact
which has the
old behavior for precise checks. -
TypeChecker#annotations...
-methods now throw aStateError
if one or more
annotations on an element are not resolvable. This is usually a sign of a
mispelling, missing import, or missing dependency. -
Added
TypeChecker.any
, which delegates to multiple otherTypeChecker
implementations when making a type check.
v0.5.9
0.5.9
-
Update the minimum Dart SDK to
1.22.1
. -
Deprecated
builder.dart
: importsource_gen.dart
instead. -
Added
TypeChecker
, a high-level API for performing static type checks:import 'package:analyzer/dart/element/type.dart'; import 'package:source_gen/source_gen.dart'; void checkType(DartType dartType) { // Checks compared to runtime type `SomeClass`. print(const TypeChecker.forRuntime(SomeClass).isExactlyType(dartType)); // Checks compared to a known Url/Symbol: const TypeChecker.forUrl('package:foo/foo.dart#SomeClass'); // Checks compared to another resolved `DartType`: const TypeChecker.forStatic(anotherDartType); }
-
Failing to add a
library
directive to a library that is being used as a
generator target that generates partial files (part of
) is now an explicit
error that gives a hint on how to name and fix your library:> Could not find library identifier so a "part of" cannot be built. > > Consider adding the following to your source file: > > "library foo.bar;"
In Dart SDK
>=1.25.0
this can be relaxed aspart of
can refer to a path.
To opt-in,GeneratorBuilder
now has a new flag,requireLibraryDirective
.
Set it tofalse
, and also set yoursdk
constraint appropriately:sdk: '>=1.25.0 <2.0.0'
-
Added
LibraryReader
, a utility class forLibraryElement
that exposes
high-level APIs, includingfindType
, which traversesexport
directives
for publicly exported types. For example, to findGenerator
from
package:source_gen/source_gen.dart
:void example(LibraryElement pkgSourceGen) { var library = new LibraryReader(pkgSourceGen); // Instead of pkgSourceGen.getType('Generator'), which is null. library.findType('Generator'); }
-
Added
ConstantReader
, a high-level API for reading from constant (static)
values from Dart source code (usually represented byDartObject
from the
analyzer
package):abstract class ConstantReader { factory ConstantReader(DartObject object) => ... // Other methods and properties also exist. /// Reads[ field] from the constant as another constant value. ConstantReader read(String field); /// Reads [field] from the constant as a boolean. /// /// If the resulting value is `null`, uses [defaultTo] if defined. bool readBool(String field, {bool defaultTo()}); /// Reads [field] from the constant as an int. /// /// If the resulting value is `null`, uses [defaultTo] if defined. int readInt(String field, {int defaultTo()}); /// Reads [field] from the constant as a string. /// /// If the resulting value is `null`, uses [defaultTo] if defined. String readString(String field, {String defaultTo()}); }
0.5.8
0.5.4
Readme updates
v0.5.1+5