Skip to content

Commit d0c9fcb

Browse files
DanTupCommit Queue
authored andcommitted
[analysis_server] Exclude obvious Enum value access from Inline Values
Fixes Dart-Code/Dart-Code#5444 Change-Id: I92bf301236e97682269e2284f2602ceb5f83619f Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/415861 Reviewed-by: Phil Quitslund <[email protected]> Commit-Queue: Brian Wilkerson <[email protected]> Reviewed-by: Brian Wilkerson <[email protected]>
1 parent bd75a0e commit d0c9fcb

File tree

2 files changed

+49
-1
lines changed

2 files changed

+49
-1
lines changed

pkg/analysis_server/lib/src/lsp/handlers/handler_inline_value.dart

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -243,7 +243,13 @@ class _InlineValueVisitor extends GeneralizingAstVisitor<void> {
243243

244244
// Never produce values for the left side of a property access.
245245
var isTarget = parent is PropertyAccess && node == parent.realTarget;
246-
if (!isTarget) {
246+
247+
// Never produce values for obvious enum getters (this includes `values`).
248+
var isEnumGetter =
249+
node.element is GetterElement &&
250+
node.element?.enclosingElement2 is EnumElement2;
251+
252+
if (!isTarget && !isEnumGetter) {
247253
collector.recordExpression(node.element, node.offset, node.length);
248254
}
249255
}

pkg/analysis_server/test/lsp/inline_value_test.dart

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,48 @@ void f(String /*[0*/s/*0]*/) {
114114
);
115115
}
116116

117+
Future<void> test_property_getter_enum_value_excluded() async {
118+
experimentalInlineValuesProperties = true;
119+
120+
code = TestCode.parse(r'''
121+
enum MyEnum {
122+
one,
123+
}
124+
125+
void f(MyEnum x) {
126+
print(/*[0*/x/*0]*/ == MyEnum.one); // MyEnum.one excluded
127+
print(/*[1*/MyEnum.one.index/*1]*/);
128+
^
129+
}
130+
''');
131+
132+
await verify_values(
133+
code,
134+
ofTypes: {
135+
0: InlineValueVariableLookup,
136+
1: InlineValueEvaluatableExpression,
137+
},
138+
);
139+
}
140+
141+
Future<void> test_property_getter_enum_values_excluded() async {
142+
experimentalInlineValuesProperties = true;
143+
144+
code = TestCode.parse(r'''
145+
enum MyEnum {
146+
one,
147+
}
148+
149+
void f() {
150+
print(MyEnum.values); // MyEnum.values excluded
151+
print(/*[0*/MyEnum.values.length/*0]*/);
152+
^
153+
}
154+
''');
155+
156+
await verify_values(code, ofType: InlineValueEvaluatableExpression);
157+
}
158+
117159
Future<void> test_property_method() async {
118160
experimentalInlineValuesProperties = true;
119161

0 commit comments

Comments
 (0)