Skip to content
Merged
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
2 changes: 1 addition & 1 deletion src/Symfony/Component/Yaml/Parser.php
Original file line number Diff line number Diff line change
Expand Up @@ -775,7 +775,7 @@ private function parseValue(string $value, int $flags, string $context): mixed
}

if ($this->isCurrentLineComment()) {
continue;
break;
}

$lines[] = trim($this->currentLine);
Expand Down
37 changes: 24 additions & 13 deletions src/Symfony/Component/Yaml/Tests/ParserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1752,26 +1752,23 @@ public function testParseMultiLineUnquotedString()
}

/**
* @dataProvider getUnquotedMultilineScalarIgnoresCommentsData
* @dataProvider getUnquotedMultilineScalarHandlesCommentsAndBlanksData
*/
public function testUnquotedMultilineScalarIgnoresComments(string $yaml, array $expected)
public function testUnquotedMultilineScalarHandlesCommentsAndBlanks(string $yaml, array $expected)
{
$this->assertSame($expected, $this->parser->parse($yaml));
}

public static function getUnquotedMultilineScalarIgnoresCommentsData()
public static function getUnquotedMultilineScalarHandlesCommentsAndBlanksData()
{
yield 'comments interspersed' => [
yield 'comments interspersed stops scalar' => [
<<<YAML
key: unquoted
# this comment should be ignored
next line
# another comment
final line
# this comment terminates
another_key: works
YAML,
[
'key' => 'unquoted next line final line',
'key' => 'unquoted',
'another_key' => 'works',
],
];
Expand All @@ -1789,17 +1786,16 @@ public static function getUnquotedMultilineScalarIgnoresCommentsData()
],
];

yield 'blank lines and comments' => [
yield 'blank lines are preserved and comment stops scalar' => [
Copy link
Member

Choose a reason for hiding this comment

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

I think we should also have a test covering that an exception is thrown when the comment is not at the end.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I've added a new dedicated test testUnquotedMultilineScalarThrowsOnOrphanedLineAfterComment

<<<YAML
key: unquoted
next line

# this comment should be ignored
final line
# this comment terminates the scalar
another_key: works
YAML,
[
'key' => "unquoted next line\nfinal line",
'key' => 'unquoted next line',
'another_key' => 'works',
],
];
Expand All @@ -1818,6 +1814,21 @@ public static function getUnquotedMultilineScalarIgnoresCommentsData()
];
}

public function testUnquotedMultilineScalarThrowsOnOrphanedLineAfterComment()
{
$this->expectException(ParseException::class);
$this->expectExceptionMessage('Unable to parse at line 3 (near " next line")');

$yaml = <<<YAML
key: unquoted
# this comment terminates
next line
another_key: works
YAML;

$this->parser->parse($yaml);
}

/**
* @dataProvider unquotedStringWithTrailingComment
*/
Expand Down
Loading