Unverified Commit 0cc087c2 authored by Jonah Williams's avatar Jonah Williams Committed by GitHub

[flutter_tools] migrate some base libraries to null safety (#77738)

parent c13a8ca4
...@@ -2,13 +2,11 @@ ...@@ -2,13 +2,11 @@
// Use of this source code is governed by a BSD-style license that can be // Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file. // found in the LICENSE file.
// @dart = 2.8
/// Throw a specialized exception for expected situations /// Throw a specialized exception for expected situations
/// where the tool should exit with a clear message to the user /// where the tool should exit with a clear message to the user
/// and no stack trace unless the --verbose option is specified. /// and no stack trace unless the --verbose option is specified.
/// For example: network errors. /// For example: network errors.
void throwToolExit(String message, { int exitCode }) { void throwToolExit(String message, { int? exitCode }) {
throw ToolExit(message, exitCode: exitCode); throw ToolExit(message, exitCode: exitCode);
} }
...@@ -20,7 +18,7 @@ class ToolExit implements Exception { ...@@ -20,7 +18,7 @@ class ToolExit implements Exception {
ToolExit(this.message, { this.exitCode }); ToolExit(this.message, { this.exitCode });
final String message; final String message;
final int exitCode; final int? exitCode;
@override @override
String toString() => 'Exception: $message'; String toString() => 'Exception: $message';
......
...@@ -2,16 +2,14 @@ ...@@ -2,16 +2,14 @@
// Use of this source code is governed by a BSD-style license that can be // Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file. // found in the LICENSE file.
// @dart = 2.8
import 'dart:async'; import 'dart:async';
import 'dart:math' as math; import 'dart:math' as math;
import 'package:intl/intl.dart'; import 'package:intl/intl.dart';
import 'package:meta/meta.dart'; import 'package:file/file.dart';
import '../convert.dart'; import '../convert.dart';
import 'file_system.dart';
/// Convert `foo_bar` to `fooBar`. /// Convert `foo_bar` to `fooBar`.
String camelCase(String str) { String camelCase(String str) {
...@@ -30,7 +28,7 @@ final RegExp _upperRegex = RegExp(r'[A-Z]'); ...@@ -30,7 +28,7 @@ final RegExp _upperRegex = RegExp(r'[A-Z]');
/// Convert `fooBar` to `foo_bar`. /// Convert `fooBar` to `foo_bar`.
String snakeCase(String str, [ String sep = '_' ]) { String snakeCase(String str, [ String sep = '_' ]) {
return str.replaceAllMapped(_upperRegex, return str.replaceAllMapped(_upperRegex,
(Match m) => '${m.start == 0 ? '' : sep}${m[0].toLowerCase()}'); (Match m) => '${m.start == 0 ? '' : sep}${m[0]!.toLowerCase()}');
} }
String toTitleCase(String str) { String toTitleCase(String str) {
...@@ -75,13 +73,9 @@ String getSizeAsMB(int bytesLength) { ...@@ -75,13 +73,9 @@ String getSizeAsMB(int bytesLength) {
/// removed, and calculate a diff of changes when a new list of items is /// removed, and calculate a diff of changes when a new list of items is
/// available. /// available.
class ItemListNotifier<T> { class ItemListNotifier<T> {
ItemListNotifier() { ItemListNotifier(): _items = <T>{};
_items = <T>{};
}
ItemListNotifier.from(List<T> items) { ItemListNotifier.from(List<T> items) : _items = Set<T>.of(items);
_items = Set<T>.of(items);
}
Set<T> _items; Set<T> _items;
...@@ -150,8 +144,8 @@ class SettingsFile { ...@@ -150,8 +144,8 @@ class SettingsFile {
/// Given a data structure which is a Map of String to dynamic values, return /// Given a data structure which is a Map of String to dynamic values, return
/// the same structure (`Map<String, dynamic>`) with the correct runtime types. /// the same structure (`Map<String, dynamic>`) with the correct runtime types.
Map<String, dynamic> castStringKeyedMap(dynamic untyped) { Map<String, dynamic>? castStringKeyedMap(dynamic untyped) {
final Map<dynamic, dynamic> map = untyped as Map<dynamic, dynamic>; final Map<dynamic, dynamic>? map = untyped as Map<dynamic, dynamic>?;
return map?.cast<String, dynamic>(); return map?.cast<String, dynamic>();
} }
...@@ -197,10 +191,10 @@ const int kMinColumnWidth = 10; ...@@ -197,10 +191,10 @@ const int kMinColumnWidth = 10;
/// is such that less than [kMinColumnWidth] characters can fit in the /// is such that less than [kMinColumnWidth] characters can fit in the
/// [columnWidth], then the indent is truncated to allow the text to fit. /// [columnWidth], then the indent is truncated to allow the text to fit.
String wrapText(String text, { String wrapText(String text, {
@required int columnWidth, required int columnWidth,
@required bool shouldWrap, required bool shouldWrap,
int hangingIndent, int? hangingIndent,
int indent, int? indent,
}) { }) {
assert(columnWidth >= 0); assert(columnWidth >= 0);
if (text == null || text.isEmpty) { if (text == null || text.isEmpty) {
...@@ -239,7 +233,7 @@ String wrapText(String text, { ...@@ -239,7 +233,7 @@ String wrapText(String text, {
shouldWrap: shouldWrap, shouldWrap: shouldWrap,
); );
} }
String hangingIndentString; String? hangingIndentString;
final String indentString = ' ' * indent; final String indentString = ' ' * indent;
result.addAll(notIndented.map<String>( result.addAll(notIndented.map<String>(
(String line) { (String line) {
...@@ -252,7 +246,7 @@ String wrapText(String text, { ...@@ -252,7 +246,7 @@ String wrapText(String text, {
truncatedIndent = truncatedIndent.substring(0, math.max(columnWidth - kMinColumnWidth, 0)); truncatedIndent = truncatedIndent.substring(0, math.max(columnWidth - kMinColumnWidth, 0));
} }
final String result = '$truncatedIndent$line'; final String result = '$truncatedIndent$line';
hangingIndentString ??= ' ' * hangingIndent; hangingIndentString ??= ' ' * hangingIndent!;
return result; return result;
}, },
)); ));
...@@ -288,13 +282,12 @@ class _AnsiRun { ...@@ -288,13 +282,12 @@ class _AnsiRun {
/// then it overrides the [outputPreferences.wrapText] setting. /// then it overrides the [outputPreferences.wrapText] setting.
List<String> _wrapTextAsLines(String text, { List<String> _wrapTextAsLines(String text, {
int start = 0, int start = 0,
int columnWidth, required int columnWidth,
@required bool shouldWrap, required bool shouldWrap,
}) { }) {
if (text == null || text.isEmpty) { if (text == null || text.isEmpty) {
return <String>['']; return <String>[''];
} }
assert(columnWidth != null);
assert(start >= 0); assert(start >= 0);
// Splits a string so that the resulting list has the same number of elements // Splits a string so that the resulting list has the same number of elements
...@@ -308,9 +301,9 @@ List<String> _wrapTextAsLines(String text, { ...@@ -308,9 +301,9 @@ List<String> _wrapTextAsLines(String text, {
final StringBuffer current = StringBuffer(); final StringBuffer current = StringBuffer();
for (final Match match in characterOrCode.allMatches(input)) { for (final Match match in characterOrCode.allMatches(input)) {
current.write(match[0]); current.write(match[0]);
if (match[0].length < 4) { if (match[0]!.length < 4) {
// This is a regular character, write it out. // This is a regular character, write it out.
result.add(_AnsiRun(current.toString(), match[0])); result.add(_AnsiRun(current.toString(), match[0]!));
current.clear(); current.clear();
} }
} }
...@@ -328,7 +321,7 @@ List<String> _wrapTextAsLines(String text, { ...@@ -328,7 +321,7 @@ List<String> _wrapTextAsLines(String text, {
return result; return result;
} }
String joinRun(List<_AnsiRun> list, int start, [ int end ]) { String joinRun(List<_AnsiRun> list, int start, [ int? end ]) {
return list.sublist(start, end).map<String>((_AnsiRun run) => run.original).join().trim(); return list.sublist(start, end).map<String>((_AnsiRun run) => run.original).join().trim();
} }
...@@ -348,7 +341,7 @@ List<String> _wrapTextAsLines(String text, { ...@@ -348,7 +341,7 @@ List<String> _wrapTextAsLines(String text, {
} }
int currentLineStart = 0; int currentLineStart = 0;
int lastWhitespace; int? lastWhitespace;
// Find the start of the current line. // Find the start of the current line.
for (int index = 0; index < splitLine.length; ++index) { for (int index = 0; index < splitLine.length; ++index) {
if (splitLine[index].character.isNotEmpty && isWhitespace(splitLine[index])) { if (splitLine[index].character.isNotEmpty && isWhitespace(splitLine[index])) {
......
...@@ -2,8 +2,6 @@ ...@@ -2,8 +2,6 @@
// Use of this source code is governed by a BSD-style license that can be // Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file. // found in the LICENSE file.
// @dart = 2.8
// Hide the original utf8 [Codec] so that we can export our own implementation // Hide the original utf8 [Codec] so that we can export our own implementation
// which adds additional error handling. // which adds additional error handling.
import 'dart:convert' hide utf8; import 'dart:convert' hide utf8;
...@@ -48,7 +46,7 @@ class Utf8Decoder extends cnv.Utf8Decoder { ...@@ -48,7 +46,7 @@ class Utf8Decoder extends cnv.Utf8Decoder {
final bool reportErrors; final bool reportErrors;
@override @override
String convert(List<int> codeUnits, [ int start = 0, int end ]) { String convert(List<int> codeUnits, [ int start = 0, int? end ]) {
final String result = super.convert(codeUnits, start, end); final String result = super.convert(codeUnits, start, end);
// Finding a unicode replacement character indicates that the input // Finding a unicode replacement character indicates that the input
// was malformed. // was malformed.
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment