22// for details. All rights reserved. Use of this source code is governed by a
33// BSD-style license that can be found in the LICENSE file.
44
5- // @dart = 2.9
6-
75import 'package:async/async.dart' ;
86import 'package:dwds/src/loaders/require.dart' ;
7+ import 'package:logging/logging.dart' ;
98import 'package:path/path.dart' as p;
109import 'package:source_maps/parser.dart' ;
1110import 'package:source_maps/source_maps.dart' ;
@@ -46,7 +45,7 @@ class Location {
4645 // https://docs.google.com/document/d/1U1RGAehQwRypUTovF1KRlpiOFze0b-_2gc6fAH0KY0k
4746 return Location ._(
4847 JsLocation .fromZeroBased (module, jsLine, jsColumn),
49- DartLocation .fromZeroBased (dartUri, dartLine, dartColumn),
48+ DartLocation .fromZeroBased (dartUri, dartLine ?? 0 , dartColumn ?? 0 ),
5049 );
5150 }
5251
@@ -118,6 +117,8 @@ class JsLocation {
118117
119118/// Contains meta data for known [Location] s.
120119class Locations {
120+ final _logger = Logger ('Locations' );
121+
121122 /// [Location] data for Dart server path.
122123 final Map <String , Set <Location >> _sourceToLocation = {};
123124 final Map <String , AsyncMemoizer <Set <Location >>> _locationMemoizer = {};
@@ -134,7 +135,7 @@ class Locations {
134135 final Modules _modules;
135136 final String _root;
136137
137- String _entrypoint;
138+ late String _entrypoint;
138139
139140 Locations (this ._assetReader, this ._modules, this ._root);
140141
@@ -151,7 +152,11 @@ class Locations {
151152 /// Returns all [Location] data for a provided Dart source.
152153 Future <Set <Location >> locationsForDart (String serverPath) async {
153154 final module = await _modules.moduleForSource (serverPath);
154- await _locationsForModule (module);
155+ if (module == null ) {
156+ _logger.warning ('No module for server path $serverPath ' );
157+ } else {
158+ await _locationsForModule (module);
159+ }
155160 return _sourceToLocation[serverPath] ?? {};
156161 }
157162
@@ -161,21 +166,26 @@ class Locations {
161166 _entrypoint, Uri .parse (url).path);
162167 final cache = _moduleToLocations[module];
163168 if (cache != null ) return cache;
164- return await _locationsForModule (module) ?? {};
169+ if (module == null ) {
170+ _logger.warning ('No module for $url ' );
171+ } else {
172+ await _locationsForModule (module);
173+ }
174+ return _moduleToLocations[module] ?? {};
165175 }
166176
167177 /// Find the [Location] for the given Dart source position.
168178 ///
169179 /// The [line] number is 1-based.
170- Future <Location > locationForDart (DartUri uri, int line, int column) async {
180+ Future <Location ? > locationForDart (DartUri uri, int line, int column) async {
171181 final locations = await locationsForDart (uri.serverPath);
172182 return _bestDartLocation (locations, line, column);
173183 }
174184
175185 /// Find the [Location] for the given JS source position.
176186 ///
177187 /// The [line] number is 0-based.
178- Future <Location > locationForJs (String url, int line, int column) async {
188+ Future <Location ? > locationForJs (String url, int line, int column) async {
179189 final locations = await locationsForUrl (url);
180190 return _bestJsLocation (locations, line, column);
181191 }
@@ -185,9 +195,9 @@ class Locations {
185195 /// Dart columns for breakpoints are either exact or start at the
186196 /// beginning of the line - return the first existing location
187197 /// that comes after the given column.
188- Location _bestDartLocation (
198+ Location ? _bestDartLocation (
189199 Iterable <Location > locations, int line, int column) {
190- Location bestLocation;
200+ Location ? bestLocation;
191201 for (var location in locations) {
192202 if (location.dartLocation.line == line &&
193203 location.dartLocation.column >= column) {
@@ -209,8 +219,9 @@ class Locations {
209219 /// the closest location to the current one:
210220 ///
211221 /// https://github.com/microsoft/vscode-js-debug/blob/536f96bae61a3d87546b61bc7916097904c81429/src/common/sourceUtils.ts#L286
212- Location _bestJsLocation (Iterable <Location > locations, int line, int column) {
213- Location bestLocation;
222+ Location ? _bestJsLocation (
223+ Iterable <Location > locations, int line, int column) {
224+ Location ? bestLocation;
214225 for (var location in locations) {
215226 if (location.jsLocation.compareToLine (line, column) <= 0 ) {
216227 bestLocation ?? = location;
@@ -239,9 +250,10 @@ class Locations {
239250 .add (location);
240251 }
241252 for (var lineNumber in lineNumberToLocation.keys) {
253+ final locations = lineNumberToLocation[lineNumber]! ;
242254 tokenPosTable.add ([
243255 lineNumber,
244- for (var location in lineNumberToLocation[lineNumber] ) ...[
256+ for (var location in locations ) ...[
245257 location.tokenPos,
246258 location.dartLocation.column
247259 ]
@@ -257,13 +269,15 @@ class Locations {
257269 ///
258270 /// This will populate the [_sourceToLocation] and [_moduleToLocations] maps.
259271 Future <Set <Location >> _locationsForModule (String module) async {
260- _locationMemoizer.putIfAbsent (module, () => AsyncMemoizer ());
272+ final memoizer =
273+ _locationMemoizer.putIfAbsent (module, () => AsyncMemoizer ());
261274
262- return await _locationMemoizer[module].runOnce (() async {
263- if (module == null ) return {};
264- if (_moduleToLocations[module] != null ) return _moduleToLocations[module];
275+ return await memoizer.runOnce (() async {
276+ if (_moduleToLocations.containsKey (module)) {
277+ return _moduleToLocations[module]! ;
278+ }
265279 final result = < Location > {};
266- if (module? .isEmpty ?? true ) return _moduleToLocations[module] = result;
280+ if (module.isEmpty) return _moduleToLocations[module] = result;
267281 if (module.endsWith ('dart_sdk' ) || module.endsWith ('dart_library' )) {
268282 return result;
269283 }
0 commit comments