github dart-lang/source_gen v0.5.9

latest releases: source_gen-v0.9.10+5, source_gen-v1.2.1, source_gen-v1.2.0...
6 years ago

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()});
    }

Don't miss a new source_gen release

NewReleases is sending notifications on new releases.