Unverified Commit 3267fbc0 authored by Nate's avatar Nate Committed by GitHub

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)
parent 9c4df1f1
...@@ -68,40 +68,28 @@ class ResampleFlagVariant extends TestVariant<TestScenario> { ...@@ -68,40 +68,28 @@ class ResampleFlagVariant extends TestVariant<TestScenario> {
late TestScenario currentValue; late TestScenario currentValue;
bool get resample { bool get resample {
switch (currentValue) { return switch (currentValue) {
case TestScenario.resampleOn90Hz: TestScenario.resampleOn90Hz || TestScenario.resampleOn59Hz => true,
case TestScenario.resampleOn59Hz: TestScenario.resampleOff90Hz || TestScenario.resampleOff59Hz => false,
return true; };
case TestScenario.resampleOff90Hz:
case TestScenario.resampleOff59Hz:
return false;
}
} }
double get frequency { double get frequency {
switch (currentValue) { return switch (currentValue) {
case TestScenario.resampleOn90Hz: TestScenario.resampleOn90Hz || TestScenario.resampleOff90Hz => 90.0,
case TestScenario.resampleOff90Hz: TestScenario.resampleOn59Hz || TestScenario.resampleOff59Hz => 59.0,
return 90.0; };
case TestScenario.resampleOn59Hz:
case TestScenario.resampleOff59Hz:
return 59.0;
}
} }
Map<String, dynamic>? result; Map<String, dynamic>? result;
@override @override
String describeValue(TestScenario value) { String describeValue(TestScenario value) {
switch (value) { return switch (value) {
case TestScenario.resampleOn90Hz: TestScenario.resampleOn90Hz => 'resample on with 90Hz input',
return 'resample on with 90Hz input'; TestScenario.resampleOn59Hz => 'resample on with 59Hz input',
case TestScenario.resampleOn59Hz: TestScenario.resampleOff90Hz => 'resample off with 90Hz input',
return 'resample on with 59Hz input'; TestScenario.resampleOff59Hz => 'resample off with 59Hz input',
case TestScenario.resampleOff90Hz: };
return 'resample off with 90Hz input';
case TestScenario.resampleOff59Hz:
return 'resample off with 59Hz input';
}
} }
@override @override
......
...@@ -60,12 +60,12 @@ class _FilteredChildAnimationPageState extends State<FilteredChildAnimationPage> ...@@ -60,12 +60,12 @@ class _FilteredChildAnimationPageState extends State<FilteredChildAnimationPage>
} }
String get _title { String get _title {
switch (_filterType) { return switch (_filterType) {
case FilterType.opacity: return 'Fading Child Animation'; FilterType.opacity => 'Fading Child Animation',
case FilterType.rotateTransform: return 'Transformed Child Animation'; FilterType.rotateTransform => 'Transformed Child Animation',
case FilterType.rotateFilter: return 'Matrix Filtered Child Animation'; FilterType.rotateFilter => 'Matrix Filtered Child Animation',
case null: return 'Static Child'; null => 'Static Child',
} };
} }
static Widget _makeChild(int rows, int cols, double fontSize, bool complex) { static Widget _makeChild(int rows, int cols, double fontSize, bool complex) {
......
...@@ -109,28 +109,17 @@ Future<void> _setAppVersion(int version) async { ...@@ -109,28 +109,17 @@ Future<void> _setAppVersion(int version) async {
} }
String _testTypeToIndexFile(ServiceWorkerTestType type) { String _testTypeToIndexFile(ServiceWorkerTestType type) {
late String indexFile; return switch (type) {
switch (type) { ServiceWorkerTestType.blockedServiceWorkers => 'index_with_blocked_service_workers.html',
case ServiceWorkerTestType.blockedServiceWorkers: ServiceWorkerTestType.withFlutterJs => 'index_with_flutterjs.html',
indexFile = 'index_with_blocked_service_workers.html'; ServiceWorkerTestType.withoutFlutterJs => 'index_without_flutterjs.html',
case ServiceWorkerTestType.withFlutterJs: ServiceWorkerTestType.withFlutterJsShort => 'index_with_flutterjs_short.html',
indexFile = 'index_with_flutterjs.html'; ServiceWorkerTestType.withFlutterJsEntrypointLoadedEvent => 'index_with_flutterjs_entrypoint_loaded.html',
case ServiceWorkerTestType.withoutFlutterJs: ServiceWorkerTestType.withFlutterJsTrustedTypesOn => 'index_with_flutterjs_el_tt_on.html',
indexFile = 'index_without_flutterjs.html'; ServiceWorkerTestType.withFlutterJsNonceOn => 'index_with_flutterjs_el_nonce.html',
case ServiceWorkerTestType.withFlutterJsShort: ServiceWorkerTestType.withFlutterJsCustomServiceWorkerVersion => 'index_with_flutterjs_custom_sw_version.html',
indexFile = 'index_with_flutterjs_short.html'; ServiceWorkerTestType.generatedEntrypoint => 'generated_entrypoint.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;
} }
Future<void> _rebuildApp({ required int version, required ServiceWorkerTestType testType, required String target }) async { Future<void> _rebuildApp({ required int version, required ServiceWorkerTestType testType, required String target }) async {
......
...@@ -49,53 +49,39 @@ class UnpublishException implements Exception { ...@@ -49,53 +49,39 @@ class UnpublishException implements Exception {
enum Channel { dev, beta, stable } enum Channel { dev, beta, stable }
String getChannelName(Channel channel) { String getChannelName(Channel channel) {
switch (channel) { return switch (channel) {
case Channel.beta: Channel.beta => 'beta',
return 'beta'; Channel.dev => 'dev',
case Channel.dev: Channel.stable => 'stable',
return 'dev'; };
case Channel.stable:
return 'stable';
}
} }
Channel fromChannelName(String? name) { Channel fromChannelName(String? name) {
switch (name) { return switch (name) {
case 'beta': 'beta' => Channel.beta,
return Channel.beta; 'dev' => Channel.dev,
case 'dev': 'stable' => Channel.stable,
return Channel.dev; _ => throw ArgumentError('Invalid channel name.'),
case 'stable': };
return Channel.stable;
default:
throw ArgumentError('Invalid channel name.');
}
} }
enum PublishedPlatform { linux, macos, windows } enum PublishedPlatform { linux, macos, windows }
String getPublishedPlatform(PublishedPlatform platform) { String getPublishedPlatform(PublishedPlatform platform) {
switch (platform) { return switch (platform) {
case PublishedPlatform.linux: PublishedPlatform.linux => 'linux',
return 'linux'; PublishedPlatform.macos => 'macos',
case PublishedPlatform.macos: PublishedPlatform.windows => 'windows',
return 'macos'; };
case PublishedPlatform.windows:
return 'windows';
}
} }
PublishedPlatform fromPublishedPlatform(String name) { PublishedPlatform fromPublishedPlatform(String name) {
switch (name) { return switch (name) {
case 'linux': 'linux' => PublishedPlatform.linux,
return PublishedPlatform.linux; 'macos' => PublishedPlatform.macos,
case 'macos': 'windows' => PublishedPlatform.windows,
return PublishedPlatform.macos; _ => throw ArgumentError('Invalid published platform name.'),
case 'windows': };
return PublishedPlatform.windows;
default:
throw ArgumentError('Invalid published platform name.');
}
} }
/// A helper class for classes that want to run a process, optionally have the /// A helper class for classes that want to run a process, optionally have the
......
...@@ -47,12 +47,10 @@ class Remote { ...@@ -47,12 +47,10 @@ class Remote {
/// The name of the remote. /// The name of the remote.
String get name { String get name {
switch (_name) { return switch (_name) {
case RemoteName.upstream: RemoteName.upstream => 'upstream',
return 'upstream'; RemoteName.mirror => 'mirror',
case RemoteName.mirror: };
return 'mirror';
}
} }
/// The URL of the remote. /// The URL of the remote.
......
...@@ -295,15 +295,11 @@ class Version { ...@@ -295,15 +295,11 @@ class Version {
@override @override
String toString() { String toString() {
switch (type) { return switch (type) {
case VersionType.stable: VersionType.stable => '$x.$y.$z',
return '$x.$y.$z'; VersionType.development => '$x.$y.$z-$m.$n.pre',
case VersionType.development: VersionType.latest => '$x.$y.$z-$m.$n.pre.$commits',
return '$x.$y.$z-$m.$n.pre'; VersionType.gitDescribe => '$x.$y.$z-$m.$n.pre.$commits',
case VersionType.latest: };
return '$x.$y.$z-$m.$n.pre.$commits';
case VersionType.gitDescribe:
return '$x.$y.$z-$m.$n.pre.$commits';
}
} }
} }
...@@ -2089,14 +2089,11 @@ enum ReportedDurationTestFlavor { ...@@ -2089,14 +2089,11 @@ enum ReportedDurationTestFlavor {
} }
String _reportedDurationTestToString(ReportedDurationTestFlavor flavor) { String _reportedDurationTestToString(ReportedDurationTestFlavor flavor) {
switch (flavor) { return switch (flavor) {
case ReportedDurationTestFlavor.debug: ReportedDurationTestFlavor.debug => 'debug',
return 'debug'; ReportedDurationTestFlavor.profile => 'profile',
case ReportedDurationTestFlavor.profile: ReportedDurationTestFlavor.release => 'release',
return 'profile'; };
case ReportedDurationTestFlavor.release:
return 'release';
}
} }
class ReportedDurationTest { class ReportedDurationTest {
......
...@@ -32,13 +32,11 @@ class ExtendedStandardMessageCodec extends StandardMessageCodec { ...@@ -32,13 +32,11 @@ class ExtendedStandardMessageCodec extends StandardMessageCodec {
@override @override
dynamic readValueOfType(int type, ReadBuffer buffer) { dynamic readValueOfType(int type, ReadBuffer buffer) {
switch (type) { return switch (type) {
case _dateTime: _dateTime => DateTime.fromMillisecondsSinceEpoch(buffer.getInt64()),
return DateTime.fromMillisecondsSinceEpoch(buffer.getInt64()); _pair => Pair(readValue(buffer), readValue(buffer)),
case _pair: _ => super.readValueOfType(type, buffer),
return Pair(readValue(buffer), readValue(buffer)); };
default: return super.readValueOfType(type, buffer);
}
} }
} }
......
...@@ -75,16 +75,12 @@ class OperationToken extends ExpressionToken { ...@@ -75,16 +75,12 @@ class OperationToken extends ExpressionToken {
Operation operation; Operation operation;
static String? opString(Operation operation) { static String? opString(Operation operation) {
switch (operation) { return switch (operation) {
case Operation.Addition: Operation.Addition => ' + ',
return ' + '; Operation.Subtraction => ' - ',
case Operation.Subtraction: Operation.Multiplication => ' \u00D7 ',
return ' - '; Operation.Division => ' \u00F7 ',
case Operation.Multiplication: };
return ' \u00D7 ';
case Operation.Division:
return ' \u00F7 ';
}
} }
} }
......
...@@ -163,14 +163,11 @@ class ScrollableTabsDemoState extends State<ScrollableTabsDemo> with SingleTicke ...@@ -163,14 +163,11 @@ class ScrollableTabsDemoState extends State<ScrollableTabsDemo> with SingleTicke
isScrollable: true, isScrollable: true,
indicator: getIndicator(), indicator: getIndicator(),
tabs: _allPages.map<Tab>((_Page page) { tabs: _allPages.map<Tab>((_Page page) {
switch (_demoStyle) { return switch (_demoStyle) {
case TabsDemoStyle.iconsAndText: TabsDemoStyle.iconsAndText => Tab(text: page.text, icon: Icon(page.icon)),
return Tab(text: page.text, icon: Icon(page.icon)); TabsDemoStyle.iconsOnly => Tab(icon: Icon(page.icon)),
case TabsDemoStyle.iconsOnly: TabsDemoStyle.textOnly => Tab(text: page.text),
return Tab(icon: Icon(page.icon)); };
case TabsDemoStyle.textOnly:
return Tab(text: page.text);
}
}).toList() }).toList()
), ),
), ),
......
...@@ -221,18 +221,13 @@ class ExpandingBottomSheetState extends State<ExpandingBottomSheet> with TickerP ...@@ -221,18 +221,13 @@ class ExpandingBottomSheetState extends State<ExpandingBottomSheet> with TickerP
// Returns the correct width of the ExpandingBottomSheet based on the number of // Returns the correct width of the ExpandingBottomSheet based on the number of
// products in the cart. // products in the cart.
double _widthFor(int numProducts) { double _widthFor(int numProducts) {
switch (numProducts) { return switch (numProducts) {
case 0: 0 => _kWidthForCartIcon,
return _kWidthForCartIcon; 1 => 136.0,
case 1: 2 => 192.0,
return 136.0; 3 => 248.0,
case 2: _ => 278.0,
return 192.0; };
case 3:
return 248.0;
default:
return 278.0;
}
} }
// Returns true if the cart is open or opening and false otherwise. // Returns true if the cart is open or opening and false otherwise.
......
...@@ -411,20 +411,14 @@ class _PlatformItem extends StatelessWidget { ...@@ -411,20 +411,14 @@ class _PlatformItem extends StatelessWidget {
final ValueChanged<GalleryOptions>? onOptionsChanged; final ValueChanged<GalleryOptions>? onOptionsChanged;
String _platformLabel(TargetPlatform platform) { String _platformLabel(TargetPlatform platform) {
switch (platform) { return switch (platform) {
case TargetPlatform.android: TargetPlatform.android => 'Mountain View',
return 'Mountain View'; TargetPlatform.fuchsia => 'Fuchsia',
case TargetPlatform.fuchsia: TargetPlatform.iOS => 'Cupertino',
return 'Fuchsia'; TargetPlatform.linux => 'Material Desktop (linux)',
case TargetPlatform.iOS: TargetPlatform.macOS => 'Material Desktop (macOS)',
return 'Cupertino'; TargetPlatform.windows => 'Material Desktop (Windows)',
case TargetPlatform.linux: };
return 'Material Desktop (linux)';
case TargetPlatform.macOS:
return 'Material Desktop (macOS)';
case TargetPlatform.windows:
return 'Material Desktop (Windows)';
}
} }
@override @override
......
...@@ -174,17 +174,12 @@ class _OptionsState extends State<Options> { ...@@ -174,17 +174,12 @@ class _OptionsState extends State<Options> {
} }
VisualDensity _profileToDensity(String? profile) { VisualDensity _profileToDensity(String? profile) {
switch (profile) { return switch (profile) {
case 'standard': 'standard' => VisualDensity.standard,
return VisualDensity.standard; 'comfortable' => VisualDensity.comfortable,
case 'comfortable': 'compact' => VisualDensity.compact,
return VisualDensity.comfortable; 'custom' || _ => widget.model.density,
case 'compact': };
return VisualDensity.compact;
case 'custom':
default:
return widget.model.density;
}
} }
@override @override
......
...@@ -478,14 +478,11 @@ class _TestMenusState extends State<_TestMenus> { ...@@ -478,14 +478,11 @@ class _TestMenusState extends State<_TestMenus> {
void _setCheck(TestMenu item) { void _setCheck(TestMenu item) {
debugPrint('App: Set Checkbox item ${item.label}'); debugPrint('App: Set Checkbox item ${item.label}');
setState(() { setState(() {
switch (checkboxState) { checkboxState = switch (checkboxState) {
case false: false => true,
checkboxState = true; true => null,
case true: null => false,
checkboxState = null; };
case null:
checkboxState = false;
}
}); });
} }
......
...@@ -152,19 +152,13 @@ class PathCommandAnimation { ...@@ -152,19 +152,13 @@ class PathCommandAnimation {
} }
String toDart() { String toDart() {
String dartCommandClass; final String dartCommandClass = switch (type) {
switch (type) { 'M' => '_PathMoveTo',
case 'M': 'C' => '_PathCubicTo',
dartCommandClass = '_PathMoveTo'; 'L' => '_PathLineTo',
case 'C': 'Z' => '_PathClose',
dartCommandClass = '_PathCubicTo'; _ => throw Exception('unsupported path command: $type'),
case 'L': };
dartCommandClass = '_PathLineTo';
case 'Z':
dartCommandClass = '_PathClose';
default:
throw Exception('unsupported path command: $type');
}
final StringBuffer sb = StringBuffer(); final StringBuffer sb = StringBuffer();
sb.write('${kIndent * 4}const $dartCommandClass(\n'); sb.write('${kIndent * 4}const $dartCommandClass(\n');
for (final List<Point<double>> pointFrames in points) { for (final List<Point<double>> pointFrames in points) {
......
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