Skip to content

Commit 833d320

Browse files
authored
Detect roles that start with !~ (#146)
* Detect roles that start with `!~` * Fixup to detect `~!` * Add preprocessing for regex Co-authored-by: Hugo van Kemenade <[email protected]> * Fix indent
1 parent 212dd3d commit 833d320

File tree

3 files changed

+26
-0
lines changed

3 files changed

+26
-0
lines changed

sphinxlint/checkers.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -556,3 +556,20 @@ def check_unnecessary_parentheses(filename, lines, options):
556556
for lno, line in enumerate(lines, start=1):
557557
for match in rst.ROLE_WITH_UNNECESSARY_PARENTHESES_RE.finditer(line):
558558
yield lno, f"Unnecessary parentheses in {match.group(0).strip()!r}"
559+
560+
561+
@checker(".rst", ".po")
562+
def check_exclamation_and_tilde(file, lines, options):
563+
"""Check for roles that start with an exclamation mark and tilde (`!~`).
564+
565+
Bad: :meth:`!~list.pop`
566+
Good: :meth:`!pop`
567+
"""
568+
for lno, line in enumerate(lines, start=1):
569+
if not ("~" in line and "!" in line and "`" in line):
570+
continue
571+
for match in rst.ROLE_WITH_EXCLAMATION_AND_TILDE_RE.finditer(line):
572+
yield (
573+
lno,
574+
f"Found a role with both `!` and `~` in {match.group(0).strip()!r}.",
575+
)

sphinxlint/rst.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -291,3 +291,5 @@ def inline_markup_gen(start_string, end_string, extra_allowed_before=""):
291291
ROLE_MISSING_CLOSING_BACKTICK_RE = re.compile(rf"({ROLE_HEAD}`[^`]+?)[^`]*$")
292292

293293
ROLE_WITH_UNNECESSARY_PARENTHESES_RE = re.compile(r"(^|\s):(func|meth):`[^`]+\(\)`")
294+
295+
ROLE_WITH_EXCLAMATION_AND_TILDE_RE = re.compile(rf"{ROLE_HEAD}`[!~]{{2}}[^`]*`")
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
.. expect: Found a role with both `!` and `~` in ':meth:`!~list.pop`'. (exclamation-and-tilde)
2+
.. expect: Found a role with both `!` and `~` in ':meth:`~!list.pop`'. (exclamation-and-tilde)
3+
4+
:meth:`!~list.pop` cannot be used to display ``pop()`` and avoid
5+
reference warnings; instead, it renders as ``~list.pop()``.
6+
We should instead write :meth:`!pop` to correctly display ``pop()``.
7+
:meth:`~!list.pop` doesn’t work either.

0 commit comments

Comments
 (0)