Skip to content

Commit 3d625df

Browse files
stereotype441Commit Queue
authored and
Commit Queue
committed
Add comment and metadata nodes to PatternVariableDeclaration.
Bug: #49750 Change-Id: I16f3614795b6b53d2112d6093d3ef7df13abb69c Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/270921 Commit-Queue: Paul Berry <[email protected]> Reviewed-by: Konstantin Shcheglov <[email protected]> Reviewed-by: Brian Wilkerson <[email protected]>
1 parent f6d66b6 commit 3d625df

File tree

6 files changed

+65
-8
lines changed

6 files changed

+65
-8
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4134,7 +4134,7 @@ abstract class PatternAssignmentStatement implements Statement {
41344134
///
41354135
/// Clients may not extend, implement or mix-in this class.
41364136
@experimental
4137-
abstract class PatternVariableDeclaration implements AstNode {
4137+
abstract class PatternVariableDeclaration implements AnnotatedNode {
41384138
/// Return the equal sign separating the pattern from the expression.
41394139
Token get equals;
41404140

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

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9898,7 +9898,7 @@ class PatternAssignmentStatementImpl extends StatementImpl
98989898
/// patternDeclaration ::=
98999899
/// ( 'final' | 'var' ) [DartPattern] '=' [Expression]
99009900
@experimental
9901-
class PatternVariableDeclarationImpl extends AstNodeImpl
9901+
class PatternVariableDeclarationImpl extends AnnotatedNodeImpl
99029902
implements PatternVariableDeclaration {
99039903
@override
99049904
final Token equals;
@@ -9917,16 +9917,18 @@ class PatternVariableDeclarationImpl extends AstNodeImpl
99179917
required this.pattern,
99189918
required this.equals,
99199919
required this.expression,
9920+
required super.comment,
9921+
required super.metadata,
99209922
}) {
99219923
_becomeParentOf(pattern);
99229924
_becomeParentOf(expression);
99239925
}
99249926

99259927
@override
9926-
Token get beginToken => keyword;
9928+
Token get endToken => expression.endToken;
99279929

99289930
@override
9929-
Token get endToken => expression.endToken;
9931+
Token get firstTokenAfterCommentAndMetadata => keyword;
99309932

99319933
@override
99329934
ChildEntities get _childEntities => super._childEntities
@@ -9941,6 +9943,7 @@ class PatternVariableDeclarationImpl extends AstNodeImpl
99419943

99429944
@override
99439945
void visitChildren(AstVisitor visitor) {
9946+
super.visitChildren(visitor);
99449947
pattern.accept(visitor);
99459948
expression.accept(visitor);
99469949
}

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -960,6 +960,7 @@ class ToSourceVisitor implements AstVisitor<void> {
960960

961961
@override
962962
void visitPatternVariableDeclaration(PatternVariableDeclaration node) {
963+
_visitNodeList(node.metadata, separator: ' ', suffix: ' ');
963964
sink.write(node.keyword.lexeme);
964965
sink.write(' ');
965966
_visitNode(node.pattern);

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1109,7 +1109,9 @@ class AstComparator implements AstVisitor<bool> {
11091109
@override
11101110
bool visitPatternVariableDeclaration(PatternVariableDeclaration node) {
11111111
var other = _other as PatternVariableDeclaration;
1112-
return isEqualTokens(node.keyword, other.keyword) &&
1112+
return isEqualNodes(
1113+
node.documentationComment, other.documentationComment) &&
1114+
isEqualTokens(node.keyword, other.keyword) &&
11131115
isEqualNodes(node.pattern, other.pattern) &&
11141116
isEqualTokens(node.equals, other.equals) &&
11151117
isEqualNodes(node.expression, other.expression);

pkg/analyzer/lib/src/fasta/ast_builder.dart

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4770,15 +4770,16 @@ class AstBuilder extends StackListener {
47704770
Token keyword, Token equals, Token semicolon) {
47714771
var expression = pop() as ExpressionImpl;
47724772
var pattern = pop() as DartPatternImpl;
4773-
// TODO(paulberry): make use of metadata
4774-
// ignore: unused_local_variable
47754773
var metadata = pop() as List<AnnotationImpl>?;
4774+
var comment = _findComment(metadata, keyword);
47764775
push(PatternVariableDeclarationStatementImpl(
47774776
declaration: PatternVariableDeclarationImpl(
47784777
keyword: keyword,
47794778
pattern: pattern,
47804779
equals: equals,
4781-
expression: expression),
4780+
expression: expression,
4781+
comment: comment,
4782+
metadata: metadata),
47824783
semicolon: semicolon));
47834784
}
47844785

pkg/analyzer/test/generated/patterns_parser_test.dart

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5692,6 +5692,11 @@ f(x) {
56925692
assertParsedNodeText(node, r'''
56935693
PatternVariableDeclarationStatement
56945694
declaration: PatternVariableDeclaration
5695+
metadata
5696+
Annotation
5697+
atSign: @
5698+
name: SimpleIdentifier
5699+
token: annotation
56955700
keyword: final
56965701
pattern: ObjectPattern
56975702
type: NamedType
@@ -5724,6 +5729,11 @@ f(x) {
57245729
assertParsedNodeText(node, r'''
57255730
PatternVariableDeclarationStatement
57265731
declaration: PatternVariableDeclaration
5732+
metadata
5733+
Annotation
5734+
atSign: @
5735+
name: SimpleIdentifier
5736+
token: annotation
57275737
keyword: final
57285738
pattern: ListPattern
57295739
leftBracket: [
@@ -5749,6 +5759,11 @@ f(x) {
57495759
assertParsedNodeText(node, r'''
57505760
PatternVariableDeclarationStatement
57515761
declaration: PatternVariableDeclaration
5762+
metadata
5763+
Annotation
5764+
atSign: @
5765+
name: SimpleIdentifier
5766+
token: annotation
57525767
keyword: final
57535768
pattern: MapPattern
57545769
leftBracket: {
@@ -5778,6 +5793,11 @@ f(x) {
57785793
assertParsedNodeText(node, r'''
57795794
PatternVariableDeclarationStatement
57805795
declaration: PatternVariableDeclaration
5796+
metadata
5797+
Annotation
5798+
atSign: @
5799+
name: SimpleIdentifier
5800+
token: annotation
57815801
keyword: final
57825802
pattern: ParenthesizedPattern
57835803
leftParenthesis: (
@@ -5802,6 +5822,11 @@ f(x) {
58025822
assertParsedNodeText(node, r'''
58035823
PatternVariableDeclarationStatement
58045824
declaration: PatternVariableDeclaration
5825+
metadata
5826+
Annotation
5827+
atSign: @
5828+
name: SimpleIdentifier
5829+
token: annotation
58055830
keyword: final
58065831
pattern: RecordPattern
58075832
leftParenthesis: (
@@ -5828,6 +5853,11 @@ f(x) {
58285853
assertParsedNodeText(node, r'''
58295854
PatternVariableDeclarationStatement
58305855
declaration: PatternVariableDeclaration
5856+
metadata
5857+
Annotation
5858+
atSign: @
5859+
name: SimpleIdentifier
5860+
token: annotation
58315861
keyword: var
58325862
pattern: ObjectPattern
58335863
type: NamedType
@@ -5860,6 +5890,11 @@ f(x) {
58605890
assertParsedNodeText(node, r'''
58615891
PatternVariableDeclarationStatement
58625892
declaration: PatternVariableDeclaration
5893+
metadata
5894+
Annotation
5895+
atSign: @
5896+
name: SimpleIdentifier
5897+
token: annotation
58635898
keyword: var
58645899
pattern: ListPattern
58655900
leftBracket: [
@@ -5885,6 +5920,11 @@ f(x) {
58855920
assertParsedNodeText(node, r'''
58865921
PatternVariableDeclarationStatement
58875922
declaration: PatternVariableDeclaration
5923+
metadata
5924+
Annotation
5925+
atSign: @
5926+
name: SimpleIdentifier
5927+
token: annotation
58885928
keyword: var
58895929
pattern: MapPattern
58905930
leftBracket: {
@@ -5914,6 +5954,11 @@ f(x) {
59145954
assertParsedNodeText(node, r'''
59155955
PatternVariableDeclarationStatement
59165956
declaration: PatternVariableDeclaration
5957+
metadata
5958+
Annotation
5959+
atSign: @
5960+
name: SimpleIdentifier
5961+
token: annotation
59175962
keyword: var
59185963
pattern: ParenthesizedPattern
59195964
leftParenthesis: (
@@ -5938,6 +5983,11 @@ f(x) {
59385983
assertParsedNodeText(node, r'''
59395984
PatternVariableDeclarationStatement
59405985
declaration: PatternVariableDeclaration
5986+
metadata
5987+
Annotation
5988+
atSign: @
5989+
name: SimpleIdentifier
5990+
token: annotation
59415991
keyword: var
59425992
pattern: RecordPattern
59435993
leftParenthesis: (

0 commit comments

Comments
 (0)