Chromium Code Reviews| 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 analyzer.src.services.lint; | 5 library analyzer.src.services.lint; |
| 6 | 6 |
| 7 import 'dart:collection'; | |
| 8 | |
| 7 import 'package:analyzer/dart/ast/ast.dart'; | 9 import 'package:analyzer/dart/ast/ast.dart'; |
| 8 import 'package:analyzer/dart/ast/visitor.dart'; | |
| 9 import 'package:analyzer/src/generated/engine.dart'; | 10 import 'package:analyzer/src/generated/engine.dart'; |
| 10 import 'package:analyzer/src/generated/error.dart'; | 11 import 'package:analyzer/src/generated/error.dart'; |
| 11 import 'package:analyzer/src/generated/source.dart'; | |
| 12 import 'package:analyzer/src/task/model.dart'; | 12 import 'package:analyzer/src/task/model.dart'; |
| 13 import 'package:analyzer/task/model.dart'; | 13 import 'package:analyzer/task/model.dart'; |
| 14 | 14 |
| 15 const List<Linter> _noLints = const <Linter>[]; | 15 const List<Linter> _noLints = const <Linter>[]; |
| 16 | 16 |
| 17 /// The descriptor used to associate lints with analysis contexts in | 17 /// The descriptor used to associate lints with analysis contexts in |
| 18 /// configuration data. | 18 /// configuration data. |
| 19 final ResultDescriptor<List<Linter>> CONFIGURED_LINTS_KEY = | 19 final ResultDescriptor<List<Linter>> CONFIGURED_LINTS_KEY = |
| 20 new ResultDescriptorImpl('configured.lints', _noLints); | 20 new ResultDescriptorImpl('configured.lints', _noLints); |
| 21 | 21 |
| 22 /// Shared lint registry. | |
| 23 LintRegistry lintRegistry = new LintRegistry(); | |
| 24 | |
| 22 /// Return lints associated with this [context], or an empty list if there are | 25 /// Return lints associated with this [context], or an empty list if there are |
| 23 /// none. | 26 /// none. |
| 24 List<Linter> getLints(AnalysisContext context) => | 27 List<Linter> getLints(AnalysisContext context) => |
| 25 context.getConfigurationData(CONFIGURED_LINTS_KEY) ?? _noLints; | 28 context.getConfigurationData(CONFIGURED_LINTS_KEY) ?? _noLints; |
| 26 | 29 |
| 27 /// Associate these [lints] with the given [context]. | 30 /// Associate these [lints] with the given [context]. |
| 28 void setLints(AnalysisContext context, List<Linter> lints) { | 31 void setLints(AnalysisContext context, List<Linter> lints) { |
| 29 context.setConfigurationData(CONFIGURED_LINTS_KEY, lints); | 32 context.setConfigurationData(CONFIGURED_LINTS_KEY, lints); |
| 30 } | 33 } |
| 31 | 34 |
| 32 /// Implementers contribute lint warnings via the provided error [reporter]. | 35 /// Implementers contribute lint warnings via the provided error [reporter]. |
| 33 abstract class Linter { | 36 abstract class Linter { |
| 34 /// Used to report lint warnings. | 37 /// Used to report lint warnings. |
| 35 /// NOTE: this is set by the framework before visit begins. | 38 /// NOTE: this is set by the framework before visit begins. |
| 36 ErrorReporter reporter; | 39 ErrorReporter reporter; |
| 37 | 40 |
| 41 /// Linter name. | |
| 42 String get name; | |
| 43 | |
| 38 /// Return a visitor to be passed to compilation units to perform lint | 44 /// Return a visitor to be passed to compilation units to perform lint |
| 39 /// analysis. | 45 /// analysis. |
| 40 /// Lint errors are reported via this [Linter]'s error [reporter]. | 46 /// Lint errors are reported via this [Linter]'s error [reporter]. |
| 41 AstVisitor getVisitor(); | 47 AstVisitor getVisitor(); |
| 42 } | 48 } |
| 43 | 49 |
| 44 /// Traverses a library's worth of dart code at a time to generate lint warnings | |
| 45 /// over the set of sources. | |
| 46 /// | |
| 47 /// See [LintCode]. | |
| 48 class LintGenerator { | |
| 49 static const List<Linter> _noLints = const <Linter>[]; | |
| 50 | 50 |
| 51 final Iterable<CompilationUnit> _compilationUnits; | 51 /// Manages lint timing. |
| 52 final AnalysisErrorListener _errorListener; | 52 class LintRegistry { |
| 53 final Iterable<Linter> _linters; | 53 /// Dictionary mapping lints (by name) to timers. |
| 54 final Map<String, Stopwatch> timers = new HashMap<String, Stopwatch>(); | |
| 54 | 55 |
| 55 LintGenerator(this._compilationUnits, this._errorListener, | 56 /// Get a timer associated with the given lint rule (or create one if none |
| 56 [Iterable<Linter> linters]) | 57 /// exists). |
| 57 : _linters = linters ?? _noLints; | 58 Stopwatch getTimer(Linter linter) { |
| 58 | 59 Stopwatch stopwatch = timers[linter.name]; |
|
Brian Wilkerson
2016/03/15 22:57:24
Or you could use putIfAbsent.
pquitslund
2016/03/15 23:09:17
Good call. Keep forgetting that one!
| |
| 59 void generate() { | 60 if (stopwatch == null) { |
| 60 PerformanceStatistics.lint.makeCurrentWhile(() { | 61 stopwatch = (timers[linter.name] = new Stopwatch()); |
| 61 _compilationUnits.forEach((cu) { | 62 } |
| 62 if (cu.element != null) { | 63 return stopwatch; |
| 63 _generate(cu, cu.element.source); | |
| 64 } | |
| 65 }); | |
| 66 }); | |
| 67 } | |
| 68 | |
| 69 void _generate(CompilationUnit unit, Source source) { | |
| 70 ErrorReporter errorReporter = new ErrorReporter(_errorListener, source); | |
| 71 _linters.forEach((l) => l.reporter = errorReporter); | |
| 72 Iterable<AstVisitor> visitors = _linters.map((l) => l.getVisitor()); | |
| 73 unit.accept(new DelegatingAstVisitor(visitors.where((v) => v != null))); | |
| 74 } | 64 } |
| 75 } | 65 } |
| OLD | NEW |