Skip to content

Instantly share code, notes, and snippets.

@justinfagnani
Created July 17, 2014 00:53
Show Gist options
  • Save justinfagnani/8e0b18cf4604f039c859 to your computer and use it in GitHub Desktop.
Save justinfagnani/8e0b18cf4604f039c859 to your computer and use it in GitHub Desktop.
code_transformers / analyzer
class InterfaceTransformer extends Transformer with ResolverTransformer {
final Resolvers resolvers;
InterfaceTransformer(this.resolvers);
Future<bool> isPrimary(AssetId id) => new Future.value(id.extension == '.dart');
Future<bool> shouldApplyResolver(Asset asset) {
return asset.readAsString().then((contents) {
var cu = parseCompilationUnit(contents, suppressErrors: true);
var isPart = cu.directives.any((Directive d) => d is PartOfDirective);
return !isPart;
});
}
applyResolver(Transform transform, Resolver resolver) {
var input = transform.primaryInput;
var library = resolver.getLibrary(transform.primaryInput.id);
var interfaceFinder = new TypedInterfaceFinder(resolver, library);
library.accept(interfaceFinder);
for (ClassElement interface in interfaceFinder.jsInterfaces) {
var name = interface.name;
for (var accessor in interface.accessors) {
// accessor.name == ''
}
for (var method in interface.methods) {
// methods is empty
}
}
}
}
class TypedInterfaceFinder extends RecursiveElementVisitor {
final ClassElement jsInterfaceClass;
final Set<ClassElement> jsInterfaces = new Set<ClassElement>();
final LibraryElement entryLibrary;
final Resolver resolver;
TypedInterfaceFinder(Resolver resolver, this.entryLibrary)
: resolver = resolver,
jsInterfaceClass = resolver.getType('js.interfaces.JsInterface');
bool get importsJsInterfaces => jsInterfaceClass != null;
visitLibraryElement(LibraryElement element) {
// We don't visit other libraries, since this transformer runs on each
// library separately. We also only run on libraries that have
// JsInterface available.
if (element == entryLibrary && importsJsInterfaces) {
super.visitLibraryElement(element);
}
}
bool isJsInterface(ClassElement e) {
if (e.isPrivate) return false;
bool isJsInterface = false;
if (e.allSupertypes.contains(jsInterfaceClass.type)) {
isJsInterface = true;
}
if (isJsInterface) {
// validate
}
return isJsInterface;
}
visitClassElement(ClassElement element) {
if (isJsInterface(element)) {
jsInterfaces.add(element);
}
super.visitClassElement(element);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment