Skip to content

Commit 78f5cb3

Browse files
scheglovCommit Queue
authored andcommitted
DeCo. Add 'keyword' to FunctionTypedFormalParameter.
Bug: #61701 Change-Id: Ib77276030a67c096f209520bc5ba9938cb724e76 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/459041 Reviewed-by: Brian Wilkerson <brianwilkerson@google.com> Commit-Queue: Konstantin Shcheglov <scheglov@google.com> Reviewed-by: Johnni Winther <johnniwinther@google.com>
1 parent acf8e20 commit 78f5cb3

File tree

5 files changed

+212
-0
lines changed

5 files changed

+212
-0
lines changed

pkg/analyzer/api.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1256,6 +1256,7 @@ package:analyzer/dart/ast/ast.dart:
12561256
returnType (getter: TypeAnnotation?)
12571257
typeParameters (getter: TypeParameterList?)
12581258
FunctionTypedFormalParameter (class extends Object implements NormalFormalParameter):
1259+
keyword (getter: Token?)
12591260
name (getter: Token)
12601261
parameters (getter: FormalParameterList)
12611262
question (getter: Token?)

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

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12174,6 +12174,10 @@ final class FunctionTypeAliasImpl extends TypeAliasImpl
1217412174
@AnalyzerPublicApi(message: 'exported by lib/dart/ast/ast.dart')
1217512175
abstract final class FunctionTypedFormalParameter
1217612176
implements NormalFormalParameter {
12177+
/// The token representing either the `final` or `var` keyword, or
12178+
/// `null` if no keyword was used.
12179+
Token? get keyword;
12180+
1217712181
@override
1217812182
Token get name;
1217912183

@@ -12199,6 +12203,7 @@ abstract final class FunctionTypedFormalParameter
1219912203
childEntitiesOrder: [
1220012204
GenerateNodeProperty('covariantKeyword', isSuper: true),
1220112205
GenerateNodeProperty('requiredKeyword', isSuper: true),
12206+
GenerateNodeProperty('keyword'),
1220212207
GenerateNodeProperty('returnType'),
1220312208
GenerateNodeProperty('name', isSuper: true, superNullAssertOverride: true),
1220412209
GenerateNodeProperty('typeParameters'),
@@ -12208,6 +12213,10 @@ abstract final class FunctionTypedFormalParameter
1220812213
)
1220912214
final class FunctionTypedFormalParameterImpl extends NormalFormalParameterImpl
1221012215
implements FunctionTypedFormalParameter {
12216+
@generated
12217+
@override
12218+
final Token? keyword;
12219+
1221112220
@generated
1221212221
TypeAnnotationImpl? _returnType;
1221312222

@@ -12227,6 +12236,7 @@ final class FunctionTypedFormalParameterImpl extends NormalFormalParameterImpl
1222712236
required super.metadata,
1222812237
required super.covariantKeyword,
1222912238
required super.requiredKeyword,
12239+
required this.keyword,
1223012240
required TypeAnnotationImpl? returnType,
1223112241
required super.name,
1223212242
required TypeParameterListImpl? typeParameters,
@@ -12258,6 +12268,9 @@ final class FunctionTypedFormalParameterImpl extends NormalFormalParameterImpl
1225812268
if (requiredKeyword case var requiredKeyword?) {
1225912269
return requiredKeyword;
1226012270
}
12271+
if (keyword case var keyword?) {
12272+
return keyword;
12273+
}
1226112274
if (returnType case var returnType?) {
1226212275
return returnType.beginToken;
1226312276
}
@@ -12309,6 +12322,7 @@ final class FunctionTypedFormalParameterImpl extends NormalFormalParameterImpl
1230912322
ChildEntities get _childEntities => super._childEntities
1231012323
..addToken('covariantKeyword', covariantKeyword)
1231112324
..addToken('requiredKeyword', requiredKeyword)
12325+
..addToken('keyword', keyword)
1231212326
..addNode('returnType', returnType)
1231312327
..addToken('name', name)
1231412328
..addNode('typeParameters', typeParameters)

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2072,6 +2072,7 @@ class AstBuilder extends StackListener {
20722072
metadata: metadata,
20732073
covariantKeyword: covariantKeyword,
20742074
requiredKeyword: requiredKeyword,
2075+
keyword: varOrFinal,
20752076
returnType: typeOrFunctionTypedParameter.returnType,
20762077
typeParameters: typeOrFunctionTypedParameter.typeParameters,
20772078
parameters: typeOrFunctionTypedParameter.parameters,
@@ -2289,6 +2290,7 @@ class AstBuilder extends StackListener {
22892290
metadata: null,
22902291
covariantKeyword: null,
22912292
requiredKeyword: null,
2293+
keyword: null,
22922294
name: StringToken(TokenType.IDENTIFIER, '', 0),
22932295
returnType: returnType,
22942296
typeParameters: typeParameters,

pkg/analyzer/lib/src/summary2/ast_binary_reader.dart

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -675,6 +675,15 @@ class AstBinaryReader {
675675
requiredKeyword: AstBinaryFlags.isRequired(flags)
676676
? Tokens.required_()
677677
: null,
678+
keyword: () {
679+
if (AstBinaryFlags.isFinal(flags)) {
680+
return Tokens.final_();
681+
}
682+
if (AstBinaryFlags.isVar(flags)) {
683+
return Tokens.var_();
684+
}
685+
return null;
686+
}(),
678687
returnType: returnType,
679688
typeParameters: typeParameters,
680689
question: null,

pkg/analyzer/test/src/dart/parser/class_test.dart

Lines changed: 186 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,130 @@ ConstructorDeclaration
144144
''');
145145
}
146146

147+
test_constructor_formalParameter_functionTyped_const() {
148+
var parseResult = parseStringWithErrors(r'''
149+
class A {
150+
A(const int a(String x));
151+
}
152+
''');
153+
parseResult.assertErrors([
154+
error(ParserErrorCode.extraneousModifier, 14, 5),
155+
error(ParserErrorCode.functionTypedParameterVar, 14, 5),
156+
]);
157+
158+
var node = parseResult.findNode.singleConstructorDeclaration;
159+
assertParsedNodeText(node, r'''
160+
ConstructorDeclaration
161+
returnType: SimpleIdentifier
162+
token: A
163+
parameters: FormalParameterList
164+
leftParenthesis: (
165+
parameter: FunctionTypedFormalParameter
166+
returnType: NamedType
167+
name: int
168+
name: a
169+
parameters: FormalParameterList
170+
leftParenthesis: (
171+
parameter: SimpleFormalParameter
172+
type: NamedType
173+
name: String
174+
name: x
175+
rightParenthesis: )
176+
rightParenthesis: )
177+
body: EmptyFunctionBody
178+
semicolon: ;
179+
''');
180+
}
181+
182+
test_constructor_formalParameter_functionTyped_final() {
183+
var parseResult = parseStringWithErrors(r'''
184+
class A {
185+
A(final int a(String x));
186+
}
187+
''');
188+
parseResult.assertErrors([
189+
error(ParserErrorCode.functionTypedParameterVar, 14, 5),
190+
]);
191+
192+
var node = parseResult.findNode.singleConstructorDeclaration;
193+
assertParsedNodeText(node, r'''
194+
ConstructorDeclaration
195+
returnType: SimpleIdentifier
196+
token: A
197+
parameters: FormalParameterList
198+
leftParenthesis: (
199+
parameter: FunctionTypedFormalParameter
200+
keyword: final
201+
returnType: NamedType
202+
name: int
203+
name: a
204+
parameters: FormalParameterList
205+
leftParenthesis: (
206+
parameter: SimpleFormalParameter
207+
type: NamedType
208+
name: String
209+
name: x
210+
rightParenthesis: )
211+
rightParenthesis: )
212+
body: EmptyFunctionBody
213+
semicolon: ;
214+
''');
215+
}
216+
217+
test_constructor_formalParameter_simple_const() {
218+
var parseResult = parseStringWithErrors(r'''
219+
class A {
220+
A(const int a);
221+
}
222+
''');
223+
parseResult.assertErrors([
224+
error(ParserErrorCode.extraneousModifier, 14, 5),
225+
]);
226+
227+
var node = parseResult.findNode.singleConstructorDeclaration;
228+
assertParsedNodeText(node, r'''
229+
ConstructorDeclaration
230+
returnType: SimpleIdentifier
231+
token: A
232+
parameters: FormalParameterList
233+
leftParenthesis: (
234+
parameter: SimpleFormalParameter
235+
keyword: const
236+
type: NamedType
237+
name: int
238+
name: a
239+
rightParenthesis: )
240+
body: EmptyFunctionBody
241+
semicolon: ;
242+
''');
243+
}
244+
245+
test_constructor_formalParameter_simple_final() {
246+
var parseResult = parseStringWithErrors(r'''
247+
class A {
248+
A(final int a);
249+
}
250+
''');
251+
parseResult.assertNoErrors();
252+
253+
var node = parseResult.findNode.singleConstructorDeclaration;
254+
assertParsedNodeText(node, r'''
255+
ConstructorDeclaration
256+
returnType: SimpleIdentifier
257+
token: A
258+
parameters: FormalParameterList
259+
leftParenthesis: (
260+
parameter: SimpleFormalParameter
261+
keyword: final
262+
type: NamedType
263+
name: int
264+
name: a
265+
rightParenthesis: )
266+
body: EmptyFunctionBody
267+
semicolon: ;
268+
''');
269+
}
270+
147271
test_nameWithTypeParameters_hasTypeParameters() {
148272
useDeclaringConstructorsAst = true;
149273
var parseResult = parseStringWithErrors(r'''
@@ -605,6 +729,39 @@ ClassDeclaration
605729
''');
606730
}
607731

732+
test_primaryConstructor_declaringFormalParameter_functionTyped_const() {
733+
useDeclaringConstructorsAst = true;
734+
var parseResult = parseStringWithErrors(r'''
735+
class A(const int a(String x)) {}
736+
''');
737+
parseResult.assertErrors([error(ParserErrorCode.extraneousModifier, 8, 5)]);
738+
739+
var node = parseResult.findNode.singleClassDeclaration;
740+
assertParsedNodeText(node, r'''
741+
ClassDeclaration
742+
classKeyword: class
743+
namePart: PrimaryConstructorDeclaration
744+
typeName: A
745+
formalParameters: FormalParameterList
746+
leftParenthesis: (
747+
parameter: FunctionTypedFormalParameter
748+
returnType: NamedType
749+
name: int
750+
name: a
751+
parameters: FormalParameterList
752+
leftParenthesis: (
753+
parameter: SimpleFormalParameter
754+
type: NamedType
755+
name: String
756+
name: x
757+
rightParenthesis: )
758+
rightParenthesis: )
759+
body: BlockClassBody
760+
leftBracket: {
761+
rightBracket: }
762+
''');
763+
}
764+
608765
test_primaryConstructor_declaringFormalParameter_functionTyped_final() {
609766
useDeclaringConstructorsAst = true;
610767
var parseResult = parseStringWithErrors(r'''
@@ -621,6 +778,7 @@ ClassDeclaration
621778
formalParameters: FormalParameterList
622779
leftParenthesis: (
623780
parameter: FunctionTypedFormalParameter
781+
keyword: final
624782
returnType: NamedType
625783
name: int
626784
name: a
@@ -654,6 +812,7 @@ ClassDeclaration
654812
formalParameters: FormalParameterList
655813
leftParenthesis: (
656814
parameter: FunctionTypedFormalParameter
815+
keyword: var
657816
returnType: NamedType
658817
name: int
659818
name: a
@@ -671,6 +830,33 @@ ClassDeclaration
671830
''');
672831
}
673832

833+
test_primaryConstructor_declaringFormalParameter_simple_const() {
834+
useDeclaringConstructorsAst = true;
835+
var parseResult = parseStringWithErrors(r'''
836+
class A(const int a) {}
837+
''');
838+
parseResult.assertErrors([error(ParserErrorCode.extraneousModifier, 8, 5)]);
839+
840+
var node = parseResult.findNode.singleClassDeclaration;
841+
assertParsedNodeText(node, r'''
842+
ClassDeclaration
843+
classKeyword: class
844+
namePart: PrimaryConstructorDeclaration
845+
typeName: A
846+
formalParameters: FormalParameterList
847+
leftParenthesis: (
848+
parameter: SimpleFormalParameter
849+
keyword: const
850+
type: NamedType
851+
name: int
852+
name: a
853+
rightParenthesis: )
854+
body: BlockClassBody
855+
leftBracket: {
856+
rightBracket: }
857+
''');
858+
}
859+
674860
test_primaryConstructor_declaringFormalParameter_simple_final() {
675861
useDeclaringConstructorsAst = true;
676862
var parseResult = parseStringWithErrors(r'''

0 commit comments

Comments
 (0)