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
5 changes: 4 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,7 @@
.pub/
build/

pubspec_overrides.yaml
pubspec_overrides.yaml

# ASDF
.tool-versions
29 changes: 15 additions & 14 deletions functional_data_generator/lib/builder.dart
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import 'dart:io';

import 'package:analyzer/dart/ast/ast.dart';
import 'package:analyzer/dart/element/element.dart';
import 'package:analyzer/dart/element/element2.dart';
import 'package:build/build.dart';
import 'package:collection/collection.dart';
import 'package:functional_data/functional_data.dart';
Expand All @@ -12,13 +12,13 @@ Builder functionalData(BuilderOptions options) => SharedPartBuilder([FunctionalD

class FunctionalDataGenerator extends GeneratorForAnnotation<FunctionalData> {
@override
Future<String> generateForAnnotatedElement(Element element, ConstantReader annotation, BuildStep buildStep) =>
Future<String> generateForAnnotatedElement(Element2 element, ConstantReader annotation, BuildStep buildStep) =>
_generateDataType(element, annotation, buildStep);
}

String? _getCustomEquality(List<ElementAnnotation> annotations) {
final annotation = annotations.firstWhereOrNull(
(a) => a.computeConstantValue()?.type?.getDisplayString(withNullability: false) == 'CustomEquality');
final annotation =
annotations.firstWhereOrNull((a) => a.computeConstantValue()?.type?.getDisplayString() == 'CustomEquality');
if (annotation != null) {
final source = annotation.toSource();
final customEquality = source.substring('@CustomEquality('.length, source.length - 1).replaceAll('?', '');
Expand All @@ -39,8 +39,8 @@ class Pair<T, S> {
final S second;
}

Future<String> _generateDataType(Element element, ConstantReader annotation, BuildStep buildStep) async {
if (element is! ClassElement) {
Future<String> _generateDataType(Element2 element, ConstantReader annotation, BuildStep buildStep) async {
if (element is! ClassElement2) {
throw Exception('FunctionalData annotation must only be used on classes');
}

Expand All @@ -63,25 +63,26 @@ Future<String> _generateDataType(Element element, ConstantReader annotation, Bui
throw Exception('[$element]: generateLenses requires copyWith to be generated');
}

final className = element.name.replaceAll('\$', '');
final className = (element.name3 ?? '').replaceAll('\$', '');

final classElement = element;

// The one that can be used to specify every field
final genericConstructor = classElement.constructors.firstWhere((element) => element.name.isEmpty);
final positionalFields = genericConstructor.parameters.where((p) => p.isPositional).map((p) => p.name).toList();
final genericConstructor = classElement.constructors2.firstWhere((element) => element.name3 == 'new');
final positionalFields =
genericConstructor.formalParameters.where((p) => p.isPositional).map((p) => p.name3).toList();

final fieldsWithIndex =
await Future.wait(classElement.fields.where((f) => !f.isSynthetic && !f.isStatic).map((f) async {
final declaration = await buildStep.resolver.astNodeFor(f) as VariableDeclaration?;
await Future.wait(classElement.fields2.where((f) => !f.isSynthetic && !f.isStatic).map((f) async {
final declaration = await buildStep.resolver.astNodeFor(f.firstFragment) as VariableDeclaration?;
final declarationList = declaration?.parent as VariableDeclarationList?;
final positionalIndex = positionalFields.indexOf(f.name);
final positionalIndex = positionalFields.indexOf(f.name3);
return Pair(
positionalIndex == -1 ? 9999 : positionalIndex,
Field(
f.name,
f.name3 ?? '',
declarationList?.type?.toSource() ?? 'dynamic',
_getCustomEquality(f.metadata),
_getCustomEquality(f.metadata2.annotations),
isPositional: positionalIndex != -1,
),
);
Expand Down
10 changes: 5 additions & 5 deletions functional_data_generator/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,13 @@ environment:
sdk: '>=2.19.0 <4.0.0'

dependencies:
analyzer: '>=2.0.0 <7.0.0'
build: ^2.0.1
analyzer: '^7.4.0'
build: ^3.0.0
collection: ^1.15.0
functional_data: ^1.1.1
source_gen: ^1.2.7
source_gen: ^3.1.0
yaml: ^3.1.2

dev_dependencies:
build_resolvers: "^2.0.2"
build_runner: ^2.2.0
build_resolvers: "^3.0.2"
build_runner: ^2.2.0
4 changes: 2 additions & 2 deletions pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name: functional_data_workspace

environment:
sdk: '>=2.19.0 <3.0.0'
sdk: '>=2.19.0 <4.0.0'
dev_dependencies:
melos: ^5.3.0
melos: ^6.3.0