Skip to content

Commit

Permalink
Handle bad source input during syntax highlighting (#2696)
Browse files Browse the repository at this point in the history
If the end condition of a syntax highlighting rule isn't met (i.e., a
code block in a comment is missing a closing ```) it's possible for the
end spans for a rule to be null. Handle this case instead of throwing an
exception.

Fixes b/178035326
  • Loading branch information
bkonyi authored Feb 11, 2021
1 parent 30ddd4f commit e6616ab
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 1 deletion.
3 changes: 2 additions & 1 deletion packages/devtools_app/lib/src/debugger/span_parser.dart
Original file line number Diff line number Diff line change
Expand Up @@ -462,7 +462,8 @@ class _MultilineMatcher extends _Matcher {
(captures ?? endCaptures ?? beginCaptures) == null) {
assert(beginSpans.length == 1);
beginSpans.first._end = scanner.position;
} else {
} else if (endSpans != null) {
// endSpans can be null if we reach EOF and haven't completed a match.
results.addAll(endSpans);
}
return results;
Expand Down
29 changes: 29 additions & 0 deletions packages/devtools_app/test/span_parser_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,13 @@ void
var
''';

const openCodeBlock = '''
/// This is an open code block:
/// ```dart
///
/// This should not cause parsing to fail.
''';

void spanTester({
@required ScopeSpan span,
@required List<String> scopes,
Expand Down Expand Up @@ -1315,5 +1322,27 @@ void main() {
length: 1,
);
});

test('handles malformed input', () {
final spans = SpanParser.parse(grammar, openCodeBlock);
expect(spans.length, 2);
spanTester(
span: spans[0],
scopes: ['comment.block.documentation.dart'],
line: 1,
column: 1,
length: 91,
);
spanTester(
span: spans[1],
scopes: [
'comment.block.documentation.dart',
'variable.other.source.dart',
],
line: 2,
column: 12,
length: 48,
);
});
});
}

0 comments on commit e6616ab

Please sign in to comment.