Skip to content

Commit 945e6e5

Browse files
linawolfjaapio
authored andcommitted
[TASK] Warn when code-block has no content
resolves #821
1 parent 7c66b32 commit 945e6e5

File tree

2 files changed

+12
-0
lines changed

2 files changed

+12
-0
lines changed

src/RestructuredText/Directives/CodeBlockDirective.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,12 @@ public function process(
6262
BlockContext $blockContext,
6363
Directive $directive,
6464
): Node|null {
65+
if ($blockContext->getDocumentIterator()->isEmpty()) {
66+
$this->logger->warning('The code-block has no content. Did you properly indent the code? ', $blockContext->getLoggerInformation());
67+
68+
return null;
69+
}
70+
6571
$node = new CodeNode(
6672
$blockContext->getDocumentIterator()->toArray(),
6773
);

src/RestructuredText/Parser/LinesIterator.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
use OutOfBoundsException;
1818

1919
use function chr;
20+
use function count;
2021
use function explode;
2122
use function max;
2223
use function mb_strpos;
@@ -133,6 +134,11 @@ public function toArray(): array
133134
return $this->lines;
134135
}
135136

137+
public function isEmpty(): bool
138+
{
139+
return count($this->lines) === 0 || (count($this->lines) === 1 && trim($this->lines[0]) === '');
140+
}
141+
136142
/** @psalm-assert-if-false non-empty-string $line */
137143
public static function isEmptyLine(string|null $line): bool
138144
{

0 commit comments

Comments
 (0)