8000 bug #17733 [Yaml] Fix wrong line number when comments are inserted in… · symfony/symfony@c04a999 · GitHub
[go: up one dir, main page]

Skip to content

Commit c04a999

Browse files
committed
bug #17733 [Yaml] Fix wrong line number when comments are inserted in the middle of a block. (paradajozsef)
This PR was submitted for the master branch but it was merged into the 2.7 branch instead (closes #17733). Discussion ---------- [Yaml] Fix wrong line number when comments are inserted in the middle of a block. | Q | A | ------------- | --- | Bug fix? | yes | New feature? | no | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | #15437 | License | MIT | Doc PR | - @xabbuh what is your opinion a solution like this? (counting the skipped comment lines and when exception occurs add them to the current line count.) Commits ------- d83e346 [Yaml] Fix wrong line number when comments are inserted in the middle of a block.
2 parents 9fdb90b + d83e346 commit c04a999

File tree

2 files changed

+117
-1
lines changed

2 files changed

+117
-1
lines changed

src/Symfony/Component/Yaml/Parser.php

Lines changed: 49 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -426,7 +426,7 @@ private function getNextEmbedBlock($indentation = null, $inSequence = false)
426426
}
427427

428428
// we ignore "comment" lines only when we are not inside a scalar block
429-
if (empty($blockScalarIndentations) && $this->isCurrentLineComment()) {
429+
if (empty($blockScalarIndentations) && $this->isCurrentLineComment() && false === $this->checkIfPreviousNonCommentLineIsCollectionItem()) {
430430
continue;
431431
}
432432

@@ -462,10 +462,18 @@ private function moveToNextLine()
462462

463463
/**
464464
* Moves the parser to the previous line.
465+
*
466+
* @return bool
465467
*/
466468
private function moveToPreviousLine()
467469
{
470+
if ($this->currentLineNb < 1) {
471+
return false;
472+
}
473+
468474
$this->currentLine = $this->lines[--$this->currentLineNb];
475+
476+
return true;
469477
}
470478

471479
/**
@@ -778,4 +786,44 @@ private function isBlockScalarHeader()
778786
{
779787
return (bool) preg_match('~'.self::BLOCK_SCALAR_HEADER_PATTERN.'$~', $this->currentLine);
780788
}
789+
790+
/**
791+
* Returns true if the current line is a collection item.
792+
*
793+
* @return bool
794+
*/
795+
private function isCurrentLineCollectionItem()
796+
{
797+
$ltrimmedLine = ltrim($this->currentLine, ' ');
798+
799+
return '' !== $ltrimmedLine && '-' === $ltrimmedLine[0];
800+
}
801+
802+
/**
803+
* Tests whether the current comment line is in a collection.
804+
*
805+
* @return bool
806+
*/
807+
private function checkIfPreviousNonCommentLineIsCollectionItem()
808+
{
809+
$isCollectionItem = false;
810+
$moves = 0;
811+
while ($this->moveToPreviousLine()) {
812+
++$moves;
813+
// If previous line is a comment, move back again.
814+
if ($this->isCurrentLineComment()) {
815+
continue;
816+
}
817+
$isCollectionItem = $this->isCurrentLineCollectionItem();
818+
break;
819+
}
820+
821+
// Move parser back to previous line.
822+
while ($moves > 0) {
823+
$this->moveToNextLine();
824+
--$moves;
825+
}
826+
827+
return $isCollectionItem;
828+
}
781829
}

src/Symfony/Component/Yaml/Tests/ParserTest.php

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1040,6 +1040,74 @@ public function testAdditionallyIndentedLinesAreParsedAsNewLinesInFoldedBlocks()
10401040
$this->parser->parse($yaml)
10411041
);
10421042
}
1043+
1044+
/**
1045+
* @param $lineNumber
1046+
* @param $yaml
1047+
* @dataProvider parserThrowsExceptionWithCorrectLineNumberProvider
1048+
*/
1049+
public function testParserThrowsExceptionWithCorrectLineNumber($lineNumber, $yaml)
1050+
{
1051+
$this->setExpectedException(
1052+
'\Symfony\Component\Yaml\Exception\ParseException',
1053+
sprintf('Unexpected characters near "," at line %d (near "bar: "123",").', $lineNumber)
1054+
);
1055+
1056+
$this->parser->parse($yaml);
1057+
}
1058+
1059+
public function parserThrowsExceptionWithCorrectLineNumberProvider()
1060+
{
1061+
return array(
1062+
array(
1063+
4,
1064+
<<<YAML
1065+
foo:
1066+
-
1067+
# bar
1068+
bar: "123",
1069+
YAML
1070+
),
1071+
array(
1072+
5,
1073+
<<<YAML
1074+
foo:
1075+
-
1076+
# bar
1077+
# bar
1078+
bar: "123",
1079+
YAML
1080+
),
1081+
array(
1082+
8,
1083+
<<<YAML
1084+
foo:
1085+
-
1086+
# foobar
1087+
baz: 123
1088+
bar:
1089+
-
1090+
# bar
1091+
bar: "123",
1092+
YAML
1093+
),
1094+
array(
1095+
10,
1096+
<<<YAML
1097+
foo:
1098+
-
1099+
# foobar
1100+
# foobar
1101+
baz: 123
1102+
bar:
1103+
-
1104+
# bar
1105+
# bar
1106+
bar: "123",
1107+
YAML
1108+
),
1109+
);
1110+
}
10431111
}
10441112

10451113
class B

0 commit comments

Comments
 (0)
0