Skip to content

Commit 2bfc616

Browse files
authored
DOP-3211: ref label (#444)
* created new diagnostic error, implemented on ref nodes without children * ensure correct args for ChildlessRef, added test * edited ChildlessRef message * re-editted message
1 parent e11f4ea commit 2bfc616

File tree

3 files changed

+51
-0
lines changed

3 files changed

+51
-0
lines changed

snooty/diagnostics.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -329,6 +329,23 @@ def __init__(
329329
self.candidates = candidates
330330

331331

332+
class ChildlessRef(Diagnostic):
333+
severity = Diagnostic.Level.error
334+
335+
def __init__(
336+
self,
337+
target: str,
338+
start: Union[int, Tuple[int, int]],
339+
end: Union[None, int, Tuple[int, int]] = None,
340+
) -> None:
341+
super().__init__(
342+
f'Reference found without label: "{target}". Be sure to add label text to the :ref: itself OR place the target reference directly before a heading',
343+
start,
344+
end,
345+
)
346+
self.target = target
347+
348+
332349
class TodoInfo(Diagnostic):
333350
severity = Diagnostic.Level.info
334351

snooty/postprocess.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
AmbiguousTarget,
3535
CannotOpenFile,
3636
ChapterAlreadyExists,
37+
ChildlessRef,
3738
Diagnostic,
3839
DuplicatedExternalToc,
3940
DuplicateDirective,
@@ -1393,6 +1394,12 @@ def enter_node(self, fileid_stack: FileIdStack, node: n.Node) -> None:
13931394

13941395
injection_candidate.children = cloned_title_nodes
13951396

1397+
if not node.children:
1398+
line = node.span[0]
1399+
self.context.diagnostics[fileid_stack.current].append(
1400+
ChildlessRef(node.target, line)
1401+
)
1402+
13961403
def attempt_disambugation(
13971404
self, fileid: FileId, candidates: Sequence[TargetDatabase.Result]
13981405
) -> Sequence[TargetDatabase.Result]:

snooty/test_postprocess.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
from . import diagnostics
88
from .diagnostics import (
99
ChapterAlreadyExists,
10+
ChildlessRef,
1011
DocUtilsParseError,
1112
DuplicatedExternalToc,
1213
DuplicateDirective,
@@ -1165,6 +1166,32 @@ def test_language_selector() -> None:
11651166
]
11661167

11671168

1169+
def test_childless_ref_include() -> None:
1170+
with make_test(
1171+
{
1172+
Path(
1173+
"source/index.txt"
1174+
): """
1175+
.. _invisible-ref:
1176+
1177+
.. include:: /includes/fact.rst
1178+
""",
1179+
Path(
1180+
"source/includes/fact.rst"
1181+
): """
1182+
We have a link below here, but you can't see it!
1183+
1184+
See that info at :ref:`invisible-ref`
1185+
""",
1186+
}
1187+
) as result:
1188+
assert {
1189+
k: [type(diag) for diag in v] for k, v in result.diagnostics.items() if v
1190+
} == {
1191+
FileId("includes/fact.rst"): [ChildlessRef]
1192+
}, "Childless Ref diagnostics error raised"
1193+
1194+
11681195
def test_correct_diagnostic_path() -> None:
11691196
with make_test(
11701197
{

0 commit comments

Comments
 (0)