Skip to content

Releases: dart-lang/source_gen

v0.7.4+2

31 Jan 18:05
fffde65
Compare
Choose a tag to compare
  • BUG FIX: ConstantReader.revive() now properly returns no URL fragment
    when the constant expression is resolved from a top-level or static-field.
    The documentation had said otherwise, and it was impossible to tell the
    difference between a constructor and a field. Now, fields are always in
    the form of accessor = {clazz}.{field} or {topLevelField}.

  • Fix file URIs on windows.

v0.7.4+1

22 Jan 22:42
Compare
Choose a tag to compare
  • 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

18 Jan 22:50
5496758
Compare
Choose a tag to compare
  • Added typeNameOf, which is a safe way to get the name of a DartType,
    even when the type is a FunctionType, which has a null name in newer
    versions of the Dart analyzer.

  • Added LibraryReader.pathToUrl(Uri|String), which computes the import or
    export path necessary to reach the provided URL from the current library.
    Also added pathToAsset and pathToElement as convenience functions.

  • Expanded package:build support to allow version 0.12.0.

v0.7.0

26 Jul 15:54
Compare
Choose a tag to compare
  • Breaking changes: See the wiki
    for help upgrading.

    • Generator.generate now operates on a LibraryReader rather than being
      called for every Element within a library. Generators can iterate over
      elements using LibraryReader.allElements. GeneratorForAnnotation will
      continue to call generateForAnnotatedElement repeatedly for each element.
    • GeneratorForAnnotation passes in a ConstantReader for the annotation
      instance rather than re-creating it using mirrors.
    • GeneratorBuilder is replaced with PartBuilder and LibraryBuilder
      depending on whether the output is meant to be included in a part file.
    • Removed JsonSerializable and related classes. These are moved to
      package:json_serializable.
    • Removed lib/builder.dart. Import through source_gen.dart instead.
    • Removed OutputFormatter typedef.
  • Add LibraryReader.allElements - a utility to iterate across all Element
    instances contained in Dart library.

  • Add LibraryReader.element to get back to the LibraryElement instance.

  • Add ConstantReader.objectValue to get back to the DartObject instance.

  • Add ConstantReader.peek to read a value that returns null 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 to TypeChecker.annotationsOf,
    TypeChecker.annotationsOfExact, TypeChecker.firstAnnotationOf, and
    TypeChecker.firstAnnotationOfExact. Setting this to false 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 the library,
    which is more useful for tracking down the actual files.

v0.6.1+1

18 Jul 19:02
Compare
Choose a tag to compare
  • Added spanForElement; returns a SourceSpan for an analyzer Element.
  • Logs a warning to the console when a GeneratorBuilder outputs a part file
    for a given input, but that input does not define part 'name.g.dart';.

v0.6.0

06 Jul 17:56
Compare
Choose a tag to compare
  • Breaking change: TypeChecker#annotationsOf|firstAnnotationOf now
    returns annotations that are assignable to the TypeChecker's type. As a
    result we've added #annotationsOfExact|firstAnnotationOfExact which has the
    old behavior for precise checks.

  • TypeChecker#annotations...-methods now throw a StateError 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 other TypeChecker
    implementations when making a type check.

v0.5.9

23 Jun 16:43
Compare
Choose a tag to compare

0.5.9

  • Update the minimum Dart SDK to 1.22.1.

  • Deprecated builder.dart: import source_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 as part of can refer to a path.
    To opt-in, GeneratorBuilder now has a new flag, requireLibraryDirective.
    Set it to false, and also set your sdk constraint appropriately:

      sdk: '>=1.25.0 <2.0.0'
  • Added LibraryReader, a utility class for LibraryElement that exposes
    high-level APIs, including findType, which traverses export directives
    for publicly exported types. For example, to find Generator 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 by DartObject 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

01 Jun 17:55
Compare
Choose a tag to compare

Add formatOutput optional parameter to the GeneratorBuilder constructor. This is a lamda of the form String formatOutput(String originalCode) which allows you to do custom formatting.

0.5.4

27 Jan 21:53
Compare
Choose a tag to compare

Update to latest build, build_runner, and build_test releases.

Readme updates

01 Nov 19:55
Compare
Choose a tag to compare
v0.5.1+5