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