Skip to content

Commit 731a7d8

Browse files
committed
refactor: introduce codeFrom
1 parent 2972a11 commit 731a7d8

File tree

4 files changed

+42
-52
lines changed

4 files changed

+42
-52
lines changed

lib/src/copyable_macro.dart

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -121,14 +121,10 @@ macro class Copyable implements ClassDeclarationsMacro, ClassDefinitionMacro {
121121
// Ensure all constructor params have a type.
122122
if (params.any((p) => p.type == null)) return null;
123123

124-
final (objectIdentifier, undefinedIdentifier) = await (
125-
// ignore: deprecated_member_use
126-
builder.resolveIdentifier(dartCore, 'Object'),
127-
// ignore: deprecated_member_use
128-
builder.resolveIdentifier(dataClassMacro, 'undefined'),
124+
final (object, undefined) = await (
125+
builder.codeFrom(dartCore, 'Object'),
126+
builder.codeFrom(dataClassMacro, 'undefined'),
129127
).wait;
130-
final object = NamedTypeAnnotationCode(name: objectIdentifier);
131-
final undefined = NamedTypeAnnotationCode(name: undefinedIdentifier);
132128

133129
final body = FunctionBodyCode.fromParts(
134130
[

lib/src/equatable_macro.dart

Lines changed: 18 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -50,20 +50,12 @@ macro class Equatable implements ClassDeclarationsMacro, ClassDefinitionMacro {
5050
MemberDeclarationBuilder builder,
5151
) async {
5252
final (object, boolean) = await (
53-
// ignore: deprecated_member_use
54-
builder.resolveIdentifier(dartCore, 'Object'),
55-
// ignore: deprecated_member_use
56-
builder.resolveIdentifier(dartCore, 'bool'),
53+
builder.codeFrom(dartCore, 'Object'),
54+
builder.codeFrom(dartCore, 'bool'),
5755
).wait;
5856
return builder.declareInType(
5957
DeclarationCode.fromParts(
60-
[
61-
'external ',
62-
NamedTypeAnnotationCode(name: boolean),
63-
' operator==(',
64-
NamedTypeAnnotationCode(name: object),
65-
' other);',
66-
],
58+
['external ', boolean, ' operator==(', object, ' other);'],
6759
),
6860
);
6961
}
@@ -72,16 +64,9 @@ macro class Equatable implements ClassDeclarationsMacro, ClassDefinitionMacro {
7264
ClassDeclaration clazz,
7365
MemberDeclarationBuilder builder,
7466
) async {
75-
// ignore: deprecated_member_use
76-
final integer = await builder.resolveIdentifier(dartCore, 'int');
67+
final integer = await builder.codeFrom(dartCore, 'int');
7768
return builder.declareInType(
78-
DeclarationCode.fromParts(
79-
[
80-
'external ',
81-
NamedTypeAnnotationCode(name: integer),
82-
' get hashCode;',
83-
],
84-
),
69+
DeclarationCode.fromParts(['external ', integer, ' get hashCode;']),
8570
);
8671
}
8772

@@ -95,24 +80,22 @@ macro class Equatable implements ClassDeclarationsMacro, ClassDefinitionMacro {
9580
);
9681
if (equality == null) return;
9782

98-
final (equalsMethod, deepEquals, fields, identical) = await (
83+
final (equalsMethod, deepEquals, identical, fields) = await (
9984
builder.buildMethod(equality.identifier),
100-
// ignore: deprecated_member_use
101-
builder.resolveIdentifier(dataClassMacro, 'deepEquals'),
85+
builder.codeFrom(dataClassMacro, 'deepEquals'),
86+
builder.codeFrom(dartCore, 'identical'),
10287
builder.allFieldsOf(clazz),
103-
// ignore: deprecated_member_use
104-
builder.resolveIdentifier(dartCore, 'identical'),
10588
).wait;
106-
89+
10790
if (fields.isEmpty) {
10891
return equalsMethod.augment(
10992
FunctionBodyCode.fromParts(
11093
[
11194
'{',
112-
'if (', NamedTypeAnnotationCode(name: identical),' (this, other)',')', 'return true;',
95+
'if (', identical,' (this, other)',')', 'return true;',
11396
'return other is ${clazz.identifier.name} && ',
114-
'other.runtimeType == runtimeType;',
115-
'}',
97+
'other.runtimeType == runtimeType;',
98+
'}',
11699
],
117100
),
118101
);
@@ -124,13 +107,13 @@ macro class Equatable implements ClassDeclarationsMacro, ClassDefinitionMacro {
124107
FunctionBodyCode.fromParts(
125108
[
126109
'{',
127-
'if (', NamedTypeAnnotationCode(name: identical),' (this, other)',')', 'return true;',
110+
'if (', identical,' (this, other)',')', 'return true;',
128111
'return other is ${clazz.identifier.name} && ',
129-
'other.runtimeType == runtimeType && ',
112+
'other.runtimeType == runtimeType && ',
130113
for (final field in fieldNames)
131-
...[NamedTypeAnnotationCode(name: deepEquals), '(${field}, other.$field)', if (field != lastField) ' && '],
114+
...[deepEquals, '(${field}, other.$field)', if (field != lastField) ' && '],
132115
';',
133-
'}',
116+
'}',
134117
],
135118
),
136119
);
@@ -148,8 +131,7 @@ macro class Equatable implements ClassDeclarationsMacro, ClassDefinitionMacro {
148131

149132
final (hashCodeMethod, object, fields) = await (
150133
builder.buildMethod(hashCode.identifier),
151-
// ignore: deprecated_member_use
152-
builder.resolveIdentifier(dartCore, 'Object'),
134+
builder.codeFrom(dartCore, 'Object'),
153135
builder.allFieldsOf(clazz),
154136
).wait;
155137

@@ -159,7 +141,7 @@ macro class Equatable implements ClassDeclarationsMacro, ClassDefinitionMacro {
159141
FunctionBodyCode.fromParts(
160142
[
161143
'=> ',
162-
NamedTypeAnnotationCode(name: object),
144+
object,
163145
'.hashAll([',
164146
fieldNames.join(', '),
165147
']);',

lib/src/macro_extensions.dart

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,11 @@ typedef ConstructorParams = ({
88
});
99

1010
extension DefinitionBuilderX on DefinitionBuilder {
11+
Future<NamedTypeAnnotationCode> codeFrom(Uri library, String name) async {
12+
// ignore: deprecated_member_use
13+
return _codeFrom(library, name, resolveIdentifier);
14+
}
15+
1116
Future<TypeAnnotation?> resolveType(
1217
FormalParameterDeclaration declaration,
1318
ClassDeclaration clazz,
@@ -36,6 +41,11 @@ extension DefinitionBuilderX on DefinitionBuilder {
3641
}
3742

3843
extension DeclarationBuilderX on DeclarationBuilder {
44+
Future<NamedTypeAnnotationCode> codeFrom(Uri library, String name) async {
45+
// ignore: deprecated_member_use
46+
return _codeFrom(library, name, resolveIdentifier);
47+
}
48+
3949
Future<TypeAnnotation?> resolveType(
4050
FormalParameterDeclaration declaration,
4151
ClassDeclaration clazz,
@@ -63,6 +73,15 @@ extension DeclarationBuilderX on DeclarationBuilder {
6373
}
6474
}
6575

76+
Future<NamedTypeAnnotationCode> _codeFrom(
77+
Uri library,
78+
String name,
79+
Future<Identifier> resolveIdentifier(Uri library, String name),
80+
) async {
81+
final identifier = await resolveIdentifier(library, name);
82+
return NamedTypeAnnotationCode(name: identifier);
83+
}
84+
6685
Future<List<FieldDeclaration>> _allFieldsOf(
6786
ClassDeclaration clazz,
6887
Future<List<FieldDeclaration>> fieldsOf(TypeDeclaration type),

lib/src/stringable_macro.dart

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -44,16 +44,9 @@ macro class Stringable implements ClassDeclarationsMacro, ClassDefinitionMacro {
4444
ClassDeclaration clazz,
4545
MemberDeclarationBuilder builder,
4646
) async {
47-
// ignore: deprecated_member_use
48-
final string = await builder.resolveIdentifier(dartCore, 'String');
47+
final string = await builder.codeFrom(dartCore, 'String');
4948
return builder.declareInType(
50-
DeclarationCode.fromParts(
51-
[
52-
'external ',
53-
NamedTypeAnnotationCode(name: string),
54-
' toString();',
55-
],
56-
),
49+
DeclarationCode.fromParts(['external ', string, ' toString();']),
5750
);
5851
}
5952

0 commit comments

Comments
 (0)