Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright (c) 2015, the Dart project authors. Please see the AUTHORS file | |
| 2 // for details. All rights reserved. Use of this source code is governed by a | |
| 3 // BSD-style license that can be found in the LICENSE file. | |
| 4 | |
| 5 library lint; | |
| 6 | |
| 7 import 'package:analyzer/analyzer.dart'; | |
| 8 import 'package:analyzer/src/generated/utilities_general.dart'; | |
| 9 import 'package:analyzer/src/generated/engine.dart'; | |
| 10 import 'package:analyzer/src/generated/source.dart'; | |
| 11 import 'package:analyzer/src/visitors.dart'; | |
| 12 | |
| 13 | |
| 14 /// Implementers contribute lint warnings via the provided error [reporter]. | |
| 15 abstract class Linter { | |
| 16 /// Used to report lint warnings. | |
| 17 /// NOTE: this is set by the framework before visit begins. | |
| 18 ErrorReporter reporter; | |
| 19 | |
| 20 /// Return a visitor to be passed to compilation units to perform lint | |
| 21 /// analysis. | |
| 22 /// Lint errors are reported via this [Linter]'s error [reporter]. | |
| 23 AstVisitor getVisitor(); | |
| 24 } | |
| 25 | |
| 26 /// Traverses a library's worth of dart code at a time to generate lint warnings | |
| 27 /// over the set of sources. | |
| 28 /// | |
| 29 /// See [LintCode]. | |
| 30 class LintGenerator { | |
| 31 | |
| 32 /// A global container for contributed linters. | |
| 33 static final List<Linter> LINTERS = <Linter>[]; | |
| 34 | |
| 35 final Iterable<CompilationUnit> _compilationUnits; | |
| 36 final AnalysisErrorListener _errorListener; | |
| 37 final Iterable<Linter> _linters; | |
| 38 | |
| 39 LintGenerator(this._compilationUnits, this._errorListener, | |
| 40 [Iterable<Linter> linters]) | |
| 41 : _linters = linters != null ? linters : LINTERS; | |
|
scheglov
2015/01/16 00:30:20
Strange format...
pquitslund
2015/01/16 00:43:21
Done.
| |
| 42 | |
| 43 void generate() { | |
| 44 TimeCounter_TimeCounterHandle timeCounter = | |
| 45 PerformanceStatistics.lint.start(); | |
| 46 try { | |
| 47 _compilationUnits.forEach((cu) { | |
| 48 if (cu.element != null) { | |
| 49 _generate(cu, cu.element.source); | |
| 50 } | |
| 51 }); | |
| 52 } finally { | |
| 53 timeCounter.stop(); | |
| 54 } | |
| 55 } | |
| 56 | |
| 57 void _generate(CompilationUnit unit, Source source) { | |
| 58 ErrorReporter errorReporter = new ErrorReporter(_errorListener, source); | |
| 59 _linters.forEach((l) => l.reporter = errorReporter); | |
| 60 Iterable<AstVisitor> visitors = _linters.map((l) => l.getVisitor()); | |
| 61 unit.accept(new DelegatingAstVisitor(visitors.where((v) => v != null))); | |
| 62 } | |
| 63 } | |
| OLD | NEW |