Skip to content

Commit fc43c8a

Browse files
scheglovCommit Queue
authored and
Commit Queue
committed
Extension type. Issue 53625. Report REPRESENTATION_FIELD_TRAILING_COMMA.
Bug: #53625 Change-Id: I89d34b35b3e8b5977d1f99d550f91ce58e1c2150 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/328340 Reviewed-by: Brian Wilkerson <[email protected]> Commit-Queue: Konstantin Shcheglov <[email protected]> Reviewed-by: Phil Quitslund <[email protected]>
1 parent e1efba0 commit fc43c8a

File tree

6 files changed

+52
-5
lines changed

6 files changed

+52
-5
lines changed

pkg/analysis_server/lib/src/services/correction/error_fix_status.yaml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3210,6 +3210,10 @@ ParserErrorCode.REDIRECTION_IN_NON_FACTORY_CONSTRUCTOR:
32103210
ParserErrorCode.REPRESENTATION_FIELD_MODIFIER:
32113211
status: needsFix
32123212
notes: Remove it.
3213+
ParserErrorCode.REPRESENTATION_FIELD_TRAILING_COMMA:
3214+
status: needsFix
3215+
notes: |-
3216+
Remove the comma.
32133217
ParserErrorCode.SEALED_ENUM:
32143218
status: needsEvaluation
32153219
ParserErrorCode.SEALED_MIXIN:

pkg/analyzer/lib/src/dart/error/syntactic_errors.g.dart

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1818,6 +1818,13 @@ class ParserErrorCode extends ErrorCode {
18181818
correctionMessage: "Try removing the modifier.",
18191819
);
18201820

1821+
static const ParserErrorCode REPRESENTATION_FIELD_TRAILING_COMMA =
1822+
ParserErrorCode(
1823+
'REPRESENTATION_FIELD_TRAILING_COMMA',
1824+
"The representation field can't have a trailing comma.",
1825+
correctionMessage: "Try removing the trailing comma.",
1826+
);
1827+
18211828
static const ParserErrorCode SEALED_ENUM = ParserErrorCode(
18221829
'SEALED_ENUM',
18231830
"Enums can't be declared to be 'sealed'.",

pkg/analyzer/lib/src/error/error_code_values.g.dart

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -863,6 +863,7 @@ const List<ErrorCode> errorCodeValues = [
863863
ParserErrorCode.REDIRECTING_CONSTRUCTOR_WITH_BODY,
864864
ParserErrorCode.REDIRECTION_IN_NON_FACTORY_CONSTRUCTOR,
865865
ParserErrorCode.REPRESENTATION_FIELD_MODIFIER,
866+
ParserErrorCode.REPRESENTATION_FIELD_TRAILING_COMMA,
866867
ParserErrorCode.SEALED_ENUM,
867868
ParserErrorCode.SEALED_MIXIN,
868869
ParserErrorCode.SEALED_MIXIN_CLASS,

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

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2819,11 +2819,18 @@ class AstBuilder extends StackListener {
28192819
fieldName = firstFormalParameter.name!;
28202820
// Check for multiple fields.
28212821
final maybeComma = firstFormalParameter.endToken.next;
2822-
if (maybeComma != null && maybeComma != formalParameterList.endToken) {
2823-
errorReporter.errorReporter?.reportErrorForToken(
2824-
ParserErrorCode.MULTIPLE_REPRESENTATION_FIELDS,
2825-
maybeComma,
2826-
);
2822+
if (maybeComma != null && maybeComma.type == TokenType.COMMA) {
2823+
if (formalParameterList.parameters.length == 1) {
2824+
errorReporter.errorReporter?.reportErrorForToken(
2825+
ParserErrorCode.REPRESENTATION_FIELD_TRAILING_COMMA,
2826+
maybeComma,
2827+
);
2828+
} else {
2829+
errorReporter.errorReporter?.reportErrorForToken(
2830+
ParserErrorCode.MULTIPLE_REPRESENTATION_FIELDS,
2831+
maybeComma,
2832+
);
2833+
}
28272834
}
28282835
} else {
28292836
errorReporter.errorReporter?.reportErrorForToken(

pkg/analyzer/messages.yaml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20474,6 +20474,9 @@ ParserErrorCode:
2047420474
REPRESENTATION_FIELD_MODIFIER:
2047520475
problemMessage: "Representation fields can't have modifiers."
2047620476
correctionMessage: Try removing the modifier.
20477+
REPRESENTATION_FIELD_TRAILING_COMMA:
20478+
problemMessage: The representation field can't have a trailing comma.
20479+
correctionMessage: Try removing the trailing comma.
2047720480
SETTER_IN_FUNCTION:
2047820481
problemMessage: "Setters can't be defined within methods or functions."
2047920482
correctionMessage: Try moving the setter outside the method or function.

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

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,31 @@ ExtensionTypeDeclaration
115115
''');
116116
}
117117

118+
test_error_trailingComma() {
119+
final parseResult = parseStringWithErrors(r'''
120+
extension type A(int it,) {}
121+
''');
122+
parseResult.assertErrors([
123+
error(ParserErrorCode.REPRESENTATION_FIELD_TRAILING_COMMA, 23, 1),
124+
]);
125+
126+
final node = parseResult.findNode.singleExtensionTypeDeclaration;
127+
assertParsedNodeText(node, r'''
128+
ExtensionTypeDeclaration
129+
extensionKeyword: extension
130+
typeKeyword: type
131+
name: A
132+
representation: RepresentationDeclaration
133+
leftParenthesis: (
134+
fieldType: NamedType
135+
name: int
136+
fieldName: it
137+
rightParenthesis: )
138+
leftBracket: {
139+
rightBracket: }
140+
''');
141+
}
142+
118143
test_featureNotEnabled() {
119144
final parseResult = parseStringWithErrors(r'''
120145
// @dart = 3.1

0 commit comments

Comments
 (0)