| 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 import 'package:analyzer/src/task/model.dart'; |
| 13 import 'package:analyzer/task/model.dart'; |
| 12 | 14 |
| 13 /// A registry containing mappings of contexts to their associated configured | 15 const List<Linter> _noLints = const <Linter>[]; |
| 14 /// lints. | 16 |
| 15 final Map<AnalysisContext, List<Linter>> lintRegistry = | 17 /// The descriptor used to associate lints with analysis contexts in |
| 16 <AnalysisContext, List<Linter>>{}; | 18 /// configuration data. |
| 19 final ResultDescriptor<List<Linter>> CONFIGURED_LINTS_KEY = |
| 20 new ResultDescriptorImpl('configured.lints', _noLints); |
| 21 |
| 22 /// Return lints associated with this [context], or an empty list if there are |
| 23 /// none. |
| 24 List<Linter> getLints(AnalysisContext context) => |
| 25 context.getConfigurationData(CONFIGURED_LINTS_KEY) ?? _noLints; |
| 26 |
| 27 /// Associate these [lints] with the given [context]. |
| 28 void setLints(AnalysisContext context, List<Linter> lints) { |
| 29 context.setConfigurationData(CONFIGURED_LINTS_KEY, lints); |
| 30 } |
| 17 | 31 |
| 18 /// Implementers contribute lint warnings via the provided error [reporter]. | 32 /// Implementers contribute lint warnings via the provided error [reporter]. |
| 19 abstract class Linter { | 33 abstract class Linter { |
| 20 /// Used to report lint warnings. | 34 /// Used to report lint warnings. |
| 21 /// NOTE: this is set by the framework before visit begins. | 35 /// NOTE: this is set by the framework before visit begins. |
| 22 ErrorReporter reporter; | 36 ErrorReporter reporter; |
| 23 | 37 |
| 24 /// Return a visitor to be passed to compilation units to perform lint | 38 /// Return a visitor to be passed to compilation units to perform lint |
| 25 /// analysis. | 39 /// analysis. |
| 26 /// Lint errors are reported via this [Linter]'s error [reporter]. | 40 /// Lint errors are reported via this [Linter]'s error [reporter]. |
| 27 AstVisitor getVisitor(); | 41 AstVisitor getVisitor(); |
| 28 } | 42 } |
| 29 | 43 |
| 30 /// Traverses a library's worth of dart code at a time to generate lint warnings | 44 /// Traverses a library's worth of dart code at a time to generate lint warnings |
| 31 /// over the set of sources. | 45 /// over the set of sources. |
| 32 /// | 46 /// |
| 33 /// See [LintCode]. | 47 /// See [LintCode]. |
| 34 class LintGenerator { | 48 class LintGenerator { |
| 35 /// A global container for contributed linters. | 49 static const List<Linter> _noLints = const <Linter>[]; |
| 36 @deprecated // Use lintRegistry. | |
| 37 static final List<Linter> LINTERS = <Linter>[]; | |
| 38 | 50 |
| 39 final Iterable<CompilationUnit> _compilationUnits; | 51 final Iterable<CompilationUnit> _compilationUnits; |
| 40 final AnalysisErrorListener _errorListener; | 52 final AnalysisErrorListener _errorListener; |
| 41 final Iterable<Linter> _linters; | 53 final Iterable<Linter> _linters; |
| 42 | 54 |
| 43 LintGenerator(this._compilationUnits, this._errorListener, | 55 LintGenerator(this._compilationUnits, this._errorListener, |
| 44 [Iterable<Linter> linters]) | 56 [Iterable<Linter> linters]) |
| 45 : _linters = linters ?? LINTERS; | 57 : _linters = linters ?? _noLints; |
| 46 | 58 |
| 47 void generate() { | 59 void generate() { |
| 48 PerformanceStatistics.lint.makeCurrentWhile(() { | 60 PerformanceStatistics.lint.makeCurrentWhile(() { |
| 49 _compilationUnits.forEach((cu) { | 61 _compilationUnits.forEach((cu) { |
| 50 if (cu.element != null) { | 62 if (cu.element != null) { |
| 51 _generate(cu, cu.element.source); | 63 _generate(cu, cu.element.source); |
| 52 } | 64 } |
| 53 }); | 65 }); |
| 54 }); | 66 }); |
| 55 } | 67 } |
| 56 | 68 |
| 57 void _generate(CompilationUnit unit, Source source) { | 69 void _generate(CompilationUnit unit, Source source) { |
| 58 ErrorReporter errorReporter = new ErrorReporter(_errorListener, source); | 70 ErrorReporter errorReporter = new ErrorReporter(_errorListener, source); |
| 59 _linters.forEach((l) => l.reporter = errorReporter); | 71 _linters.forEach((l) => l.reporter = errorReporter); |
| 60 Iterable<AstVisitor> visitors = _linters.map((l) => l.getVisitor()); | 72 Iterable<AstVisitor> visitors = _linters.map((l) => l.getVisitor()); |
| 61 unit.accept(new DelegatingAstVisitor(visitors.where((v) => v != null))); | 73 unit.accept(new DelegatingAstVisitor(visitors.where((v) => v != null))); |
| 62 } | 74 } |
| 63 } | 75 } |
| OLD | NEW |