diff --git a/src/Composer/Json/JsonManipulator.php b/src/Composer/Json/JsonManipulator.php index f11dfac08..f929732fe 100644 --- a/src/Composer/Json/JsonManipulator.php +++ b/src/Composer/Json/JsonManipulator.php @@ -430,6 +430,11 @@ class JsonManipulator return false; } + // 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); + } + $this->contents = $matches['start'] . $matches['end']; if (preg_match('#^\{\s*\}\s*$#', $this->contents)) { $this->contents = "{\n}"; 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()); + + } }