Skip to content

Update RegExps in documentation_comment.dart #3571

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Nov 5, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 10 additions & 13 deletions lib/src/model/documentation_comment.dart
Original file line number Diff line number Diff line change
Expand Up @@ -14,22 +14,19 @@ import 'package:meta/meta.dart';
import 'package:path/path.dart' as p show Context;

final _templatePattern = RegExp(
r'[ ]*{@template\s+(.+?)}([\s\S]+?){@endtemplate}[ ]*(\n?)',
multiLine: true);
r'[ ]*\{@template\s+([^\s}].*?)\}([^]+?)\{@endtemplate\}[ ]*(\n?)');
final _htmlPattern = RegExp(
r'[ ]*{@inject-html\s*}([\s\S]+?){@end-inject-html}[ ]*\n?',
multiLine: true);
r'[ ]*\{@inject-html\s*\}([^]+?)\{@end-inject-html\}[ ]*\n?');

/// Matches all tool directives (even some invalid ones). This is so
/// we can give good error messages if the directive is malformed, instead of
/// just silently emitting it as-is.
final _basicToolPattern = RegExp(
r'[ ]*{@tool\s+([^}]+)}\n?([\s\S]+?)\n?{@end-tool}[ ]*\n?',
multiLine: true);
r'[ ]*{@tool\s+([^\s}][^}]*)}\n?([^]+?)\n?{@end-tool}[ ]*\n?');

final _examplePattern = RegExp(r'{@example\s+([^}]+)}');
final _examplePattern = RegExp(r'{@example\s+([^\s}][^}]*)}');

final _macroRegExp = RegExp(r'{@macro\s+([^}]+)}');
final _macroRegExp = RegExp(r'{@macro\s+([^\s}][^}]*)}');

final _htmlInjectRegExp = RegExp(r'<dartdoc-html>([a-f0-9]+)</dartdoc-html>');

Expand Down Expand Up @@ -169,7 +166,7 @@ mixin DocumentationComment
'required',
};

static final _nameBreak = RegExp('[\\s}]');
static final _nameBreak = RegExp(r'[\s}]');

// TODO(srawlins): Implement more checks; see
// https://github.com/dart-lang/dartdoc/issues/1814.
Expand Down Expand Up @@ -391,7 +388,7 @@ mixin DocumentationComment
/// Matches all youtube directives (even some invalid ones). This is so
/// we can give good error messages if the directive is malformed, instead of
/// just silently emitting it as-is.
static final _basicYouTubePattern = RegExp(r'''{@youtube\s+([^}]+)}''');
static final _basicYouTubePattern = RegExp(r'{@youtube\s+([^\s}][^}]*)}');

/// Matches YouTube IDs from supported YouTube URLs.
static final _validYouTubeUrlPattern =
Expand Down Expand Up @@ -476,7 +473,7 @@ mixin DocumentationComment
/// Matches all animation directives (even some invalid ones). This is so we
/// can give good error messages if the directive is malformed, instead of
/// just silently emitting it as-is.
static final _basicAnimationPattern = RegExp(r'''{@animation\s+([^}]+)}''');
static final _basicAnimationPattern = RegExp(r'{@animation\s+([^\s}][^}]*)}');

/// Matches valid JavaScript identifiers.
static final _validIdPattern = RegExp(r'^[a-zA-Z_]\w*$');
Expand Down Expand Up @@ -675,8 +672,8 @@ mixin DocumentationComment
/// Match group 4 is the unquoted arg, if any.
static final RegExp _argPattern = RegExp(r'([a-zA-Z\-_0-9]+=)?' // option name
r'(?:' // Start a new non-capture group for the two possibilities.
r'''(["'])((?:\\{2})*|(?:.*?[^\\](?:\\{2})*))\2|''' // with quotes.
r'([^ ]+))'); // without quotes.
r'''(["'])((?:[^\\\r\n]|\\.)*?)\2|''' // with quotes.
Copy link
Member Author

@lrhn lrhn Nov 4, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be equivalent, but simpler. The original RE stopped at the next quote not preceded by an odd number of backslashes.
The new one must match the character after a backslash as content, whether it's a backslash or not.
Effect should be the same, just much easier to read (IMO).

r'(\S+))'); // without quotes.
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a change. It didn't seem reasonable to accept newlines or tabs as part of an unquoted argument. Matching non-whitespace seemed better than just non-space-char.


/// Helper to process arguments given as a (possibly quoted) string.
///
Expand Down