Skip to content

Commit 6c09956

Browse files
pqkevmoo
authored andcommitted
Error reporting for spans.
R=brianwilkerson@google.com, scheglov@google.com Review URL: https://codereview.chromium.org/1418973002 . (cherry picked from commit 0881b53)
1 parent baab34a commit 6c09956

File tree

2 files changed

+33
-0
lines changed

2 files changed

+33
-0
lines changed

Diff for: pkg/analyzer/lib/src/generated/error.dart

+11
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ library engine.error;
66

77
import 'dart:collection';
88

9+
import 'package:source_span/source_span.dart';
10+
911
import 'ast.dart' show AstNode;
1012
import 'element.dart';
1113
import 'java_core.dart';
@@ -2662,6 +2664,15 @@ class ErrorReporter {
26622664
new AnalysisError(_source, offset, length, errorCode, arguments));
26632665
}
26642666

2667+
/**
2668+
* Report an error with the given [errorCode] and [arguments]. The location of
2669+
* the error is specified by the given [span].
2670+
*/
2671+
void reportErrorForSpan(ErrorCode errorCode, SourceSpan span,
2672+
[List<Object> arguments]) {
2673+
reportErrorForOffset(errorCode, span.start.offset, span.length, arguments);
2674+
}
2675+
26652676
/**
26662677
* Report an error with the given [errorCode] and [arguments]. The [token] is
26672678
* used to compute the location of the error.

Diff for: pkg/analyzer/test/generated/all_the_rest_test.dart

+22
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ import 'package:analyzer/src/generated/utilities_collection.dart';
3030
import 'package:analyzer/src/generated/utilities_dart.dart';
3131
import 'package:analyzer/src/task/dart.dart';
3232
import 'package:path/path.dart';
33+
import 'package:source_span/source_span.dart';
3334
import 'package:unittest/unittest.dart';
3435

3536
import '../reflective_tests.dart';
@@ -7334,6 +7335,27 @@ class ErrorReporterTest extends EngineTestCase {
73347335
expect(error.offset, element.nameOffset);
73357336
}
73367337

7338+
void test_reportErrorForSpan() {
7339+
GatheringErrorListener listener = new GatheringErrorListener();
7340+
ErrorReporter reporter = new ErrorReporter(listener, new TestSource());
7341+
7342+
var src = '''
7343+
foo: bar
7344+
zap: baz
7345+
''';
7346+
7347+
int offset = src.indexOf('baz');
7348+
int length = 'baz'.length;
7349+
7350+
SourceSpan span = new SourceFile(src).span(offset, offset + length);
7351+
7352+
reporter.reportErrorForSpan(
7353+
AnalysisOptionsWarningCode.UNSUPPORTED_OPTION, span, ['test', 'zap']);
7354+
expect(listener.errors, hasLength(1));
7355+
expect(listener.errors.first.offset, offset);
7356+
expect(listener.errors.first.length, length);
7357+
}
7358+
73377359
void test_reportTypeErrorForNode_differentNames() {
73387360
DartType firstType = createType("/test1.dart", "A");
73397361
DartType secondType = createType("/test2.dart", "B");

0 commit comments

Comments
 (0)