From 5b29b7c8e3dfa5fccc535637fdfdaf688255209b Mon Sep 17 00:00:00 2001 From: Deamon Date: Tue, 29 Aug 2017 15:29:11 +0200 Subject: [PATCH 1/3] delete last coma if deleting last element before end of file --- src/Composer/Json/JsonManipulator.php | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/Composer/Json/JsonManipulator.php b/src/Composer/Json/JsonManipulator.php index f11dfac08..6810ece27 100644 --- a/src/Composer/Json/JsonManipulator.php +++ b/src/Composer/Json/JsonManipulator.php @@ -430,6 +430,11 @@ class JsonManipulator return false; } + //check if we do not left a coma alone on previous line if it was the last line + if(preg_match('#,\s*$#', $matches['start']) && preg_match('#^\}$#', $matches['end'])) { + $matches['start'] = rtrim(preg_replace('#,(\s*)$#', '$1', $matches['start']), $this->indent); + } + $this->contents = $matches['start'] . $matches['end']; if (preg_match('#^\{\s*\}\s*$#', $this->contents)) { $this->contents = "{\n}"; From 3d753b117bdfdd67cd91be2b08d573390f26b05d Mon Sep 17 00:00:00 2001 From: Deamon Date: Mon, 11 Sep 2017 15:58:03 +0200 Subject: [PATCH 2/3] add test on remove mainkey at end of file --- .../Test/Json/JsonManipulatorTest.php | 30 +++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/tests/Composer/Test/Json/JsonManipulatorTest.php b/tests/Composer/Test/Json/JsonManipulatorTest.php index 097d7d9f4..5dd4f2c87 100644 --- a/tests/Composer/Test/Json/JsonManipulatorTest.php +++ b/tests/Composer/Test/Json/JsonManipulatorTest.php @@ -2331,4 +2331,34 @@ class JsonManipulatorTest extends \PHPUnit_Framework_TestCase } ', $manipulator->getContents()); } + + public function testRemoveMainKeyAtEndOfFile() + { + $manipulator = new JsonManipulator('{ + "require": { + "package/a": "*" + } +} +'); + $this->assertTrue($manipulator->addMainKey('homepage', 'http...')); + $this->assertTrue($manipulator->addMainKey('license', 'mit')); + $this->assertEquals('{ + "require": { + "package/a": "*" + }, + "homepage": "http...", + "license": "mit" +} +', $manipulator->getContents()); + + $this->assertTrue($manipulator->removeMainKey('homepage')); + $this->assertTrue($manipulator->removeMainKey('license')); + $this->assertEquals('{ + "require": { + "package/a": "*" + } +} +', $manipulator->getContents()); + + } } From 802849d52cae9f22ff1696cdcac9afd68da3d83a Mon Sep 17 00:00:00 2001 From: Jordi Boggiano Date: Mon, 11 Sep 2017 16:09:30 +0200 Subject: [PATCH 3/3] Wording tweaks --- src/Composer/Json/JsonManipulator.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Composer/Json/JsonManipulator.php b/src/Composer/Json/JsonManipulator.php index 6810ece27..f929732fe 100644 --- a/src/Composer/Json/JsonManipulator.php +++ b/src/Composer/Json/JsonManipulator.php @@ -430,8 +430,8 @@ class JsonManipulator return false; } - //check if we do not left a coma alone on previous line if it was the last line - if(preg_match('#,\s*$#', $matches['start']) && preg_match('#^\}$#', $matches['end'])) { + // check that we are not leaving a dangling comma on the previous line if the last line was removed + if (preg_match('#,\s*$#', $matches['start']) && preg_match('#^\}$#', $matches['end'])) { $matches['start'] = rtrim(preg_replace('#,(\s*)$#', '$1', $matches['start']), $this->indent); }