OLD | NEW |
| (Empty) |
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 | |
3 // BSD-style license that can be found in the LICENSE file. | |
4 | |
5 /// Types needed to implement "strong" checking in the Dart analyzer. This is | |
6 /// intended to be used by `analyzer_cli` and `analysis_server` packages. | |
7 library dev_compiler.strong_mode; | |
8 | |
9 import 'package:analyzer/src/generated/engine.dart' | |
10 show | |
11 AnalysisContext, | |
12 AnalysisContextImpl, | |
13 AnalysisEngine, | |
14 AnalysisErrorInfo, | |
15 AnalysisErrorInfoImpl; | |
16 import 'package:analyzer/src/generated/error.dart' | |
17 show | |
18 AnalysisError, | |
19 AnalysisErrorListener, | |
20 CompileTimeErrorCode, | |
21 ErrorCode, | |
22 ErrorSeverity, | |
23 HintCode, | |
24 StaticTypeWarningCode; | |
25 import 'package:analyzer/src/generated/source.dart' show Source; | |
26 import 'package:args/args.dart'; | |
27 | |
28 import 'src/analysis_context.dart' show enableDevCompilerInference; | |
29 import 'src/checker/checker.dart' show CodeChecker; | |
30 import 'src/checker/rules.dart' show TypeRules; | |
31 | |
32 /// A type checker for Dart code that operates under stronger rules, and has | |
33 /// the ability to do local type inference in some situations. | |
34 // TODO(jmesserly): remove this class. | |
35 class StrongChecker { | |
36 final AnalysisContext _context; | |
37 final CodeChecker _checker; | |
38 final _ErrorCollector _reporter; | |
39 | |
40 StrongChecker._(this._context, this._checker, this._reporter); | |
41 | |
42 factory StrongChecker(AnalysisContext context, StrongModeOptions options) { | |
43 // TODO(vsm): Remove this once analyzer_cli is completely switched to the | |
44 // task model. | |
45 if (!AnalysisEngine.instance.useTaskModel) { | |
46 enableDevCompilerInference(context, options); | |
47 var rules = new TypeRules(context.typeProvider); | |
48 var reporter = new _ErrorCollector(options.hints); | |
49 var checker = new CodeChecker(rules, reporter); | |
50 return new StrongChecker._(context, checker, reporter); | |
51 } | |
52 return new StrongChecker._(context, null, null); | |
53 } | |
54 | |
55 /// Computes and returns DDC errors for the [source]. | |
56 AnalysisErrorInfo computeErrors(Source source) { | |
57 var errors = new List<AnalysisError>(); | |
58 if (_checker != null) { | |
59 _reporter.errors = errors; | |
60 | |
61 for (Source librarySource in _context.getLibrariesContaining(source)) { | |
62 var resolved = _context.resolveCompilationUnit2(source, librarySource); | |
63 _checker.visitCompilationUnit(resolved); | |
64 } | |
65 _reporter.errors = null; | |
66 } | |
67 return new AnalysisErrorInfoImpl(errors, _context.getLineInfo(source)); | |
68 } | |
69 } | |
70 | |
71 class _ErrorCollector implements AnalysisErrorListener { | |
72 List<AnalysisError> errors; | |
73 final bool hints; | |
74 _ErrorCollector(this.hints); | |
75 | |
76 void onError(AnalysisError error) { | |
77 // Unless DDC hints are requested, filter them out. | |
78 var HINT = ErrorSeverity.INFO.ordinal; | |
79 if (hints || error.errorCode.errorSeverity.ordinal > HINT) { | |
80 errors.add(error); | |
81 } | |
82 } | |
83 } | |
84 | |
85 // TODO(jmesserly): this type is dead now. It's preserved because analyzer_cli | |
86 // passes the `hints` option. | |
87 class StrongModeOptions { | |
88 /// Whether to include hints about dynamic invokes and runtime checks. | |
89 // TODO(jmesserly): this option is not used yet by DDC server mode or batch | |
90 // compile to JS. | |
91 final bool hints; | |
92 | |
93 const StrongModeOptions({this.hints: false}); | |
94 | |
95 StrongModeOptions.fromArguments(ArgResults args, {String prefix: ''}) | |
96 : hints = args[prefix + 'hints']; | |
97 | |
98 static ArgParser addArguments(ArgParser parser, | |
99 {String prefix: '', bool hide: false}) { | |
100 return parser | |
101 ..addFlag(prefix + 'hints', | |
102 help: 'Display hints about dynamic casts and dispatch operations', | |
103 defaultsTo: false, | |
104 hide: hide); | |
105 } | |
106 | |
107 bool operator ==(Object other) { | |
108 if (other is! StrongModeOptions) return false; | |
109 StrongModeOptions s = other; | |
110 return hints == s.hints; | |
111 } | |
112 } | |
OLD | NEW |