Repository: VKCOM/noverify
Pull request #822 (2020-11-30 11:20:31)

fix quickfix


Patch:
diff --git a/src/quickfix/quickfix.go b/src/quickfix/quickfix.go
index 02b1c98..a554710 100644
--- a/src/quickfix/quickfix.go
+++ b/src/quickfix/quickfix.go
@@ -3,6 +3,7 @@ package quickfix
 import (
 	"bytes"
 	"os"
+	"sort"
 )
 
 // TODO: add quickfixes support in lang server?
@@ -24,6 +25,10 @@ func Apply(filename string, contents []byte, fixes []TextEdit) error {
 		return nil
 	}
 
+	sort.Slice(fixes, func(i, j int) bool {
+		return fixes[i].StartPos < fixes[j].StartPos
+	})
+
 	var buf bytes.Buffer
 	buf.Grow(len(contents))
 	writeFixes(&buf, contents, fixes)


Test patch:
diff --git a/src/tests/golden/testdata/quickfix/arraySyntax.php b/src/tests/golden/testdata/quickfix/arraySyntax.php
index 8d65301..34e05e6 100644
--- a/src/tests/golden/testdata/quickfix/arraySyntax.php
+++ b/src/tests/golden/testdata/quickfix/arraySyntax.php
@@ -2,6 +2,12 @@
 
 class Foo {}
 
+$_ = array(1,2,3);
+$_ = array( new Foo,    "Hello",
+        1,2,
+        3,4,
+);
+
 function f() {
     $_ = array();
     $_ = array(1,2,);
@@ -26,7 +32,7 @@ function f() {
     b(array(1,2));
 
     // So far, we cannot replace recursively, so replacement in
-    // multidimensional arrays will only be performed for internal arrays.
+    // multidimensional arrays will only be performed for external arrays.
     $_ = array(array(1,2,3), array(1,2,3));
     $_ = array(array("info" => new Foo, "for" => 2, "home" => function() {}), array(1,2,3));
 }
diff --git a/src/tests/golden/testdata/quickfix/arraySyntax.php.fix.expected b/src/tests/golden/testdata/quickfix/arraySyntax.php.fix.expected
index bbf14c2..ea6d90e 100644
--- a/src/tests/golden/testdata/quickfix/arraySyntax.php.fix.expected
+++ b/src/tests/golden/testdata/quickfix/arraySyntax.php.fix.expected
@@ -2,6 +2,12 @@
 
 class Foo {}
 
+$_ = [1,2,3];
+$_ = [ new Foo,    "Hello",
+        1,2,
+        3,4,
+];
+
 function f() {
     $_ = [];
     $_ = [1,2,];
@@ -26,9 +32,9 @@ function f() {
     b([1,2]);
 
     // So far, we cannot replace recursively, so replacement in
-    // multidimensional arrays will only be performed for internal arrays.
-    $_ = array([1,2,3], [1,2,3]);
-    $_ = array(["info" => new Foo, "for" => 2, "home" => function() {}], [1,2,3]);
+    // multidimensional arrays will only be performed for external arrays.
+    $_ = [array(1,2,3), array(1,2,3)];
+    $_ = [array("info" => new Foo, "for" => 2, "home" => function() {}), array(1,2,3)];
 }
 
 function b($a) {
diff --git a/src/tests/golden/testdata/quickfix/callSimplify.php b/src/tests/golden/testdata/quickfix/callSimplify.php
index 1443c05..066c6fd 100644
--- a/src/tests/golden/testdata/quickfix/callSimplify.php
+++ b/src/tests/golden/testdata/quickfix/callSimplify.php
@@ -2,6 +2,10 @@
 
 $array = ['a' => 1, 'b' => 2, 'c' => 3];
 
+if (in_array('z', array_keys($array))) { // 2
+    echo "contains z key\n";
+}
+
 function f1($k, $xs) {
     return in_array($k . '_id', array_keys($xs['data'][0])); // 1
 }
@@ -9,7 +13,3 @@ function f1($k, $xs) {
 function f2($k, $xs) {
     return in_array($k . '_id', $xs['data'][0]);
 }
-
-if (in_array('z', array_keys($array))) { // 2
-    echo "contains z key\n";
-}
diff --git a/src/tests/golden/testdata/quickfix/callSimplify.php.fix.expected b/src/tests/golden/testdata/quickfix/callSimplify.php.fix.expected
index 272554e..0c4c162 100644
--- a/src/tests/golden/testdata/quickfix/callSimplify.php.fix.expected
+++ b/src/tests/golden/testdata/quickfix/callSimplify.php.fix.expected
@@ -2,6 +2,10 @@
 
 $array = ['a' => 1, 'b' => 2, 'c' => 3];
 
+if (array_key_exists('z', $array)) { // 2
+    echo "contains z key\n";
+}
+
 function f1($k, $xs) {
     return array_key_exists($k . '_id', $xs['data'][0]); // 1
 }
@@ -9,7 +13,3 @@ function f1($k, $xs) {
 function f2($k, $xs) {
     return in_array($k . '_id', $xs['data'][0]);
 }
-
-if (array_key_exists('z', $array)) { // 2
-    echo "contains z key\n";
-}
diff --git a/src/tests/golden/testdata/quickfix/complexExamples.php b/src/tests/golden/testdata/quickfix/complexExamples.php
new file mode 100644
index 0000000..c68bfa5
--- /dev/null
+++ b/src/tests/golden/testdata/quickfix/complexExamples.php
@@ -0,0 +1,22 @@
+<?php
+
+function foo(array $a) { return 0; }
+
+$_ = array(isset($x) ? $x : $y); // expressions in global scope
+$_ = foo(array($x ? $x : 5));
+$_ = [
+    array(foo(array($x ? $x : 5))),
+    array(foo([$x ? $x : 5])),
+    [foo(array($x ? $x : 5))],
+    [[$x ? $x : 5]],
+];
+
+function f() {
+    $_ = [
+        array(foo(array($x ? $x : 5))),
+        array(foo([$x ? $x : 5])),
+        [foo(array($x ? $x : 5))],
+        [[$x ? $x : 5]],
+    ];
+    $_ = array(isset($x) ? $x : $y); // and in block
+}
diff --git a/src/tests/golden/testdata/quickfix/complexExamples.php.fix.expected b/src/tests/golden/testdata/quickfix/complexExamples.php.fix.expected
new file mode 100644
index 0000000..503b056
--- /dev/null
+++ b/src/tests/golden/testdata/quickfix/complexExamples.php.fix.expected
@@ -0,0 +1,22 @@
+<?php
+
+function foo(array $a) { return 0; }
+
+$_ = [isset($x) ? $x : $y]; // expressions in global scope
+$_ = foo([$x ? $x : 5]);
+$_ = [
+    [foo(array($x ? $x : 5))],
+    [foo([$x ? $x : 5])],
+    [foo([$x ? $x : 5])],
+    [[$x ?: 5]],
+];
+
+function f() {
+    $_ = [
+        [foo(array($x ? $x : 5))],
+        [foo([$x ? $x : 5])],
+        [foo([$x ? $x : 5])],
+        [[$x ?: 5]],
+    ];
+    $_ = [isset($x) ? $x : $y]; // and in block
+}