language icon indicating copy to clipboard operation
language copied to clipboard

Library introspection and macros that apply to imports/exports

Open jakemac53 opened this issue 2 years ago • 4 comments

See the comment thread that starts here .

It should be reasonable to expose a way of introspecting on libraries, assuming we only expose information from the appropriate phases. I would expect the following affordances:

  • Get a Library object for any resolved identifier. This would contain possibly only the Uri of the library initially, but may expose the pre-augmentation imports as well if we decide that is OK.
    • Note that conditional imports may make this weird, so we have to figure that out.
  • Get a Library object for any Declaration (this is just the surrounding library).
  • In phase 2, be able to ask what types are defined in a Library.
  • In phase 3, be able to ask for all the Declarations in a Library.
  • Support macro annotations on import and export directives, as well as possibly parts (but only if we can come up with a use case imo). These declarations would be able to give you a Library object for the directive.
    • Note that we have to decide how to deal with conditional imports here as well.

cc @GregoryConrad

jakemac53 avatar Feb 10 '23 21:02 jakemac53

Starting some work here https://dart-review.googlesource.com/c/sdk/+/311000 - I deviated a bit from the original design and attached libraries directly to declarations. So to get a library for an identifier you would first go to the declaration.

That means one less introspection API, and less async work if you already have the declaration, but also larger payloads for declarations.

Separately I am working on making payloads smaller by only sending ids for objects which have not already been sent which for this issue in particular should help a lot.

jakemac53 avatar Jun 22 '23 16:06 jakemac53

After https://dart-review.googlesource.com/c/sdk/+/312140 I am going to move this to the backlog. The remaining feature work would be the ability to run on import/export declarations as well as introspect on the imports/exports of a library.

While possibly useful those features are not necessary imo and we can come back to them if we have a compelling use case.

jakemac53 avatar Jun 30 '23 20:06 jakemac53

The remaining feature work would be the ability to run on import/export declarations

Do you mean:

@MyMacro()
import '...';

If so, when do you think such a feature would ship? A use case is the second code block in your comment from awhile ago here

GregoryConrad avatar Feb 02 '24 18:02 GregoryConrad

This is relatively easy to add on later, so the priority on it right now is low. Once we have a proper preview release for the existing features, I would like to circle back to it. I don't have any timeline to share for that, but work is moving along pretty quickly now.

jakemac53 avatar Feb 08 '24 16:02 jakemac53