From d70e7bc16716513998876e0983a31bab37487c2d Mon Sep 17 00:00:00 2001 From: Felix Angelov Date: Sun, 26 May 2024 19:34:57 -0500 Subject: [PATCH 1/3] fix: exclude static fields --- lib/src/constructable_macro.dart | 3 +++ lib/src/macro_extensions.dart | 4 ++-- lib/src/stringable_macro.dart | 6 ++++-- .../static_field_class_test.dart | 15 +++++++++++++++ test/src/constructable_macro_test.dart | 3 +++ .../static_field_class_test.dart | 16 ++++++++++++++++ test/src/copyable_macro_test.dart | 2 ++ .../static_field_class_test.dart | 19 +++++++++++++++++++ test/src/equatable_macro_test.dart | 3 +++ .../static_field_class_test.dart | 15 +++++++++++++++ test/src/stringable_macro_test.dart | 3 +++ 11 files changed, 85 insertions(+), 4 deletions(-) create mode 100644 test/src/constructable_macro/static_field_class_test.dart create mode 100644 test/src/copyable_macro/static_field_class_test.dart create mode 100644 test/src/equatable_macro/static_field_class_test.dart create mode 100644 test/src/stringable_macro/static_field_class_test.dart diff --git a/lib/src/constructable_macro.dart b/lib/src/constructable_macro.dart index 9a7a4a7..0d007f3 100644 --- a/lib/src/constructable_macro.dart +++ b/lib/src/constructable_macro.dart @@ -70,6 +70,9 @@ macro class Constructable implements ClassDeclarationsMacro { final fields = await builder.fieldsOf(clazz); + // Exclude static fields. + fields.removeWhere((f) => f.hasStatic); + // Ensure all class fields have a type. if (fields.any((f) => f.type.checkNamed(builder) == null)) return null; diff --git a/lib/src/macro_extensions.dart b/lib/src/macro_extensions.dart index b077e41..61f3c05 100644 --- a/lib/src/macro_extensions.dart +++ b/lib/src/macro_extensions.dart @@ -91,11 +91,11 @@ Future> _allFieldsOf( allFields.addAll(await fieldsOf(clazz)); var superclass = await superclassOf(clazz); - while (superclass != null && superclass.identifier != 'Object') { + while (superclass != null && superclass.identifier.name != 'Object') { allFields.addAll(await fieldsOf(superclass)); superclass = await superclassOf(superclass); } - return allFields; + return allFields..removeWhere((f) => f.hasStatic); } Future _resolveType( diff --git a/lib/src/stringable_macro.dart b/lib/src/stringable_macro.dart index 35e1dfe..11ab291 100644 --- a/lib/src/stringable_macro.dart +++ b/lib/src/stringable_macro.dart @@ -59,9 +59,11 @@ macro class Stringable implements ClassDeclarationsMacro, ClassDefinitionMacro { (m) => m.identifier.name == 'toString', ); if (toString == null) return; - final toStringMethod = await builder.buildMethod(toString.identifier); final className = clazz.identifier.name; - final fields = await builder.allFieldsOf(clazz); + final (toStringMethod, fields) = await ( + builder.buildMethod(toString.identifier), + builder.allFieldsOf(clazz), + ).wait; final toStringFields = fields.map((field) { return '${field.type.isNullable ? 'if (${field.identifier.name} != null)' : ''} "${field.identifier.name}: \${${field.identifier.name}.toString()}", '; }); diff --git a/test/src/constructable_macro/static_field_class_test.dart b/test/src/constructable_macro/static_field_class_test.dart new file mode 100644 index 0000000..6b33601 --- /dev/null +++ b/test/src/constructable_macro/static_field_class_test.dart @@ -0,0 +1,15 @@ +import 'package:data_class_macro/data_class_macro.dart'; +import 'package:test/test.dart'; + +@Constructable() +class StaticFieldClass { + static const String field = 'field'; +} + +void main() { + group(StaticFieldClass, () { + test('has a const constructor', () { + expect(const StaticFieldClass(), isA()); + }); + }); +} diff --git a/test/src/constructable_macro_test.dart b/test/src/constructable_macro_test.dart index 3a4e39c..4d215f8 100644 --- a/test/src/constructable_macro_test.dart +++ b/test/src/constructable_macro_test.dart @@ -26,6 +26,8 @@ import 'constructable_macro/single_field_nested_subclass_of_positional_single_fi as single_field_nested_subclass_of_positional_single_field_class_test; import 'constructable_macro/single_field_subclass_of_positional_single_field_class_test.dart' as single_field_subclass_of_positional_single_field_class_test; +import 'constructable_macro/static_field_class_test.dart' + as static_field_class_test; void main() { empty_class_test.main(); @@ -43,4 +45,5 @@ void main() { single_field_class_test.main(); single_field_nested_subclass_of_positional_single_field_class_test.main(); single_field_subclass_of_positional_single_field_class_test.main(); + static_field_class_test.main(); } diff --git a/test/src/copyable_macro/static_field_class_test.dart b/test/src/copyable_macro/static_field_class_test.dart new file mode 100644 index 0000000..2e6e188 --- /dev/null +++ b/test/src/copyable_macro/static_field_class_test.dart @@ -0,0 +1,16 @@ +import 'package:data_class_macro/data_class_macro.dart'; +import 'package:test/test.dart'; + +@Copyable() +class StaticFieldClass { + const StaticFieldClass(); + static const String field = 'field'; +} + +void main() { + group(StaticFieldClass, () { + test('copyWith is correct', () { + expect(StaticFieldClass().copyWith(), isA()); + }); + }); +} diff --git a/test/src/copyable_macro_test.dart b/test/src/copyable_macro_test.dart index a6ee5b7..f1425df 100644 --- a/test/src/copyable_macro_test.dart +++ b/test/src/copyable_macro_test.dart @@ -25,6 +25,7 @@ import 'copyable_macro/single_field_nested_subclass_of_positional_single_field_c as single_field_nested_subclass_of_positional_single_field_class_test; import 'copyable_macro/single_field_subclass_of_positional_single_field_class_test.dart' as single_field_subclass_of_positional_single_field_class_test; +import 'copyable_macro/static_field_class_test.dart' as static_field_class_test; void main() { empty_class_test.main(); @@ -42,4 +43,5 @@ void main() { single_field_class_test.main(); single_field_nested_subclass_of_positional_single_field_class_test.main(); single_field_subclass_of_positional_single_field_class_test.main(); + static_field_class_test.main(); } diff --git a/test/src/equatable_macro/static_field_class_test.dart b/test/src/equatable_macro/static_field_class_test.dart new file mode 100644 index 0000000..359949e --- /dev/null +++ b/test/src/equatable_macro/static_field_class_test.dart @@ -0,0 +1,19 @@ +import 'package:data_class_macro/data_class_macro.dart'; +import 'package:test/test.dart'; + +@Equatable() +class StaticFieldClass { + static const String field = 'field'; +} + +void main() { + group(StaticFieldClass, () { + test('== is correct', () { + expect(StaticFieldClass(), equals(StaticFieldClass())); + }); + + test('hashCode is correct', () { + expect(StaticFieldClass().hashCode, equals(Object.hashAll([]))); + }); + }); +} diff --git a/test/src/equatable_macro_test.dart b/test/src/equatable_macro_test.dart index 4aa1144..413bdb1 100644 --- a/test/src/equatable_macro_test.dart +++ b/test/src/equatable_macro_test.dart @@ -26,6 +26,8 @@ import 'equatable_macro/single_field_nested_subclass_of_positional_single_field_ as single_field_nested_subclass_of_positional_single_field_class_test; import 'equatable_macro/single_field_subclass_of_positional_single_field_class_test.dart' as single_field_subclass_of_positional_single_field_class_test; +import 'equatable_macro/static_field_class_test.dart' + as static_field_class_test; void main() { empty_class_test.main(); @@ -43,4 +45,5 @@ void main() { single_field_class_test.main(); single_field_nested_subclass_of_positional_single_field_class_test.main(); single_field_subclass_of_positional_single_field_class_test.main(); + static_field_class_test.main(); } diff --git a/test/src/stringable_macro/static_field_class_test.dart b/test/src/stringable_macro/static_field_class_test.dart new file mode 100644 index 0000000..4dee3bd --- /dev/null +++ b/test/src/stringable_macro/static_field_class_test.dart @@ -0,0 +1,15 @@ +import 'package:data_class_macro/data_class_macro.dart'; +import 'package:test/test.dart'; + +@Stringable() +class StaticFieldClass { + static const field = 'field'; +} + +void main() { + group(StaticFieldClass, () { + test('toString is correct', () { + expect(StaticFieldClass().toString(), equals('StaticFieldClass()')); + }); + }); +} diff --git a/test/src/stringable_macro_test.dart b/test/src/stringable_macro_test.dart index 39d2ed0..1440085 100644 --- a/test/src/stringable_macro_test.dart +++ b/test/src/stringable_macro_test.dart @@ -26,6 +26,8 @@ import 'stringable_macro/single_field_nested_subclass_of_positional_single_field as single_field_nested_subclass_of_positional_single_field_class_test; import 'stringable_macro/single_field_subclass_of_positional_single_field_class_test.dart' as single_field_subclass_of_positional_single_field_class_test; +import 'stringable_macro/static_field_class_test.dart' + as static_field_class_test; void main() { empty_class_test.main(); @@ -43,4 +45,5 @@ void main() { single_field_class_test.main(); single_field_nested_subclass_of_positional_single_field_class_test.main(); single_field_subclass_of_positional_single_field_class_test.main(); + static_field_class_test.main(); } From 78a504b9c0f2be22f395b849df1f55f74b2e04f1 Mon Sep 17 00:00:00 2001 From: Felix Angelov Date: Sun, 26 May 2024 19:35:33 -0500 Subject: [PATCH 2/3] tweak --- lib/src/constructable_macro.dart | 3 --- 1 file changed, 3 deletions(-) diff --git a/lib/src/constructable_macro.dart b/lib/src/constructable_macro.dart index 0d007f3..9a7a4a7 100644 --- a/lib/src/constructable_macro.dart +++ b/lib/src/constructable_macro.dart @@ -70,9 +70,6 @@ macro class Constructable implements ClassDeclarationsMacro { final fields = await builder.fieldsOf(clazz); - // Exclude static fields. - fields.removeWhere((f) => f.hasStatic); - // Ensure all class fields have a type. if (fields.any((f) => f.type.checkNamed(builder) == null)) return null; From 4a0b274bbd27334796e07f86d91a3c61a7b9ef16 Mon Sep 17 00:00:00 2001 From: Felix Angelov Date: Sun, 26 May 2024 19:38:39 -0500 Subject: [PATCH 3/3] fix --- lib/src/constructable_macro.dart | 3 +++ 1 file changed, 3 insertions(+) diff --git a/lib/src/constructable_macro.dart b/lib/src/constructable_macro.dart index 9a7a4a7..0d007f3 100644 --- a/lib/src/constructable_macro.dart +++ b/lib/src/constructable_macro.dart @@ -70,6 +70,9 @@ macro class Constructable implements ClassDeclarationsMacro { final fields = await builder.fieldsOf(clazz); + // Exclude static fields. + fields.removeWhere((f) => f.hasStatic); + // Ensure all class fields have a type. if (fields.any((f) => f.type.checkNamed(builder) == null)) return null;