Skip to content
Open
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
40 changes: 20 additions & 20 deletions context_di_generator/lib/generator/feature_generator.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import 'package:analyzer/dart/element/element2.dart';
import 'package:analyzer/dart/element/element.dart';
import 'package:analyzer/dart/element/type.dart';
import 'package:build/build.dart';
import 'package:context_di/annotations.dart';
Expand All @@ -7,11 +7,11 @@ import 'package:source_gen/source_gen.dart';
class FeatureGenerator extends GeneratorForAnnotation<Feature> {
@override
String generateForAnnotatedElement(
Element2 element,
Element element,
ConstantReader annotation,
BuildStep buildStep,
) {
if (element is! ClassElement2) {
if (element is! ClassElement) {
throw InvalidGenerationSourceError(
'Feature annotation can only be applied to classes.',
);
Expand All @@ -21,13 +21,13 @@ class FeatureGenerator extends GeneratorForAnnotation<Feature> {
final topLevelFactories = StringBuffer();

if (annotation.read('generateFactoryTypes').boolValue) {
for (final annotation in element.metadata2.annotations) {
final type = annotation.element2?.enclosingElement2?.displayName;
for (final annotation in element.metadata.annotations) {
final type = annotation.element?.enclosingElement?.displayName;

if (type == 'Factory') {
final reader = ConstantReader(annotation.computeConstantValue());
final typeValue = reader.read('type').typeValue.getDisplayString();
final typeElement = reader.read('type').typeValue.element3 as ClassElement2;
final typeElement = reader.read('type').typeValue.element as ClassElement;
final paramsType = reader.peek('params')?.typeValue;
final paramsTypeValue = paramsType?.getDisplayString();

Expand All @@ -38,7 +38,7 @@ class FeatureGenerator extends GeneratorForAnnotation<Feature> {
topLevelFactories.writeln(");\n");
} else {
buffer.writeln("typedef Create$typeValue = $typeValue Function(BuildContext);\n");
if (typeElement.constructors2.first.formalParameters.isEmpty) {
if (typeElement.constructors.first.formalParameters.isEmpty) {
topLevelFactories.writeln("$typeValue create$typeValue(BuildContext context) => $typeValue();\n");
} else {
topLevelFactories.writeln("$typeValue create$typeValue(BuildContext context) => $typeValue(");
Expand All @@ -50,14 +50,14 @@ class FeatureGenerator extends GeneratorForAnnotation<Feature> {
}
}

buffer.writeln('mixin _\$${element.name3}Mixin on FeatureDependencies {');
buffer.writeln('mixin _\$${element.name}Mixin on FeatureDependencies {');
buffer.writeln(' @override');
buffer.writeln(' List<Registration> register() {');
buffer.writeln(' return [');

for (final annotation in element.metadata2.annotations) {
for (final annotation in element.metadata.annotations) {
final reader = ConstantReader(annotation.computeConstantValue());
final type = annotation.element2?.enclosingElement2?.displayName;
final type = annotation.element?.enclosingElement?.displayName;
if (type == null) continue;

switch (type) {
Expand All @@ -82,16 +82,16 @@ class FeatureGenerator extends GeneratorForAnnotation<Feature> {
void _generateSingletonRegistration(StringBuffer buffer, ConstantReader reader) {
final typeValue = reader.read('type').typeValue.getDisplayString();
final asValue = reader.peek('as')?.typeValue.getDisplayString();
final dispose = reader.peek('dispose')?.objectValue.toFunctionValue2()?.displayName;
final typeElement = reader.read('type').typeValue.element3 as ClassElement2;
final dispose = reader.peek('dispose')?.objectValue.toFunctionValue()?.displayName;
final typeElement = reader.read('type').typeValue.element as ClassElement;

if (asValue != null) {
buffer.writeln(' registerSingletonAs<$typeValue, $asValue>(');
} else {
buffer.writeln(' registerSingleton(');
}

if (typeElement.constructors2.first.formalParameters.isEmpty) {
if (typeElement.constructors.first.formalParameters.isEmpty) {
buffer.writeln(' (context) => $typeValue(),');
} else {
buffer.writeln(' (context) => $typeValue(');
Expand All @@ -108,7 +108,7 @@ class FeatureGenerator extends GeneratorForAnnotation<Feature> {
void _generateFactoryRegistration(StringBuffer buffer, ConstantReader reader) {
final typeValue = reader.read('type').typeValue.getDisplayString();
final paramsTypeValue = reader.peek('params')?.typeValue;
final typeElement = reader.read('type').typeValue.element3 as ClassElement2;
final typeElement = reader.read('type').typeValue.element as ClassElement;

if (paramsTypeValue != null) {
buffer.writeln(' registerParamsFactory(');
Expand All @@ -121,7 +121,7 @@ class FeatureGenerator extends GeneratorForAnnotation<Feature> {

buffer.writeln(' registerFactory(');

if (typeElement.constructors2.first.formalParameters.isEmpty) {
if (typeElement.constructors.first.formalParameters.isEmpty) {
buffer.writeln(' (context) => $typeValue(),');
} else {
buffer.writeln(' (context) => $typeValue(');
Expand All @@ -132,14 +132,14 @@ class FeatureGenerator extends GeneratorForAnnotation<Feature> {
buffer.writeln(' ),');
}

String _generateConstructorParams(ClassElement2 element, String typeValue, [DartType? paramsTypeValue]) {
String _generateConstructorParams(ClassElement element, String typeValue, [DartType? paramsTypeValue]) {
final constructorName = typeValue.split('.').last;
final constructor = element.constructors2
final constructor = element.constructors
.where(
(c) => c.displayName == constructorName,
)
.firstOrNull ??
element.constructors2.first;
element.constructors.first;

final paramsFields = paramsTypeValue?.extractParamsFields();

Expand All @@ -148,7 +148,7 @@ class FeatureGenerator extends GeneratorForAnnotation<Feature> {
}

final params = constructor.formalParameters.map((param) {
if (param.type.element3?.displayName == 'BuildContext') return ifNamed(param, 'context');
if (param.type.element?.displayName == 'BuildContext') return ifNamed(param, 'context');

if (paramsFields != null) {
final paramName = param.displayName;
Expand All @@ -172,7 +172,7 @@ class FeatureGenerator extends GeneratorForAnnotation<Feature> {
extension _ExtractParamsFields on DartType {
Set<String> extractParamsFields() => switch (this) {
RecordType(:final namedFields) => namedFields.map((e) => e.name).toSet(),
InterfaceType(:final element3) when element3 is ClassElement2 => element3.fields2.map((e) => e.displayName).toSet(),
InterfaceType(:final element) when element is ClassElement => element.fields.map((e) => e.displayName).toSet(),
_ => throw Exception('Unsupported params type \$this can be a class or record type, not: \${this.element}')
};
}
4 changes: 2 additions & 2 deletions context_di_generator/pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name: context_di_generator
description: "Flutter Provider based Dependency Injection Tool Generator"
version: 0.2.5
version: 0.2.6
homepage: https://github.com/begekoth/context_di

environment:
Expand All @@ -11,4 +11,4 @@ dependencies:
context_di: ^0.2.5
source_gen: '>=3.1.0 <5.0.0'
build: '>=3.0.0 <5.0.0'
analyzer: ">=7.4.0 <8.0.0"
analyzer: ^9.0.0