2
2
// for details. All rights reserved. Use of this source code is governed by a
3
3
// BSD-style license that can be found in the LICENSE file.
4
4
5
- // @dart = 2.9
6
-
7
5
import 'package:async/async.dart' ;
8
6
import 'package:dwds/src/loaders/require.dart' ;
7
+ import 'package:logging/logging.dart' ;
9
8
import 'package:path/path.dart' as p;
10
9
import 'package:source_maps/parser.dart' ;
11
10
import 'package:source_maps/source_maps.dart' ;
@@ -46,7 +45,7 @@ class Location {
46
45
// https://docs.google.com/document/d/1U1RGAehQwRypUTovF1KRlpiOFze0b-_2gc6fAH0KY0k
47
46
return Location ._(
48
47
JsLocation .fromZeroBased (module, jsLine, jsColumn),
49
- DartLocation .fromZeroBased (dartUri, dartLine, dartColumn),
48
+ DartLocation .fromZeroBased (dartUri, dartLine ?? 0 , dartColumn ?? 0 ),
50
49
);
51
50
}
52
51
@@ -118,6 +117,8 @@ class JsLocation {
118
117
119
118
/// Contains meta data for known [Location] s.
120
119
class Locations {
120
+ final _logger = Logger ('Locations' );
121
+
121
122
/// [Location] data for Dart server path.
122
123
final Map <String , Set <Location >> _sourceToLocation = {};
123
124
final Map <String , AsyncMemoizer <Set <Location >>> _locationMemoizer = {};
@@ -134,7 +135,7 @@ class Locations {
134
135
final Modules _modules;
135
136
final String _root;
136
137
137
- String _entrypoint;
138
+ late String _entrypoint;
138
139
139
140
Locations (this ._assetReader, this ._modules, this ._root);
140
141
@@ -151,7 +152,11 @@ class Locations {
151
152
/// Returns all [Location] data for a provided Dart source.
152
153
Future <Set <Location >> locationsForDart (String serverPath) async {
153
154
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
+ }
155
160
return _sourceToLocation[serverPath] ?? {};
156
161
}
157
162
@@ -161,21 +166,26 @@ class Locations {
161
166
_entrypoint, Uri .parse (url).path);
162
167
final cache = _moduleToLocations[module];
163
168
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] ?? {};
165
175
}
166
176
167
177
/// Find the [Location] for the given Dart source position.
168
178
///
169
179
/// 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 {
171
181
final locations = await locationsForDart (uri.serverPath);
172
182
return _bestDartLocation (locations, line, column);
173
183
}
174
184
175
185
/// Find the [Location] for the given JS source position.
176
186
///
177
187
/// 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 {
179
189
final locations = await locationsForUrl (url);
180
190
return _bestJsLocation (locations, line, column);
181
191
}
@@ -185,9 +195,9 @@ class Locations {
185
195
/// Dart columns for breakpoints are either exact or start at the
186
196
/// beginning of the line - return the first existing location
187
197
/// that comes after the given column.
188
- Location _bestDartLocation (
198
+ Location ? _bestDartLocation (
189
199
Iterable <Location > locations, int line, int column) {
190
- Location bestLocation;
200
+ Location ? bestLocation;
191
201
for (var location in locations) {
192
202
if (location.dartLocation.line == line &&
193
203
location.dartLocation.column >= column) {
@@ -209,8 +219,9 @@ class Locations {
209
219
/// the closest location to the current one:
210
220
///
211
221
/// 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;
214
225
for (var location in locations) {
215
226
if (location.jsLocation.compareToLine (line, column) <= 0 ) {
216
227
bestLocation ?? = location;
@@ -239,9 +250,10 @@ class Locations {
239
250
.add (location);
240
251
}
241
252
for (var lineNumber in lineNumberToLocation.keys) {
253
+ final locations = lineNumberToLocation[lineNumber]! ;
242
254
tokenPosTable.add ([
243
255
lineNumber,
244
- for (var location in lineNumberToLocation[lineNumber] ) ...[
256
+ for (var location in locations ) ...[
245
257
location.tokenPos,
246
258
location.dartLocation.column
247
259
]
@@ -257,13 +269,15 @@ class Locations {
257
269
///
258
270
/// This will populate the [_sourceToLocation] and [_moduleToLocations] maps.
259
271
Future <Set <Location >> _locationsForModule (String module) async {
260
- _locationMemoizer.putIfAbsent (module, () => AsyncMemoizer ());
272
+ final memoizer =
273
+ _locationMemoizer.putIfAbsent (module, () => AsyncMemoizer ());
261
274
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
+ }
265
279
final result = < Location > {};
266
- if (module? .isEmpty ?? true ) return _moduleToLocations[module] = result;
280
+ if (module.isEmpty) return _moduleToLocations[module] = result;
267
281
if (module.endsWith ('dart_sdk' ) || module.endsWith ('dart_library' )) {
268
282
return result;
269
283
}
0 commit comments