[test util] display error info on failed parse
Example output:
```
Invalid argument(s): Content produced diagnostics when parsed:
MISSING_IDENTIFIER: Expected an identifier. - 9:15
EXPECTED_TOKEN: Expected to find ';'. - 9:7
MISSING_IDENTIFIER: Expected an identifier. - 9:12
MISSING_IDENTIFIER: Expected an identifier. - 9:10
EXPECTED_TOKEN: Expected to find ':'. - 9:15
UNEXPECTED_TOKEN: Unexpected text ';'. - 9:15
EXPECTED_TOKEN: Expected to find ';'. - 9:15
```
Change-Id: Icbeeaca147e70e553a3c2b7809edff1c8fb8721f
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/195049
Commit-Queue: Phil Quitslund <pquitslund@google.com>
Reviewed-by: Konstantin Shcheglov <scheglov@google.com>
Reviewed-by: Brian Wilkerson <brianwilkerson@google.com>
diff --git a/pkg/analyzer/lib/dart/analysis/utilities.dart b/pkg/analyzer/lib/dart/analysis/utilities.dart
index 28ff71b..801d224 100644
--- a/pkg/analyzer/lib/dart/analysis/utilities.dart
+++ b/pkg/analyzer/lib/dart/analysis/utilities.dart
@@ -88,11 +88,18 @@
featureSet: scanner.featureSet,
);
var unit = parser.parseCompilationUnit(token);
- unit.lineInfo = LineInfo(scanner.lineStarts);
+ var lineInfo = LineInfo(scanner.lineStarts);
+ unit.lineInfo = lineInfo;
ParseStringResult result =
ParseStringResultImpl(content, unit, errorCollector.errors);
if (throwIfDiagnostics && result.errors.isNotEmpty) {
- throw ArgumentError('Content produced diagnostics when parsed');
+ var buffer = StringBuffer();
+ for (var error in result.errors) {
+ var location = lineInfo.getLocation(error.offset);
+ buffer.writeln(' ${error.errorCode.name}: ${error.message} - '
+ '${location.lineNumber}:${location.columnNumber}');
+ }
+ throw ArgumentError('Content produced diagnostics when parsed:\n$buffer');
}
return result;
}