sdk icon indicating copy to clipboard operation
sdk copied to clipboard

Auto-generate identifiers for `record_use`

Open mosuem opened this issue 1 year ago • 1 comments

With the upcoming record_use feature, users can record usage of certain Dart objects by placing @RecordUse annotations on the objects. To retrieve the recordings, they need to provide the identifier of the object, a 3-tuple of URI, parent class, and name. This is a source of errors, as typos can be made, and the identifiers have to be updated whenever the annotations are placed elsewhere.

To alleviate this, we could auto-generate the IDs, for example using macros. Example:

//in lib/src/myfile.dart
class MyClass {
  @RecordUse
  static bool isPositive(int i) => i > 0; 
}

@RecordUse
class MyAnnotation {
  const MyAnnotation();
}

//in lib/src/ids.g.dart
const MyClass_isPositive_ID = Identifier(
  uri: 'package:mypackage/lib/src/myfile.dart',
  parent: 'MyClass',
  name: 'isPositive',
);

const MyAnnotation_ID = Identifier(
  uri: 'package:mypackage/lib/src/myfile.dart',
  name: 'MyAnnotation',
);

//in hook/link.dart
import '../lib/src/ids.g.dart';

void main(List<String> arguments){
  link(arguments, (config, output) async {
    final uses = config.recordedUses;
    
    final args = uses.constArgumentsTo(MyClass_isPositive_ID));

    final fields = uses.instanceReferencesTo(MyAnnotation_ID);

    ... // Do something with the information, such as tree-shaking native assets
  });
}

cc @dcharkes @mkustermann @jakemac53

mosuem avatar Jun 19 '24 07:06 mosuem

I don't know anything about the referenced record_use feature - but yes generating these identifiers should be trivial using a macro.

jakemac53 avatar Jun 20 '24 17:06 jakemac53