Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 8 additions & 6 deletions .github/workflows/dart.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name: Dart CI

env:
DART_VERSION: "3.6.2"
DART_VERSION: "3.8.3"

on:
push:
Expand All @@ -14,7 +14,7 @@ jobs:
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v3
- uses: actions/checkout@v5
- uses: dart-lang/setup-dart@v1
with:
sdk: ${{ env.DART_VERSION }}
Expand All @@ -30,8 +30,10 @@ jobs:
run: dart format -o none --set-exit-if-changed .
- name: dart analyze
run: dart analyze --fatal-infos --fatal-warnings .
- name: activate dependency_validator
run: dart pub global activate dependency_validator
- name: dependency_validator
run: dart run dependency_validator
run: dart pub global run dependency_validator
- name: dart pub publish --dry-run
run: dart pub publish --dry-run

Expand All @@ -40,7 +42,7 @@ jobs:
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v3
- uses: actions/checkout@v5
- uses: dart-lang/setup-dart@v1
with:
sdk: ${{ env.DART_VERSION }}
Expand All @@ -59,7 +61,7 @@ jobs:
dart pub global activate coverage
dart pub global run coverage:format_coverage --packages=.dart_tool/package_config.json --report-on=lib --lcov -o ./coverage/lcov.info -i ./coverage
- name: Upload coverage to Codecov
uses: codecov/codecov-action@v3
uses: codecov/codecov-action@v5
env:
CODECOV_TOKEN: ${{ secrets.CODECOV_TOKEN }}
with:
Expand All @@ -74,7 +76,7 @@ jobs:
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v3
- uses: actions/checkout@v5
- uses: dart-lang/setup-dart@v1
- name: Dart version
run: |
Expand Down
19 changes: 19 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,22 @@
## 2.6.0

- Migrate to `analyzer: ^8.1.1` and `build: ^3.0.2`.

- build: ^3.0.2
- analyzer: ^8.1.1
- dart_style: ^3.1.2
- mime: ^2.0.0
- collection: ^1.19.1
- logging: ^1.3.0

- build_runner: ^2.7.0
- build_test: ^3.3.2
- lints: ^6.0.0

- Comment `#dependency_validator: ^5.0.2` (incompatible with `analyzer: ^8.1.1`).

- Dart CI: `DART_VERSION: "3.8.3"`

## 2.5.3

- `ReflectionBuilder`: improve use of `const` for generated code.
Expand Down
4 changes: 2 additions & 2 deletions example/reflection_factory_bridge_example.reflection.g.dart

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions example/reflection_factory_example.reflection.g.dart

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

43 changes: 28 additions & 15 deletions lib/src/analyzer/input_analyzer.dart
Original file line number Diff line number Diff line change
Expand Up @@ -251,14 +251,21 @@ class InputAnalyzerResolved {

if (result is LibraryElementResult) {
var libraryElement = result.element;
if (libraryElement.classes.isEmpty) {
libraryElement = await _resolveLibraryElement(libraryElement);
}

mainLibraries.add(libraryElement);
}
}

