identify the scope/owner of a simpleidentifier

36 views
Skip to first unread message

Brett Sutton

unread,
Dec 2, 2023, 6:11:57 PM12/2/23
to Dart Analyzer Discussion
I'm building a dart obfuscation tool

I need to suppress obfuscation when an identifier is exported.

If an class is exported then all of its methods and fields are also considered exported and as such must not be obfuscated.

```dart
export 'somelibrary.dart';
```

```dart
// somelibrary.dart
class Test {
    String header;
    void amethod() {
       header = 'a';
    }
}
```
So my problem is with the line `header = 'a'`.

I'm using the GeneralizingAstVisitor which yields:

```
* ClassDeclarationImpl (class Sample {Sample({required this.name}) {header = 'hi';} void  amethod() {} void method({String? arg}) {} String name; late final String header;})
* ConstructorDeclarationImpl (Sample({required this.name}) {header = 'hi';})
* BlockImpl ({header = 'hi';})
* ExpressionStatementImpl (header = 'hi';)
* AssignmentExpressionImpl (header = 'hi')
* SimpleIdentifierImpl (header)
```
The issue is that I can't find a way to determine if the SimpleIdentifier 'header' is a field of Test.

I can see that the SimpleIdentifierImpl.scopeLookupResult has the required information but SimpleIdentifierImpl isn't a public class so I can't access it in code.

I could navigate up the tree which would yield a ClassDeclaration but this would still leave me with the question as to whether 'header' is a field, local or parameter.

An help would be appreciated.


Brian Wilkerson

unread,
Dec 3, 2023, 1:32:13 PM12/3/23
to analyzer...@dartlang.org
You should be able to ask the `SimpleIdentifier` associated with 'header' for its 'staticElement'. The static element should be a synthetic `PropertyAccessElement` whose `variable` is a `FieldElement` representing the field named 'header'.

--
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/d4e40e26-c174-4c31-8397-602ca979ca5fn%40dartlang.org.
Reply all
Reply to author
Forward
0 new messages