Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitc04a999

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 parents9fdb90b +d83e346 commitc04a999

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
privatefunctionmoveToPreviousLine()
467469
{
470+
if ($this->currentLineNb <1) {
471+
returnfalse;
472+
}
473+
468474
$this->currentLine =$this->lines[--$this->currentLineNb];
475+
476+
returntrue;
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+
privatefunctionisCurrentLineCollectionItem()
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+
privatefunctioncheckIfPreviousNonCommentLineIsCollectionItem()
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+
publicfunctiontestParserThrowsExceptionWithCorrectLineNumber($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+
publicfunctionparserThrowsExceptionWithCorrectLineNumberProvider()
1060+
{
1061+
returnarray(
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)

[8]ページ先頭

©2009-2025 Movatter.jp