Implement switch expressions in dev/ (#139048)

I previously made a PR (#136140) that used `switch` expressions to make some parts of the Flutter codebase easier to understand. It was assigned to the framework team, and @christopherfujino let me know that it was too large to effectively review and recommended breaking it up into smaller pull requests.

Here's a PR that only targets files in the `dev/` directory. Hopefully this will be easier to work with!

(solves issue https://github.com/flutter/flutter/issues/136139)
This commit is contained in:
Nate
2023-11-28 16:40:28 -07:00
committed by GitHub
parent 9c4df1f1f5
commit 3267fbc07a
15 changed files with 117 additions and 197 deletions

View File

@@ -109,28 +109,17 @@ Future<void> _setAppVersion(int version) async {
}
String _testTypeToIndexFile(ServiceWorkerTestType type) {
late String indexFile;
switch (type) {
case ServiceWorkerTestType.blockedServiceWorkers:
indexFile = 'index_with_blocked_service_workers.html';
case ServiceWorkerTestType.withFlutterJs:
indexFile = 'index_with_flutterjs.html';
case ServiceWorkerTestType.withoutFlutterJs:
indexFile = 'index_without_flutterjs.html';
case ServiceWorkerTestType.withFlutterJsShort:
indexFile = 'index_with_flutterjs_short.html';
case ServiceWorkerTestType.withFlutterJsEntrypointLoadedEvent:
indexFile = 'index_with_flutterjs_entrypoint_loaded.html';
case ServiceWorkerTestType.withFlutterJsTrustedTypesOn:
indexFile = 'index_with_flutterjs_el_tt_on.html';
case ServiceWorkerTestType.withFlutterJsNonceOn:
indexFile = 'index_with_flutterjs_el_nonce.html';
case ServiceWorkerTestType.withFlutterJsCustomServiceWorkerVersion:
indexFile = 'index_with_flutterjs_custom_sw_version.html';
case ServiceWorkerTestType.generatedEntrypoint:
indexFile = 'generated_entrypoint.html';
}
return indexFile;
return switch (type) {
ServiceWorkerTestType.blockedServiceWorkers => 'index_with_blocked_service_workers.html',
ServiceWorkerTestType.withFlutterJs => 'index_with_flutterjs.html',
ServiceWorkerTestType.withoutFlutterJs => 'index_without_flutterjs.html',
ServiceWorkerTestType.withFlutterJsShort => 'index_with_flutterjs_short.html',
ServiceWorkerTestType.withFlutterJsEntrypointLoadedEvent => 'index_with_flutterjs_entrypoint_loaded.html',
ServiceWorkerTestType.withFlutterJsTrustedTypesOn => 'index_with_flutterjs_el_tt_on.html',
ServiceWorkerTestType.withFlutterJsNonceOn => 'index_with_flutterjs_el_nonce.html',
ServiceWorkerTestType.withFlutterJsCustomServiceWorkerVersion => 'index_with_flutterjs_custom_sw_version.html',
ServiceWorkerTestType.generatedEntrypoint => 'generated_entrypoint.html',
};
}
Future<void> _rebuildApp({ required int version, required ServiceWorkerTestType testType, required String target }) async {

View File

@@ -49,53 +49,39 @@ class UnpublishException implements Exception {
enum Channel { dev, beta, stable }
String getChannelName(Channel channel) {
switch (channel) {
case Channel.beta:
return 'beta';
case Channel.dev:
return 'dev';
case Channel.stable:
return 'stable';
}
return switch (channel) {
Channel.beta => 'beta',
Channel.dev => 'dev',
Channel.stable => 'stable',
};
}
Channel fromChannelName(String? name) {
switch (name) {
case 'beta':
return Channel.beta;
case 'dev':
return Channel.dev;
case 'stable':
return Channel.stable;
default:
throw ArgumentError('Invalid channel name.');
}
return switch (name) {
'beta' => Channel.beta,
'dev' => Channel.dev,
'stable' => Channel.stable,
_ => throw ArgumentError('Invalid channel name.'),
};
}
enum PublishedPlatform { linux, macos, windows }
String getPublishedPlatform(PublishedPlatform platform) {
switch (platform) {
case PublishedPlatform.linux:
return 'linux';
case PublishedPlatform.macos:
return 'macos';
case PublishedPlatform.windows:
return 'windows';
}
return switch (platform) {
PublishedPlatform.linux => 'linux',
PublishedPlatform.macos => 'macos',
PublishedPlatform.windows => 'windows',
};
}
PublishedPlatform fromPublishedPlatform(String name) {
switch (name) {
case 'linux':
return PublishedPlatform.linux;
case 'macos':
return PublishedPlatform.macos;
case 'windows':
return PublishedPlatform.windows;
default:
throw ArgumentError('Invalid published platform name.');
}
return switch (name) {
'linux' => PublishedPlatform.linux,
'macos' => PublishedPlatform.macos,
'windows' => PublishedPlatform.windows,
_ => throw ArgumentError('Invalid published platform name.'),
};
}
/// A helper class for classes that want to run a process, optionally have the