Skip to content

Instantly share code, notes, and snippets.

@matanlurey
Created October 17, 2016 00:53
Show Gist options
  • Save matanlurey/3d04a60e33e34627cd160833e802a800 to your computer and use it in GitHub Desktop.
Save matanlurey/3d04a60e33e34627cd160833e802a800 to your computer and use it in GitHub Desktop.
A strawman for an extensible Linter API
import 'dart:async';
import 'package:angular2/metadata.dart';
import 'package:linter/isolate.dart';
Future<Null> main(SendPort sendPort) {
// Create a new 'Isolate'-based plugin to the linter.
final plugin = await registerPlugin(sendPort, new PluginOptions(
// To avoid serializing/deserializing needlessly, support a basic
// regular expression-based filter to check a file for before
// sending anything over the wire.
filter: '/@Component|@Directive/',
));
// Finally, run the linter.
await plugin.run(new AngularDartLinter());
}
class AngularDartLinter extends LinterPlugin {
// Everytime a file should be linted, this should be called.
@override
Future<Null> visitFile(CompilationUnit compilationUnit) {
// Search for `@Component` and `@Directive`s in compilationUnit.
// When found, call the respective function below.
return visitAnnotatedNodes(
compilationUnit, {
Component: visitComponent,
Directive: visitDirective,
});
}
Future<Null> visitComponent(Component metadata, {SourceSpan source}) async {
// An example of a lint, we are missing a template.
if (metadata.template == null && metadata.templateUrl == null) {
addLint(
LintType.ERROR,
'An @Component must have either a template or templateUrl',
source: source,
);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment