2
2
// for details. All rights reserved. Use of this source code is governed by a
3
3
// BSD-style license that can be found in the LICENSE file.
4
4
5
+ import 'package:analyzer/src/error/analyzer_error_code.dart' ;
5
6
import 'package:test_reflective_loader/test_reflective_loader.dart' ;
6
7
7
8
import '../rule_test_support.dart' ;
@@ -14,9 +15,112 @@ main() {
14
15
15
16
@reflectiveTest
16
17
class NoLeadingUnderscoresForLocalIdentifiersTest extends LintRuleTest {
18
+ @override
19
+ List <AnalyzerErrorCode > get ignoredErrorCodes => [
20
+ WarningCode .UNUSED_CATCH_STACK ,
21
+ WarningCode .UNUSED_ELEMENT ,
22
+ WarningCode .UNUSED_FIELD ,
23
+ WarningCode .UNUSED_LOCAL_VARIABLE ,
24
+ ];
25
+
17
26
@override
18
27
String get lintRule => 'no_leading_underscores_for_local_identifiers' ;
19
28
29
+ test_catchClause_error () async {
30
+ await assertDiagnostics (r'''
31
+ void f() {
32
+ try {}
33
+ catch(_error) {}
34
+ }
35
+ ''' , [
36
+ lint (28 , 6 ),
37
+ ]);
38
+ }
39
+
40
+ test_catchClause_error_justUnderscore () async {
41
+ await assertNoDiagnostics (r'''
42
+ void f() {
43
+ try {}
44
+ catch(_) {}
45
+ }
46
+ ''' );
47
+ }
48
+
49
+ test_catchClause_stackTrace () async {
50
+ await assertDiagnostics (r'''
51
+ void f() {
52
+ try {}
53
+ catch(error, _stackTrace) {}
54
+ }
55
+ ''' , [
56
+ lint (35 , 11 ),
57
+ ]);
58
+ }
59
+
60
+ test_field () async {
61
+ await assertNoDiagnostics (r'''
62
+ class C {
63
+ var _foo = 0;
64
+ }
65
+ ''' );
66
+ }
67
+
68
+ test_fieldFormalParameter () async {
69
+ // https://github.com/dart-lang/linter/issues/3127
70
+ await assertNoDiagnostics (r'''
71
+ class C {
72
+ final int _p;
73
+ C(this._p);
74
+ }
75
+ ''' );
76
+ }
77
+
78
+ test_forEach () async {
79
+ await assertDiagnostics (r'''
80
+ void f() {
81
+ for(var _x in [1,2,3]) {}
82
+ }
83
+ ''' , [
84
+ lint (21 , 2 ),
85
+ ]);
86
+ }
87
+
88
+ test_forEach_justUnderscore () async {
89
+ await assertNoDiagnostics (r'''
90
+ void f() {
91
+ for(var _ in [1,2,3]) {}
92
+ }
93
+ ''' );
94
+ }
95
+
96
+ test_forEach_noUnderscore () async {
97
+ await assertNoDiagnostics (r'''
98
+ void f() {
99
+ for(var x in [1,2,3]) {}
100
+ }
101
+ ''' );
102
+ }
103
+
104
+ test_forLoop_firstVariable () async {
105
+ await assertDiagnostics (r'''
106
+ void f() {
107
+ for (var _i = 0;;) {}
108
+ }
109
+ ''' , [
110
+ lint (22 , 2 ),
111
+ ]);
112
+ }
113
+
114
+ test_forLoop_multipleVariables () async {
115
+ await assertDiagnostics (r'''
116
+ void f() {
117
+ for (var i = 0, _j = 0;;) {}
118
+ }
119
+ ''' , [
120
+ lint (29 , 2 ),
121
+ ]);
122
+ }
123
+
20
124
test_listPattern_ifCase () async {
21
125
await assertDiagnostics (r'''
22
126
f(Object o) {
@@ -51,6 +155,54 @@ f() {
51
155
]);
52
156
}
53
157
158
+ test_localFunction () async {
159
+ await assertDiagnostics (r'''
160
+ class C {
161
+ void m() {
162
+ int _f() => 10;
163
+ }
164
+ }
165
+ ''' , [
166
+ lint (31 , 2 ),
167
+ ]);
168
+ }
169
+
170
+ test_localVariable () async {
171
+ await assertDiagnostics (r'''
172
+ void f() {
173
+ var _foo = 0;
174
+ }
175
+ ''' , [
176
+ lint (17 , 4 ),
177
+ ]);
178
+ }
179
+
180
+ test_localVariable_internalUnderscore () async {
181
+ await assertNoDiagnostics (r'''
182
+ void f() {
183
+ var p_p = 0;
184
+ }
185
+ ''' );
186
+ }
187
+
188
+ test_localVariable_justUnderscore () async {
189
+ await assertNoDiagnostics (r'''
190
+ void f() {
191
+ var _ = 0;
192
+ }
193
+ ''' );
194
+ }
195
+
196
+ test_localVariable_multipleVariables () async {
197
+ await assertDiagnostics (r'''
198
+ void f() {
199
+ var x = 1, _y = 2;
200
+ }
201
+ ''' , [
202
+ lint (24 , 2 ),
203
+ ]);
204
+ }
205
+
54
206
test_mapPattern_destructured () async {
55
207
await assertDiagnostics (r'''
56
208
f() {
@@ -83,6 +235,14 @@ f() {
83
235
]);
84
236
}
85
237
238
+ test_method () async {
239
+ await assertNoDiagnostics (r'''
240
+ class C {
241
+ void _m() {}
242
+ }
243
+ ''' );
244
+ }
245
+
86
246
test_objectPattern_destructured () async {
87
247
await assertDiagnostics (r'''
88
248
class A {
@@ -159,6 +319,46 @@ f(A a) {
159
319
''' );
160
320
}
161
321
322
+ test_parameter () async {
323
+ await assertDiagnostics (r'''
324
+ void f(int _p) {}
325
+ ''' , [
326
+ lint (11 , 2 ),
327
+ ]);
328
+ }
329
+
330
+ test_parameter_internalUnderscore () async {
331
+ await assertNoDiagnostics (r'''
332
+ void f(int p_p) {}
333
+ ''' );
334
+ }
335
+
336
+ test_parameter_justUnderscore () async {
337
+ await assertNoDiagnostics (r'''
338
+ void f(int _) {}
339
+ ''' );
340
+ }
341
+
342
+ test_parameter_named () async {
343
+ await assertNoDiagnostics (r'''
344
+ // ignore: private_optional_parameter
345
+ void f({int? _n}) {}
346
+ ''' );
347
+ }
348
+
349
+ test_parameter_namedRequired () async {
350
+ await assertNoDiagnostics (r'''
351
+ // ignore: private_optional_parameter
352
+ void f({required int _n}) {}
353
+ ''' );
354
+ }
355
+
356
+ test_parameter_noUnderscore () async {
357
+ await assertNoDiagnostics (r'''
358
+ void f(int p) {}
359
+ ''' );
360
+ }
361
+
162
362
test_recordPattern_destructured () async {
163
363
await assertDiagnostics (r'''
164
364
f() {
@@ -210,4 +410,28 @@ f() {
210
410
lint (40 , 2 ),
211
411
]);
212
412
}
413
+
414
+ test_superFormalParameter () async {
415
+ await assertNoDiagnostics (r'''
416
+ class C {
417
+ int _i;
418
+ C(this._i);
419
+ }
420
+ class D extends C {
421
+ D(super._i);
422
+ }
423
+ ''' );
424
+ }
425
+
426
+ test_topLevelVariable () async {
427
+ await assertNoDiagnostics (r'''
428
+ var _foo = 0;
429
+ ''' );
430
+ }
431
+
432
+ test_typedef () async {
433
+ await assertNoDiagnostics (r'''
434
+ typedef _T = void Function(String);
435
+ ''' );
436
+ }
213
437
}
0 commit comments