branch: externals/phps-mode commit d4566586764e1ce3ca88e8b55e68dcac4fe849c9 Author: Christian Johansson <christ...@cvj.se> Commit: Christian Johansson <christ...@cvj.se>
Passing bookkeeping test for $this inside arrow function --- phps-mode-parser-sdt.el | 76 +++++++++++++++++++++++++++++----------------- test/phps-mode-test-ast.el | 6 ++-- 2 files changed, 51 insertions(+), 31 deletions(-) diff --git a/phps-mode-parser-sdt.el b/phps-mode-parser-sdt.el index 3382481f34..7904d8ccf4 100644 --- a/phps-mode-parser-sdt.el +++ b/phps-mode-parser-sdt.el @@ -642,30 +642,40 @@ ;; Should add one scope look-ahead to determine if we should ;; ignore function scope before a $this-> or self:: or static:: operator (when (< scope-index (1- scope-count)) - (setq next-scope (nth (1+ scope-index) scope)) - (setq next-scope-type (car next-scope)) - (cond - ((equal next-scope-type 'global) - (setq next-scope-is-global t)) - ((equal next-scope-type 'object-operator) - (let ((downcased-scope-name (downcase (car (cdr next-scope))))) - (when (string= downcased-scope-name "$this") - (setq next-scope-is-this-object-operator t)))) - ((equal next-scope-type 'static-member) - (let ((downcased-scope-name (downcase (car (cdr next-scope))))) - (when (or - (string= downcased-scope-name "self") - (string= downcased-scope-name "static")) - - (let ((potential-uri-count (length potential-uris)) - (potential-uri-index 0)) - (while (< potential-uri-index potential-uri-count) - (setf - (nth potential-uri-index potential-uris) - (format " static%s" (nth potential-uri-index potential-uris))) - (setq potential-uri-index (1+ potential-uri-index)))) - - (setq next-scope-is-self-static-member-operator t)))))) + (let ((next-scope-index (1+ scope-index))) + (setq next-scope (nth next-scope-index scope)) + (setq next-scope-type (car next-scope)) + + ;; When next scope is arrow function, ignore it + (while (and + (equal next-scope-type 'arrow-function) + (< next-scope-index (1- scope-count))) + (setq next-scope-index (1+ next-scope-index)) + (setq next-scope (nth next-scope-index scope)) + (setq next-scope-type (car next-scope))) + + (cond + ((equal next-scope-type 'global) + (setq next-scope-is-global t)) + ((equal next-scope-type 'object-operator) + (let ((downcased-scope-name (downcase (car (cdr next-scope))))) + (when (string= downcased-scope-name "$this") + (setq next-scope-is-this-object-operator t)))) + ((equal next-scope-type 'static-member) + (let ((downcased-scope-name (downcase (car (cdr next-scope))))) + (when (or + (string= downcased-scope-name "self") + (string= downcased-scope-name "static")) + + (let ((potential-uri-count (length potential-uris)) + (potential-uri-index 0)) + (while (< potential-uri-index potential-uri-count) + (setf + (nth potential-uri-index potential-uris) + (format " static%s" (nth potential-uri-index potential-uris))) + (setq potential-uri-index (1+ potential-uri-index)))) + + (setq next-scope-is-self-static-member-operator t))))))) (let ((space-type (car item)) (space-name (car (cdr item)))) @@ -769,6 +779,7 @@ (let ((potential-uri-count (length potential-uris)) (potential-uri-index 0) (matching-uri)) + (message "potential-uris: %S" potential-uris) ;; Iterate potential-uris, select first match or if no match return the first (while (< potential-uri-index potential-uri-count) @@ -3344,6 +3355,7 @@ symbol-end) phps-mode-parser-sdt--bookkeeping-symbol-assignment-stack)))))))) + ;; Method Declaration ((equal attributed-class-statement-type 'method) ;; Go through stacks and modify symbol namespaces ;; - add function scope but only for: @@ -3367,6 +3379,7 @@ 'parameter-list)) (is-static-p)) + ;; Is static method? (when-let (method-modifiers (plist-get attributed-class-statement @@ -3374,7 +3387,10 @@ (dolist (method-modifier method-modifiers) (when (equal method-modifier 'static) (setq is-static-p t)))) + (when (and function-start function-end) + + ;; Add $this symbol in scope unless method is static (unless is-static-p (push (list @@ -3383,6 +3399,10 @@ (plist-get attributed-class-statement 'ast-start) (plist-get attributed-class-statement 'ast-end)) phps-mode-parser-sdt--bookkeeping-symbol-assignment-stack)) + + ;; TODO if we have a arrow function scope function should come before it (to the right) + + ;; Add function scope to symbol assignments (when phps-mode-parser-sdt--bookkeeping-symbol-assignment-stack (dolist ( symbol-list @@ -3402,6 +3422,8 @@ (setcar (cdr symbol-list) symbol-scope)))))) + + ;; Add function scope to symbol reads (when phps-mode-parser-sdt--bookkeeping-symbol-stack (dolist ( symbol-list @@ -3416,14 +3438,12 @@ (< symbol-start function-start) (> symbol-start function-end)) (let ((symbol-scope (car (cdr symbol-list)))) - (push - (list 'function function-name) - symbol-scope) + (push (list 'function function-name) symbol-scope) (setcar (cdr symbol-list) symbol-scope))))))) - ;; Bookkeep method parameters + ;; Create symbol assignments out of method parameters (let ((symbol-scope phps-mode-parser-sdt--bookkeeping-namespace)) (push (list 'function function-name) symbol-scope) diff --git a/test/phps-mode-test-ast.el b/test/phps-mode-test-ast.el index 80e901d542..091167e56a 100644 --- a/test/phps-mode-test-ast.el +++ b/test/phps-mode-test-ast.el @@ -364,12 +364,12 @@ '((" class MyClass function there id $this" ((139 196))) ((153 158) 1) ((68 73) 0))) (phps-mode-test-ast--should-bookkeep - "<?php\n\nclass myClass\n{\n private $test = 'abc';\n public function test($d)\n {\n return fn($e) => $this->test . $d . $e;\n }\n}\n\n$a = new myClass();\necho $a->test('def')('ghi');" + "<?php\n\nclass myClass\n{\n private $tost = 'abc';\n public function test($d)\n {\n return fn($e) => $this->tost . $d . $e;\n }\n}\n\n$a = new myClass();\necho $a->test('def')('ghi');" "Bookkeeping of $this reference inside arrow function inside of method" - '((" class myClass id $test" 1) ((36 41) 1) (" class myClass function test id $this" 1) (" class myClass function test id $d" 1) ((76 78) 1) (" class myClass function test arrow function 1 id $e" 1) ((104 106) 1) ((111 116) 1) ((118 122) 1) ((125 127) 1) ((130 132) 1) (" id $a" 1) ((143 145) 1))) + '((" class myClass function test id $d" ((76 78))) (" class myClass function test id $this" ((84 139))) (" class myClass arrow 1 function test id $e" ((104 106))) (" class myClass id $tost" ((36 41))) ((104 106) 1) ((130 132) 1) ((125 127) 1) ((118 122) 1) ((111 116) 1) (" id $a" ((143 145))) ((143 145) 1) ((168 170) 1))) (phps-mode-test-ast--should-bookkeep - "<?php class myClass { static $var = '123'; static function myMethod($a) { return fn($b) => self::$var . $a . $b; }} echo myClass::myMethod('4')('5');" + "<?php\n\nclass myClass\n{\n static $var = '123';\n static function myMethod($a)\n {\n return fn($b) => self::$var . $a . $b;\n }\n}\n\necho myClass::myMethod('4')('5');" "Bookkeeping of self reference inside arrow function inside of static method" '((" class myClass static id $var" 1) ((30 34) 1) (" class myClass function myMethod id $a" 1) ((69 71) 1) (" class myClass function myMethod arrow function 1 id $b" 1) ((85 87) 1) ((98 102) 1) ((105 107) 1) ((110 112) 1)))