Skip to content

Commit 972c794

Browse files
author
Anna Gringauze
authored
Migrate modules and locations to null safety (#1663)
* Migrate modules and locations to null safety * Address CR comments
1 parent c265b33 commit 972c794

File tree

4 files changed

+58
-36
lines changed

4 files changed

+58
-36
lines changed

dwds/lib/src/debugging/location.dart

Lines changed: 32 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,9 @@
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-
75
import 'package:async/async.dart';
86
import 'package:dwds/src/loaders/require.dart';
7+
import 'package:logging/logging.dart';
98
import 'package:path/path.dart' as p;
109
import 'package:source_maps/parser.dart';
1110
import '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.
120119
class 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
}

dwds/lib/src/debugging/modules.dart

Lines changed: 24 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,15 @@
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-
75
import 'package:async/async.dart';
6+
import 'package:logging/logging.dart';
87

98
import '../loaders/strategy.dart';
109
import '../utilities/dart_uri.dart';
1110

1211
/// Tracks modules for the compiled application.
1312
class Modules {
13+
final _logger = Logger('Modules');
1414
final String _root;
1515

1616
// The Dart server path to containing module.
@@ -22,9 +22,9 @@ class Modules {
2222

2323
final Map<String, String> _libraryToModule = {};
2424

25-
String _entrypoint;
25+
late String _entrypoint;
2626

27-
Modules(String root) : _root = root == '' ? '/' : root;
27+
Modules(this._root);
2828

2929
/// Initializes the mapping from source to module.
3030
///
@@ -41,18 +41,18 @@ class Modules {
4141
}
4242

4343
/// Returns the containing module for the provided Dart server path.
44-
Future<String> moduleForSource(String serverPath) async {
44+
Future<String?> moduleForSource(String serverPath) async {
4545
await _moduleMemoizer.runOnce(_initializeMapping);
4646
return _sourceToModule[serverPath];
4747
}
4848

4949
/// Returns the containing library importUri for the provided Dart server path.
50-
Future<Uri> libraryForSource(String serverPath) async {
50+
Future<Uri?> libraryForSource(String serverPath) async {
5151
await _moduleMemoizer.runOnce(_initializeMapping);
5252
return _sourceToLibrary[serverPath];
5353
}
5454

55-
Future<String> moduleForlibrary(String libraryUri) async {
55+
Future<String?> moduleForlibrary(String libraryUri) async {
5656
await _moduleMemoizer.runOnce(_initializeMapping);
5757
return _libraryToModule[libraryUri];
5858
}
@@ -71,21 +71,27 @@ class Modules {
7171
final scriptToModule = await provider.scriptToModule;
7272

7373
for (var library in libraryToScripts.keys) {
74+
final scripts = libraryToScripts[library]!;
7475
final libraryServerPath = library.startsWith('dart:')
7576
? library
7677
: DartUri(library, _root).serverPath;
7778

78-
_sourceToModule[libraryServerPath] = scriptToModule[library];
79-
_sourceToLibrary[libraryServerPath] = Uri.parse(library);
80-
_libraryToModule[library] = scriptToModule[library];
81-
82-
for (var script in libraryToScripts[library]) {
83-
final scriptServerPath = script.startsWith('dart:')
84-
? script
85-
: DartUri(script, _root).serverPath;
86-
87-
_sourceToModule[scriptServerPath] = scriptToModule[library];
88-
_sourceToLibrary[scriptServerPath] = Uri.parse(library);
79+
if (scriptToModule.containsKey(library)) {
80+
final module = scriptToModule[library]!;
81+
_sourceToModule[libraryServerPath] = module;
82+
_sourceToLibrary[libraryServerPath] = Uri.parse(library);
83+
_libraryToModule[library] = module;
84+
85+
for (var script in scripts) {
86+
final scriptServerPath = script.startsWith('dart:')
87+
? script
88+
: DartUri(script, _root).serverPath;
89+
90+
_sourceToModule[scriptServerPath] = module;
91+
_sourceToLibrary[scriptServerPath] = Uri.parse(library);
92+
}
93+
} else {
94+
_logger.warning('No module found for library $library');
8995
}
9096
}
9197
}

dwds/test/debugger_test.dart

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,7 @@ void main() async {
102102
globalLoadStrategy = TestStrategy();
103103
final root = 'fakeRoot';
104104
locations = Locations(FakeAssetReader(), FakeModules(), root);
105+
locations.initialize('fake_entrypoint');
105106
skipLists = SkipLists();
106107
debugger = await Debugger.create(
107108
webkitDebugger,

dwds/test/location_test.dart

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ void main() {
2424
final assetReader = FakeAssetReader();
2525
final modules = MockModules();
2626
final locations = Locations(assetReader, modules, '');
27+
locations.initialize('fake_entrypoint');
2728

2829
group('JS locations |', () {
2930
group('location |', () {

0 commit comments

Comments
 (0)