Skip to content
Closed
Show file tree
Hide file tree
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
6 changes: 6 additions & 0 deletions sphinxlint.py
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,10 @@
# .. versionchanged:: 3.6
three_dot_directive_re = re.compile(r"\.\.\. %s::" % all_directives)

# Find directive missing space after double colon, like:
# .. versionchanged::3.6
missing_space_directive_re = re.compile(r"(?<!\.)\.\. %s::[^\s]" % simplename)
Copy link
Collaborator

Choose a reason for hiding this comment

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

Suggested change
missing_space_directive_re = re.compile(r"(?<!\.)\.\. %s::[^\s]" % simplename)
missing_space_directive_re = re.compile(r"(?<!\.)\.\. %s::[^`\s]" % simplename)

Would this tweak be enough?

>>> simplename = r"(?:(?!_)\w)+(?:[-._+:](?:(?!_)\w)+)*"
>>> missing_space_directive_re = re.compile(r"(?<!\.)\.\. %s::[^`\s]" % simplename)
>>> missing_space_directive_re.match('`.. foo::123`')
>>> missing_space_directive_re.match('``.. foo::123``')
>>> missing_space_directive_re.match('``.. foo::``')
>>> missing_space_directive_re.match(':dir:`.. foo::`')
>>> missing_space_directive_re.match('.. foo::')
>>> missing_space_directive_re.match('.. foo:: ')
>>> missing_space_directive_re.match('| .. foo:: |')
>>> missing_space_directive_re.match('.. foo::`bar`')
>>> missing_space_directive_re.match('.. foo:: valid')
>>> missing_space_directive_re.match('.. foo::invalid')
<re.Match object; span=(0, 9), match='.. foo::i'>
>>> missing_space_directive_re.match('.. foo::3.7 invalid')
<re.Match object; span=(0, 9), match='.. foo::3'>

It catches the last two (invalid), and ignores all the other ones. It will miss something like .. foo::`bar` (the user might have wanted .. foo:: `bar`, which should be valid), but this seems quite unlikely.


# Find role used with double backticks instead of simple backticks like:
# :const:``None``
# instead of:
Expand Down Expand Up @@ -217,6 +221,8 @@ def check_suspicious_constructs(file, lines):
yield lno, "comment seems to be intended as a directive"
if three_dot_directive_re.search(line):
yield lno, "directive should start with two dots, not three."
if missing_space_directive_re.search(line):
yield lno, "missing space after :: in directive"
if double_backtick_role.search(line):
yield lno, "role use a single backtick, double backtick found."
if role_glued_with_word.search(line):
Expand Down
4 changes: 4 additions & 0 deletions tests/fixtures/xfail/directive-missing-space.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Missing space after ``::`` here:

.. versionchanged::0.1
Foo bar baz.
3 changes: 3 additions & 0 deletions tests/fixtures/xpass/directive-no-arguments.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Some directives can be invoked without arguments or content.

.. contents::