Skip to content

Commit 865dcc5

Browse files
kallentuCommit Queue
authored and
Commit Queue
committed
[analyzer] Dot Shorthands: AST - DotShorthandInvocation and DotShorthandPropertyAccess.
This CL adds new nodes for the dot shorthands feature. These new nodes will be used, alongside a context type, to resolve to a method/constructor invocation or a static field/getter or tearoff. Design decision for this CL made here: https://docs.google.com/document/d/1rJuwytXFyG9Ir9key146_hAa7uhEXk4Otqd_3RpSg8A/edit?usp=sharing&resourcekey=0-hRydkMfiTwDEwsX3fN4taQ Bug: #59835 Change-Id: Iab78bd9e55e488656d1138b1d5d6fc5c9ed64bde Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/418201 Reviewed-by: Brian Wilkerson <[email protected]> Reviewed-by: Paul Berry <[email protected]> Commit-Queue: Kallen Tu <[email protected]>
1 parent 67113cf commit 865dcc5

File tree

8 files changed

+307
-2
lines changed

8 files changed

+307
-2
lines changed

pkg/analyzer/api.txt

+20
Original file line numberDiff line numberDiff line change
@@ -379,6 +379,8 @@ package:analyzer/dart/ast/ast.dart:
379379
visitDeclaredVariablePattern (method: R? Function(DeclaredVariablePattern))
380380
visitDefaultFormalParameter (method: R? Function(DefaultFormalParameter))
381381
visitDoStatement (method: R? Function(DoStatement))
382+
visitDotShorthandInvocation (method: R? Function(DotShorthandInvocation))
383+
visitDotShorthandPropertyAccess (method: R? Function(DotShorthandPropertyAccess))
382384
visitDottedName (method: R? Function(DottedName))
383385
visitDoubleLiteral (method: R? Function(DoubleLiteral))
384386
visitEmptyFunctionBody (method: R? Function(EmptyFunctionBody))
@@ -733,6 +735,12 @@ package:analyzer/dart/ast/ast.dart:
733735
rightParenthesis (getter: Token)
734736
semicolon (getter: Token)
735737
whileKeyword (getter: Token)
738+
DotShorthandInvocation (class extends InvocationExpression, experimental):
739+
memberName (getter: SimpleIdentifier)
740+
period (getter: Token)
741+
DotShorthandPropertyAccess (class extends Expression, experimental):
742+
period (getter: Token)
743+
propertyName (getter: Token)
736744
DottedName (class extends Object implements AstNode):
737745
components (getter: NodeList<SimpleIdentifier>)
738746
DoubleLiteral (class extends Object implements Literal):
@@ -1943,6 +1951,8 @@ package:analyzer/dart/ast/visitor.dart:
19431951
visitDefaultFormalParameter (method: R? Function(DefaultFormalParameter))
19441952
visitDirective (method: R? Function(Directive))
19451953
visitDoStatement (method: R? Function(DoStatement))
1954+
visitDotShorthandInvocation (method: R? Function(DotShorthandInvocation))
1955+
visitDotShorthandPropertyAccess (method: R? Function(DotShorthandPropertyAccess))
19461956
visitDottedName (method: R? Function(DottedName))
19471957
visitDoubleLiteral (method: R? Function(DoubleLiteral))
19481958
visitEmptyFunctionBody (method: R? Function(EmptyFunctionBody))
@@ -2139,6 +2149,8 @@ package:analyzer/dart/ast/visitor.dart:
21392149
visitDeclaredVariablePattern (method: R? Function(DeclaredVariablePattern))
21402150
visitDefaultFormalParameter (method: R? Function(DefaultFormalParameter))
21412151
visitDoStatement (method: R? Function(DoStatement))
2152+
visitDotShorthandInvocation (method: R? Function(DotShorthandInvocation))
2153+
visitDotShorthandPropertyAccess (method: R? Function(DotShorthandPropertyAccess))
21422154
visitDottedName (method: R? Function(DottedName))
21432155
visitDoubleLiteral (method: R? Function(DoubleLiteral))
21442156
visitEmptyFunctionBody (method: R? Function(EmptyFunctionBody))
@@ -2313,6 +2325,8 @@ package:analyzer/dart/ast/visitor.dart:
23132325
visitDeclaredVariablePattern (method: R? Function(DeclaredVariablePattern))
23142326
visitDefaultFormalParameter (method: R? Function(DefaultFormalParameter))
23152327
visitDoStatement (method: R? Function(DoStatement))
2328+
visitDotShorthandInvocation (method: R? Function(DotShorthandInvocation))
2329+
visitDotShorthandPropertyAccess (method: R? Function(DotShorthandPropertyAccess))
23162330
visitDottedName (method: R? Function(DottedName))
23172331
visitDoubleLiteral (method: R? Function(DoubleLiteral))
23182332
visitEmptyFunctionBody (method: R? Function(EmptyFunctionBody))
@@ -2487,6 +2501,8 @@ package:analyzer/dart/ast/visitor.dart:
24872501
visitDeclaredVariablePattern (method: R? Function(DeclaredVariablePattern))
24882502
visitDefaultFormalParameter (method: R? Function(DefaultFormalParameter))
24892503
visitDoStatement (method: R? Function(DoStatement))
2504+
visitDotShorthandInvocation (method: R? Function(DotShorthandInvocation))
2505+
visitDotShorthandPropertyAccess (method: R? Function(DotShorthandPropertyAccess))
24902506
visitDottedName (method: R? Function(DottedName))
24912507
visitDoubleLiteral (method: R? Function(DoubleLiteral))
24922508
visitEmptyFunctionBody (method: R? Function(EmptyFunctionBody))
@@ -2662,6 +2678,8 @@ package:analyzer/dart/ast/visitor.dart:
26622678
visitDeclaredVariablePattern (method: T? Function(DeclaredVariablePattern))
26632679
visitDefaultFormalParameter (method: T? Function(DefaultFormalParameter))
26642680
visitDoStatement (method: T? Function(DoStatement))
2681+
visitDotShorthandInvocation (method: T? Function(DotShorthandInvocation))
2682+
visitDotShorthandPropertyAccess (method: T? Function(DotShorthandPropertyAccess))
26652683
visitDottedName (method: T? Function(DottedName))
26662684
visitDoubleLiteral (method: T? Function(DoubleLiteral))
26672685
visitEmptyFunctionBody (method: T? Function(EmptyFunctionBody))
@@ -2836,6 +2854,8 @@ package:analyzer/dart/ast/visitor.dart:
28362854
visitDeclaredVariablePattern (method: R? Function(DeclaredVariablePattern))
28372855
visitDefaultFormalParameter (method: R? Function(DefaultFormalParameter))
28382856
visitDoStatement (method: R? Function(DoStatement))
2857+
visitDotShorthandInvocation (method: R? Function(DotShorthandInvocation))
2858+
visitDotShorthandPropertyAccess (method: R? Function(DotShorthandPropertyAccess))
28392859
visitDottedName (method: R? Function(DottedName))
28402860
visitDoubleLiteral (method: R? Function(DoubleLiteral))
28412861
visitEmptyFunctionBody (method: R? Function(EmptyFunctionBody))

