How to get the LibraryElement of an arbitrary Dart code

237 views
Skip to first unread message

Nico Rodsevich

unread,
Apr 10, 2019, 12:33:30 AM4/10/19
to Dart Analyzer Discussion
When using 
parseCompilationUnit("...");
I can get an AST tree of arbitrary dart code.
I want to achieve the same with the LibraryElement, through the dart source provided as a String; but in the docs https://github.com/dart-lang/sdk/blob/master/pkg/analyzer/doc/tutorial/analysis.md (Thanks a lot for the one that uploaded them) I can't see any clue apart from writing the string contents to a tmp file on disk (almost a sin).
How can I get what I'm looking for?

Brian Wilkerson

unread,
Apr 10, 2019, 11:45:11 AM4/10/19
to analyzer...@dartlang.org
In order to get a `LibraryElement`, you need to get a resolved AST. There isn't an equivalent to `parseCompilationUnit` that will get you a fully resolved AST. The reason for that is because resolution requires a lot more information, most of which is normally computed by looking at the surrounding directory structure.

You basically need to create an `AnalysisContextCollection` as described in https://github.com/dart-lang/sdk/blob/master/pkg/analyzer/doc/tutorial/analysis.md. That API reads from the file system, but all access to the file system goes through the `ResourceProvider` (an optional parameter that isn't yet described in the docs). There is an implementation of `ResourceProvider` named `MemoryResourceProvider` that allows you to simulate a file system from strings stored in memory, which will negate the need to wrote a temporary file to disk.

--
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.
Visit this group at https://groups.google.com/a/dartlang.org/group/analyzer-discuss/.

hacker1024

unread,
Jun 24, 2022, 1:52:12 PM6/24/22
to Dart Analyzer Discussion, nicoro...@gmail.com
Reply all
Reply to author
Forward
0 new messages