Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[ysm] handle linebreak followed by an immediate root-level comment #1476

Merged
merged 12 commits into from
Mar 28, 2024
6 changes: 6 additions & 0 deletions src/Util/YamlSourceManipulator.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)) {
Expand Down
6 changes: 3 additions & 3 deletions tests/Util/YamlSourceManipulatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down Expand Up @@ -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']);
Expand Down
13 changes: 13 additions & 0 deletions tests/Util/yaml_fixtures/new_line_with_root_level_comment.test
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
security:
firewalls:
# main:
===
$data['security'] = [
'firewalls' => 'a firewall',
'providers' => 'a provider'
];
===
security:
firewalls: 'a firewall'
providers: 'a provider'
# main:
Loading