Skip to content

Commit 82afcb4

Browse files
munificentCommit Queue
authored andcommitted
Make the static error test updater not write comments at column zero.
The old formatter had a special rule that if a line comment as at the left edge of the page, it would stay there regardless of the surrounding indentation. So if you had: ``` class C { m() { // comment } } ``` After formatting, the comment would still be there instead of being indented. The intent of that was to not shift over code that had been commented out. But all of the IDEs I tested don't actually work that way. When they comment out code, they tend to put the `//` at the indentation of the surrounding code. So the new formatter doesn't have this special rule and always indents line comments following the surrounding indentation. This is good because it also means that code generators that don't write any leading whitespace will still get nicely formatted comments. However, the static error test updater took advantage of this rule and would write static error marker comments at column zero if needed to get the carets to align with the code on the previous line and assume that the formatter wouldn't move the comment. This fixes the static error test updater. It always writes comments using indentation from the previous line of code and if the caret doesn't fit that way, it uses an explicit column marker. Fix #57042. Change-Id: I40fd7cd19d08dc228b6a6797e6a26965d1343d32 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/394363 Reviewed-by: Nate Bosch <[email protected]> Auto-Submit: Bob Nystrom <[email protected]> Commit-Queue: Nate Bosch <[email protected]>
1 parent 0df705a commit 82afcb4

File tree

2 files changed

+39
-46
lines changed

2 files changed

+39
-46
lines changed

pkg/test_runner/lib/src/update_errors.dart

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -105,13 +105,15 @@ String updateErrorExpectations(
105105
var previousLength = -1;
106106

107107
for (var error in errorsHere) {
108-
// Try to indent the line nicely to match either the existing expectation
109-
// that is being regenerated, or, barring that, the previous line of code.
110-
var indent = indentation[i + 1] ?? previousIndent;
108+
// Try to indent the line nicely to match the existing expectation that
109+
// is being regenerated. If that collides with the carets, then indent
110+
// the line based on the preceding line of code. If the caret still
111+
// doesn't fit with that indentation, we'll use an explicit location.
112+
var indent = indentation[i + 1];
113+
if (indent == null || error.column - 1 < indent + 2) {
114+
indent = previousIndent;
115+
}
111116

112-
// If the error is to the left of the indent and the "//", sacrifice the
113-
// indentation.
114-
if (error.column - 1 < indent + 2) indent = 0;
115117
var comment = "${" " * indent}//";
116118

117119
// Write the location line, unless we already have an identical one. Allow
@@ -120,12 +122,11 @@ String updateErrorExpectations(
120122
(previousLength != 0 &&
121123
error.length != 0 &&
122124
error.length != previousLength)) {
123-
// If the error can't fit in a line comment, or no source location is
124-
// specified, use an explicit location.
125-
if (error.column <= 2) {
125+
// If the error location starts to the left of the line comment, or no
126+
// error length is specified, use an explicit location.
127+
if (error.column - 1 < indent + 2) {
126128
if (error.length == 0) {
127-
result.add("$comment [error column "
128-
"${error.column}]");
129+
result.add("$comment [error column ${error.column}]");
129130
} else {
130131
result.add("$comment [error column "
131132
"${error.column}, length ${error.length}]");

pkg/test_runner/test/update_errors_test.dart

Lines changed: 27 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -229,54 +229,46 @@ main() {
229229
}
230230
""");
231231

232-
// Discards indentation if it would collide with carets.
232+
// Discards previous error indentation if it would collide with carets.
233233
expectUpdate("""
234+
// This fits:
234235
int i = "bad";
235-
/\/ ^^
236-
/\/ [analyzer] previous.error
236+
/\/ ^^
237+
/\/ [analyzer] previous.error
237238
238-
main() {
239-
int i =
240-
"bad";
241-
}
239+
// This does not:
240+
int j = "bad";
241+
/\/ ^^
242+
/\/ [analyzer] previous.error
242243
""", errors: [
243-
makeError(line: 1, column: 9, length: 5, cfeError: "Error."),
244-
makeError(line: 7, column: 3, length: 5, analyzerError: "new.error"),
244+
makeError(line: 2, column: 9, length: 5, cfeError: "Error."),
245+
makeError(line: 7, column: 9, length: 5, cfeError: "Error."),
245246
], expected: """
247+
// This fits:
246248
int i = "bad";
249+
/\/^^^^^
250+
/\/ [cfe] Error.
251+
252+
// This does not:
253+
int j = "bad";
247254
/\/ ^^^^^
248255
/\/ [cfe] Error.
249-
250-
main() {
251-
int i =
252-
"bad";
253-
/\/^^^^^
254-
/\/ [analyzer] new.error
255-
}
256256
""");
257257

258-
// Uses an explicit error location if there's no room for the carets.
258+
// Uses explicit location if indenting based on preceding code would collide
259+
// with carets.
259260
expectUpdate("""
260-
int i =
261-
"bad";
262-
/\/ ^^
263-
/\/ [analyzer] previous.error
264-
265-
int j =
266-
"bad";
261+
main() {
262+
["bad"];
263+
}
267264
""", errors: [
268-
makeError(line: 2, column: 1, length: 5, analyzerError: "updated.error"),
269-
makeError(line: 7, column: 1, length: 5, cfeError: "Error."),
265+
makeError(line: 2, column: 4, length: 5, cfeError: "Error."),
270266
], expected: """
271-
int i =
272-
"bad";
273-
/\/ [error column 1, length 5]
274-
/\/ [analyzer] updated.error
275-
276-
int j =
277-
"bad";
278-
/\/ [error column 1, length 5]
279-
/\/ [cfe] Error.
267+
main() {
268+
["bad"];
269+
/\/ [error column 4, length 5]
270+
/\/ [cfe] Error.
271+
}
280272
""");
281273

282274
// Uses length one if there's no length.

0 commit comments

Comments
 (0)