Skip to content

Commit 59bde59

Browse files
authored
Update macro examples for the latest apis (#3010)
1 parent f359124 commit 59bde59

File tree

8 files changed

+127
-91
lines changed

8 files changed

+127
-91
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
*.stamp
2525
.dart_tool
2626
.packages
27+
pubspec_overrides.yaml
2728

2829
# Gyp generated files
2930
*.xcodeproj

working/macros/example/benchmark/simple.dart

Lines changed: 57 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -117,9 +117,9 @@ Host app mode: $hostMode
117117
macroExecutionStrategy == 'aot' ? 'exe' : 'jit-snapshot',
118118
'--packages=.dart_tool/package_config.json',
119119
'--enable-experiment=macros',
120-
bootstrapFile.uri.toFilePath(),
121120
'-o',
122121
kernelOutputFile.uri.toFilePath(),
122+
bootstrapFile.uri.toFilePath(),
123123
]);
124124

125125
if (buildSnapshotResult.exitCode != 0) {
@@ -150,31 +150,34 @@ Host app mode: $hostMode
150150
for (var i = 1; i <= 111; i++) {
151151
var _shouldLog = i == 1 || i == 11 || i == 111;
152152
if (_shouldLog) _log('Running DataClass macro for the ${i}th time');
153-
if (instanceId.shouldExecute(DeclarationKind.clazz, Phase.types)) {
153+
if (instanceId.shouldExecute(DeclarationKind.classType, Phase.types)) {
154154
if (_shouldLog) _log('Running types phase');
155155
var result = await executor.executeTypesPhase(
156156
instanceId, myClass, SimpleIdentifierResolver());
157157
if (i == 1) results.add(result);
158158
}
159-
if (instanceId.shouldExecute(DeclarationKind.clazz, Phase.declarations)) {
159+
if (instanceId.shouldExecute(
160+
DeclarationKind.classType, Phase.declarations)) {
160161
if (_shouldLog) _log('Running declarations phase');
161162
var result = await executor.executeDeclarationsPhase(
162163
instanceId,
163164
myClass,
164165
SimpleIdentifierResolver(),
166+
SimpleTypeDeclarationResolver(),
165167
SimpleTypeResolver(),
166-
SimpleClassIntrospector());
168+
SimpleTypeIntrospector());
167169
if (i == 1) results.add(result);
168170
}
169-
if (instanceId.shouldExecute(DeclarationKind.clazz, Phase.definitions)) {
171+
if (instanceId.shouldExecute(
172+
DeclarationKind.classType, Phase.definitions)) {
170173
if (_shouldLog) _log('Running definitions phase');
171174
var result = await executor.executeDefinitionsPhase(
172175
instanceId,
173176
myClass,
174177
SimpleIdentifierResolver(),
178+
SimpleTypeDeclarationResolver(),
175179
SimpleTypeResolver(),
176-
SimpleClassIntrospector(),
177-
FakeTypeDeclarationResolver(),
180+
SimpleTypeIntrospector(),
178181
FakeTypeInferrer());
179182
if (i == 1) results.add(result);
180183
}
@@ -189,7 +192,11 @@ Host app mode: $hostMode
189192
var first111RunsEnd = _watch.elapsed;
190193

191194
_log('Building augmentation library');
192-
var library = executor.buildAugmentationLibrary(results, (identifier) {
195+
var library = executor.buildAugmentationLibrary(
196+
results,
197+
(identifier) => identifier == myClass.identifier
198+
? myClass
199+
: throw UnsupportedError('Can only resolve myClass'), (identifier) {
193200
if (['bool', 'Object', 'String', 'int'].contains(identifier.name)) {
194201
return ResolvedIdentifier(
195202
kind: IdentifierKind.topLevelMember,
@@ -251,24 +258,34 @@ final stringType = NamedTypeAnnotationImpl(
251258
isNullable: false,
252259
typeArguments: const []);
253260

254-
final objectClass = ClassDeclarationImpl(
261+
final objectClass = IntrospectableClassDeclarationImpl(
255262
id: RemoteInstance.uniqueId,
256263
identifier: objectIdentifier,
257264
interfaces: [],
258-
isAbstract: false,
259-
isExternal: false,
265+
hasAbstract: false,
266+
hasBase: false,
267+
hasExternal: false,
268+
hasFinal: false,
269+
hasInterface: false,
270+
hasMixin: false,
271+
hasSealed: false,
260272
mixins: [],
261273
superclass: null,
262274
typeParameters: []);
263275

264276
final myClassIdentifier =
265277
IdentifierImpl(id: RemoteInstance.uniqueId, name: 'MyClass');
266-
final myClass = ClassDeclarationImpl(
278+
final myClass = IntrospectableClassDeclarationImpl(
267279
id: RemoteInstance.uniqueId,
268280
identifier: myClassIdentifier,
269281
interfaces: [],
270-
isAbstract: false,
271-
isExternal: false,
282+
hasAbstract: false,
283+
hasBase: false,
284+
hasExternal: false,
285+
hasFinal: false,
286+
hasInterface: false,
287+
hasMixin: false,
288+
hasSealed: false,
272289
mixins: [],
273290
superclass: NamedTypeAnnotationImpl(
274291
id: RemoteInstance.uniqueId,
@@ -280,7 +297,7 @@ final myClass = ClassDeclarationImpl(
280297

281298
final myClassFields = [
282299
FieldDeclarationImpl(
283-
definingClass: myClassIdentifier,
300+
definingType: myClassIdentifier,
284301
id: RemoteInstance.uniqueId,
285302
identifier: IdentifierImpl(id: RemoteInstance.uniqueId, name: 'myString'),
286303
isExternal: false,
@@ -289,7 +306,7 @@ final myClassFields = [
289306
isStatic: false,
290307
type: stringType),
291308
FieldDeclarationImpl(
292-
definingClass: myClassIdentifier,
309+
definingType: myClassIdentifier,
293310
id: RemoteInstance.uniqueId,
294311
identifier: IdentifierImpl(id: RemoteInstance.uniqueId, name: 'myBool'),
295312
isExternal: false,
@@ -301,7 +318,7 @@ final myClassFields = [
301318

302319
final myClassMethods = [
303320
MethodDeclarationImpl(
304-
definingClass: myClassIdentifier,
321+
definingType: myClassIdentifier,
305322
id: RemoteInstance.uniqueId,
306323
identifier: IdentifierImpl(id: RemoteInstance.uniqueId, name: '=='),
307324
isAbstract: false,
@@ -328,7 +345,7 @@ final myClassMethods = [
328345
typeParameters: [],
329346
),
330347
MethodDeclarationImpl(
331-
definingClass: myClassIdentifier,
348+
definingType: myClassIdentifier,
332349
id: RemoteInstance.uniqueId,
333350
identifier: IdentifierImpl(id: RemoteInstance.uniqueId, name: 'hashCode'),
334351
isAbstract: false,
@@ -343,7 +360,7 @@ final myClassMethods = [
343360
typeParameters: [],
344361
),
345362
MethodDeclarationImpl(
346-
definingClass: myClassIdentifier,
363+
definingType: myClassIdentifier,
347364
id: RemoteInstance.uniqueId,
348365
identifier: IdentifierImpl(id: RemoteInstance.uniqueId, name: 'toString'),
349366
isAbstract: false,
@@ -366,26 +383,24 @@ abstract class Fake {
366383
}
367384

368385
/// Returns data as if everything was [myClass].
369-
class SimpleClassIntrospector extends Fake implements ClassIntrospector {
386+
class SimpleTypeIntrospector implements TypeIntrospector {
370387
@override
371388
Future<List<ConstructorDeclaration>> constructorsOf(
372-
covariant ClassDeclaration clazz) async =>
389+
IntrospectableType type) async =>
373390
[];
374391

375392
@override
376-
Future<List<FieldDeclaration>> fieldsOf(
377-
covariant ClassDeclaration clazz) async =>
378-
clazz == myClass ? myClassFields : [];
393+
Future<List<FieldDeclaration>> fieldsOf(IntrospectableType type) async =>
394+
type == myClass ? myClassFields : [];
379395

380396
@override
381-
Future<List<MethodDeclaration>> methodsOf(
382-
covariant ClassDeclaration clazz) async =>
383-
clazz == myClass ? myClassMethods : [];
397+
Future<List<MethodDeclaration>> methodsOf(IntrospectableType type) async =>
398+
type == myClass ? myClassMethods : [];
384399

385400
@override
386-
Future<ClassDeclaration?> superclassOf(
387-
covariant ClassDeclaration clazz) async =>
388-
clazz == myClass ? objectClass : null;
401+
Future<List<EnumValueDeclaration>> valuesOf(
402+
IntrospectableEnumDeclaration type) async =>
403+
[];
389404
}
390405

391406
/// This is a very basic identifier resolver, it does no actual resolution.
@@ -396,8 +411,18 @@ class SimpleIdentifierResolver implements IdentifierResolver {
396411
IdentifierImpl(id: RemoteInstance.uniqueId, name: name);
397412
}
398413

399-
class FakeTypeDeclarationResolver extends Fake
400-
implements TypeDeclarationResolver {}
414+
class SimpleTypeDeclarationResolver implements TypeDeclarationResolver {
415+
@override
416+
Future<TypeDeclaration> declarationOf(covariant Identifier identifier) async {
417+
if (identifier == myClass.identifier) {
418+
return myClass;
419+
} else if (identifier == objectClass.identifier) {
420+
return objectClass;
421+
} else {
422+
throw UnsupportedError('Could not resolve identifier ${identifier.name}');
423+
}
424+
}
425+
}
401426

402427
class FakeTypeInferrer extends Fake implements TypeInferrer {}
403428

working/macros/example/bin/run.dart

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -60,10 +60,11 @@ void main() async {
6060
.toFilePath(),
6161
'--output',
6262
bootstrapKernelFile.path,
63-
'--source',
64-
bootstrapFile.path,
65-
'--source',
66-
'lib/data_class.dart',
63+
'--source=${bootstrapFile.path}',
64+
'--source=lib/auto_dispose.dart',
65+
'--source=lib/data_class.dart',
66+
'--source=lib/json_serializable.dart',
67+
'--source=lib/observable.dart',
6768
for (var source in await _allSources(feAnalyzerSharedRoot.path))
6869
'--source=$source',
6970
'--packages-file=.dart_tool/package_config.json',

working/macros/example/lib/auto_dispose.dart

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,21 +15,21 @@ macro class AutoDispose implements ClassDeclarationsMacro, ClassDefinitionMacro
1515

1616
@override
1717
void buildDeclarationsForClass(
18-
ClassDeclaration clazz, ClassMemberDeclarationBuilder builder) async {
18+
IntrospectableClassDeclaration clazz, MemberDeclarationBuilder builder) async {
1919
var methods = await builder.methodsOf(clazz);
2020
if (methods.any((d) => d.identifier.name == 'dispose')) {
2121
// Don't need to add the dispose method, it already exists.
2222
return;
2323
}
2424

25-
builder.declareInClass(DeclarationCode.fromParts([
25+
builder.declareInType(DeclarationCode.fromParts([
2626
'external void dispose();',
2727
]));
2828
}
2929

3030
@override
3131
Future<void> buildDefinitionForClass(
32-
ClassDeclaration clazz, ClassDefinitionBuilder builder) async {
32+
IntrospectableClassDeclaration clazz, TypeDefinitionBuilder builder) async {
3333
var disposableIdentifier =
3434
// ignore: deprecated_member_use
3535
await builder.resolveIdentifier(

0 commit comments

Comments
 (0)