Skip to content

Commit 61865ec

Browse files
authored
[semantics] Use a switch over a map to enumerate checks (#162424)
- eliminates a const map and a typedef - leverage switch + enum language check to ensure coverage - eliminates a runtime null check - Can make the check class private
1 parent 2c145ce commit 61865ec

File tree

2 files changed

+8
-30
lines changed

2 files changed

+8
-30
lines changed

packages/flutter/lib/src/semantics/semantics.dart

Lines changed: 8 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -101,31 +101,14 @@ final int _kUnblockedUserActions =
101101
SemanticsAction.didGainAccessibilityFocus.index |
102102
SemanticsAction.didLoseAccessibilityFocus.index;
103103

104-
/// Function signature for checks in [DebugSemanticsRoleChecks.kChecks].
105-
///
106-
/// The check is run against any `node` that is sent to the platform.
107-
///
108-
/// To access the flags and properties, one should call the
109-
/// [SemanticsNode.getSemanticsData].
110-
@visibleForTesting
111-
typedef DebugSemanticsRoleCheck = FlutterError? Function(SemanticsNode node);
112-
113104
/// A static class to conduct semantics role checks.
114-
///
115-
/// When adding a new [SemanticsRole], one must also add a corresponding check
116-
/// to [kChecks].
117-
@visibleForTesting
118-
sealed class DebugSemanticsRoleChecks {
119-
/// A map to map each [SemanticsRole] to its check.
120-
static const Map<SemanticsRole, DebugSemanticsRoleCheck> kChecks =
121-
<SemanticsRole, DebugSemanticsRoleCheck>{
122-
SemanticsRole.none: _noCheckRequired,
123-
SemanticsRole.tab: _semanticsTab,
124-
SemanticsRole.tabBar: _semanticsTabBar,
125-
SemanticsRole.tabPanel: _noCheckRequired,
126-
};
127-
128-
static FlutterError? _checkSemanticsData(SemanticsNode node) => kChecks[node.role]!(node);
105+
sealed class _DebugSemanticsRoleChecks {
106+
static FlutterError? _checkSemanticsData(SemanticsNode node) => switch (node.role) {
107+
SemanticsRole.none => _noCheckRequired,
108+
SemanticsRole.tab => _semanticsTab,
109+
SemanticsRole.tabBar => _semanticsTabBar,
110+
SemanticsRole.tabPanel => _noCheckRequired,
111+
}(node);
129112

130113
static FlutterError? _noCheckRequired(SemanticsNode node) => null;
131114

@@ -3060,7 +3043,7 @@ class SemanticsNode with DiagnosticableTreeMixin {
30603043
assert(_dirty);
30613044
final SemanticsData data = getSemanticsData();
30623045
assert(() {
3063-
final FlutterError? error = DebugSemanticsRoleChecks._checkSemanticsData(this);
3046+
final FlutterError? error = _DebugSemanticsRoleChecks._checkSemanticsData(this);
30643047
if (error != null) {
30653048
throw error;
30663049
}

packages/flutter/test/widgets/semantics_role_checks_test.dart

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,10 @@
44

55
import 'dart:ui';
66

7-
import 'package:flutter/rendering.dart';
87
import 'package:flutter/widgets.dart';
98
import 'package:flutter_test/flutter_test.dart';
109

1110
void main() {
12-
test('All semantics roles has checks', () {
13-
expect(SemanticsRole.values.toSet(), DebugSemanticsRoleChecks.kChecks.keys.toSet());
14-
});
15-
1611
group('tab', () {
1712
testWidgets('failure case, empty', (WidgetTester tester) async {
1813
await tester.pumpWidget(

0 commit comments

Comments
 (0)