31
31
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32
32
*/
33
33
34
- import 'package:meta/meta.dart' show visibleForTesting, immutable, required;
35
-
36
- // ignore_for_file: avoid_as
34
+ import 'package:meta/meta.dart' show visibleForTesting, immutable;
37
35
38
36
/// Dart lint violation class representation.
39
37
@immutable
@@ -61,32 +59,61 @@ class CodeStyleViolation {
61
59
62
60
/// Create a new [CodeStyleViolation] .
63
61
const CodeStyleViolation ({
64
- @required this .severity,
65
- @required this .type,
66
- @required this .file,
67
- @required this .line,
68
- @required this .lineColumn,
69
- @required this .rule,
70
- @required this .ruleDescription,
71
- }) : assert (severity != null , "severity can't be null" ),
72
- assert (type != null , "type can't be null" ),
73
- assert (file != null , "file can't be null" ),
74
- assert (line != null , "line can't be null" ),
75
- assert (lineColumn != null , "lineColumn can't be null" ),
76
- assert (rule != null , "rule can't be null" ),
77
- assert (ruleDescription != null , "ruleDescription can't be null" );
62
+ required this .severity,
63
+ required this .type,
64
+ required this .file,
65
+ required this .line,
66
+ required this .lineColumn,
67
+ required this .rule,
68
+ required this .ruleDescription,
69
+ });
78
70
79
71
///
80
- CodeStyleViolation .fromJson (final Map <String , Object > json)
81
- : type = json['type' ] as String ,
82
- file = json['file' ] as String ,
83
- line = json['line' ] as int ,
84
- lineColumn = json['lineColumn' ] as int ,
85
- rule = json['rule' ] as String ,
86
- ruleDescription = json['ruleDescription' ] as String ,
87
- severity = ViolationSeverity .fromMap (
88
- json['severity' ] as Map <String , Object >,
89
- );
72
+ factory CodeStyleViolation .fromJson (final Map <String , Object > json) {
73
+ final Object ? type = json['type' ];
74
+ final Object ? file = json['file' ];
75
+ final Object ? line = json['line' ];
76
+ final Object ? lineColumn = json['lineColumn' ];
77
+ final Object ? rule = json['rule' ];
78
+ final Object ? ruleDescription = json['ruleDescription' ];
79
+ final Object ? severity = json['severity' ];
80
+
81
+ return CodeStyleViolation (
82
+ type: type is String
83
+ ? type
84
+ : throw ArgumentError .value (type, 'type' , 'invalid violation type' ),
85
+ file: file is String
86
+ ? file
87
+ : throw ArgumentError .value (type, 'file' , 'invalid file path' ),
88
+ line: line is int
89
+ ? line
90
+ : throw ArgumentError .value (line, 'line' , 'invalid line number' ),
91
+ lineColumn: lineColumn is int
92
+ ? lineColumn
93
+ : throw ArgumentError .value (
94
+ lineColumn,
95
+ 'lineColumn' ,
96
+ 'invalid line column' ,
97
+ ),
98
+ rule: rule is String
99
+ ? rule
100
+ : throw ArgumentError .value (rule, 'rule' , 'invalid rule name' ),
101
+ ruleDescription: ruleDescription is String
102
+ ? ruleDescription
103
+ : throw ArgumentError .value (
104
+ ruleDescription,
105
+ 'ruleDescription' ,
106
+ 'invalid rule description' ,
107
+ ),
108
+ severity: severity is Map <String , Object >
109
+ ? ViolationSeverity .fromMap (severity)
110
+ : throw ArgumentError .value (
111
+ severity,
112
+ 'severity' ,
113
+ 'is not a map of string to object' ,
114
+ ),
115
+ );
116
+ }
90
117
91
118
///
92
119
Map <String , Object > toJson () {
@@ -119,15 +146,15 @@ class CodeStyleViolation {
119
146
@override
120
147
bool operator == (Object other) =>
121
148
identical (this , other) ||
122
- other is CodeStyleViolation &&
123
- runtimeType == other.runtimeType &&
124
- severity == other.severity &&
125
- type == other.type &&
126
- rule == other.rule &&
127
- ruleDescription == other.ruleDescription &&
128
- file == other.file &&
129
- line == other.line &&
130
- lineColumn == other.lineColumn;
149
+ other is CodeStyleViolation &&
150
+ runtimeType == other.runtimeType &&
151
+ severity == other.severity &&
152
+ type == other.type &&
153
+ rule == other.rule &&
154
+ ruleDescription == other.ruleDescription &&
155
+ file == other.file &&
156
+ line == other.line &&
157
+ lineColumn == other.lineColumn;
131
158
132
159
@override
133
160
int get hashCode =>
@@ -172,9 +199,17 @@ class ViolationSeverity {
172
199
final String id;
173
200
174
201
/// Create [ViolationSeverity] from the parameter [json] .
175
- ViolationSeverity .fromMap (final Map <String , Object > json)
176
- : level = json['level' ] as int ,
177
- id = json['id' ] as String ;
202
+ factory ViolationSeverity .fromMap (final Map <String , Object ?> json) {
203
+ final Object ? level = json['level' ];
204
+ final Object ? id = json['id' ];
205
+
206
+ return ViolationSeverity .private (
207
+ level is int
208
+ ? level
209
+ : throw ArgumentError .value (level, 'level' , 'invalid level' ),
210
+ id is String ? id : throw ArgumentError .value (id, 'id' , 'invalid id' ),
211
+ );
212
+ }
178
213
179
214
/// Transform [ViolationSeverity] to a [Map] .
180
215
Map <String , Object > toJson () {
@@ -183,18 +218,20 @@ class ViolationSeverity {
183
218
184
219
/// Create [ViolationSeverity] with [level] and [id] .
185
220
@visibleForTesting
186
- const ViolationSeverity .private (this .level, this .id)
187
- : assert (level != null , "severity can't be null" ),
188
- assert (id != null , "id can't be null" );
221
+ const ViolationSeverity .private (this .level, this .id);
189
222
190
223
/// Create [ViolationSeverity] from the specified [id] .
191
224
///
192
225
/// Notes: the id need to be one of the items in [supportedSeverities] .
193
226
factory ViolationSeverity .withId (final String id) {
194
- final ViolationSeverity lintSeverity = supportedSeverities.firstWhere (
195
- (ViolationSeverity element) => element.id == id,
196
- orElse: () => null ,
197
- );
227
+ ViolationSeverity ? lintSeverity;
228
+
229
+ for (final ViolationSeverity severity in supportedSeverities) {
230
+ if (severity.id == id) {
231
+ lintSeverity = severity;
232
+ break ;
233
+ }
234
+ }
198
235
199
236
if (lintSeverity == null ) {
200
237
throw ArgumentError .value (
@@ -210,10 +247,10 @@ class ViolationSeverity {
210
247
@override
211
248
bool operator == (Object other) =>
212
249
identical (this , other) ||
213
- other is ViolationSeverity &&
214
- runtimeType == other.runtimeType &&
215
- level == other.level &&
216
- id == other.id;
250
+ other is ViolationSeverity &&
251
+ runtimeType == other.runtimeType &&
252
+ level == other.level &&
253
+ id == other.id;
217
254
218
255
@override
219
256
int get hashCode => level.hashCode ^ id.hashCode;
@@ -223,5 +260,5 @@ class ViolationSeverity {
223
260
224
261
/// Contains the list of supported lint rules severities.
225
262
static final List <ViolationSeverity > supportedSeverities =
226
- < ViolationSeverity > [info, warning, error, invalid];
263
+ < ViolationSeverity > [info, warning, error, invalid];
227
264
}
0 commit comments