[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[elpa] externals/phpinspect b01d5dfbc3 3/4: Add tests for aliased import
From: |
ELPA Syncer |
Subject: |
[elpa] externals/phpinspect b01d5dfbc3 3/4: Add tests for aliased imports + fix bugs |
Date: |
Mon, 19 Aug 2024 06:58:49 -0400 (EDT) |
branch: externals/phpinspect
commit b01d5dfbc3c9f8deec7d58e505235b38239edafe
Author: Hugo Thunnissen <devel@hugot.nl>
Commit: Hugo Thunnissen <devel@hugot.nl>
Add tests for aliased imports + fix bugs
---
phpinspect-imports.el | 61 ++++++++++++++++++++++++++++++++++++++++++++++-----
phpinspect-type.el | 8 +++++++
test/test-imports.el | 39 ++++++++++++++++++++++++++++++++
3 files changed, 102 insertions(+), 6 deletions(-)
diff --git a/phpinspect-imports.el b/phpinspect-imports.el
index 2704a66199..cf153229cd 100644
--- a/phpinspect-imports.el
+++ b/phpinspect-imports.el
@@ -127,8 +127,56 @@ NAMESPACE-META itself is returned without alterations."
parent-token)
(lambda (token)
(and (phpinspect-use-p token)
- (phpinspect-word-p (cadr token))
- (phpinspect--type= import-type (phpinspect-use-name-to-type (cadadr
token)))))))
+ (eq (car (phpinspect--use-to-type-cons token))
+ (phpinspect--type-bare-name-sym import-type))))))
+
+(define-inline phpinspect-codify-token-delimiters (token)
+ (inline-letevals (token)
+ (inline-quote
+ (cond
+ ((phpinspect-namespace-p ,token) '("namespace" . ""))
+ ((phpinspect-use-p ,token) '("use" . ""))
+ ((phpinspect-annotation-p ,token) '("@" . ""))
+ ((phpinspect-root-p ,token) '("<?php" . ""))
+ ((phpinspect-token-type-p ,token :string) '("'" . "'"))
+ ((phpinspect-block-p ,token) '("{" . "}"))
+ ((phpinspect-list-p ,token) '("(" . ")"))
+ ((phpinspect-array-p ,token) '("[" . "]"))
+ ((phpinspect-object-attrib-p ,token) '("->" . ""))
+ ((phpinspect-variable-p ,token) '("$" . ""))
+ ((phpinspect-doc-block-p ,token) '("/**" . "**/"))))))
+
+(defun phpinspect-codify-token (token-meta)
+ "Pretty print TOKEN-META.
+
+Note: this function is very imcomplete and not guaranteed to work
+correctly. It might result in deleted code when used in
+unforeseen scenario's."
+ (let ((delimiters (phpinspect-codify-token-delimiters (phpinspect-meta-token
token-meta))))
+
+ (when delimiters
+ (insert (car delimiters)))
+
+ ;; FIXME: if this code is ever to be used for broader cases than formatting
+ ;; use statements, it will have to support a much wider array of token
types
+ ;; and should be tested extensively.
+ (phpinspect-splayt-traverse-lr (child (phpinspect-meta-children
token-meta))
+ (let ((token (phpinspect-meta-token child)))
+ (cond ((phpinspect-word-p token)
+ (insert (concat " " (cadr token))))
+ ((phpinspect-terminator-p token)
+ (insert (cadr token)))
+ ((and (listp token) (keywordp (car token)))
+ (phpinspect-codify-token child))
+ (t (error "Unable to determine code format of object %s"
token)))))
+
+ (when delimiters
+ (insert (cdr delimiters)))))
+
+(defun phpinspect-codify-token-to-string (token-meta)
+ (with-temp-buffer
+ (phpinspect-codify-token token-meta)
+ (buffer-string)))
(defun phpinspect-remove-unneeded-use-statements (types buffer imports
parent-token)
(dolist (import imports)
@@ -214,9 +262,10 @@ group."
(goto-char start)
(delete-region start end)
(dolist (statement statements)
- (insert (format "use %s;%c" (car statement) ?\n)))
+ (phpinspect-codify-token (cdr statement))
+ (insert-char ?\n))
- (if (and (looking-at "[[:blank:]\n]+"))
+ (if (looking-at "[[:blank:]\n]+")
;; Delete excess trailing whitespace (there's more than 2
between the
;; last use statement and the next token)
(when (< 1 (- (match-end 0) (match-beginning 0)))
@@ -224,7 +273,7 @@ group."
(insert-char ?\n))
;; Insert an extra newline (there's only one between the last use
;; statement and the next token)
- (insert-char ?\n)))))))
+ (insert-char ?\n)))))))
(defun phpinspect-fix-imports ()
"Find types that are used in the current buffer and make sure
@@ -298,7 +347,7 @@ that there are import (\"use\") statements for them."
used-types buffer (append imports namespace-imports) project
token-meta)
(phpinspect-remove-unneeded-use-statements
- used-types buffer namespace-imports token-meta)
+ used-types buffer (append imports namespace-imports) token-meta)
(let ((parent (phpinspect-meta-find-parent-matching-token
token-meta #'phpinspect-namespace-or-root-p)))
diff --git a/phpinspect-type.el b/phpinspect-type.el
index 0f55db1762..02d6f4d1cf 100644
--- a/phpinspect-type.el
+++ b/phpinspect-type.el
@@ -46,6 +46,7 @@
:documentation
"When the type is a collection, this attribute is set to the type
that the collection is expected to contain")
+ (-bare-name-sym-slot nil)
(fully-qualified nil
:type bool
:documentation
@@ -132,6 +133,13 @@ See https://wiki.php.net/rfc/static_return_type ."
"Return just the name, without namespace part, of TYPE."
(phpinspect--get-bare-class-name-from-fqn (phpinspect--type-name type)))
+(define-inline phpinspect--type-bare-name-sym (type)
+ "Return a `phpinspect-name' of the name, without namespace part, of TYPE."
+ (inline-letevals (type)
+ (inline-quote
+ (with-memoization (phpinspect--type--bare-name-sym-slot ,type)
+ (phpinspect-intern-name (phpinspect--type-bare-name ,type))))))
+
(cl-defmethod phpinspect--type= ((type1 phpinspect--type) (type2
phpinspect--type))
(eq (phpinspect--type-name-symbol type1) (phpinspect--type-name-symbol
type2)))
diff --git a/test/test-imports.el b/test/test-imports.el
index e6d4809652..91439c9bdf 100644
--- a/test/test-imports.el
+++ b/test/test-imports.el
@@ -172,3 +172,42 @@ class Baz {
private Foo $foo;
}"
(buffer-string)))))))
+
+(ert-deftest phpinspect-fix-imports-aliased ()
+ (let ((project (phpinspect--make-dummy-composer-project)))
+ (with-temp-buffer
+ (let* ((buffer (phpinspect-make-buffer :buffer (current-buffer)
+ :-project project)))
+
+ (insert "<?php
+
+namespace Not\\App;
+
+use App\\Foo as FooBar;
+
+function bar(): Bar {}
+
+class Baz {
+ private FooBar $foo;
+ private Foo $notAliased;
+}")
+ ;; Ensure buffer is made aware of changes
+ (setq phpinspect-current-buffer buffer)
+ (add-hook 'after-change-functions #'phpinspect-after-change-function)
+
+ (phpinspect-fix-imports)
+ (should (string= "<?php
+
+namespace Not\\App;
+
+use App\\Bar;
+use App\\Foo as FooBar;
+use App\\Foo;
+
+function bar(): Bar {}
+
+class Baz {
+ private FooBar $foo;
+ private Foo $notAliased;
+}"
+ (buffer-string)))))))