@wordpress/docgen 2.45.0 → 2.45.1-next.v.202605131006.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -15,6 +15,15 @@ const hasClassWithName = ( node, name ) =>
15
15
  const hasFunctionWithName = ( node, name ) =>
16
16
  node.type === 'FunctionDeclaration' && node.id.name === name;
17
17
 
18
+ const hasTSDeclareFunction = ( node, name ) =>
19
+ node.type === 'TSDeclareFunction' && node.id?.name === name;
20
+
21
+ const hasImplementationWithName = ( node, name ) =>
22
+ hasFunctionWithName( node, name ) ||
23
+ ( node.type === 'ExportNamedDeclaration' &&
24
+ node.declaration &&
25
+ hasFunctionWithName( node.declaration, name ) );
26
+
18
27
  const hasVariableWithName = ( node, name ) =>
19
28
  node.type === 'VariableDeclaration' &&
20
29
  node.declarations.some( ( declaration ) => {
@@ -32,6 +41,8 @@ const hasNamedExportWithName = ( node, name ) =>
32
41
  node.type === 'ExportNamedDeclaration' &&
33
42
  ( ( node.declaration && hasClassWithName( node.declaration, name ) ) ||
34
43
  ( node.declaration && hasFunctionWithName( node.declaration, name ) ) ||
44
+ ( node.declaration &&
45
+ hasTSDeclareFunction( node.declaration, name ) ) ||
35
46
  ( node.declaration && hasVariableWithName( node.declaration, name ) ) );
36
47
 
37
48
  const hasImportWithName = ( node, name ) =>
@@ -91,19 +102,27 @@ const getJSDoc = ( token, entry, ast, parseDependency ) => {
91
102
  return (
92
103
  hasClassWithName( node, entry.localName ) ||
93
104
  hasFunctionWithName( node, entry.localName ) ||
105
+ hasTSDeclareFunction( node, entry.localName ) ||
94
106
  hasVariableWithName( node, entry.localName ) ||
95
107
  hasNamedExportWithName( node, entry.localName ) ||
96
108
  hasImportWithName( node, entry.localName )
97
109
  );
98
110
  } );
99
- if ( candidates.length !== 1 ) {
111
+ if ( candidates.length === 0 ) {
100
112
  return doc;
101
113
  }
102
- const node = candidates[ 0 ];
103
- if ( isImportDeclaration( node ) ) {
104
- doc = getJSDocFromDependency( node, entry, parseDependency );
105
- } else {
106
- doc = getJSDocFromToken( node );
114
+ const implementationNode = candidates.find( ( node ) =>
115
+ hasImplementationWithName( node, entry.localName )
116
+ );
117
+ for ( const node of candidates ) {
118
+ if ( isImportDeclaration( node ) ) {
119
+ doc = getJSDocFromDependency( node, entry, parseDependency );
120
+ } else {
121
+ doc = getJSDocFromToken( node, implementationNode ?? node );
122
+ }
123
+ if ( doc !== undefined ) {
124
+ return doc;
125
+ }
107
126
  }
108
127
  return doc;
109
128
  }
@@ -14,10 +14,14 @@ const getTypeAnnotation = require( './get-type-annotation' );
14
14
  * a object representing the leading JSDoc comment of the token,
15
15
  * if any.
16
16
  *
17
- * @param {Object} token Espree token.
17
+ * @param {Object} token Espree token to extract the leading JSDoc comment from.
18
+ * @param {Object} [typeAnnotationToken] Espree token to use for type inference. Defaults to `token`.
19
+ * Pass the function implementation node when the JSDoc lives on
20
+ * an overload signature so that inferred types reflect the
21
+ * implementation's parameter/return types.
18
22
  * @return {Object} Object representing the JSDoc comment.
19
23
  */
20
- module.exports = ( token ) => {
24
+ module.exports = ( token, typeAnnotationToken = token ) => {
21
25
  let jsdoc;
22
26
  const comments = getLeadingComments( token );
23
27
  if ( comments && /^\*\r?\n/.test( comments ) ) {
@@ -44,7 +48,7 @@ module.exports = ( token ) => {
44
48
 
45
49
  return {
46
50
  ...tag,
47
- type: getTypeAnnotation( tag, token, index ),
51
+ type: getTypeAnnotation( tag, typeAnnotationToken, index ),
48
52
  description:
49
53
  tag.description === '\n'
50
54
  ? tag.description.trim()
@@ -55,7 +59,7 @@ module.exports = ( token ) => {
55
59
  if ( jsdoc.tags.length === 0 ) {
56
60
  const potentialTypeAnnotation = getTypeAnnotation(
57
61
  { tag: 'type' },
58
- token,
62
+ typeAnnotationToken,
59
63
  0
60
64
  );
61
65
  if ( potentialTypeAnnotation ) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@wordpress/docgen",
3
- "version": "2.45.0",
3
+ "version": "2.45.1-next.v.202605131006.0+2a3d07cab",
4
4
  "description": "Autogenerate public API documentation from exports and JSDoc comments.",
5
5
  "author": "The WordPress Contributors",
6
6
  "license": "GPL-2.0-or-later",
@@ -48,5 +48,5 @@
48
48
  "publishConfig": {
49
49
  "access": "public"
50
50
  },
51
- "gitHead": "8c229eaed0e88c9827e2da3d73a78f9ddd77714b"
51
+ "gitHead": "8804fa29bc78a1d98e5a4d40c3e180ddd907016c"
52
52
  }