From 07614b59348f41876ee9cc75e7832a77ca43e479 Mon Sep 17 00:00:00 2001 From: Jesse Rushlow <40327885+jrushlow@users.noreply.github.com> Date: Thu, 28 Mar 2024 13:00:20 -0400 Subject: [PATCH] bug #1476 [ysm] handle linebreak followed by an immediate root-level comment --- src/Util/YamlSourceManipulator.php | 6 ++++++ tests/Util/YamlSourceManipulatorTest.php | 6 +++--- .../new_line_with_root_level_comment.test | 13 +++++++++++++ 3 files changed, 22 insertions(+), 3 deletions(-) create mode 100644 tests/Util/yaml_fixtures/new_line_with_root_level_comment.test diff --git a/src/Util/YamlSourceManipulator.php b/src/Util/YamlSourceManipulator.php index 6e84325aa..4f329b06d 100644 --- a/src/Util/YamlSourceManipulator.php +++ b/src/Util/YamlSourceManipulator.php @@ -915,6 +915,12 @@ private function advanceCurrentPosition(int $newPosition): void return; } + if (str_starts_with($advancedContent, "\n#") || str_starts_with($advancedContent, "\r\n#")) { + $this->log('A linebreak followed by a root-level comment, no indent changes'); + + return; + } + if (str_contains($advancedContent, "\n")) { $lines = explode("\n", $advancedContent); if (!empty($lines)) { diff --git a/tests/Util/YamlSourceManipulatorTest.php b/tests/Util/YamlSourceManipulatorTest.php index 97fa53d4a..f7997ce1a 100644 --- a/tests/Util/YamlSourceManipulatorTest.php +++ b/tests/Util/YamlSourceManipulatorTest.php @@ -48,7 +48,7 @@ public function testSetData(string $startingSource, array $newData, string $expe $this->assertSame($expectedSource, $actualContents); } - private function getYamlDataTests() + private function getYamlDataTests(): \Generator { $finder = new Finder(); $finder->in(__DIR__.'/yaml_fixtures') @@ -82,14 +82,14 @@ private function getYamlDataTests() */ } - public function getYamlDataTestsUnixSlashes() + public function getYamlDataTestsUnixSlashes(): \Generator { foreach ($this->getYamlDataTests() as $key => $data) { yield 'unix_'.$key => $data; } } - public function getYamlDataTestsWindowsSlashes() + public function getYamlDataTestsWindowsSlashes(): \Generator { foreach ($this->getYamlDataTests() as $key => $data) { $data['source'] = str_replace("\n", "\r\n", $data['source']); diff --git a/tests/Util/yaml_fixtures/new_line_with_root_level_comment.test b/tests/Util/yaml_fixtures/new_line_with_root_level_comment.test new file mode 100644 index 000000000..c5c281ac7 --- /dev/null +++ b/tests/Util/yaml_fixtures/new_line_with_root_level_comment.test @@ -0,0 +1,13 @@ +security: + firewalls: +# main: +=== +$data['security'] = [ + 'firewalls' => 'a firewall', + 'providers' => 'a provider' +]; +=== +security: + firewalls: 'a firewall' + providers: 'a provider' +# main: \ No newline at end of file