[ddc] Migrate module_metadata to null safety
* Migrate the library and tests.
* Update comments per effective dart recommendations.
Change-Id: Ibc8da1fe68c977fb3c084208b96734354ad2705b
Issue: https://github.com/dart-lang/sdk/issues/46617
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/215122
Reviewed-by: Anna Gringauze <annagrin@google.com>
Commit-Queue: Nicholas Shahan <nshahan@google.com>
diff --git a/pkg/dev_compiler/lib/src/kernel/module_metadata.dart b/pkg/dev_compiler/lib/src/kernel/module_metadata.dart
index 5669874..416f0d8 100644
--- a/pkg/dev_compiler/lib/src/kernel/module_metadata.dart
+++ b/pkg/dev_compiler/lib/src/kernel/module_metadata.dart
@@ -2,9 +2,7 @@
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
-// @dart = 2.9
-
-/// Module metadata format version
+/// Module metadata format version.
///
/// Module reader always creates the current version but is able to read
/// metadata files with later versions as long as the changes are backward
@@ -22,10 +20,10 @@
this.patchVersion,
);
- /// Current metadata version
+ /// The current metadata version.
///
- /// Version follows simple semantic versioning format 'major.minor.patch'
- /// See https://semver.org
+ /// Version follows simple semantic versioning format 'major.minor.patch'.
+ /// See: https://semver.org
///
/// TODO(annagrin): create metadata package, make version the same as the
/// metadata package version, automate updating with the package update
@@ -34,7 +32,7 @@
/// Current metadata version created by the reader
String get version => '$majorVersion.$minorVersion.$patchVersion';
- /// Is this metadata version compatible with the given version
+ /// True if this metadata version is compatible with [version].
///
/// The minor and patch version changes never remove any fields that current
/// version supports, so the reader can create current metadata version from
@@ -55,28 +53,27 @@
}
}
-/// Library metadata
+/// Metadata used by the debugger to describe a library.
///
-/// Represents library metadata used in the debugger,
-/// supports reading from and writing to json
+/// Supports reading from and writing to json.
/// See: https://goto.google.com/dart-web-debugger-metadata
class LibraryMetadata {
/// Library name as defined in pubspec.yaml
final String name;
- /// Library importUri
+ /// URI used to import the library.
///
- /// Example package:path/path.dart
+ /// Example: package:path/path.dart
final String importUri;
- /// Library fileUri
+ /// File URI for the library.
///
- /// Example file:///path/to/path/path.dart
+ /// Example: file:///path/to/path/path.dart
final String fileUri;
- /// All file uris from the library
+ /// All file URIs (include part files) from the library.
///
- /// Can be relative paths to the directory of the fileUri
+ /// Can be relative paths to the directory of the fileUri.
final List<String> partUris;
LibraryMetadata(this.name, this.importUri, this.fileUri, this.partUris);
@@ -98,37 +95,35 @@
}
}
-/// Module metadata
+/// Metadata used by the debugger to describe a module.
///
-/// Represents module metadata used in the debugger,
-/// supports reading from and writing to json
+/// Supports reading from and writing to json.
/// See: https://goto.google.com/dart-web-debugger-metadata
class ModuleMetadata {
- /// Metadata format version
- String version;
+ /// The version of this metadata.
+ final String version;
- /// Module name
+ /// Name of the js module created by the compiler.
///
- /// Used as a name of the js module created by the compiler and
- /// as key to store and load modules in the debugger and the browser
+ /// Used as a key to store and load modules in the debugger and the browser.
final String name;
- /// Name of the function enclosing the module
+ /// Name of the function enclosing the module.
///
- /// Used by debugger to determine the top dart scope
+ /// Used by debugger to determine the top dart scope.
final String closureName;
- /// Source map uri
+ /// URI of the source map for this module.
final String sourceMapUri;
- /// Module uri
+ /// URI of the module.
final String moduleUri;
- /// The uri where DDC wrote a full .dill file for this module.
+ /// The URI where DDC wrote a full .dill file for this module.
///
- /// Can be `null` if the module was compiled without the option to output the
- /// .dill fle.
- final String fullDillUri;
+ /// Will be `null` when the module was compiled without the option to output
+ /// the .dill fle.
+ final String? fullDillUri;
final Map<String, LibraryMetadata> libraries = {};
@@ -138,14 +133,13 @@
ModuleMetadata(this.name, this.closureName, this.sourceMapUri, this.moduleUri,
this.fullDillUri, this.soundNullSafety,
- {this.version}) {
- version ??= ModuleMetadataVersion.current.version;
- }
+ {String? version})
+ : version = version ??= ModuleMetadataVersion.current.version;
- /// Add [library] to metadata
+ /// Add [library] to this metadata.
///
- /// Used for filling the metadata in the compiler or for reading from
- /// stored metadata files.
+ /// Used for filling the metadata in the compiler or for reading from stored
+ /// metadata files.
void addLibrary(LibraryMetadata library) {
if (!libraries.containsKey(library.importUri)) {
libraries[library.importUri] = library;
@@ -165,9 +159,8 @@
moduleUri = json['moduleUri'] as String,
fullDillUri = json['fullDillUri'] as String,
soundNullSafety = json['soundNullSafety'] as bool {
- var fileVersion = json['version'] as String;
if (!ModuleMetadataVersion.current.isCompatibleWith(version)) {
- throw Exception('Unsupported metadata version $fileVersion');
+ throw Exception('Unsupported metadata version $version');
}
for (var l in json['libraries'] as List<dynamic>) {
diff --git a/pkg/dev_compiler/test/module_metadata_test.dart b/pkg/dev_compiler/test/module_metadata_test.dart
index 9d6c862..e3a74e0 100644
--- a/pkg/dev_compiler/test/module_metadata_test.dart
+++ b/pkg/dev_compiler/test/module_metadata_test.dart
@@ -2,19 +2,17 @@
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
-// @dart = 2.9
-
import 'dart:convert';
import 'dart:io';
import 'package:dev_compiler/src/kernel/module_metadata.dart';
import 'package:test/test.dart';
-// Test creating, reading and writing debugger metadata
+/// Test creating, reading and writing debugger metadata.
void main() {
group('Module metadata', () {
- Directory tempDir;
- File file;
+ late Directory tempDir;
+ late File file;
setUpAll(() {
var systemTempDir = Directory.systemTemp;
@@ -28,16 +26,16 @@
});
test('create, write, and read', () async {
- // create metadata
+ // Create metadata.
var version = ModuleMetadataVersion.current.version;
var module = createMetadata(version);
testMetadataFields(module, version);
- // write metadata
+ // Write metadata.
file.writeAsBytesSync(utf8.encode(json.encode(module)));
expect(file.existsSync(), true);
- // read metadata
+ // Read metadata.
var moduleJson = json.decode(utf8.decode(file.readAsBytesSync()));
var newModule =
ModuleMetadata.fromJson(moduleJson as Map<String, dynamic>);
@@ -45,7 +43,7 @@
});
test('read later backward-compatible patch version', () async {
- // create metadata with next patch version
+ // Create metadata with next patch version.
var version = ModuleMetadataVersion(
ModuleMetadataVersion.current.majorVersion,
ModuleMetadataVersion.current.minorVersion,
@@ -54,11 +52,11 @@
var module = createMetadata(version);
- // write metadata
+ // Write metadata.
file.writeAsBytesSync(utf8.encode(json.encode(module)));
expect(file.existsSync(), true);
- // read metadata
+ // Read metadata.
var moduleJson = json.decode(utf8.decode(file.readAsBytesSync()));
var newModule =
ModuleMetadata.fromJson(moduleJson as Map<String, dynamic>);
@@ -66,7 +64,7 @@
});
test('read later backward-compatible minor version', () async {
- // create metadata with next minor version
+ // Create metadata with next minor version.
var version = ModuleMetadataVersion(
ModuleMetadataVersion.current.majorVersion,
ModuleMetadataVersion.current.minorVersion + 1,
@@ -74,11 +72,11 @@
.version;
var module = createMetadata(version);
- // write metadata
+ // Write metadata.
file.writeAsBytesSync(utf8.encode(json.encode(module)));
expect(file.existsSync(), true);
- // read metadata
+ // Read metadata.
var moduleJson = json.decode(utf8.decode(file.readAsBytesSync()));
var newModule =
ModuleMetadata.fromJson(moduleJson as Map<String, dynamic>);
@@ -86,7 +84,7 @@
});
test('fail to read later non-backward-compatible major version', () async {
- // create metadata with next minor version
+ // Create metadata with next minor version.
var version = ModuleMetadataVersion(
ModuleMetadataVersion.current.majorVersion + 1,
ModuleMetadataVersion.current.minorVersion + 1,
@@ -94,13 +92,13 @@
.version;
var module = createMetadata(version);
- // write metadata
+ // Write metadata.
file.writeAsBytesSync(utf8.encode(json.encode(module)));
expect(file.existsSync(), true);
- // try read metadata, expect to fail
+ // Try read metadata, expect to fail.
var moduleJson = json.decode(utf8.decode(file.readAsBytesSync()));
- ModuleMetadata newModule;
+ ModuleMetadata? newModule;
try {
newModule = ModuleMetadata.fromJson(moduleJson as Map<String, dynamic>);
} catch (e) {
@@ -120,7 +118,7 @@
'file:///source/library/lib/test.dart', ['src/test2.dart']));
void testMetadataFields(ModuleMetadata module, String version) {
- // reader always creates current metadata version
+ // Reader always creates current metadata version.
expect(module.version, version);
expect(module.name, 'module');
expect(module.closureName, 'closure');
@@ -130,8 +128,7 @@
expect(module.soundNullSafety, true);
var libUri = module.libraries.keys.first;
- var lib = module.libraries[libUri];
-
+ var lib = module.libraries[libUri]!;
expect(libUri, 'package:library/test.dart');
expect(lib.name, 'library');
expect(lib.importUri, 'package:library/test.dart');