From 3267fbc07ad39bddf31bb63b5b01f286e6503ecd Mon Sep 17 00:00:00 2001 From: Nate Date: Tue, 28 Nov 2023 16:40:28 -0700 Subject: [PATCH] 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) --- .../test/measure_scroll_smoothness.dart | 40 +++++-------- .../lib/src/filtered_child_animation.dart | 12 ++-- dev/bots/service_worker_test.dart | 33 ++++------- dev/bots/unpublish_package.dart | 58 +++++++------------ dev/conductor/core/lib/src/repository.dart | 10 ++-- dev/conductor/core/lib/src/version.dart | 16 ++--- dev/devicelab/lib/tasks/perf_tests.dart | 13 ++--- .../channels/lib/src/basic_messaging.dart | 12 ++-- .../lib/demo/calculator/logic.dart | 16 ++--- .../demo/material/scrollable_tabs_demo.dart | 13 ++--- .../demo/shrine/expanding_bottom_sheet.dart | 19 +++--- .../flutter_gallery/lib/gallery/options.dart | 22 +++---- dev/manual_tests/lib/density.dart | 17 ++---- dev/manual_tests/lib/menu_anchor.dart | 13 ++--- dev/tools/vitool/lib/vitool.dart | 20 +++---- 15 files changed, 117 insertions(+), 197 deletions(-) diff --git a/dev/benchmarks/complex_layout/test/measure_scroll_smoothness.dart b/dev/benchmarks/complex_layout/test/measure_scroll_smoothness.dart index 54f8afc248b..987933b6b6a 100644 --- a/dev/benchmarks/complex_layout/test/measure_scroll_smoothness.dart +++ b/dev/benchmarks/complex_layout/test/measure_scroll_smoothness.dart @@ -68,40 +68,28 @@ class ResampleFlagVariant extends TestVariant { late TestScenario currentValue; bool get resample { - switch (currentValue) { - case TestScenario.resampleOn90Hz: - case TestScenario.resampleOn59Hz: - return true; - case TestScenario.resampleOff90Hz: - case TestScenario.resampleOff59Hz: - return false; - } + return switch (currentValue) { + TestScenario.resampleOn90Hz || TestScenario.resampleOn59Hz => true, + TestScenario.resampleOff90Hz || TestScenario.resampleOff59Hz => false, + }; } double get frequency { - switch (currentValue) { - case TestScenario.resampleOn90Hz: - case TestScenario.resampleOff90Hz: - return 90.0; - case TestScenario.resampleOn59Hz: - case TestScenario.resampleOff59Hz: - return 59.0; - } + return switch (currentValue) { + TestScenario.resampleOn90Hz || TestScenario.resampleOff90Hz => 90.0, + TestScenario.resampleOn59Hz || TestScenario.resampleOff59Hz => 59.0, + }; } Map? result; @override String describeValue(TestScenario value) { - switch (value) { - case TestScenario.resampleOn90Hz: - return 'resample on with 90Hz input'; - case TestScenario.resampleOn59Hz: - return 'resample on with 59Hz input'; - case TestScenario.resampleOff90Hz: - return 'resample off with 90Hz input'; - case TestScenario.resampleOff59Hz: - return 'resample off with 59Hz input'; - } + return switch (value) { + TestScenario.resampleOn90Hz => 'resample on with 90Hz input', + TestScenario.resampleOn59Hz => 'resample on with 59Hz input', + TestScenario.resampleOff90Hz => 'resample off with 90Hz input', + TestScenario.resampleOff59Hz => 'resample off with 59Hz input', + }; } @override diff --git a/dev/benchmarks/macrobenchmarks/lib/src/filtered_child_animation.dart b/dev/benchmarks/macrobenchmarks/lib/src/filtered_child_animation.dart index 4ab3b0eca78..a245f8fc46e 100644 --- a/dev/benchmarks/macrobenchmarks/lib/src/filtered_child_animation.dart +++ b/dev/benchmarks/macrobenchmarks/lib/src/filtered_child_animation.dart @@ -60,12 +60,12 @@ class _FilteredChildAnimationPageState extends State } String get _title { - switch (_filterType) { - case FilterType.opacity: return 'Fading Child Animation'; - case FilterType.rotateTransform: return 'Transformed Child Animation'; - case FilterType.rotateFilter: return 'Matrix Filtered Child Animation'; - case null: return 'Static Child'; - } + return switch (_filterType) { + FilterType.opacity => 'Fading Child Animation', + FilterType.rotateTransform => 'Transformed Child Animation', + FilterType.rotateFilter => 'Matrix Filtered Child Animation', + null => 'Static Child', + }; } static Widget _makeChild(int rows, int cols, double fontSize, bool complex) { diff --git a/dev/bots/service_worker_test.dart b/dev/bots/service_worker_test.dart index 5860ce9f8c6..219d36be728 100644 --- a/dev/bots/service_worker_test.dart +++ b/dev/bots/service_worker_test.dart @@ -109,28 +109,17 @@ Future _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 _rebuildApp({ required int version, required ServiceWorkerTestType testType, required String target }) async { diff --git a/dev/bots/unpublish_package.dart b/dev/bots/unpublish_package.dart index d614dc9b025..74290d5fb7a 100644 --- a/dev/bots/unpublish_package.dart +++ b/dev/bots/unpublish_package.dart @@ -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 diff --git a/dev/conductor/core/lib/src/repository.dart b/dev/conductor/core/lib/src/repository.dart index 99d64b9a60c..d11dc2ddfe3 100644 --- a/dev/conductor/core/lib/src/repository.dart +++ b/dev/conductor/core/lib/src/repository.dart @@ -47,12 +47,10 @@ class Remote { /// The name of the remote. String get name { - switch (_name) { - case RemoteName.upstream: - return 'upstream'; - case RemoteName.mirror: - return 'mirror'; - } + return switch (_name) { + RemoteName.upstream => 'upstream', + RemoteName.mirror => 'mirror', + }; } /// The URL of the remote. diff --git a/dev/conductor/core/lib/src/version.dart b/dev/conductor/core/lib/src/version.dart index 4edc1769563..00477d16252 100644 --- a/dev/conductor/core/lib/src/version.dart +++ b/dev/conductor/core/lib/src/version.dart @@ -295,15 +295,11 @@ class Version { @override String toString() { - switch (type) { - case VersionType.stable: - return '$x.$y.$z'; - case VersionType.development: - return '$x.$y.$z-$m.$n.pre'; - case VersionType.latest: - return '$x.$y.$z-$m.$n.pre.$commits'; - case VersionType.gitDescribe: - return '$x.$y.$z-$m.$n.pre.$commits'; - } + return switch (type) { + VersionType.stable => '$x.$y.$z', + VersionType.development => '$x.$y.$z-$m.$n.pre', + VersionType.latest => '$x.$y.$z-$m.$n.pre.$commits', + VersionType.gitDescribe => '$x.$y.$z-$m.$n.pre.$commits', + }; } } diff --git a/dev/devicelab/lib/tasks/perf_tests.dart b/dev/devicelab/lib/tasks/perf_tests.dart index 59b35ff8512..d000c2e0d90 100644 --- a/dev/devicelab/lib/tasks/perf_tests.dart +++ b/dev/devicelab/lib/tasks/perf_tests.dart @@ -2089,14 +2089,11 @@ enum ReportedDurationTestFlavor { } String _reportedDurationTestToString(ReportedDurationTestFlavor flavor) { - switch (flavor) { - case ReportedDurationTestFlavor.debug: - return 'debug'; - case ReportedDurationTestFlavor.profile: - return 'profile'; - case ReportedDurationTestFlavor.release: - return 'release'; - } + return switch (flavor) { + ReportedDurationTestFlavor.debug => 'debug', + ReportedDurationTestFlavor.profile => 'profile', + ReportedDurationTestFlavor.release => 'release', + }; } class ReportedDurationTest { diff --git a/dev/integration_tests/channels/lib/src/basic_messaging.dart b/dev/integration_tests/channels/lib/src/basic_messaging.dart index e964f9125c3..9eb1f02c29d 100644 --- a/dev/integration_tests/channels/lib/src/basic_messaging.dart +++ b/dev/integration_tests/channels/lib/src/basic_messaging.dart @@ -32,13 +32,11 @@ class ExtendedStandardMessageCodec extends StandardMessageCodec { @override dynamic readValueOfType(int type, ReadBuffer buffer) { - switch (type) { - case _dateTime: - return DateTime.fromMillisecondsSinceEpoch(buffer.getInt64()); - case _pair: - return Pair(readValue(buffer), readValue(buffer)); - default: return super.readValueOfType(type, buffer); - } + return switch (type) { + _dateTime => DateTime.fromMillisecondsSinceEpoch(buffer.getInt64()), + _pair => Pair(readValue(buffer), readValue(buffer)), + _ => super.readValueOfType(type, buffer), + }; } } diff --git a/dev/integration_tests/flutter_gallery/lib/demo/calculator/logic.dart b/dev/integration_tests/flutter_gallery/lib/demo/calculator/logic.dart index fa9e2d3bb67..ab409254339 100644 --- a/dev/integration_tests/flutter_gallery/lib/demo/calculator/logic.dart +++ b/dev/integration_tests/flutter_gallery/lib/demo/calculator/logic.dart @@ -75,16 +75,12 @@ class OperationToken extends ExpressionToken { Operation operation; static String? opString(Operation operation) { - switch (operation) { - case Operation.Addition: - return ' + '; - case Operation.Subtraction: - return ' - '; - case Operation.Multiplication: - return ' \u00D7 '; - case Operation.Division: - return ' \u00F7 '; - } + return switch (operation) { + Operation.Addition => ' + ', + Operation.Subtraction => ' - ', + Operation.Multiplication => ' \u00D7 ', + Operation.Division => ' \u00F7 ', + }; } } diff --git a/dev/integration_tests/flutter_gallery/lib/demo/material/scrollable_tabs_demo.dart b/dev/integration_tests/flutter_gallery/lib/demo/material/scrollable_tabs_demo.dart index fdf9d9705f9..c62b9888781 100644 --- a/dev/integration_tests/flutter_gallery/lib/demo/material/scrollable_tabs_demo.dart +++ b/dev/integration_tests/flutter_gallery/lib/demo/material/scrollable_tabs_demo.dart @@ -163,14 +163,11 @@ class ScrollableTabsDemoState extends State with SingleTicke isScrollable: true, indicator: getIndicator(), tabs: _allPages.map((_Page page) { - switch (_demoStyle) { - case TabsDemoStyle.iconsAndText: - return Tab(text: page.text, icon: Icon(page.icon)); - case TabsDemoStyle.iconsOnly: - return Tab(icon: Icon(page.icon)); - case TabsDemoStyle.textOnly: - return Tab(text: page.text); - } + return switch (_demoStyle) { + TabsDemoStyle.iconsAndText => Tab(text: page.text, icon: Icon(page.icon)), + TabsDemoStyle.iconsOnly => Tab(icon: Icon(page.icon)), + TabsDemoStyle.textOnly => Tab(text: page.text), + }; }).toList() ), ), diff --git a/dev/integration_tests/flutter_gallery/lib/demo/shrine/expanding_bottom_sheet.dart b/dev/integration_tests/flutter_gallery/lib/demo/shrine/expanding_bottom_sheet.dart index de2cfa43ef7..5e7a9516685 100644 --- a/dev/integration_tests/flutter_gallery/lib/demo/shrine/expanding_bottom_sheet.dart +++ b/dev/integration_tests/flutter_gallery/lib/demo/shrine/expanding_bottom_sheet.dart @@ -221,18 +221,13 @@ class ExpandingBottomSheetState extends State with TickerP // Returns the correct width of the ExpandingBottomSheet based on the number of // products in the cart. double _widthFor(int numProducts) { - switch (numProducts) { - case 0: - return _kWidthForCartIcon; - case 1: - return 136.0; - case 2: - return 192.0; - case 3: - return 248.0; - default: - return 278.0; - } + return switch (numProducts) { + 0 => _kWidthForCartIcon, + 1 => 136.0, + 2 => 192.0, + 3 => 248.0, + _ => 278.0, + }; } // Returns true if the cart is open or opening and false otherwise. diff --git a/dev/integration_tests/flutter_gallery/lib/gallery/options.dart b/dev/integration_tests/flutter_gallery/lib/gallery/options.dart index 53b542ff4eb..8e195a69162 100644 --- a/dev/integration_tests/flutter_gallery/lib/gallery/options.dart +++ b/dev/integration_tests/flutter_gallery/lib/gallery/options.dart @@ -411,20 +411,14 @@ class _PlatformItem extends StatelessWidget { final ValueChanged? onOptionsChanged; String _platformLabel(TargetPlatform platform) { - switch (platform) { - case TargetPlatform.android: - return 'Mountain View'; - case TargetPlatform.fuchsia: - return 'Fuchsia'; - case TargetPlatform.iOS: - return 'Cupertino'; - case TargetPlatform.linux: - return 'Material Desktop (linux)'; - case TargetPlatform.macOS: - return 'Material Desktop (macOS)'; - case TargetPlatform.windows: - return 'Material Desktop (Windows)'; - } + return switch (platform) { + TargetPlatform.android => 'Mountain View', + TargetPlatform.fuchsia => 'Fuchsia', + TargetPlatform.iOS => 'Cupertino', + TargetPlatform.linux => 'Material Desktop (linux)', + TargetPlatform.macOS => 'Material Desktop (macOS)', + TargetPlatform.windows => 'Material Desktop (Windows)', + }; } @override diff --git a/dev/manual_tests/lib/density.dart b/dev/manual_tests/lib/density.dart index 783b7d0108a..801ac9c3094 100644 --- a/dev/manual_tests/lib/density.dart +++ b/dev/manual_tests/lib/density.dart @@ -174,17 +174,12 @@ class _OptionsState extends State { } VisualDensity _profileToDensity(String? profile) { - switch (profile) { - case 'standard': - return VisualDensity.standard; - case 'comfortable': - return VisualDensity.comfortable; - case 'compact': - return VisualDensity.compact; - case 'custom': - default: - return widget.model.density; - } + return switch (profile) { + 'standard' => VisualDensity.standard, + 'comfortable' => VisualDensity.comfortable, + 'compact' => VisualDensity.compact, + 'custom' || _ => widget.model.density, + }; } @override diff --git a/dev/manual_tests/lib/menu_anchor.dart b/dev/manual_tests/lib/menu_anchor.dart index 11a5ebd84ee..df218cafa0a 100644 --- a/dev/manual_tests/lib/menu_anchor.dart +++ b/dev/manual_tests/lib/menu_anchor.dart @@ -478,14 +478,11 @@ class _TestMenusState extends State<_TestMenus> { void _setCheck(TestMenu item) { debugPrint('App: Set Checkbox item ${item.label}'); setState(() { - switch (checkboxState) { - case false: - checkboxState = true; - case true: - checkboxState = null; - case null: - checkboxState = false; - } + checkboxState = switch (checkboxState) { + false => true, + true => null, + null => false, + }; }); } diff --git a/dev/tools/vitool/lib/vitool.dart b/dev/tools/vitool/lib/vitool.dart index 311a8c27873..8312370b78f 100644 --- a/dev/tools/vitool/lib/vitool.dart +++ b/dev/tools/vitool/lib/vitool.dart @@ -152,19 +152,13 @@ class PathCommandAnimation { } String toDart() { - String dartCommandClass; - switch (type) { - case 'M': - dartCommandClass = '_PathMoveTo'; - case 'C': - dartCommandClass = '_PathCubicTo'; - case 'L': - dartCommandClass = '_PathLineTo'; - case 'Z': - dartCommandClass = '_PathClose'; - default: - throw Exception('unsupported path command: $type'); - } + final String dartCommandClass = switch (type) { + 'M' => '_PathMoveTo', + 'C' => '_PathCubicTo', + 'L' => '_PathLineTo', + 'Z' => '_PathClose', + _ => throw Exception('unsupported path command: $type'), + }; final StringBuffer sb = StringBuffer(); sb.write('${kIndent * 4}const $dartCommandClass(\n'); for (final List> pointFrames in points) {