if (libraryName.isNotEmpty) {
var library = await resolver.findLibraryByName(libraryName);
if (library != null) {
mainLibraries.add(library);
var libraryElement = await resolver.findLibraryByName(libraryName);
if (libraryElement != null) {
if (libraryElement.classes.isEmpty) {
libraryElement = await _resolveLibraryElement(libraryElement);
}
mainLibraries.add(libraryElement);
}
}

Expand Down Expand Up @@ -287,6 +294,16 @@ class InputAnalyzerResolved {
return candidateClasses;
}

// Attempt to load a `LibraryElement` with sub-elements resolved:
Future<LibraryElement> _resolveLibraryElement(
LibraryElement libraryElement) async {
var libraryAssetId = await resolver.assetIdForElement(libraryElement);
var libraryElementResolved = await resolver.libraryFor(
libraryAssetId,
);
return libraryElementResolved;
}

Future<bool> hasCodeToGenerate() async {
final libraryReader = await this.libraryReader;

Expand Down Expand Up @@ -353,13 +370,13 @@ class InputAnalyzerResolved {

throw StateError(
"Code generated but NO reflection part directive was found for input file: $inputId\n"
" > Can't generate ONE of the output files:\n"
" » Can't generate ONE of the output files:\n"
" -- $genSiblingId\n"
" -- $genSubId\n"
" > Please ADD one of the directives below to the input file:\n"
" » Please ADD one of the directives below to the input file:\n"
" part '${outputsPaths.siblingPath}';\n"
" part '${outputsPaths.subPath}';\n"
" > Found part directives:\n"
" » Found part directives:\n"
"${gParts.map((e) => ' -- $e').join('\n')}\n");
}

Expand Down Expand Up @@ -440,16 +457,13 @@ extension _LibraryElementExtension on LibraryElement {

List<ClassElement> get exportedClasses =>
_exportedClasses[this] ??= UnmodifiableListView(
topLevelElements.whereType<ClassElement>().toList(growable: false));
fragments.expand((e) => e.classes.map((c) => c.element)));

static final Expando<List<LibraryElement>> _allExports =
Expando<List<LibraryElement>>();

List<LibraryElement> get allExports => _allExports[this] ??=
UnmodifiableListView(definingCompilationUnit.libraryExports
.map((e) => e.exportedLibrary)
.nonNulls
.toList(growable: false));
UnmodifiableListView(exportedLibraries.nonNulls.toList(growable: false));

static final Expando<Set<ClassElement>> _allExportedClasses =
Expando<Set<ClassElement>>();
Expand All @@ -461,17 +475,16 @@ extension _LibraryElementExtension on LibraryElement {
Expando<Set<ClassElement>>();

Set<ClassElement> get allImportedClasses =>
_allImportedClasses[this] ??= UnmodifiableSetView(units
.expand((e) =>
e.library.importedLibraries.expand((e) => e.exportedClasses))
_allImportedClasses[this] ??= UnmodifiableSetView(fragments
.expand((e) => e.importedLibraries.expand((e) => e.exportedClasses))
.toSet());

static final Expando<Set<ClassElement>> _allUnitsClasses =
Expando<Set<ClassElement>>();

Set<ClassElement> get allUnitsClasses =>
_allUnitsClasses[this] ??= UnmodifiableSetView(
units.expand((e) => e.library.exportedClasses).toSet());
fragments.expand((e) => e.classes.map((c) => c.element)).toSet());

static final Expando<Set<ClassElement>> _allClassesFromExportedClassesUnits =
Expando<Set<ClassElement>>();
Expand Down
48 changes: 42 additions & 6 deletions lib/src/analyzer/library.dart
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,40 @@ class AnnotatedElement {
const AnnotatedElement(this.annotation, this.element);
}

extension LibraryElementExtension on LibraryElement {
String get libraryName {
var name = this.name;
if (name != null && name.isNotEmpty) return name;

var uri = firstFragment.source.uri;
return uri.toString();
}

Iterable<Fragment> get topLevelFragments sync* {
for (var unit in fragments) {
yield* unit.classes;
yield* unit.enums;
yield* unit.extensions;
yield* unit.extensionTypes;
yield* unit.functions;
yield* unit.mixins;
yield* unit.topLevelVariables;
yield* unit.typeAliases;
}
}

Iterable<Element> get topLevelElements =>
topLevelFragments.map((e) => e.element);
}

class LibraryReader {
final LibraryElement element;

LibraryReader(this.element);

/// All the compilation unit of this element ([CompilationUnitElement]).
Iterable<PartElement> get allParts => element.definingCompilationUnit.parts;
Iterable<PartInclude> get allParts =>
element.fragments.expand((e) => e.partIncludes);

/// All of the declarations in this library.
Iterable<Element> get allElements => element.topLevelElements;
Expand Down Expand Up @@ -57,7 +84,7 @@ class LibraryReader {
elements = allElements;
}

return elements.where((e) => e.metadata.isNotEmpty);
return elements.where((e) => e.metadata.annotations.isNotEmpty);
}

/// All of the declarations in this library annotated with [checker].
Expand All @@ -67,8 +94,8 @@ class LibraryReader {

/// All of the elements names in this library
/// (classes, enums, mixins, functions, extensions, typeAliases, topLevelVariables).
Iterable<String> get elementsNames => element.units
.expand((CompilationUnitElement cu) => <String?>[
Iterable<String> get elementsNames => element.fragments
.expand((cu) => <String?>[
...cu.classes.map((e) => e.name),
...cu.enums.map((e) => e.name),
...cu.mixins.map((e) => e.name),
Expand All @@ -81,10 +108,11 @@ class LibraryReader {

/// All of the elements representing classes in this library.
Iterable<ClassElement> get classes =>
element.units.expand((CompilationUnitElement cu) => cu.classes);
element.fragments.expand((cu) => cu.classes.map((c) => c.element));

/// All of the elements representing enums in this library.
Iterable<EnumElement> get enums => element.units.expand((cu) => cu.enums);
Iterable<EnumElement> get enums =>
element.fragments.expand((cu) => cu.enums.map((c) => c.element));
}

extension ElementExtension on Element {
Expand Down Expand Up @@ -116,3 +144,11 @@ extension IterableElementExtension on Iterable<Element> {
where((e) =>
e.isAnnotatedWith(checker, throwOnUnresolved: throwOnUnresolved));
}

extension ConstructorElementExtension on ConstructorElement {
String get codeName {
var name = this.name;
if (name == null || name == 'new') return '';
return name;
}
}
12 changes: 7 additions & 5 deletions lib/src/analyzer/type_checker.dart
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ abstract class TypeChecker {
Element element, {
bool throwOnUnresolved = true,
}) {
if (element.metadata.isEmpty) {
if (element.metadata.annotations.isEmpty) {
return null;
}
final results =
Expand All @@ -70,7 +70,7 @@ abstract class TypeChecker {
Element element, {
bool throwOnUnresolved = true,
}) {
if (element.metadata.isEmpty) {
if (element.metadata.annotations.isEmpty) {
return null;
}
final results =
Expand All @@ -91,7 +91,7 @@ abstract class TypeChecker {
int annotationIndex, {
bool throwOnUnresolved = true,
}) {
final annotation = element.metadata[annotationIndex];
final annotation = element.metadata.annotations[annotationIndex];
final result = annotation.computeConstantValue();

if (result == null && throwOnUnresolved) {
Expand Down Expand Up @@ -119,7 +119,8 @@ abstract class TypeChecker {
bool Function(DartType) predicate, {
bool throwOnUnresolved = true,
}) sync* {
for (var i = 0; i < element.metadata.length; i++) {
final annotations = element.metadata.annotations;
for (var i = 0; i < annotations.length; i++) {
final value = _computeConstantValue(
element,
i,
Expand Down Expand Up @@ -274,7 +275,8 @@ class UnresolvedAnnotationException implements Exception {
final parsedLibrary = annotatedElement.session!
.getParsedLibraryByElement(annotatedElement.library!)
as ParsedLibraryResult;
final declaration = parsedLibrary.getElementDeclaration(annotatedElement);
final declaration =
parsedLibrary.getFragmentDeclaration(annotatedElement.firstFragment);
if (declaration == null) {
return null;
}
Expand Down
2 changes: 1 addition & 1 deletion lib/src/analyzer/utils.dart
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ String computePartUrl(AssetId input, AssetId output) =>
String urlOfElement(Element element) => element.kind == ElementKind.DYNAMIC
? 'dart:core#dynamic'
// using librarySource.uri – in case the element is in a part
: normalizeUrl(element.librarySource!.uri)
: normalizeUrl(element.library!.uri)
.replace(fragment: element.name)
.toString();

Expand Down
2 changes: 1 addition & 1 deletion lib/src/reflection_factory_base.dart
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import 'reflection_factory_utils.dart';
/// Class with all registered reflections ([ClassReflection]).
class ReflectionFactory {
// ignore: constant_identifier_names
static const String VERSION = '2.5.3';
static const String VERSION = '2.6.0';

static final ReflectionFactory _instance = ReflectionFactory._();

Expand Down
Loading