[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[elpa] externals/phpinspect 33922a6ff2 2/2: Implement parsing and indexa
From: |
ELPA Syncer |
Subject: |
[elpa] externals/phpinspect 33922a6ff2 2/2: Implement parsing and indexation of @throws annotations |
Date: |
Mon, 19 Aug 2024 03:58:51 -0400 (EDT) |
branch: externals/phpinspect
commit 33922a6ff285829c0ca7a4ca99db421cdfa3f1ea
Author: Hugo Thunnissen <devel@hugot.nl>
Commit: Hugo Thunnissen <devel@hugot.nl>
Implement parsing and indexation of @throws annotations
---
phpinspect-index.el | 24 ++++++++++++++++++------
phpinspect-parser.el | 3 +++
phpinspect-token-predicates.el | 3 +++
phpinspect-type.el | 4 ++++
test/test-index.el | 7 +++++--
5 files changed, 33 insertions(+), 8 deletions(-)
diff --git a/phpinspect-index.el b/phpinspect-index.el
index 4464fb9547..20a27ab08a 100644
--- a/phpinspect-index.el
+++ b/phpinspect-index.el
@@ -138,7 +138,7 @@ function (think \"new\" statements, return types etc.)."
(phpinspect--log "Indexing function")
(let* ((php-func (cadr scope))
(declaration (cadr php-func))
- name type arguments)
+ name type arguments throws used-types)
(pcase-setq `(,name ,arguments ,type)
(phpinspect--index-function-declaration
@@ -159,17 +159,26 @@ function (think \"new\" statements, return types etc.)."
(phpinspect--apply-annotation-type return-annotation-type type
type-resolver)
(setq type (funcall type-resolver (phpinspect--make-type :name
return-annotation-type)))))
+ (when-let ((throw-annotations (seq-filter #'phpinspect-throws-annotation-p
comment-before)))
+ (dolist (tr throw-annotations)
+ (when-let ((type (phpinspect-var-annotation-type tr)))
+ (push (funcall type-resolver (phpinspect--make-type :name type))
throws)
+ (push type used-types))))
+
+
(when add-used-types
- (let ((used-types (phpinspect--find-used-types-in-tokens
- `(,(seq-find #'phpinspect-block-p php-func)))))
- (when type (push (phpinspect--type-bare-name type) used-types))
- (funcall add-used-types used-types)))
+ (setq used-types (nconc used-types
+ (phpinspect--find-used-types-in-tokens
+ `(,(seq-find #'phpinspect-block-p php-func)))))
+ (when type (push (phpinspect--type-bare-name type) used-types))
+ (funcall add-used-types used-types))
(phpinspect--log "Creating function object")
(phpinspect--make-function
:scope `(,(car scope))
:token php-func
:name (concat (if namespace (concat namespace "\\") "") name)
+ :throws throws
:return-type (or type phpinspect--null-type)
:arguments arguments)))
@@ -563,6 +572,7 @@ NAMESPACE will be assumed the root namespace if not
provided"
Covers usage of types:
- with the \"new\" keyword
- as function argument/return types
+- Types used in annotations (@var, @throws etc.)
see `phpinspect--index-class' for indexation of types used in
classes (like property typehints).
@@ -583,7 +593,9 @@ Returns a list of type name strings."
((phpinspect-comment-p token)
(setq used-types-rear
(nconc used-types-rear
(phpinspect--find-used-types-in-tokens (cdr token)))))
- ((and (or (phpinspect-var-annotation-p token)
(phpinspect-param-annotation-p token))
+ ((and (or (phpinspect-var-annotation-p token)
+ (phpinspect-param-annotation-p token)
+ (phpinspect-throws-annotation-p token))
(phpinspect-var-annotation-type token))
(setq used-types-rear
(setcdr used-types-rear (cons
(phpinspect-var-annotation-type token) nil))))
diff --git a/phpinspect-parser.el b/phpinspect-parser.el
index 3b4eb0718b..5164420883 100644
--- a/phpinspect-parser.el
+++ b/phpinspect-parser.el
@@ -499,6 +499,9 @@ nature like argument lists"
((string= annotation-name "method")
(cons :method-annotation
(phpinspect--parse-annotation-parameters 4)))
+ ((string= annotation-name "throws")
+ (cons :throws-annotation
+ (phpinspect--parse-annotation-parameters 1)))
(t
(list :annotation annotation-name))))
(list :annotation nil)))
diff --git a/phpinspect-token-predicates.el b/phpinspect-token-predicates.el
index a17d19e36d..7c68103838 100644
--- a/phpinspect-token-predicates.el
+++ b/phpinspect-token-predicates.el
@@ -151,6 +151,9 @@ Type can be any of the token types returned by
(defun phpinspect-param-annotation-p (token)
(phpinspect-token-type-p token :param-annotation))
+(defun phpinspect-throws-annotation-p (token)
+ (phpinspect-token-type-p token :throws-annotation))
+
(defun phpinspect-return-annotation-p (token)
(phpinspect-token-type-p token :return-annotation))
diff --git a/phpinspect-type.el b/phpinspect-type.el
index 012749177b..0f55db1762 100644
--- a/phpinspect-type.el
+++ b/phpinspect-type.el
@@ -233,6 +233,10 @@ NAMESPACE may be nil, or a string with a namespace FQN."
:type symbol
:documentation
"A symbol associated with the name of the function")
+ (throws nil
+ :type list
+ :documentation "List of exception types that function throws
(according to doc
+block).")
(token nil
:type phpinspect-function-p
:documentation
diff --git a/test/test-index.el b/test/test-index.el
index 2469245f36..0f9e7c2fc2 100644
--- a/test/test-index.el
+++ b/test/test-index.el
@@ -94,7 +94,9 @@ use UsedTrait;
private PropertyType $property;
-/** @param ParamAnnotation $par */
+/** @param ParamAnnotation $par
+@throws ThrowAnnotationException */
+*/
public function makeThing($par): Thing
{
if ((new Monkey())->tree() === true) {
@@ -116,7 +118,8 @@ if ($param instanceof InstanceOffed) {
'("Cheese" "Bacon" "Ham" "Bagel" "Monkey"
"ExtendedThing"
"StaticThing" "Thing" "ThingFactory" "Potato"
"OtherThing"
"InnerFunctionParam" "PropertyType" "InstanceOffed"
- "NestedArray" "UsedTrait" "VarAnnotation"
"ParamAnnotation"))
+ "NestedArray" "UsedTrait" "VarAnnotation"
"ParamAnnotation"
+ "ThrowAnnotationException"))
#'string<))
(sort used-types (lambda (s1 s2) (string< (phpinspect-name-string
s1) (phpinspect-name-string s2))))))))