Skip to content

Commit 75d1ff0

Browse files
committed
[linter] Issue 295. Skip comment tokens with GitHub code blocks (in ```).
[email protected], [email protected] BUG= https://github.com/dart-lang/linter/issues/295 Review URL: https://codereview.chromium.org/2302003003 .
1 parent e1e00ed commit 75d1ff0

File tree

2 files changed

+55
-2
lines changed

2 files changed

+55
-2
lines changed

pkg/analyzer/lib/src/generated/parser.dart

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5178,9 +5178,22 @@ class Parser {
51785178
List<CommentReference> _parseCommentReferences(
51795179
List<DocumentationCommentToken> tokens) {
51805180
List<CommentReference> references = <CommentReference>[];
5181+
bool isInGitHubCodeBlock = false;
51815182
for (DocumentationCommentToken token in tokens) {
51825183
String comment = token.lexeme;
5183-
comment = _removeCodeBlocksGitHub(comment);
5184+
// Skip GitHub code blocks.
5185+
// https://help.github.com/articles/creating-and-highlighting-code-blocks/
5186+
if (tokens.length != 1) {
5187+
if (comment.indexOf('```') != -1) {
5188+
isInGitHubCodeBlock = !isInGitHubCodeBlock;
5189+
}
5190+
if (isInGitHubCodeBlock) {
5191+
continue;
5192+
}
5193+
}
5194+
// Remove GitHub include code.
5195+
comment = _removeGitHubInlineCode(comment);
5196+
// Find references.
51845197
int length = comment.length;
51855198
List<List<int>> codeBlockRanges = _getCodeBlockRanges(comment);
51865199
int leftIndex = comment.indexOf('[');
@@ -8883,7 +8896,7 @@ class Parser {
88838896
return token;
88848897
}
88858898

8886-
String _removeCodeBlocksGitHub(String comment) {
8899+
String _removeGitHubInlineCode(String comment) {
88878900
int index = 0;
88888901
while (true) {
88898902
int beginIndex = comment.indexOf('`', index);

pkg/analyzer/test/generated/parser_test.dart

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6534,6 +6534,46 @@ void''');
65346534
expect(reference.offset, 16);
65356535
}
65366536

6537+
void test_parseCommentReferences_skipCodeBlock_gitHub_multiLine() {
6538+
List<DocumentationCommentToken> tokens = <DocumentationCommentToken>[
6539+
new DocumentationCommentToken(
6540+
TokenType.MULTI_LINE_COMMENT,
6541+
r'''
6542+
/**
6543+
* First.
6544+
* ```dart
6545+
* Some [int] reference.
6546+
* ```
6547+
* Last.
6548+
*/
6549+
''',
6550+
3)
6551+
];
6552+
List<CommentReference> references =
6553+
parse("parseCommentReferences", <Object>[tokens], "")
6554+
as List<CommentReference>;
6555+
expect(references, isEmpty);
6556+
}
6557+
6558+
void test_parseCommentReferences_skipCodeBlock_gitHub_multiLine_lines() {
6559+
String commentText = r'''
6560+
/// First.
6561+
/// ```dart
6562+
/// Some [int] reference.
6563+
/// ```
6564+
/// Last.
6565+
''';
6566+
List<DocumentationCommentToken> tokens = commentText
6567+
.split('\n')
6568+
.map((line) => new DocumentationCommentToken(
6569+
TokenType.SINGLE_LINE_COMMENT, line, 0))
6570+
.toList();
6571+
List<CommentReference> references =
6572+
parse("parseCommentReferences", <Object>[tokens], "")
6573+
as List<CommentReference>;
6574+
expect(references, isEmpty);
6575+
}
6576+
65376577
void test_parseCommentReferences_skipCodeBlock_gitHub_notTerminated() {
65386578
List<DocumentationCommentToken> tokens = <DocumentationCommentToken>[
65396579
new DocumentationCommentToken(

0 commit comments

Comments
 (0)