pkg/analyzer/lib/dart/ast/ast.dart

+2
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,8 @@ export 'package:analyzer/src/dart/ast/ast.dart'
9292
DefaultFormalParameter,
9393
Directive,
9494
DoStatement,
95+
DotShorthandInvocation,
96+
DotShorthandPropertyAccess,
9597
DottedName,
9698
DoubleLiteral,
9799
EmptyFunctionBody,

pkg/analyzer/lib/dart/ast/visitor.dart

+57
Original file line numberDiff line numberDiff line change
@@ -279,6 +279,14 @@ class GeneralizingAstVisitor<R> implements AstVisitor<R> {
279279
@override
280280
R? visitDoStatement(DoStatement node) => visitStatement(node);
281281

282+
@override
283+
R? visitDotShorthandInvocation(DotShorthandInvocation node) =>
284+
visitExpression(node);
285+
286+
@override
287+
R? visitDotShorthandPropertyAccess(DotShorthandPropertyAccess node) =>
288+
visitExpression(node);
289+
282290
@override
283291
R? visitDottedName(DottedName node) => visitNode(node);
284292

@@ -1035,6 +1043,18 @@ class RecursiveAstVisitor<R> implements AstVisitor<R> {
10351043
return null;
10361044
}
10371045

1046+
@override
1047+
R? visitDotShorthandInvocation(DotShorthandInvocation node) {
1048+
node.visitChildren(this);
1049+
return null;
1050+
}
1051+
1052+
@override
1053+
R? visitDotShorthandPropertyAccess(DotShorthandPropertyAccess node) {
1054+
node.visitChildren(this);
1055+
return null;
1056+
}
1057+
10381058
@override
10391059
R? visitDottedName(DottedName node) {
10401060
node.visitChildren(this);
@@ -1965,6 +1985,12 @@ class SimpleAstVisitor<R> implements AstVisitor<R> {
19651985
@override
19661986
R? visitDoStatement(DoStatement node) => null;
19671987

1988+
@override
1989+
R? visitDotShorthandInvocation(DotShorthandInvocation node) => null;
1990+
1991+
@override
1992+
R? visitDotShorthandPropertyAccess(DotShorthandPropertyAccess node) => null;
1993+
19681994
@override
19691995
R? visitDottedName(DottedName node) => null;
19701996

@@ -2508,6 +2534,13 @@ class ThrowingAstVisitor<R> implements AstVisitor<R> {
25082534
@override
25092535
R? visitDoStatement(DoStatement node) => _throw(node);
25102536

2537+
@override
2538+
R? visitDotShorthandInvocation(DotShorthandInvocation node) => _throw(node);
2539+
2540+
@override
2541+
R? visitDotShorthandPropertyAccess(DotShorthandPropertyAccess node) =>
2542+
_throw(node);
2543+
25112544
@override
25122545
R? visitDottedName(DottedName node) => _throw(node);
25132546

@@ -3266,6 +3299,22 @@ class TimedAstVisitor<T> implements AstVisitor<T> {
32663299
return result;
32673300
}
32683301

3302+
@override
3303+
T? visitDotShorthandInvocation(DotShorthandInvocation node) {
3304+
stopwatch.start();
3305+
T? result = _baseVisitor.visitDotShorthandInvocation(node);
3306+
stopwatch.stop();
3307+
return result;
3308+
}
3309+
3310+
@override
3311+
T? visitDotShorthandPropertyAccess(DotShorthandPropertyAccess node) {
3312+
stopwatch.start();
3313+
T? result = _baseVisitor.visitDotShorthandPropertyAccess(node);
3314+
stopwatch.stop();
3315+
return result;
3316+
}
3317+
32693318
@override
32703319
T? visitDottedName(DottedName node) {
32713320
stopwatch.start();
@@ -4472,6 +4521,14 @@ class UnifyingAstVisitor<R> implements AstVisitor<R> {
44724521
@override
44734522
R? visitDoStatement(DoStatement node) => visitNode(node);
44744523

4524+
@override
4525+
R? visitDotShorthandInvocation(DotShorthandInvocation node) =>
4526+
visitNode(node);
4527+
4528+
@override
4529+
R? visitDotShorthandPropertyAccess(DotShorthandPropertyAccess node) =>
4530+
visitNode(node);
4531+
44754532
@override
44764533
R? visitDottedName(DottedName node) => visitNode(node);
44774534

pkg/analyzer/lib/src/dart/ast/ast.dart

+140-2
Original file line numberDiff line numberDiff line change
@@ -1283,6 +1283,10 @@ abstract class AstVisitor<R> {
12831283

12841284
R? visitDoStatement(DoStatement node);
12851285

1286+
R? visitDotShorthandInvocation(DotShorthandInvocation node);
1287+
1288+
R? visitDotShorthandPropertyAccess(DotShorthandPropertyAccess node);
1289+
12861290
R? visitDottedName(DottedName node);
12871291

12881292
R? visitDoubleLiteral(DoubleLiteral node);
@@ -5392,6 +5396,140 @@ final class DoStatementImpl extends StatementImpl implements DoStatement {
53925396
}
53935397
}
53945398

5399+
/// A node that represents a dot shorthand static method or constructor
5400+
/// invocation.
5401+
///
5402+
/// For example, `.parse('42')`.
5403+
///
5404+
/// dotShorthandHead ::=
5405+
/// '.' [SimpleIdentifier] [TypeArgumentList]? [ArgumentList]
5406+
@experimental
5407+
@AnalyzerPublicApi(message: 'exported by lib/dart/ast/ast.dart')
5408+
abstract final class DotShorthandInvocation extends InvocationExpression {
5409+
/// The name of the constructor or static method invocation.
5410+
SimpleIdentifier get memberName;
5411+
5412+
/// The token representing the period.
5413+
Token get period;
5414+
}
5415+
5416+
final class DotShorthandInvocationImpl extends InvocationExpressionImpl
5417+
implements DotShorthandInvocation {
5418+
@override
5419+
final Token period;
5420+
5421+
SimpleIdentifierImpl _memberName;
5422+
5423+
/// Initializes a newly created dot shorthand invocation.
5424+
DotShorthandInvocationImpl({
5425+
required this.period,
5426+
required SimpleIdentifierImpl memberName,
5427+
required super.typeArguments,
5428+
required super.argumentList,
5429+
}) : _memberName = memberName {
5430+
_becomeParentOf(_memberName);
5431+
}
5432+
5433+
@override
5434+
Token get beginToken => period;
5435+
5436+
@override
5437+
Token get endToken => argumentList.endToken;
5438+
5439+
@override
5440+
ExpressionImpl get function => memberName;
5441+
5442+
@override
5443+
SimpleIdentifierImpl get memberName => _memberName;
5444+
5445+
set memberName(SimpleIdentifierImpl identifier) {
5446+
_memberName = _becomeParentOf(identifier);
5447+
}
5448+
5449+
@override
5450+
Precedence get precedence => Precedence.postfix;
5451+
5452+
@override
5453+
ChildEntities get _childEntities => ChildEntities()
5454+
..addToken('period', period)
5455+
..addNode('memberName', memberName)
5456+
..addNode('typeArguments', typeArguments)
5457+
..addNode('argumentList', argumentList);
5458+
5459+
@override
5460+
E? accept<E>(AstVisitor<E> visitor) =>
5461+
visitor.visitDotShorthandInvocation(this);
5462+
5463+
@override
5464+
void resolveExpression(ResolverVisitor resolver, TypeImpl contextType) {
5465+
resolver.visitDotShorthandInvocation(this, contextType: contextType);
5466+
}
5467+
5468+
@override
5469+
void visitChildren(AstVisitor visitor) {
5470+
memberName.accept(visitor);
5471+
typeArguments?.accept(visitor);
5472+
argumentList.accept(visitor);
5473+
}
5474+
}
5475+
5476+
/// A node that represents a dot shorthand property access of a field or a
5477+
/// static getter.
5478+
///
5479+
/// For example, `.zero`.
5480+
///
5481+
/// dotShorthandHead ::= '.' [SimpleIdentifier]
5482+
@experimental
5483+
@AnalyzerPublicApi(message: 'exported by lib/dart/ast/ast.dart')
5484+
abstract final class DotShorthandPropertyAccess extends Expression {
5485+
/// The token representing the period.
5486+
Token get period;
5487+
5488+
/// The name of the property being accessed.
5489+
Token get propertyName;
5490+
}
5491+
5492+
final class DotShorthandPropertyAccessImpl extends ExpressionImpl
5493+
implements DotShorthandPropertyAccess {
5494+
@override
5495+
final Token period;
5496+
5497+
@override
5498+
final Token propertyName;
5499+
5500+
/// Initializes a newly created dot shorthand property access.
5501+
DotShorthandPropertyAccessImpl({
5502+
required this.period,
5503+
required this.propertyName,
5504+
});
5505+
5506+
@override
5507+
Token get beginToken => period;
5508+
5509+
@override
5510+
Token get endToken => propertyName;
5511+
5512+
@override
5513+
Precedence get precedence => Precedence.postfix;
5514+
5515+
@override
5516+
ChildEntities get _childEntities => ChildEntities()
5517+
..addToken('period', period)
5518+
..addToken('propertyName', propertyName);
5519+
5520+
@override
5521+
E? accept<E>(AstVisitor<E> visitor) =>
5522+
visitor.visitDotShorthandPropertyAccess(this);
5523+
5524+
@override
5525+
void resolveExpression(ResolverVisitor resolver, TypeImpl contextType) {
5526+
resolver.visitDotShorthandPropertyAccess(this, contextType: contextType);
5527+
}
5528+
5529+
@override
5530+
void visitChildren(AstVisitor visitor) {}
5531+
}
5532+
53955533
/// A dotted name, used in a configuration within an import or export directive.
53965534
///
53975535
/// dottedName ::=
@@ -10838,8 +10976,8 @@ final class InterpolationStringImpl extends InterpolationElementImpl
1083810976

1083910977
/// The invocation of a function or method.
1084010978
///
10841-
/// This will either be a [FunctionExpressionInvocation] or a
10842-
/// [MethodInvocation].
10979+
/// This will either be a [FunctionExpressionInvocation], [MethodInvocation],
10980+
/// or a [DotShorthandInvocation].
1084310981
@AnalyzerPublicApi(message: 'exported by lib/dart/ast/ast.dart')
1084410982
abstract final class InvocationExpression implements Expression {
1084510983
/// The list of arguments to the method.

pkg/analyzer/lib/src/dart/ast/to_source_visitor.dart

+14
Original file line numberDiff line numberDiff line change
@@ -348,6 +348,20 @@ class ToSourceVisitor implements AstVisitor<void> {
348348
sink.write(');');
349349
}
350350

351+
@override
352+
void visitDotShorthandInvocation(DotShorthandInvocation node) {
353+
_visitToken(node.period);
354+
_visitNode(node.memberName);
355+
_visitNode(node.typeArguments);
356+
_visitNode(node.argumentList);
357+
}
358+
359+
@override
360+
void visitDotShorthandPropertyAccess(DotShorthandPropertyAccess node) {
361+
_visitToken(node.period);
362+
_visitToken(node.propertyName);
363+
}
364+
351365
@override
352366
void visitDottedName(DottedName node) {
353367
_visitNodeList(node.components, separator: '.');

0 commit comments

Comments
 (0)