Is there an easy way to obtain an Element/DartType for a symbol that is not imported by the current library?

14 views
Skip to first unread message

dark...@gmail.com

unread,
Aug 11, 2025, 3:07:07 PMAug 11
to Dart Analyzer Discussion
Hello!

I'm looking for the equivalent of TypeProvider, but for third-party objects.
For example, while writing lints for one of my packages, I want to use the various DartType APIs (such as InterfaceType.instantiate or those from TypeSystem).

The thing is, even though a library may be using my package, it may not have its content imported.
This can happen when user code is split into multiple libraries. For example, a widget could be using flutter/material.dart. Then 'main' uses this widget, without importing flutter/material.dart

I could theorically resursively look through all imports/exports. But that feels very inneficient. 

So to reiterate: Is it possible to obtain a DartType for an arbitrary object?
Ideally we'd have some form of get(uri, name) 

Thanks!

Brian Wilkerson

unread,
Aug 11, 2025, 6:33:37 PMAug 11
to analyzer...@dartlang.org, Konstantin Shcheglov
I'm not aware of any guaranteed performant way to do so, but if you have an `AnalysisSession`, you can use `uriConverter` to convert the known URI to a file path, and then you can use `getResolvedLibrary` to get a `LibraryElement`. From there you ought to be able to find the element you're looking for (and compute the `thisType`).

--
You received this message because you are subscribed to the Google Groups "Dart Analyzer Discussion" group.
To unsubscribe from this group and stop receiving emails from it, send an email to analyzer-discu...@dartlang.org.
To view this discussion visit https://groups.google.com/a/dartlang.org/d/msgid/analyzer-discuss/3d40438e-aa52-4594-9427-a2b0d2866083n%40dartlang.org.

Konstantin Shcheglov

unread,
Aug 11, 2025, 8:42:13 PMAug 11
to Brian Wilkerson, analyzer...@dartlang.org

  We have `Future<SomeLibraryElementResult> getLibraryByUri(String uri)` in `AnalysisSession`.

  We don't have in `AnalysisSession` API to get `LibraryElement` synchronously.
  So, you cannot do: I know that a `LibraryElement` that I have now can only be loaded if it also imports the library that I want.
  Theoretically we could add `SomeLibraryElementResult? getLibraryIfAlreadyLoaded(Uri uri)`.
--
Konstantin Scheglov
Software Engineer
Google, Inc.
Reply all
Reply to author
Forward
0 new messages