Fix multi-line error handling in analyze.dart

Preexisting comments on multi-line errors modify the length of the error
reported from the analyzer since it includes the comments in between. So
the error length increases every time you run this script, making the
error comments unwieldly.

Change-Id: Iaeab9118710738f3ebf534d85b7fd08be0bf302f
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/136526
Commit-Queue: Srujan Gaddam <srujzs@google.com>
Auto-Submit: Srujan Gaddam <srujzs@google.com>
Reviewed-by: Bob Nystrom <rnystrom@google.com>
This commit is contained in:
Srujan Gaddam 2020-02-20 19:12:11 +00:00 committed by commit-bot@chromium.org
parent 561039dd9e
commit 2109617eb2

View file

@ -21,6 +21,12 @@ bool analyzeTests(String nnbdTestDir) {
}
}
// Pre-existing multi-line errors will modify the character length in the
// errors reported by the analyzer. Strip all errors first before updating.
for (var file in files.values) {
if (!dryRun) _removeErrors(file.path);
}
// Analyze the directory both in legacy and NNBD modes.
var legacyErrors = _runAnalyzer(nnbdTestDir, nnbd: false);
var nnbdErrors = _runAnalyzer(nnbdTestDir, nnbd: true);
@ -65,7 +71,7 @@ bool analyzeTests(String nnbdTestDir) {
errorFileCount++;
}
if (!dryRun) _updateErrors(file.path, file.addedErrors);
if (!dryRun) _insertErrors(file.path, file.addedErrors);
}
if (errorCount == 0) {
@ -106,9 +112,8 @@ Map<String, List<_StaticError>> _runAnalyzer(String inputDir, {bool nnbd}) {
return errorsByFile;
}
/// Removes any previously inserted errors from the file at [path] and inserts
/// any new errors in [errors].
void _updateErrors(String path, List<_StaticError> errors) {
/// Removes pre-existing errors in the file at [path].
void _removeErrors(String path) {
// Sanity check.
if (!p.isWithin(testRoot, path)) {
throw ArgumentError("$path is outside of test directory.");
@ -125,7 +130,26 @@ void _updateErrors(String path, List<_StaticError> errors) {
} else {
changed = true;
}
}
if (changed) {
writeFile(path, result.toString());
}
}
/// Inserts any new errors in [errors] into the file at [path].
void _insertErrors(String path, List<_StaticError> errors) {
// Sanity check.
if (!p.isWithin(testRoot, path)) {
throw ArgumentError("$path is outside of test directory.");
}
var lines = readFileLines(path);
var result = StringBuffer();
var changed = false;
for (var i = 0; i < lines.length; i++) {
result.writeln(lines[i]);
// TODO(rnystrom): Inefficient.
for (var error in errors) {
if (error.line == i + 1) {