How to get the package of an Element?

35 views
Skip to first unread message

Alexandre Ardhuin

unread,
Jul 18, 2022, 11:50:11 AM7/18/22
to analyzer...@dartlang.org
Hi,

In a linter rule, I'd like to know the package of a given Element. What's the best way to do that?

Thanks in advance,
Alexandre

Brian Wilkerson

unread,
Jul 18, 2022, 12:13:34 PM7/18/22
to Dart Analyzer Discussion
With the caveat that not all files are in a package, so it's possible that the element won't have an associated package, I think the best way to get the information you're looking for is `element.context.contextRoot.workspace.findPackageFor(element.source.fullName)`. But note that there are multiple subclasses of `WorkspacePackage` and they might not all be what you're thinking of when you ask about a 'package'.

--
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 on the web visit https://groups.google.com/a/dartlang.org/d/msgid/analyzer-discuss/CAPiP%2BgtuGP2ds0Dk%3DhgbzXSUJe%2BavxjBcuPiOkuWW6dY0tOcyQ%40mail.gmail.com.

Alexandre Ardhuin

unread,
Jul 18, 2022, 12:15:37 PM7/18/22
to analyzer...@dartlang.org
Reply all
Reply to author
Forward
0 new messages