How to find the AstNode related to where an identifier is defined?

40 views
Skip to first unread message

dark...@gmail.com

unread,
Feb 24, 2024, 8:16:21 PM2/24/24
to Dart Analyzer Discussion
Is there an easy way to locate the AstNode associated to where a variable was defined?
For example consider:

Consumer(
builder: (context, ref) {
return Consumer(
builder: (context, ref) {},
);
},
);

Given a `MethodInvocation` on a "ref" object, I want to know which ref specifically was targeted, and I specifically need an AstNode (not an Element).

I want an AstNode, because I then want to visit the ancestors of that AstNode. In particular, I wish to look for enclosing InstanceCreationExpressions. And that information wouldn't be available with Elements.

dark...@gmail.com

unread,
Feb 24, 2024, 8:16:48 PM2/24/24
to Dart Analyzer Discussion, dark...@gmail.com
Forgot to say hello. So Hello!

And thanks :)

Samuel Rawlins

unread,
Feb 25, 2024, 5:26:36 PM2/25/24
to analyzer...@dartlang.org, dark...@gmail.com
I don't know of one.

--
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/1a9afe98-6a6c-44d7-a329-bc1540426096n%40dartlang.org.

Brian Wilkerson

unread,
Feb 26, 2024, 11:32:13 AM2/26/24
to analyzer...@dartlang.org, dark...@gmail.com
Is there an easy way to locate the AstNode associated to where a variable was defined?

Not that I'm aware of. If the reference is always to a local variable then you'll need to walk up the AST from the method invocation looking for the declaration of that variable.

Reply all
Reply to author
Forward
0 new messages