Fix mixed dartdoc processing

This change should fix the failing ResynthesizeAstStrongTest.test_class_documented_mix test.

Change-Id: I0b89fa5ddbb4725ec0115836e8b5e801d18678c5
Reviewed-on: https://dart-review.googlesource.com/68900
Commit-Queue: Dan Rubel <danrubel@google.com>
Reviewed-by: Brian Wilkerson <brianwilkerson@google.com>
This commit is contained in:
Dan Rubel 2018-08-08 14:59:13 +00:00 committed by commit-bot@chromium.org
parent 575a8f8381
commit 1cd9175e3e
3 changed files with 120 additions and 7 deletions

View file

@ -2608,10 +2608,15 @@ class AstBuilder extends StackListener {
// Build and return the comment
List<CommentReference> references = parseCommentReferences(dartdoc);
List<Token> tokens = <Token>[];
while (dartdoc != null) {
tokens.add(dartdoc);
List<Token> tokens = <Token>[dartdoc];
if (dartdoc.lexeme.startsWith('///')) {
dartdoc = dartdoc.next;
while (dartdoc != null) {
if (dartdoc.lexeme.startsWith('///')) {
tokens.add(dartdoc);
}
dartdoc = dartdoc.next;
}
}
return ast.documentationComment(tokens, references);
}

View file

@ -13373,7 +13373,10 @@ class C<@Foo.bar(const [], const [1], const{"":r""}, 0xFF + 2, .3, 4.5) T> {}
expectNotNullIfNoErrors(unit);
assertNoErrors();
ClassDeclaration declaration = unit.declarations[0];
expect(declaration.documentationComment, isNotNull);
Comment comment = declaration.documentationComment;
expect(comment.isDocumentation, isTrue);
expect(comment.tokens, hasLength(1));
expect(comment.tokens[0].lexeme, '/** 2 */');
expect(declaration.metadata, hasLength(1));
}
@ -13438,6 +13441,103 @@ class C<@Foo.bar(const [], const [1], const{"":r""}, 0xFF + 2, .3, 4.5) T> {}
expect(declaration.metadata, hasLength(2));
}
void test_parseCommentAndMetadata_mix1() {
createParser(r'''
/**
* aaa
*/
/**
* bbb
*/
class A {}
''');
CompilationUnit unit = parser.parseCompilationUnit2();
expectNotNullIfNoErrors(unit);
assertNoErrors();
ClassDeclaration declaration = unit.declarations[0];
expect(declaration.metadata, hasLength(0));
List<Token> tokens = declaration.documentationComment.tokens;
expect(tokens, hasLength(1));
expect(tokens[0].lexeme, contains('bbb'));
}
void test_parseCommentAndMetadata_mix2() {
createParser(r'''
/**
* aaa
*/
/// bbb
/// ccc
class B {}
''');
CompilationUnit unit = parser.parseCompilationUnit2();
expectNotNullIfNoErrors(unit);
assertNoErrors();
ClassDeclaration declaration = unit.declarations[0];
expect(declaration.metadata, hasLength(0));
List<Token> tokens = declaration.documentationComment.tokens;
expect(tokens, hasLength(2));
expect(tokens[0].lexeme, contains('bbb'));
expect(tokens[1].lexeme, contains('ccc'));
}
void test_parseCommentAndMetadata_mix3() {
createParser(r'''
/// aaa
/// bbb
/**
* ccc
*/
class C {}
''');
CompilationUnit unit = parser.parseCompilationUnit2();
expectNotNullIfNoErrors(unit);
assertNoErrors();
ClassDeclaration declaration = unit.declarations[0];
expect(declaration.metadata, hasLength(0));
List<Token> tokens = declaration.documentationComment.tokens;
expect(tokens, hasLength(1));
expect(tokens[0].lexeme, contains('ccc'));
}
test_parseCommentAndMetadata_mix4() {
createParser(r'''
/// aaa
/// bbb
/**
* ccc
*/
/// ddd
class D {}
''');
CompilationUnit unit = parser.parseCompilationUnit2();
expectNotNullIfNoErrors(unit);
assertNoErrors();
ClassDeclaration declaration = unit.declarations[0];
expect(declaration.metadata, hasLength(0));
List<Token> tokens = declaration.documentationComment.tokens;
expect(tokens, hasLength(1));
expect(tokens[0].lexeme, contains('ddd'));
}
test_parseCommentAndMetadata_mix5() {
createParser(r'''
/**
* aaa
*/
// bbb
class E {}
''');
CompilationUnit unit = parser.parseCompilationUnit2();
expectNotNullIfNoErrors(unit);
assertNoErrors();
ClassDeclaration declaration = unit.declarations[0];
expect(declaration.metadata, hasLength(0));
List<Token> tokens = declaration.documentationComment.tokens;
expect(tokens, hasLength(1));
expect(tokens[0].lexeme, contains('aaa'));
}
void test_parseCommentAndMetadata_none() {
createParser('class C {}');
CompilationUnit unit = parser.parseCompilationUnit2();

View file

@ -5836,14 +5836,22 @@ class Parser {
/// or `null` if no dartdoc token is found.
Token findDartDoc(Token token) {
Token comments = token.precedingComments;
Token dartdoc = null;
bool isMultiline = false;
while (comments != null) {
String lexeme = comments.lexeme;
if (lexeme.startsWith('/**') || lexeme.startsWith('///')) {
break;
if (lexeme.startsWith('///')) {
if (!isMultiline) {
dartdoc = comments;
isMultiline = true;
}
} else if (lexeme.startsWith('/**')) {
dartdoc = comments;
isMultiline = false;
}
comments = comments.next;
}
return comments;
return dartdoc;
}
/// Parse the comment references in a sequence of comment tokens