Skip to content

Commit

Permalink
[TASK] Warn when code-block has no content
Browse files Browse the repository at this point in the history
resolves #821
  • Loading branch information
linawolf authored and jaapio committed Jan 21, 2024
1 parent 7c66b32 commit 945e6e5
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 0 deletions.
6 changes: 6 additions & 0 deletions src/RestructuredText/Directives/CodeBlockDirective.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,12 @@ public function process(
BlockContext $blockContext,
Directive $directive,
): Node|null {
if ($blockContext->getDocumentIterator()->isEmpty()) {
$this->logger->warning('The code-block has no content. Did you properly indent the code? ', $blockContext->getLoggerInformation());

return null;
}

$node = new CodeNode(
$blockContext->getDocumentIterator()->toArray(),
);
Expand Down
6 changes: 6 additions & 0 deletions src/RestructuredText/Parser/LinesIterator.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
use OutOfBoundsException;

use function chr;
use function count;
use function explode;
use function max;
use function mb_strpos;
Expand Down Expand Up @@ -133,6 +134,11 @@ public function toArray(): array
return $this->lines;
}

public function isEmpty(): bool
{
return count($this->lines) === 0 || (count($this->lines) === 1 && trim($this->lines[0]) === '');
}

/** @psalm-assert-if-false non-empty-string $line */
public static function isEmptyLine(string|null $line): bool
{
Expand Down

0 comments on commit 945e6e5

Please sign in to comment.