[messages] Move LocatedError to its own library.

This will make it possible to re-use in other parts of the codebase
without necessarily having to import logic related to messages.

Change-Id: I6a6a6964ad824e13f45ef8da6fc50f4a120ea74a
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/462701
Commit-Queue: Paul Berry <paulberry@google.com>
Reviewed-by: Konstantin Shcheglov <scheglov@google.com>
This commit is contained in:
Paul Berry
2025-11-18 13:08:41 -08:00
committed by Commit Queue
parent 9f524dfc4d
commit 8dd459624e
5 changed files with 48 additions and 39 deletions
+1
View File
@@ -20,6 +20,7 @@ import 'dart:convert';
import 'package:analyzer_testing/package_root.dart' as pkg_root;
import 'package:analyzer_utilities/analyzer_messages.dart';
import 'package:analyzer_utilities/located_error.dart';
import 'package:analyzer_utilities/messages.dart';
import 'package:analyzer_utilities/tools.dart';
@@ -11,6 +11,7 @@ import 'dart:io';
import 'package:analyzer_testing/package_root.dart' as pkg_root;
import 'package:analyzer_utilities/analyzer_message_constant_style.dart';
import 'package:analyzer_utilities/extensions/string.dart';
import 'package:analyzer_utilities/located_error.dart';
import 'package:analyzer_utilities/messages.dart';
import 'package:analyzer_utilities/tools.dart';
import 'package:path/path.dart';
@@ -0,0 +1,44 @@
// Copyright (c) 2025, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
import 'package:source_span/source_span.dart';
/// An error with an associated source span.
class LocatedError {
final SourceSpan span;
final String message;
LocatedError(this.message, {required this.span});
@override
String toString() => '${span.location}: $message';
/// Executes [callback], converting any exceptions it generates to a
/// [LocatedError] that points to [node].
static T wrap<T>(T Function() callback, {required SourceSpan span}) {
try {
return callback();
} catch (error, stackTrace) {
if (error is! LocatedError) {
Error.throwWithStackTrace(
LocatedError(error.toString(), span: span),
stackTrace,
);
} else {
rethrow;
}
}
}
}
extension SourceSpanLocation on SourceSpan {
/// A string suitable for identifying this span in the source YAML file.
String get location {
var path = start.sourceUrl?.toFilePath() ?? '<unknown>';
// Convert line/column to 1-based because that's what most editors expect
var line = start.line + 1;
var column = start.column + 1;
return '$path:$line:$column';
}
}
+1 -39
View File
@@ -11,6 +11,7 @@ import 'dart:io';
import 'package:analyzer_testing/package_root.dart' as pkg_root;
import 'package:analyzer_utilities/analyzer_messages.dart';
import 'package:analyzer_utilities/extensions/string.dart';
import 'package:analyzer_utilities/located_error.dart';
import 'package:collection/collection.dart';
import 'package:path/path.dart';
import 'package:source_span/source_span.dart';
@@ -554,34 +555,6 @@ class LabelerConversion implements Conversion {
}) => 'labeler.$methodName($name)';
}
/// An error with an associated source span.
class LocatedError {
final SourceSpan span;
final String message;
LocatedError(this.message, {required this.span});
@override
String toString() => '${span.location}: $message';
/// Executes [callback], converting any exceptions it generates to a
/// [LocatedError] that points to [node].
static T wrap<T>(T Function() callback, {required SourceSpan span}) {
try {
return callback();
} catch (error, stackTrace) {
if (error is! LocatedError) {
Error.throwWithStackTrace(
LocatedError(error.toString(), span: span),
stackTrace,
);
} else {
rethrow;
}
}
}
}
/// In-memory representation of diagnostic information obtained from either the
/// analyzer or the front end's `messages.yaml` file. This class contains the
/// common functionality supported by both formats.
@@ -1109,14 +1082,3 @@ class _DuplicateChecker<Code> {
}
}
}
extension SourceSpanLocation on SourceSpan {
/// A string suitable for identifying this span in the source YAML file.
String get location {
var path = start.sourceUrl?.toFilePath() ?? '<unknown>';
// Convert line/column to 1-based because that's what most editors expect
var line = start.line + 1;
var column = start.column + 1;
return '$path:$line:$column';
}
}
@@ -6,6 +6,7 @@
library;
import 'package:analyzer_utilities/extensions/string.dart';
import 'package:analyzer_utilities/located_error.dart';
import 'package:analyzer_utilities/messages.dart';
Uri computeSharedGeneratedFile(Uri repoDir) {