Created
October 17, 2016 00:53
-
-
Save matanlurey/3d04a60e33e34627cd160833e802a800 to your computer and use it in GitHub Desktop.
A strawman for an extensible Linter API
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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