Skip to content

Commit

Permalink
fix: exclude static fields (#27)
Browse files Browse the repository at this point in the history
  • Loading branch information
felangel authored May 27, 2024
1 parent 731a7d8 commit 96a1856
Show file tree
Hide file tree
Showing 11 changed files with 85 additions and 4 deletions.
3 changes: 3 additions & 0 deletions lib/src/constructable_macro.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
4 changes: 2 additions & 2 deletions lib/src/macro_extensions.dart
Original file line number Diff line number Diff line change
Expand Up @@ -91,11 +91,11 @@ Future<List<FieldDeclaration>> _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<TypeAnnotation?> _resolveType(
Expand Down
6 changes: 4 additions & 2 deletions lib/src/stringable_macro.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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()}", ';
});
Expand Down
15 changes: 15 additions & 0 deletions test/src/constructable_macro/static_field_class_test.dart
Original file line number Diff line number Diff line change
@@ -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<StaticFieldClass>());
});
});
}
3 changes: 3 additions & 0 deletions test/src/constructable_macro_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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();
}
16 changes: 16 additions & 0 deletions test/src/copyable_macro/static_field_class_test.dart
Original file line number Diff line number Diff line change
@@ -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<StaticFieldClass>());
});
});
}
2 changes: 2 additions & 0 deletions test/src/copyable_macro_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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();
}
19 changes: 19 additions & 0 deletions test/src/equatable_macro/static_field_class_test.dart
Original file line number Diff line number Diff line change
@@ -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([])));
});
});
}
3 changes: 3 additions & 0 deletions test/src/equatable_macro_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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();
}
15 changes: 15 additions & 0 deletions test/src/stringable_macro/static_field_class_test.dart
Original file line number Diff line number Diff line change
@@ -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()'));
});
});
}
3 changes: 3 additions & 0 deletions test/src/stringable_macro_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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();
}

0 comments on commit 96a1856

Please sign in to comment.