Repository navigation
Expand file tree
/
Copy pathwp-hookdoc+0.2.0.patch
More file actions
74 lines (72 loc) · 3 KB
/
Copy pathwp-hookdoc+0.2.0.patch
File metadata and controls
74 lines (72 loc) · 3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
diff --git a/node_modules/wp-hookdoc/plugin/index.js b/node_modules/wp-hookdoc/plugin/index.js
index 1b3457d..f92b48c 100644
--- a/node_modules/wp-hookdoc/plugin/index.js
+++ b/node_modules/wp-hookdoc/plugin/index.js
@@ -1,9 +1,60 @@
/**
* Only use comment blocks which has a @hook tag and simulate as @class.
+ *
+ * Patched (cloudinary_wordpress): accept standard PHPDoc tag syntax inside
+ * @hook blocks and normalize it to the JSDoc syntax that JSDoc's parser
+ * expects, so source files can keep valid PHPDoc (PHPStan-clean):
+ * @param Type $name Desc -> @param {Type} $name Desc (standard PHPDoc)
+ * @param $name {Type} Desc -> @param {Type} $name Desc (legacy hookdoc)
+ * @return Type Desc -> @return {Type} Desc
+ * Tags already using the {Type} form are left untouched.
*/
'use strict';
+// Matches a PHPDoc type expression (unions, generics, arrays, FQCN, etc.).
+var TYPE = '[\\w\\\\|\\[\\]<>,\\s\\.\\$\']+';
+
+// Convert a PHPDoc type to one JSDoc's type-expression parser accepts:
+// drop the leading namespace backslash and turn separators into dots, so
+// PHPDoc FQCNs like \WP_Post or \Cloudinary\Sync become WP_Post / Cloudinary.Sync.
+function jsdocType(type) {
+ return type
+ .trim()
+ .replace(/\\+/g, '\\') // collapse accidental doubles
+ .split('|')
+ .map(function (part) {
+ return part.trim().replace(/^\\/, '').replace(/\\/g, '.');
+ })
+ .join('|');
+}
+
+function normalizeHookDocblock(comment) {
+ return comment
+ // @param Type $name desc (standard PHPDoc: type first, not yet braced)
+ .replace(
+ new RegExp('(@param\\s+)(?!\\{)(' + TYPE + '?)\\s+(\\$\\w+)', 'g'),
+ function (m, tag, type, name) {
+ return tag + '{' + jsdocType(type) + '} ' + name;
+ }
+ )
+ // @param $name {Type} desc (legacy hookdoc: name first, braced type after)
+ .replace(
+ /(@param\s+)(\$\w+)\s+\{([^}]+)\}/g,
+ function (m, tag, name, type) {
+ return tag + '{' + jsdocType(type) + '} ' + name;
+ }
+ )
+ // @return/@returns Type desc (standard PHPDoc: not yet braced)
+ .replace(
+ new RegExp('(@returns?\\s+)(?!\\{)(' + TYPE + '?)(\\s|\\*/|$)', 'g'),
+ function (m, tag, type, tail) {
+ if (!type.trim()) { return m; }
+ return tag + '{' + jsdocType(type) + '}' + tail;
+ }
+ );
+}
+
exports.handlers = {
beforeParse: function(e) {
// a JSDoc comment looks like: /**[one or more chars]*/
@@ -19,7 +70,7 @@ exports.handlers = {
// Generate parsed
comments.forEach(function(comment) {
if (/\*[ \t]*@hook[ ]/g.test(comment)) {
- parsed.push(comment.replace(/\*[ \t]*@hook[ ]/g, '* @class '));
+ parsed.push(normalizeHookDocblock(comment).replace(/\*[ \t]*@hook[ ]/g, '* @class '));
}
});
}