Skip to content
This repository was archived by the owner on Feb 25, 2025. It is now read-only.

Commit 1ffcccd

Browse files
Dan Rubelcommit-bot@chromium.org
authored andcommitted
add extension method header parser recovery
Change-Id: Ia7424a467014471c01eb30604a062430de42f84d Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/103580 Reviewed-by: Brian Wilkerson <[email protected]> Commit-Queue: Dan Rubel <[email protected]>
1 parent db36754 commit 1ffcccd

File tree

2 files changed

+46
-2
lines changed

2 files changed

+46
-2
lines changed

pkg/analyzer/test/generated/parser_fasta_test.dart

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1453,6 +1453,20 @@ class ExtensionMethodsParserTest_Fasta extends FastaParserTestCase {
14531453
));
14541454
}
14551455

1456+
void test_missing_on() {
1457+
var unit = parseCompilationUnit('extension E', errors: [
1458+
expectedError(ParserErrorCode.EXPECTED_TOKEN, 10, 1),
1459+
expectedError(ParserErrorCode.EXPECTED_TYPE_NAME, 11, 0),
1460+
expectedError(ParserErrorCode.MISSING_CLASS_BODY, 11, 0),
1461+
]);
1462+
expect(unit.declarations, hasLength(1));
1463+
var extension = unit.declarations[0] as ExtensionDeclaration;
1464+
expect(extension.name.name, 'E');
1465+
expect(extension.onKeyword.lexeme, 'on');
1466+
expect((extension.extendedType as NamedType).name.name, '');
1467+
expect(extension.members, hasLength(0));
1468+
}
1469+
14561470
void test_simple() {
14571471
var unit = parseCompilationUnit('extension E on C { }');
14581472
expect(unit.declarations, hasLength(1));
@@ -1463,6 +1477,18 @@ class ExtensionMethodsParserTest_Fasta extends FastaParserTestCase {
14631477
expect(extension.members, hasLength(0));
14641478
}
14651479

1480+
void test_simple_extends() {
1481+
var unit = parseCompilationUnit('extension E extends C { }', errors: [
1482+
expectedError(ParserErrorCode.EXPECTED_INSTEAD, 12, 7),
1483+
]);
1484+
expect(unit.declarations, hasLength(1));
1485+
var extension = unit.declarations[0] as ExtensionDeclaration;
1486+
expect(extension.name.name, 'E');
1487+
expect(extension.onKeyword.lexeme, 'extends');
1488+
expect((extension.extendedType as NamedType).name.name, 'C');
1489+
expect(extension.members, hasLength(0));
1490+
}
1491+
14661492
void test_simple_not_enabled() {
14671493
parseCompilationUnit('extension E on C { }',
14681494
errors: [
@@ -1471,6 +1497,18 @@ class ExtensionMethodsParserTest_Fasta extends FastaParserTestCase {
14711497
],
14721498
featureSet: FeatureSet.forTesting(sdkVersion: '2.3.0'));
14731499
}
1500+
1501+
void test_simple_with() {
1502+
var unit = parseCompilationUnit('extension E with C { }', errors: [
1503+
expectedError(ParserErrorCode.EXPECTED_INSTEAD, 12, 4),
1504+
]);
1505+
expect(unit.declarations, hasLength(1));
1506+
var extension = unit.declarations[0] as ExtensionDeclaration;
1507+
expect(extension.name.name, 'E');
1508+
expect(extension.onKeyword.lexeme, 'with');
1509+
expect((extension.extendedType as NamedType).name.name, 'C');
1510+
expect(extension.members, hasLength(0));
1511+
}
14741512
}
14751513

14761514
/**

pkg/front_end/lib/src/fasta/parser/parser.dart

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2066,8 +2066,14 @@ class Parser {
20662066
Token onKeyword = token.next;
20672067
if (!optional('on', onKeyword)) {
20682068
// Recovery
2069-
// TODO(danrubel): implement this.
2070-
throw "Internal error: extension recovery not implemented yet.";
2069+
if (optional('extends', onKeyword) || optional('with', onKeyword)) {
2070+
reportRecoverableError(
2071+
onKeyword, fasta.templateExpectedInstead.withArguments('on'));
2072+
} else {
2073+
reportRecoverableError(
2074+
token, fasta.templateExpectedAfterButGot.withArguments('on'));
2075+
onKeyword = rewriter.insertSyntheticKeyword(token, Keyword.ON);
2076+
}
20712077
}
20722078
TypeInfo typeInfo = computeType(onKeyword, true);
20732079
token = typeInfo.ensureTypeNotVoid(onKeyword, this);

0 commit comments

Comments
 (0)