Skip to content

Commit 49429c0

Browse files
FMorschelsrawlins
authored andcommitted
[DAS] Fixes detecting TODO comments before eof
Fixes: #61568 Change-Id: I96929bdf1a6e38848ad4332828d4e80fd2c70ce0 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/464720 Reviewed-by: Brian Wilkerson <[email protected]> Reviewed-by: Samuel Rawlins <[email protected]> Reviewed-by: Paul Berry <[email protected]> Auto-Submit: Felipe Morschel <[email protected]>
1 parent 0f6928c commit 49429c0

File tree

3 files changed

+81
-22
lines changed

3 files changed

+81
-22
lines changed

pkg/analyzer/lib/src/error/todo_finder.dart

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ class TodoFinder {
4242
///
4343
/// @param token the head of the list of tokens being searched
4444
void _gatherTodoComments(Token? token, LineInfo lineInfo) {
45-
while (token != null && !token.isEof) {
45+
while (token != null && (!token.isEof || token.precedingComments != null)) {
4646
Token? commentToken = token.precedingComments;
4747
while (commentToken != null) {
4848
if (commentToken.type == TokenType.SINGLE_LINE_COMMENT ||
@@ -52,6 +52,9 @@ class TodoFinder {
5252
commentToken = commentToken.next;
5353
}
5454
}
55+
if (token.next == token) {
56+
break;
57+
}
5558
token = token.next;
5659
}
5760
}

pkg/analyzer/test/src/diagnostics/todo_test.dart

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,16 @@ main() {
1515

1616
@reflectiveTest
1717
class TodoTest extends PubPackageResolutionTest {
18+
test_eof() async {
19+
await assertErrorsInCode(
20+
r'''
21+
main() {}
22+
// TODO: Implement something else
23+
''',
24+
[error(diag.todo, 13, 30, text: 'TODO: Implement something else')],
25+
);
26+
}
27+
1828
test_fixme() async {
1929
await assertErrorsInCode(
2030
r'''

pkg/linter/test/rules/flutter_style_todos_test.dart

Lines changed: 67 additions & 21 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/diagnostic/diagnostic.dart' as diag;
56
import 'package:test_reflective_loader/test_reflective_loader.dart';
67

78
import '../rule_test_support.dart';
@@ -37,66 +38,94 @@ class FlutterStyleTodosTest extends LintRuleTest {
3738
''',
3839
[
3940
lint(0, 17),
41+
error(diag.todo, 3, 14),
4042
lint(18, 17),
4143
lint(36, 17),
4244
lint(54, 27),
45+
error(diag.todo, 57, 24),
4346
lint(82, 18),
47+
error(diag.todo, 85, 15),
4448
lint(101, 28),
4549
lint(130, 28),
4650
lint(159, 28),
51+
error(diag.todo, 191, 53),
4752
lint(245, 64),
4853
],
4954
);
5055
}
5156

5257
test_badUsername_comma() async {
53-
await assertDiagnostics(r'// TODO(user1,user2): bla', [lint(0, 25)]);
58+
await assertDiagnostics(r'// TODO(user1,user2): bla', [
59+
lint(0, 25),
60+
error(diag.todo, 3, 22),
61+
]);
5462
}
5563

5664
test_badUsername_extraSymbols() async {
57-
await assertDiagnostics(r'// TODO(#12357): bla', [lint(0, 20)]);
65+
await assertDiagnostics(r'// TODO(#12357): bla', [
66+
lint(0, 20),
67+
error(diag.todo, 3, 17),
68+
]);
5869
}
5970

6071
test_charactersBeforeTODO() async {
61-
await assertNoDiagnostics(r'''
72+
await assertDiagnostics(
73+
r'''
6274
// comment TODO(user): bla
6375
/// final todo = Todo(name: 'test todo', description: 'todo description');
6476
/// Something interesting. TODO(someone): this is an ugly test case.
65-
''');
77+
''',
78+
[error(diag.todo, 11, 15), error(diag.todo, 129, 41)],
79+
);
6680
}
6781

6882
test_docComment() async {
69-
await assertDiagnostics(r'/// TODO(user): bla', [lint(0, 19)]);
83+
await assertDiagnostics(r'/// TODO(user): bla', [
84+
lint(0, 19),
85+
error(diag.todo, 4, 15),
86+
]);
7087
}
7188

7289
test_extraColon() async {
73-
await assertDiagnostics(r'// TODO:(user): bla', [lint(0, 19)]);
90+
await assertDiagnostics(r'// TODO:(user): bla', [
91+
lint(0, 19),
92+
error(diag.todo, 3, 16),
93+
]);
7494
}
7595

7696
test_goodPatterns() async {
77-
await assertNoDiagnostics(r'''
97+
await assertDiagnostics(
98+
r'''
7899
// TODO(somebody): something
79100
// TODO(somebody): something, https://github.com/flutter/flutter
80-
''');
101+
''',
102+
[error(diag.todo, 3, 25), error(diag.todo, 32, 61)],
103+
);
81104
}
82105

83106
test_goodPatterns_noLeadingSpace() async {
84-
await assertNoDiagnostics(r'''
107+
await assertDiagnostics(
108+
r'''
85109
//TODO(somebody): something
86110
//TODO(somebody): something, https://github.com/flutter/flutter
87-
''');
111+
''',
112+
[error(diag.todo, 2, 25), error(diag.todo, 30, 61)],
113+
);
88114
}
89115

90116
test_justTodo() async {
91-
await assertDiagnostics(r'// TODO', [lint(0, 7)]);
117+
await assertDiagnostics(r'// TODO', [lint(0, 7), error(diag.todo, 3, 4)]);
92118
}
93119

94120
test_justTodo_noLeadingSpace() async {
95-
await assertDiagnostics(r'//TODO', [lint(0, 6)]);
121+
await assertDiagnostics(r'//TODO', [lint(0, 6), error(diag.todo, 2, 4)]);
96122
}
97123

98124
test_missingColon() async {
99-
await assertDiagnostics(r'// TODO(user) bla', [lint(0, 17)]);
125+
await assertDiagnostics(r'// TODO(user) bla', [
126+
lint(0, 17),
127+
error(diag.todo, 3, 14),
128+
]);
100129
}
101130

102131
test_missingMessage() async {
@@ -105,35 +134,52 @@ class FlutterStyleTodosTest extends LintRuleTest {
105134
//TODO(somebody):
106135
// TODO(somebody):
107136
''',
108-
[lint(0, 17), lint(18, 18)],
137+
[
138+
lint(0, 17),
139+
error(diag.todo, 2, 15),
140+
lint(18, 18),
141+
error(diag.todo, 21, 15),
142+
],
109143
);
110144
}
111145

112146
test_missingParens() async {
113-
await assertDiagnostics(r'// TODO: bla', [lint(0, 12)]);
147+
await assertDiagnostics(r'// TODO: bla', [
148+
lint(0, 12),
149+
error(diag.todo, 3, 9),
150+
]);
114151
}
115152

116153
test_properFormat_dottedUsername() async {
117-
await assertNoDiagnostics(r'// TODO(user.name): bla');
154+
await assertDiagnostics(r'// TODO(user.name): bla', [
155+
error(diag.todo, 3, 20),
156+
]);
118157
}
119158

120159
test_properFormat_hyphenatedUsername() async {
121-
await assertNoDiagnostics(r'// TODO(user-name): bla');
160+
await assertDiagnostics(r'// TODO(user-name): bla', [
161+
error(diag.todo, 3, 20),
162+
]);
122163
}
123164

124165
test_properFormat_simpleUsername() async {
125-
await assertNoDiagnostics(r'// TODO(username): bla');
166+
await assertDiagnostics(r'// TODO(username): bla', [
167+
error(diag.todo, 3, 19),
168+
]);
126169
}
127170

128171
test_slashStar() async {
129-
await assertNoDiagnostics(r'/* TODO bla */');
172+
await assertDiagnostics(r'/* TODO bla */', [error(diag.todo, 3, 8)]);
130173
}
131174

132175
test_slashStarStar() async {
133-
await assertNoDiagnostics(r'/** TODO bla **/');
176+
await assertDiagnostics(r'/** TODO bla **/', [error(diag.todo, 4, 10)]);
134177
}
135178

136179
test_spaceBeforeColon() async {
137-
await assertDiagnostics(r'// TODO(user) : bla', [lint(0, 19)]);
180+
await assertDiagnostics(r'// TODO(user) : bla', [
181+
lint(0, 19),
182+
error(diag.todo, 3, 16),
183+
]);
138184
}
139185
}

0 commit comments

Comments
 (0)