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