Quellcode durchsuchen

Add detection for extra commas in arrays

Jordi Boggiano vor 13 Jahren
Ursprung
Commit
f5aa3e6c79
2 geänderte Dateien mit 11 neuen und 1 gelöschten Zeilen
  1. 1 1
      src/Composer/Json/JsonFile.php
  2. 10 0
      tests/Composer/Test/Json/JsonFileTest.php

+ 1 - 1
src/Composer/Json/JsonFile.php

@@ -122,7 +122,7 @@ class JsonFile
             case JSON_ERROR_SYNTAX:
                 $msg = 'Syntax error';
                 $charOffset = 0;
-                if (preg_match('#["}\]]\s*(,)\s*\}#', $json, $match, PREG_OFFSET_CAPTURE)) {
+                if (preg_match('#["}\]]\s*(,)\s*[}\]]#', $json, $match, PREG_OFFSET_CAPTURE)) {
                     $msg .= ', extra comma';
                 } elseif (preg_match('#(["}\]]) *\r?\n *"#', $json, $match, PREG_OFFSET_CAPTURE)) {
                     $msg .= ', missing comma';

+ 10 - 0
tests/Composer/Test/Json/JsonFileTest.php

@@ -24,6 +24,16 @@ class JsonFileTest extends \PHPUnit_Framework_TestCase
         $this->expectParseException('extra comma on line 2, char 21', $json);
     }
 
+    public function testParseErrorDetectExtraCommaInArray()
+    {
+        $json = '{
+        "foo": [
+            "bar",
+        ]
+}';
+        $this->expectParseException('extra comma on line 3, char 18', $json);
+    }
+
     public function testParseErrorDetectSingleQuotes()
     {
         $json = '{