Skip to content
This repository was archived by the owner on Feb 25, 2025. It is now read-only.

Commit 0345403

Browse files
srawlinscommit-bot@chromium.org
authored andcommitted
dart migrate: insert comment between first comment and first directive
Fixes dart-lang/sdk#44704 Change-Id: I07402a958f7940bb603253a60e43709fc07529b1 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/179920 Reviewed-by: Paul Berry <paulberry@google.com> Commit-Queue: Samuel Rawlins <srawlins@google.com>
1 parent 8c21692 commit 0345403

File tree

2 files changed

+48
-16
lines changed

2 files changed

+48
-16
lines changed

pkg/nnbd_migration/lib/src/preview/preview_site.dart

Lines changed: 31 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,10 @@ class IncrementalPlan {
179179

180180
var index = 0;
181181

182+
// Returns the next line and updates [index].
183+
//
184+
// After this function returns, [index] points to the character after the
185+
// end of the line which was returned.
182186
String getLine() {
183187
var nextIndex = code.indexOf('\n', index);
184188
if (nextIndex < 0) {
@@ -220,24 +224,37 @@ class IncrementalPlan {
220224
// [code] consists _only_ of one comment line.
221225
return '$code$newline$newline// @dart=2.9$newline';
222226
}
223-
line = getLine();
224-
lineStart = line.indexOf(_nonWhitespaceChar);
225-
while (lineStart >= 0 &&
226-
line.length > lineStart + 1 &&
227-
line.codeUnitAt(lineStart) == $slash &&
228-
line.codeUnitAt(lineStart + 1) == $slash) {
229-
// Another comment line.
227+
var previousLineIndex = index;
228+
while (true) {
229+
previousLineIndex = index;
230230
line = getLine();
231-
if (index == length) {
232-
// [code] consists _only_ of this block comment.
233-
return '$code$newline$newline// @dart=2.9$newline';
234-
}
235231
lineStart = line.indexOf(_nonWhitespaceChar);
232+
if (lineStart < 0) {
233+
// Line of whitespace; end of block comment.
234+
break;
235+
}
236+
if (line.length <= lineStart + 1) {
237+
// Only one character; not a comment; end of block comment.
238+
break;
239+
}
240+
if (line.codeUnitAt(lineStart) == $slash &&
241+
line.codeUnitAt(lineStart + 1) == $slash) {
242+
// Comment line.
243+
if (index == length) {
244+
// [code] consists _only_ of this block comment.
245+
return '$code$newline$newline// @dart=2.9$newline';
246+
}
247+
continue;
248+
} else {
249+
// Non-blank, non-comment line.
250+
break;
251+
}
236252
}
237-
// [index] points to the start of [line], which is the first
253+
// [previousLineIndex] points to the start of [line], which is the first
238254
// non-comment line following the first comment.
239-
return '${code.substring(0, index)}$newline// @dart=2.9$newline$newline'
240-
'${code.substring(index)}';
255+
return '${code.substring(0, previousLineIndex)}$newline'
256+
'// @dart=2.9$newline$newline'
257+
'${code.substring(previousLineIndex)}';
241258
} else {
242259
// [code] does not start with a block comment.
243260
return '// @dart=2.9$newline$newline$code';

pkg/nnbd_migration/test/preview/preview_site_test.dart

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -156,14 +156,29 @@ void main(List args) {
156156
void test_optOutOfNullSafety_commentThenCode() {
157157
expect(
158158
IncrementalPlan.optCodeOutOfNullSafety('// comment\n\nvoid main() {}'),
159-
equals('// comment\n\n\n// @dart=2.9\n\nvoid main() {}'));
159+
equals('// comment\n\n// @dart=2.9\n\n\nvoid main() {}'));
160160
}
161161

162162
void test_optOutOfNullSafety_commentThenCode_windows() {
163163
expect(
164164
IncrementalPlan.optCodeOutOfNullSafety(
165165
'// comment\r\n\r\nvoid main() {}'),
166-
equals('// comment\r\n\r\n\r\n// @dart=2.9\r\n\r\nvoid main() {}'));
166+
equals('// comment\r\n\r\n// @dart=2.9\r\n\r\n\r\nvoid main() {}'));
167+
}
168+
169+
void test_optOutOfNullSafety_commentThenDirective() {
170+
expect(
171+
IncrementalPlan.optCodeOutOfNullSafety(
172+
'// comment\nimport "dart:core";'),
173+
equals('// comment\n\n// @dart=2.9\n\nimport "dart:core";'));
174+
}
175+
176+
void test_optOutOfNullSafety_commentThenDirective_multiLine() {
177+
expect(
178+
IncrementalPlan.optCodeOutOfNullSafety(
179+
'// comment\n// comment\nimport "dart:core";'),
180+
equals(
181+
'// comment\n// comment\n\n// @dart=2.9\n\nimport "dart:core";'));
167182
}
168183

169184
void test_optOutOfNullSafety_empty() {

0 commit comments

Comments
 (0)