Skip to content

Commit 918b58a

Browse files
kallentuCommit Queue
authored and
Commit Queue
committed
[analyzer] Dot Shorthands: Allow const property access shorthands.
This CL adds the ability to evaluate constant DotShorthandPropertyAccesses. Added a test to test const enums, class static properties, extension types. Language tests are also passing. Bug: #59835 Change-Id: I01432b7aa17d08d4a1dc005f134b7c17cd223d75 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/422844 Reviewed-by: Paul Berry <[email protected]> Commit-Queue: Kallen Tu <[email protected]>
1 parent 823947b commit 918b58a

File tree

2 files changed

+85
-0
lines changed

2 files changed

+85
-0
lines changed

pkg/analyzer/lib/src/dart/constant/evaluation.dart

+11
Original file line numberDiff line numberDiff line change
@@ -868,6 +868,17 @@ class ConstantVisitor extends UnifyingAstVisitor<Constant> {
868868
);
869869
}
870870

871+
@override
872+
Constant visitDotShorthandPropertyAccess(
873+
covariant DotShorthandPropertyAccessImpl node) {
874+
return _getConstantValue(
875+
errorNode: node,
876+
expression: node,
877+
identifier: node.propertyName,
878+
element: node.propertyName.element,
879+
);
880+
}
881+
871882
@override
872883
Constant visitDoubleLiteral(DoubleLiteral node) {
873884
return DartObjectImpl(

pkg/analyzer/test/src/dart/resolution/dot_shorthand_property_access_test.dart

+74
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,80 @@ DotShorthandPropertyAccess
4343
''');
4444
}
4545

46+
test_const_class() async {
47+
await assertNoErrorsInCode('''
48+
class C {
49+
static const C member = const C._(1);
50+
final int x;
51+
C(this.x);
52+
const C._(this.x);
53+
}
54+
55+
void main() {
56+
const C c = .member;
57+
print(c);
58+
}
59+
''');
60+
61+
var identifier = findNode.singleDotShorthandPropertyAccess;
62+
assertResolvedNodeText(identifier, r'''
63+
DotShorthandPropertyAccess
64+
period: .
65+
propertyName: SimpleIdentifier
66+
token: member
67+
element: <testLibraryFragment>::@class::C::@getter::member#element
68+
staticType: C
69+
staticType: C
70+
''');
71+
}
72+
73+
test_const_enum() async {
74+
await assertNoErrorsInCode('''
75+
enum Color { red, green, blue }
76+
77+
void main() {
78+
const Color c = .blue;
79+
print(c);
80+
}
81+
''');
82+
83+
var identifier = findNode.singleDotShorthandPropertyAccess;
84+
assertResolvedNodeText(identifier, r'''
85+
DotShorthandPropertyAccess
86+
period: .
87+
propertyName: SimpleIdentifier
88+
token: blue
89+
element: <testLibraryFragment>::@enum::Color::@getter::blue#element
90+
staticType: Color
91+
staticType: Color
92+
''');
93+
}
94+
95+
test_const_extensionType() async {
96+
await assertNoErrorsInCode('''
97+
extension type C(int x) {
98+
static const C member = const C._(1);
99+
const C._(this.x);
100+
}
101+
102+
void main() {
103+
const C c = .member;
104+
print(c);
105+
}
106+
''');
107+
108+
var identifier = findNode.singleDotShorthandPropertyAccess;
109+
assertResolvedNodeText(identifier, r'''
110+
DotShorthandPropertyAccess
111+
period: .
112+
propertyName: SimpleIdentifier
113+
token: member
114+
element: <testLibraryFragment>::@extensionType::C::@getter::member#element
115+
staticType: C
116+
staticType: C
117+
''');
118+
}
119+
46120
test_enum_basic() async {
47121
await assertNoErrorsInCode('''
48122
enum C { red }

0 commit comments

Comments
 (0)