| 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 /// Types needed to implement "strong" checking in the Dart analyzer. This is | 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. | 6 /// intended to be used by `analyzer_cli` and `analysis_server` packages. |
| 7 library dev_compiler.strong_mode; | 7 library dev_compiler.strong_mode; |
| 8 | 8 |
| 9 import 'package:analyzer/src/generated/engine.dart' | 9 import 'package:analyzer/src/generated/engine.dart' |
| 10 show | 10 show |
| (...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 71 | 71 |
| 72 void onError(AnalysisError error) { | 72 void onError(AnalysisError error) { |
| 73 // Unless DDC hints are requested, filter them out. | 73 // Unless DDC hints are requested, filter them out. |
| 74 var HINT = ErrorSeverity.INFO.ordinal; | 74 var HINT = ErrorSeverity.INFO.ordinal; |
| 75 if (hints || error.errorCode.errorSeverity.ordinal > HINT) { | 75 if (hints || error.errorCode.errorSeverity.ordinal > HINT) { |
| 76 errors.add(error); | 76 errors.add(error); |
| 77 } | 77 } |
| 78 } | 78 } |
| 79 } | 79 } |
| 80 | 80 |
| 81 // TODO(jmesserly): this type is dead now. It's preserved because analyzer_cli |
| 82 // passes the `hints` option. |
| 81 class StrongModeOptions { | 83 class StrongModeOptions { |
| 82 /// Whether to infer types for consts and fields by looking at initializers on | |
| 83 /// the RHS. For example, in a constant declaration like: | |
| 84 /// | |
| 85 /// const A = B; | |
| 86 /// | |
| 87 /// We can infer the type of `A` based on the type of `B`. | |
| 88 /// | |
| 89 /// The inference algorithm determines what variables depend on others, and | |
| 90 /// computes types by visiting the variable dependency graph in topological | |
| 91 /// order. This ensures that the inferred type is deterministic when applying | |
| 92 /// inference on library cycles. | |
| 93 /// | |
| 94 /// When this feature is turned off, we don't use the type of `B` to infer the | |
| 95 /// type of `A`, even if `B` has a declared type. | |
| 96 final bool inferTransitively; | |
| 97 static const inferTransitivelyDefault = true; | |
| 98 | |
| 99 /// Restrict inference of fields and top-levels to those that are final and | |
| 100 /// const. | |
| 101 final bool onlyInferConstsAndFinalFields; | |
| 102 static const onlyInferConstAndFinalFieldsDefault = false; | |
| 103 | |
| 104 /// Whether to infer types downwards from local context | |
| 105 final bool inferDownwards; | |
| 106 static const inferDownwardsDefault = true; | |
| 107 | |
| 108 /// Whether to inject casts between Dart assignable types. | |
| 109 final bool relaxedCasts; | |
| 110 | |
| 111 /// Whether to include hints about dynamic invokes and runtime checks. | 84 /// Whether to include hints about dynamic invokes and runtime checks. |
| 112 // TODO(jmesserly): this option is not used yet by DDC server mode or batch | 85 // TODO(jmesserly): this option is not used yet by DDC server mode or batch |
| 113 // compile to JS. | 86 // compile to JS. |
| 114 final bool hints; | 87 final bool hints; |
| 115 | 88 |
| 116 const StrongModeOptions( | 89 const StrongModeOptions({this.hints: false}); |
| 117 {this.hints: false, | |
| 118 this.inferTransitively: inferTransitivelyDefault, | |
| 119 this.onlyInferConstsAndFinalFields: onlyInferConstAndFinalFieldsDefault, | |
| 120 this.inferDownwards: inferDownwardsDefault, | |
| 121 this.relaxedCasts: true}); | |
| 122 | 90 |
| 123 StrongModeOptions.fromArguments(ArgResults args, {String prefix: ''}) | 91 StrongModeOptions.fromArguments(ArgResults args, {String prefix: ''}) |
| 124 : relaxedCasts = args[prefix + 'relaxed-casts'], | 92 : hints = args[prefix + 'hints']; |
| 125 inferDownwards = args[prefix + 'infer-downwards'], | |
| 126 inferTransitively = args[prefix + 'infer-transitively'], | |
| 127 onlyInferConstsAndFinalFields = args[prefix + 'infer-only-finals'], | |
| 128 hints = args[prefix + 'hints']; | |
| 129 | 93 |
| 130 static ArgParser addArguments(ArgParser parser, | 94 static ArgParser addArguments(ArgParser parser, |
| 131 {String prefix: '', bool hide: false}) { | 95 {String prefix: '', bool hide: false}) { |
| 132 return parser | 96 return parser |
| 133 ..addFlag(prefix + 'hints', | 97 ..addFlag(prefix + 'hints', |
| 134 help: 'Display hints about dynamic casts and dispatch operations', | 98 help: 'Display hints about dynamic casts and dispatch operations', |
| 135 defaultsTo: false, | 99 defaultsTo: false, |
| 136 hide: hide) | |
| 137 ..addFlag(prefix + 'relaxed-casts', | |
| 138 help: 'Cast between Dart assignable types', | |
| 139 defaultsTo: true, | |
| 140 hide: hide) | |
| 141 ..addOption(prefix + 'nonnullable', | |
| 142 abbr: prefix == '' ? 'n' : null, | |
| 143 help: 'Comma separated string of non-nullable types', | |
| 144 defaultsTo: null, | |
| 145 hide: hide) | |
| 146 ..addFlag(prefix + 'infer-downwards', | |
| 147 help: 'Infer types downwards from local context', | |
| 148 defaultsTo: inferDownwardsDefault, | |
| 149 hide: hide) | |
| 150 ..addFlag(prefix + 'infer-transitively', | |
| 151 help: 'Infer consts/fields from definitions in other libraries', | |
| 152 defaultsTo: inferTransitivelyDefault, | |
| 153 hide: hide) | |
| 154 ..addFlag(prefix + 'infer-only-finals', | |
| 155 help: 'Do not infer non-const or non-final fields', | |
| 156 defaultsTo: onlyInferConstAndFinalFieldsDefault, | |
| 157 hide: hide); | 100 hide: hide); |
| 158 } | 101 } |
| 159 | 102 |
| 160 bool operator ==(Object other) { | 103 bool operator ==(Object other) { |
| 161 if (other is! StrongModeOptions) return false; | 104 if (other is! StrongModeOptions) return false; |
| 162 StrongModeOptions s = other; | 105 StrongModeOptions s = other; |
| 163 return inferTransitively == s.inferTransitively && | 106 return hints == s.hints; |
| 164 onlyInferConstsAndFinalFields == s.onlyInferConstsAndFinalFields && | |
| 165 inferDownwards == s.inferDownwards && | |
| 166 relaxedCasts == s.relaxedCasts; | |
| 167 } | 107 } |
| 168 } | 108 } |
| OLD | NEW |