I'm trying to build an obfuscator for Dart packages (as opposed to release builds).
As an example I need to change something like:
class Existing {
void aMethod() {}
}
to
class axq234ad {
void afakdadhd() {}
}
I'm looking at using the analyzer package.
I've had some small success such as this example:
import 'package:analyzer/dart/ast/ast.dart';
import 'package:analyzer/dart/ast/visitor.dart';
import 'package:codemod_core/codemod_core.dart';
/// Suggestor that renames a class
class ClassRename extends GeneralizingAstVisitor<void>
with AstVisitingSuggestor {
ClassRename(this.existingClassName, this.newClassName);
String existingClassName;
String newClassName;
bool isMatching(ClassDeclaration node) =>
node.name.value() == existingClassName;
// The actual class declaration
@override
void visitClassDeclaration(ClassDeclaration node) {
if (isMatching(node)) {
yieldPatch(newClassName, node.name.offset, node.name.end);
}
super.visitClassDeclaration(node);
}
/// A variable declaration that uses the class
@override
void visitVariableDeclaration(VariableDeclaration node) {
if (node.runtimeType.toString() == existingClassName) {
yieldPatch(newClassName, node.name.offset, node.name.end);
}
super.visitVariableDeclaration(node);
}
}
The first visitor 'visitClassDeclaration' is working nicely but the second one doesn't give me ready access to the node that holds the class name.
I can possibly muddle through the problem but I really need some decent doco and examples.
I've had a look at the LSP but it doesn't appear to use analyzer so it wasn't much help.
So I guess the question is; is there any decent doco or examples on what nodes I need to visit or perhaps what each of the visit methods 'visit' so I don't have to keep guessing?