The problem is that test.dart parsed without the context needed to eliminate the elements contained inside.
An example of this is included in the analyzer package in the Dart SDK.
Destroying it to its steps:
1 - Configuring converters
PhysicalResourceProvider resourceProvider = PhysicalResourceProvider.INSTANCE; DartSdk sdk = new FolderBasedDartSdk(resourceProvider, resourceProvider.getFolder(args[0])); var resolvers = [ new DartUriResolver(sdk), new ResourceUriResolver(resourceProvider) ];
2 - Create AnalysisContext with Transformers
AnalysisContext context = AnalysisEngine.instance.createAnalysisContext() ..sourceFactory = new SourceFactory(resolvers);
3 - Add Source to ChangeSet
Source source = new FileSource(resourceProvider.getFile(args[1])); ChangeSet changeSet = new ChangeSet()..addedSource(source);
3.1 - Apply ChangeSet to AnalysisContext
context.applyChanges(changeSet);
3.2 - Get LibraryElement
I am not sure that this is always necessary. Perhaps you can skip it for simple files. ยฏ (โโกโ) / ยฏ
LibraryElement libElement = context.computeLibraryElement(source);
4 - Disassemble your Source !
CompilationUnit resolvedUnit = context.resolveCompilationUnit(source, libElement);
5 - Launch Visitor
resolvedUnit.accept(visitor)
Unfortunately, there is much more code than just calling parseDartFile() , but the good news is that your Visitor should work without further changes.
Good luck
source share