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/src/generated/ast.dart'; |
| 8 import 'package:analyzer/src/generated/engine.dart'; |
| 9 import 'package:analyzer/src/generated/error.dart'; |
| 10 import 'package:analyzer/src/generated/source.dart'; |
| 11 import 'package:analyzer/src/generated/visitors.dart'; |
| 12 |
| 13 /// Implementers contribute lint warnings via the provided error [reporter]. |
| 14 abstract class Linter { |
| 15 /// Used to report lint warnings. |
| 16 /// NOTE: this is set by the framework before visit begins. |
| 17 ErrorReporter reporter; |
| 18 |
| 19 /// Return a visitor to be passed to compilation units to perform lint |
| 20 /// analysis. |
| 21 /// Lint errors are reported via this [Linter]'s error [reporter]. |
| 22 AstVisitor getVisitor(); |
| 23 } |
| 24 |
| 25 /// Traverses a library's worth of dart code at a time to generate lint warnings |
| 26 /// over the set of sources. |
| 27 /// |
| 28 /// See [LintCode]. |
| 29 class LintGenerator { |
| 30 |
| 31 /// A global container for contributed linters. |
| 32 static final List<Linter> LINTERS = <Linter>[]; |
| 33 |
| 34 final Iterable<CompilationUnit> _compilationUnits; |
| 35 final AnalysisErrorListener _errorListener; |
| 36 final Iterable<Linter> _linters; |
| 37 |
| 38 LintGenerator(this._compilationUnits, this._errorListener, |
| 39 [Iterable<Linter> linters]) |
| 40 : _linters = linters != null ? linters : LINTERS; |
| 41 |
| 42 void generate() { |
| 43 PerformanceStatistics.lint.makeCurrentWhile(() { |
| 44 _compilationUnits.forEach((cu) { |
| 45 if (cu.element != null) { |
| 46 _generate(cu, cu.element.source); |
| 47 } |
| 48 }); |
| 49 }); |
| 50 } |
| 51 |
| 52 void _generate(CompilationUnit unit, Source source) { |
| 53 ErrorReporter errorReporter = new ErrorReporter(_errorListener, source); |
| 54 _linters.forEach((l) => l.reporter = errorReporter); |
| 55 Iterable<AstVisitor> visitors = _linters.map((l) => l.getVisitor()); |
| 56 unit.accept(new DelegatingAstVisitor(visitors.where((v) => v != null))); |
| 57 } |
| 58 } |
OLD | NEW |