Skip to content

Commit 569e9b1

Browse files
srawlinsCommit Queue
authored and
Commit Queue
committed
linter: Move tests for no_leading_underscores_for_local_identifiers
Cq-Include-Trybots: luci.dart.try:flutter-analyze-try,analyzer-win-release-try,pkg-win-release-try Change-Id: Ia73b5e1e87e06980221faa935ec1c2bafc2b2070 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/380965 Commit-Queue: Phil Quitslund <[email protected]> Auto-Submit: Samuel Rawlins <[email protected]> Reviewed-by: Phil Quitslund <[email protected]>
1 parent 738e65b commit 569e9b1

File tree

2 files changed

+224
-149
lines changed

2 files changed

+224
-149
lines changed

pkg/linter/test/rules/no_leading_underscores_for_local_identifiers_test.dart

Lines changed: 224 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
// for details. All rights reserved. Use of this source code is governed by a
33
// BSD-style license that can be found in the LICENSE file.
44

5+
import 'package:analyzer/src/error/analyzer_error_code.dart';
56
import 'package:test_reflective_loader/test_reflective_loader.dart';
67

78
import '../rule_test_support.dart';
@@ -14,9 +15,112 @@ main() {
1415

1516
@reflectiveTest
1617
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+
1726
@override
1827
String get lintRule => 'no_leading_underscores_for_local_identifiers';
1928

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+
20124
test_listPattern_ifCase() async {
21125
await assertDiagnostics(r'''
22126
f(Object o) {
@@ -51,6 +155,54 @@ f() {
51155
]);
52156
}
53157

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+
54206
test_mapPattern_destructured() async {
55207
await assertDiagnostics(r'''
56208
f() {
@@ -83,6 +235,14 @@ f() {
83235
]);
84236
}
85237

238+
test_method() async {
239+
await assertNoDiagnostics(r'''
240+
class C {
241+
void _m() {}
242+
}
243+
''');
244+
}
245+
86246
test_objectPattern_destructured() async {
87247
await assertDiagnostics(r'''
88248
class A {
@@ -159,6 +319,46 @@ f(A a) {
159319
''');
160320
}
161321

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+
162362
test_recordPattern_destructured() async {
163363
await assertDiagnostics(r'''
164364
f() {
@@ -210,4 +410,28 @@ f() {
210410
lint(40, 2),
211411
]);
212412
}
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+
}
213437
}

0 commit comments

Comments
 (0)