Switch to using AnalysisContext.getLineInfo
diff --git a/lib/src/transformer/error_listener.dart b/lib/src/transformer/error_listener.dart index 89dbdc5..cfa3ef6 100644 --- a/lib/src/transformer/error_listener.dart +++ b/lib/src/transformer/error_listener.dart
@@ -4,12 +4,10 @@ library dev_compiler.src.transformer.error_listener; -import 'dart:math'; - import 'package:analyzer/analyzer.dart' show AnalysisError, ErrorSeverity, AnalysisErrorListener; +import 'package:analyzer/src/generated/engine.dart' show AnalysisContext; import 'package:barback/barback.dart' show TransformLogger, AssetId; -import 'package:collection/collection.dart'; import 'package:source_span/source_span.dart' show SourceSpan, SourceLocation; import 'uri_resolver.dart'; @@ -18,28 +16,18 @@ class TransformAnalysisErrorListener extends AnalysisErrorListener { final TransformLogger _logger; - LocationHelper _lastLocationHelper; - TransformAnalysisErrorListener(this._logger); - - /// Get a location helper for the provided [content] with [uri]. - /// Multiple subsequent calls with the same [uri] will return the same cached - /// instance, to accommodate the use-case of a file having many messages. - LocationHelper _getLocationHelper(String content, String uri) { - if (_lastLocationHelper?.uri != uri) { - _lastLocationHelper = new LocationHelper(content, uri); - } - return _lastLocationHelper; - } + final AnalysisContext _context; + TransformAnalysisErrorListener(this._logger, this._context); @override void onError(AnalysisError error) { var content = error.source.contents.data; var sourceUrl = error.source.uri.toString(); - var locationHelper = _getLocationHelper(content, sourceUrl); + var lineInfo = _context.getLineInfo(error.source); SourceLocation makeLocation(int offset) { - var location = locationHelper.getLocation(offset); + var location = lineInfo.getLocation(offset); return new SourceLocation(offset, - sourceUrl: sourceUrl, line: location.line, column: location.column); + sourceUrl: sourceUrl, line: location.lineNumber, column: location.columnNumber); } int start = error.offset; int end = error.offset + error.length; @@ -66,49 +54,3 @@ } } } - -/// 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 { - final String _content; - final String uri; - final _lineOffsets = <int>[]; - - LocationHelper(String content, this.uri) : _content = content { - _lineOffsets.add(0); - for (int i = 0, length = content.length; i < length; i++) { - switch (content[i]) { - case '\n': - // Consume any trailing carriage return. - while (i < length - 1 && content[i + 1] == '\r') { - i++; - } - _lineOffsets.add(i + 1); - break; - } - } - } - - /// 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 new Location(line, column); - } -}
diff --git a/lib/src/transformer/transformer.dart b/lib/src/transformer/transformer.dart index 44a198f..08a0c2a 100644 --- a/lib/src/transformer/transformer.dart +++ b/lib/src/transformer/transformer.dart
@@ -70,7 +70,7 @@ '--runtime-dir', _fakeRuntimeDir ])), - reporter: new TransformAnalysisErrorListener(transform.logger), + reporter: new TransformAnalysisErrorListener(transform.logger, context), fileSystem: fileSystem); for (var asset in inputs) {
diff --git a/test/transformer_test.dart b/test/transformer_test.dart index 328496f..07c19e7 100644 --- a/test/transformer_test.dart +++ b/test/transformer_test.dart
@@ -3,7 +3,6 @@ import 'package:barback/barback.dart' show BarbackMode, BarbackSettings; import 'package:dev_compiler/transformer.dart'; import 'package:dev_compiler/src/compiler.dart' show defaultRuntimeFiles; -import 'package:dev_compiler/src/transformer/error_listener.dart'; import 'package:test/test.dart'; import 'package:transformer_test/utils.dart'; @@ -94,23 +93,4 @@ "error: Type check failed: '2' (String) is not of type int (package:foo/Foo.dart 3 19)" ]); }); - - group('$LocationHelper', () { - getLocation(String content, int offset) => - new LocationHelper(content, '').getLocation(offset); - - test('finds lines and columns', () { - 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)); - }); - }); }