| 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'; | 7 import 'dart:collection'; |
| 8 | 8 |
| 9 import 'package:analyzer/dart/ast/ast.dart'; | 9 import 'package:analyzer/dart/ast/ast.dart'; |
| 10 import 'package:analyzer/error/listener.dart'; | 10 import 'package:analyzer/error/listener.dart'; |
| 11 import 'package:analyzer/src/generated/engine.dart'; | 11 import 'package:analyzer/src/generated/engine.dart'; |
| 12 import 'package:analyzer/src/task/model.dart'; | |
| 13 import 'package:analyzer/task/model.dart'; | |
| 14 | |
| 15 const List<Linter> _noLints = const <Linter>[]; | |
| 16 | |
| 17 /// The descriptor used to associate lints with analysis contexts in | |
| 18 /// configuration data. | |
| 19 final ResultDescriptor<List<Linter>> CONFIGURED_LINTS_KEY = | |
| 20 new ResultDescriptorImpl('configured.lints', _noLints); | |
| 21 | 12 |
| 22 /// Shared lint registry. | 13 /// Shared lint registry. |
| 23 LintRegistry lintRegistry = new LintRegistry(); | 14 LintRegistry lintRegistry = new LintRegistry(); |
| 24 | 15 |
| 25 /// Return lints associated with this [context], or an empty list if there are | 16 /// Return lints associated with this [context], or an empty list if there are |
| 26 /// none. | 17 /// none. |
| 27 List<Linter> getLints(AnalysisContext context) => | 18 List<Linter> getLints(AnalysisContext context) => |
| 28 context.getConfigurationData(CONFIGURED_LINTS_KEY) ?? _noLints; | 19 context.analysisOptions.lintRules; |
| 29 | 20 |
| 30 /// Associate these [lints] with the given [context]. | 21 /// Associate these [lints] with the given [context]. |
| 31 void setLints(AnalysisContext context, List<Linter> lints) { | 22 void setLints(AnalysisContext context, List<Linter> lints) { |
| 32 context.setConfigurationData(CONFIGURED_LINTS_KEY, lints); | 23 AnalysisOptionsImpl options = |
| 24 new AnalysisOptionsImpl.from(context.analysisOptions); |
| 25 options.lintRules = lints; |
| 26 context.analysisOptions = options; |
| 33 } | 27 } |
| 34 | 28 |
| 35 /// Implementers contribute lint warnings via the provided error [reporter]. | 29 /// Implementers contribute lint warnings via the provided error [reporter]. |
| 36 abstract class Linter { | 30 abstract class Linter { |
| 37 /// Used to report lint warnings. | 31 /// Used to report lint warnings. |
| 38 /// NOTE: this is set by the framework before visit begins. | 32 /// NOTE: this is set by the framework before visit begins. |
| 39 ErrorReporter reporter; | 33 ErrorReporter reporter; |
| 40 | 34 |
| 41 /// Linter name. | 35 /// Linter name. |
| 42 String get name; | 36 String get name; |
| 43 | 37 |
| 44 /// 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 |
| 45 /// analysis. | 39 /// analysis. |
| 46 /// Lint errors are reported via this [Linter]'s error [reporter]. | 40 /// Lint errors are reported via this [Linter]'s error [reporter]. |
| 47 AstVisitor getVisitor(); | 41 AstVisitor getVisitor(); |
| 48 } | 42 } |
| 49 | 43 |
| 50 /// Manages lint timing. | 44 /// Manages lint timing. |
| 51 class LintRegistry { | 45 class LintRegistry { |
| 52 /// Dictionary mapping lints (by name) to timers. | 46 /// Dictionary mapping lints (by name) to timers. |
| 53 final Map<String, Stopwatch> timers = new HashMap<String, Stopwatch>(); | 47 final Map<String, Stopwatch> timers = new HashMap<String, Stopwatch>(); |
| 54 | 48 |
| 55 /// Get a timer associated with the given lint rule (or create one if none | 49 /// Get a timer associated with the given lint rule (or create one if none |
| 56 /// exists). | 50 /// exists). |
| 57 Stopwatch getTimer(Linter linter) => | 51 Stopwatch getTimer(Linter linter) => |
| 58 timers.putIfAbsent(linter.name, () => new Stopwatch()); | 52 timers.putIfAbsent(linter.name, () => new Stopwatch()); |
| 59 } | 53 } |
| OLD | NEW |