Skip to content

Auto-generate identifiers for record_use #56043

@mosuem

Description

@mosuem

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    area-metaCross-cutting, high-level issues (for tracking many other implementation issues, ...).

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions