Use a Location tuple for line&column
diff --git a/lib/src/transformer/error_listener.dart b/lib/src/transformer/error_listener.dart
index 3518e40..89dbdc5 100644
--- a/lib/src/transformer/error_listener.dart
+++ b/lib/src/transformer/error_listener.dart
@@ -37,10 +37,9 @@
var sourceUrl = error.source.uri.toString();
var locationHelper = _getLocationHelper(content, sourceUrl);
SourceLocation makeLocation(int offset) {
- return locationHelper.getLineAndColumn(offset, (line, column) {
- return new SourceLocation(
- offset, sourceUrl: sourceUrl, line: line, column: column);
- });
+ var location = locationHelper.getLocation(offset);
+ return new SourceLocation(offset,
+ sourceUrl: sourceUrl, line: location.line, column: location.column);
}
int start = error.offset;
int end = error.offset + error.length;
@@ -68,6 +67,17 @@
}
}
+/// A simple source location.
+class Location {
+ final int line;
+ final int column;
+ Location(this.line, this.column);
+
+ operator==(other) =>
+ other is Location && line == other.line && column == other.column;
+ get hashCode => line.hashCode ^ column.hashCode;
+}
+
// TODO(ochafik): Drop when https://github.com/dart-lang/sdk/issues/25717 fixed.
/// Helper that computes line & column from an offset in log time.
class LocationHelper {
@@ -90,17 +100,15 @@
}
}
- /// Gets the line and column that corresponds to the [offset] in this helper's
- /// [_content] string, pass them to [callback] and return its result
- /// (continuation-passing style callback).
- dynamic/*=T*/ getLineAndColumn/*<T>*/(
- int offset, dynamic/*=T*/ callback(int line, int column)) {
+ /// Gets the location that corresponds to the [offset] in this helper's
+ /// [_content] string.
+ Location getLocation(int offset) {
var lineIndex = lowerBound(_lineOffsets, offset);
lineIndex = min(lineIndex, _lineOffsets.length - 1);
if (_lineOffsets[lineIndex] > offset) lineIndex--;
int line = lineIndex + 1;
int column = offset - _lineOffsets[lineIndex] + 1;
- return callback(line, column);
+ return new Location(line, column);
}
}
diff --git a/test/transformer_test.dart b/test/transformer_test.dart
index 4e89dbf..328496f 100644
--- a/test/transformer_test.dart
+++ b/test/transformer_test.dart
@@ -96,22 +96,21 @@
});
group('$LocationHelper', () {
- getLineAndColumn(String content, int offset) =>
- new LocationHelper(content, '')
- .getLineAndColumn(offset, (line, column) => "$line:$column");
+ getLocation(String content, int offset) =>
+ new LocationHelper(content, '').getLocation(offset);
test('finds lines and columns', () {
- expect(getLineAndColumn('', 0), "1:1");
- expect(getLineAndColumn(' ', 1), "1:2");
- expect(getLineAndColumn('\n', 0), "1:1");
- expect(getLineAndColumn(' \n', 1), "1:2");
- expect(getLineAndColumn('\n', 1), "2:1");
- expect(getLineAndColumn(' \n', 2), "2:1");
- expect(getLineAndColumn('\n\n', 1), "2:1");
- expect(getLineAndColumn(' \n\n', 2), "2:1");
- expect(getLineAndColumn(' \n \n', 2), "2:1");
- expect(getLineAndColumn(' \n \n', 3), "2:2");
- expect(getLineAndColumn(' \n \n', 4), "3:1");
+ expect(getLocation('', 0), new Location(1, 1));
+ expect(getLocation(' ', 1), new Location(1, 2));
+ expect(getLocation('\n', 0), new Location(1, 1));
+ expect(getLocation(' \n', 1), new Location(1, 2));
+ expect(getLocation('\n', 1), new Location(2, 1));
+ expect(getLocation(' \n', 2), new Location(2, 1));
+ expect(getLocation('\n\n', 1), new Location(2, 1));
+ expect(getLocation(' \n\n', 2), new Location(2, 1));
+ expect(getLocation(' \n \n', 2), new Location(2, 1));
+ expect(getLocation(' \n \n', 3), new Location(2, 2));
+ expect(getLocation(' \n \n', 4), new Location(3, 1));
});
});
}