Reland "if chains → switch expressions" (#148634)

I did a goof a while back:

![late initialization error](https://github.com/flutter/flutter/assets/10457200/47dc423f-0dd0-4869-9a11-59cb7d1ea46e)

Now that [the bug is fixed](e9d403541f), I think we're good to re-implement the if-chains cleanup!

Related:

- https://github.com/flutter/flutter/pull/147793
- https://github.com/flutter/flutter/pull/148556
- https://github.com/flutter/flutter/issues/148548
This commit is contained in:
Nate
2024-05-20 11:43:00 -06:00
committed by GitHub
parent 333c076e53
commit 73ecc8a9b2
33 changed files with 194 additions and 297 deletions

View File

@@ -131,12 +131,11 @@ abstract class DeferredComponentsValidator {
if (line.startsWith('Only in android')) {
continue;
}
TerminalColor color = TerminalColor.grey;
if (line.startsWith('+')) {
color = TerminalColor.green;
} else if (line.startsWith('-')) {
color = TerminalColor.red;
}
final TerminalColor color = switch (line[0]) {
'+' => TerminalColor.green,
'-' => TerminalColor.red,
_ => TerminalColor.grey,
};
logger.printStatus(line, color: color);
}
logger.printStatus('');

View File

@@ -115,18 +115,15 @@ class MultiRootFileSystem extends ForwardingFileSystem {
/// If the path is a multiroot uri, resolve to the actual path of the
/// underlying file system. Otherwise, return as is.
dynamic _resolve(dynamic path) {
Uri uri;
if (path == null) {
return null;
} else if (path is String) {
uri = Uri.parse(path);
} else if (path is Uri) {
uri = path;
} else if (path is FileSystemEntity) {
uri = path.uri;
} else {
throw ArgumentError('Invalid type for "path": ${(path as Object?)?.runtimeType}');
}
final Uri uri = switch (path) {
String() => Uri.parse(path),
Uri() => path,
FileSystemEntity() => path.uri,
_ => throw ArgumentError('Invalid type for "path": ${(path as Object?)?.runtimeType}'),
};
if (!uri.hasScheme || uri.scheme != _scheme) {
return path;

View File

@@ -666,11 +666,10 @@ abstract class _BuildIOSSubCommand extends BuildSubCommand {
final String logTarget = environmentType == EnvironmentType.simulator ? 'simulator' : 'device';
final String typeName = globals.artifacts!.getEngineType(TargetPlatform.ios, buildInfo.mode);
if (xcodeBuildAction == XcodeBuildAction.build) {
globals.printStatus('Building $app for $logTarget ($typeName)...');
} else {
globals.printStatus('Archiving $app...');
}
globals.printStatus(switch (xcodeBuildAction) {
XcodeBuildAction.build => 'Building $app for $logTarget ($typeName)...',
XcodeBuildAction.archive => 'Archiving $app...',
});
final XcodeBuildResult result = await buildXcodeProject(
app: app,
buildInfo: buildInfo,

View File

@@ -429,22 +429,17 @@ end
Directory simulatorBuildOutput,
) async {
const String appFrameworkName = 'App.framework';
final Status status = globals.logger.startProgress(
' ├─Building App.xcframework...',
);
final List<EnvironmentType> environmentTypes = <EnvironmentType>[
EnvironmentType.physical,
EnvironmentType.simulator,
];
final List<Directory> frameworks = <Directory>[];
try {
for (final EnvironmentType sdkType in environmentTypes) {
final Directory outputBuildDirectory =
sdkType == EnvironmentType.physical
? iPhoneBuildOutput
: simulatorBuildOutput;
for (final EnvironmentType sdkType in EnvironmentType.values) {
final Directory outputBuildDirectory = switch (sdkType) {
EnvironmentType.physical => iPhoneBuildOutput,
EnvironmentType.simulator => simulatorBuildOutput,
};
frameworks.add(outputBuildDirectory.childDirectory(appFrameworkName));
final Environment environment = Environment(
projectDir: globals.fs.currentDirectory,

View File

@@ -1429,16 +1429,12 @@ Map<String, Object?> _operationResultToMap(OperationResult result) {
}
Object? _toJsonable(Object? obj) {
if (obj is String || obj is int || obj is bool || obj is Map<Object?, Object?> || obj is List<Object?> || obj == null) {
return obj;
}
if (obj is OperationResult) {
return _operationResultToMap(obj);
}
if (obj is ToolExit) {
return obj.message;
}
return '$obj';
return switch (obj) {
String() || int() || bool() || Map<Object?, Object?>() || List<Object?>() || null => obj,
OperationResult() => _operationResultToMap(obj),
ToolExit() => obj.message,
_ => obj.toString(),
};
}
class NotifyingLogger extends DelegatingLogger {

View File

@@ -942,12 +942,11 @@ enum ImpellerStatus {
const ImpellerStatus._(this.asBool);
factory ImpellerStatus.fromBool(bool? b) {
if (b == null) {
return platformDefault;
}
return b ? enabled : disabled;
}
factory ImpellerStatus.fromBool(bool? b) => switch (b) {
true => enabled,
false => disabled,
null => platformDefault,
};
final bool? asBool;
}

View File

@@ -439,15 +439,11 @@ class IOSCoreDevice {
String? get udid => hardwareProperties?.udid;
DeviceConnectionInterface? get connectionInterface {
final String? transportType = connectionProperties?.transportType;
if (transportType != null) {
if (transportType.toLowerCase() == 'localnetwork') {
return DeviceConnectionInterface.wireless;
} else if (transportType.toLowerCase() == 'wired') {
return DeviceConnectionInterface.attached;
}
}
return null;
return switch (connectionProperties?.transportType?.toLowerCase()) {
'localnetwork' => DeviceConnectionInterface.wireless,
'wired' => DeviceConnectionInterface.attached,
_ => null,
};
}
@visibleForTesting

View File

@@ -96,16 +96,12 @@ class Node {
// Token constructors.
Node.openBrace(this.positionInMessage): type = ST.openBrace, value = '{';
Node.closeBrace(this.positionInMessage): type = ST.closeBrace, value = '}';
Node.brace(this.positionInMessage, String this.value) {
if (value == '{') {
type = ST.openBrace;
} else if (value == '}') {
type = ST.closeBrace;
} else {
// We should never arrive here.
throw L10nException('Provided value $value is not a brace.');
}
}
Node.brace(this.positionInMessage, String this.value)
: type = switch (value) {
'{' => ST.openBrace,
'}' => ST.closeBrace,
_ => throw L10nException('Provided value $value is not a brace.')
};
Node.equalSign(this.positionInMessage): type = ST.equalSign, value = '=';
Node.comma(this.positionInMessage): type = ST.comma, value = ',';
Node.string(this.positionInMessage, String this.value): type = ST.string;

View File

@@ -914,12 +914,10 @@ abstract class ResidentHandlers {
final Brightness? current = await flutterDevices.first!.vmService!.flutterBrightnessOverride(
isolateId: views.first.uiIsolate!.id!,
);
Brightness next;
if (current == Brightness.light) {
next = Brightness.dark;
} else {
next = Brightness.light;
}
final Brightness next = switch (current) {
Brightness.light => Brightness.dark,
Brightness.dark || null => Brightness.light,
};
for (final FlutterDevice? device in flutterDevices) {
final List<FlutterView> views = await device!.vmService!.getFlutterViews();
for (final FlutterView view in views) {

View File

@@ -772,15 +772,14 @@ abstract class FlutterCommand extends Command<void> {
return null;
}();
DeviceConnectionInterface? get deviceConnectionInterface {
DeviceConnectionInterface? get deviceConnectionInterface {
if ((argResults?.options.contains(FlutterOptions.kDeviceConnection) ?? false)
&& (argResults?.wasParsed(FlutterOptions.kDeviceConnection) ?? false)) {
final String? connectionType = stringArg(FlutterOptions.kDeviceConnection);
if (connectionType == 'attached') {
return DeviceConnectionInterface.attached;
} else if (connectionType == 'wireless') {
return DeviceConnectionInterface.wireless;
}
return switch (stringArg(FlutterOptions.kDeviceConnection)) {
'attached' => DeviceConnectionInterface.attached,
'wireless' => DeviceConnectionInterface.wireless,
_ => null,
};
}
return null;
}

View File

@@ -290,13 +290,11 @@ class FakeDeviceManager implements DeviceManager {
Device? getSingleEphemeralDevice(List<Device> devices) => null;
List<Device> filteredDevices(DeviceDiscoveryFilter? filter) {
if (filter?.deviceConnectionInterface == DeviceConnectionInterface.attached) {
return attachedDevices;
}
if (filter?.deviceConnectionInterface == DeviceConnectionInterface.wireless) {
return wirelessDevices;
}
return attachedDevices + wirelessDevices;
return switch (filter?.deviceConnectionInterface) {
DeviceConnectionInterface.attached => attachedDevices,
DeviceConnectionInterface.wireless => wirelessDevices,
null => attachedDevices + wirelessDevices,
};
}
}