-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add specific macro implementations and delegate to them (#97)
Part of #91 Sending this out a bit early - this is I think the smallest chunk I could tackle independently, but it would be nice to get a review before you leave. The next step would be passing the actual target through to the macro api, probably instead of the AugmentRequest.
- Loading branch information
Showing
12 changed files
with
480 additions
and
55 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
// Copyright (c) 2024, the Dart project authors. Please see the AUTHORS file | ||
// for details. All rights reserved. Use of this source code is governed by a | ||
// BSD-style license that can be found in the LICENSE file. | ||
|
||
import 'dart:async'; | ||
|
||
import 'package:dart_model/dart_model.dart'; | ||
import 'package:macro/macro.dart'; | ||
import 'package:macro_service/macro_service.dart'; | ||
|
||
/// Runs [macro] in the types phase and returns an [AugmentResponse]. | ||
Future<AugmentResponse> executeTypesMacro( | ||
Macro macro, Host host, AugmentRequest request) async { | ||
final target = request.target; | ||
// TODO: https://github.com/dart-lang/macros/issues/100. | ||
final queryResult = await host.query(Query(target: target)); | ||
final properties = | ||
queryResult.uris[target.uri]!.scopes[target.name]!.properties; | ||
switch ((properties, macro)) { | ||
case (Properties(isClass: true), ClassTypesMacro macro): | ||
return await macro.buildTypesForClass(host, request); | ||
case (_, LibraryTypesMacro()): | ||
case (_, ConstructorTypesMacro()): | ||
case (_, MethodTypesMacro()): | ||
case (_, FunctionTypesMacro()): | ||
case (_, FieldTypesMacro()): | ||
case (_, VariableTypesMacro()): | ||
case (_, EnumTypesMacro()): | ||
case (_, ExtensionTypesMacro()): | ||
case (_, ExtensionTypeTypesMacro()): | ||
case (_, MixinTypesMacro()): | ||
case (_, EnumValueTypesMacro()): | ||
case (_, TypeAliasTypesMacro()): | ||
throw UnimplementedError('Unimplemented macro target'); | ||
default: | ||
throw UnsupportedError('Unsupported macro type or invalid target:\n' | ||
'macro: $macro\ntarget: $target'); | ||
} | ||
} | ||
|
||
/// Runs [macro] in the declarations phase and returns an [AugmentResponse]. | ||
Future<AugmentResponse> executeDeclarationsMacro( | ||
Macro macro, Host host, AugmentRequest request) async { | ||
final target = request.target; | ||
// TODO: https://github.com/dart-lang/macros/issues/100. | ||
final queryResult = await host.query(Query(target: target)); | ||
final properties = | ||
queryResult.uris[target.uri]!.scopes[target.name]!.properties; | ||
|
||
switch ((properties, macro)) { | ||
case (Properties(isClass: true), ClassDeclarationsMacro macro): | ||
return await macro.buildDeclarationsForClass(host, request); | ||
case (_, LibraryDeclarationsMacro()): | ||
case (_, EnumDeclarationsMacro()): | ||
case (_, ExtensionDeclarationsMacro()): | ||
case (_, ExtensionTypeDeclarationsMacro()): | ||
case (_, MixinDeclarationsMacro()): | ||
case (_, EnumValueDeclarationsMacro()): | ||
case (_, ConstructorDeclarationsMacro()): | ||
case (_, MethodDeclarationsMacro()): | ||
case (_, FieldDeclarationsMacro()): | ||
case (_, FunctionDeclarationsMacro()): | ||
case (_, VariableDeclarationsMacro()): | ||
case (_, TypeAliasDeclarationsMacro()): | ||
throw UnimplementedError('Unimplemented macro target'); | ||
default: | ||
throw UnsupportedError('Unsupported macro type or invalid target:\n' | ||
'macro: $macro\ntarget: $target'); | ||
} | ||
} | ||
|
||
/// Runs [macro] in the definitions phase and returns an [AugmentResponse]. | ||
Future<AugmentResponse> executeDefinitionsMacro( | ||
Macro macro, Host host, AugmentRequest request) async { | ||
final target = request.target; | ||
// TODO: https://github.com/dart-lang/macros/issues/100. | ||
final queryResult = await host.query(Query(target: target)); | ||
final properties = | ||
queryResult.uris[target.uri]!.scopes[target.name]!.properties; | ||
|
||
switch ((properties, macro)) { | ||
case (Properties(isClass: true), ClassDefinitionsMacro macro): | ||
return await macro.buildDefinitionsForClass(host, request); | ||
case (_, LibraryDefinitionsMacro()): | ||
case (_, EnumDefinitionsMacro()): | ||
case (_, ExtensionDefinitionsMacro()): | ||
case (_, ExtensionTypeDefinitionsMacro()): | ||
case (_, MixinDefinitionsMacro()): | ||
case (_, EnumValueDefinitionsMacro()): | ||
case (_, ConstructorDefinitionsMacro()): | ||
case (_, MethodDefinitionsMacro()): | ||
case (_, FieldDefinitionsMacro()): | ||
case (_, FunctionDefinitionsMacro()): | ||
case (_, VariableDefinitionsMacro()): | ||
throw UnimplementedError('Unimplemented macro target'); | ||
default: | ||
throw UnsupportedError('Unsupported macro type or invalid target:\n' | ||
'macro: $macro\ntarget: $target'); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.