[flutter_tools] add support for enable null safety asserts (#61114)

Enable null safety asserts for web debug mode. This induces runtime asserts at the boundaries between null safe and non-null safe libraries. Adds integration test that validates assertion error is thrown.

#61042
This commit is contained in:
Jonah Williams
2020-07-15 09:54:39 -07:00
committed by GitHub
parent 0a64b5b271
commit cb7770b3a5
6 changed files with 53 additions and 1 deletions

View File

@@ -791,6 +791,8 @@ class WebDevFS implements DevFS {
'main_module.bootstrap.js',
generateMainModule(
entrypoint: entrypoint,
nullSafety: buildInfo.extraFrontEndOptions
?.contains('--enable-experiment=non-nullable') ?? false,
),
);
// TODO(jonahwilliams): refactor the asset code in this and the regular devfs to

View File

@@ -48,12 +48,18 @@ document.head.appendChild(requireEl);
/// the file `foo/bar/baz.dart` will generate a property named approximately
/// `foo__bar__baz`. Rather than attempt to guess, we assume the first property of
/// this object is the module.
String generateMainModule({@required String entrypoint}) {
String generateMainModule({
@required String entrypoint,
@required bool nullSafety,
}) {
return '''/* ENTRYPOINT_EXTENTION_MARKER */
// Create the main module loaded below.
define("main_module.bootstrap", ["$entrypoint", "dart_sdk"], function(app, dart_sdk) {
dart_sdk.dart.setStartAsyncSynchronously(true);
dart_sdk._debugger.registerDevtoolsFormatter();
if ($nullSafety) {
dart_sdk.dart.nonNullAsserts(true);
}
// See the generateMainModule doc comment.
var child = {};

View File

@@ -23,9 +23,21 @@ void main() {
test('generateMainModule embeds urls correctly', () {
final String result = generateMainModule(
entrypoint: 'foo/bar/main.js',
nullSafety: false,
);
// bootstrap main module has correct defined module.
expect(result, contains('define("main_module.bootstrap", ["foo/bar/main.js", "dart_sdk"], '
'function(app, dart_sdk) {'));
});
test('generateMainModule includes null safety switches', () {
final String result = generateMainModule(
entrypoint: 'foo/bar/main.js',
nullSafety: true,
);
expect(result, contains(
''' if (true) {
dart_sdk.dart.nonNullAsserts(true);'''));
});
}