@wordpress/docgen 1.18.2-next.33ec3857e2.0 → 1.19.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.
package/LICENSE.md CHANGED
@@ -1,6 +1,6 @@
1
1
  ## Gutenberg
2
2
 
3
- Copyright 2016-2021 by the contributors
3
+ Copyright 2016-2022 by the contributors
4
4
 
5
5
  **License for Contributions (on and after April 15, 2021)**
6
6
 
@@ -48,7 +48,7 @@ module.exports = ( token ) => {
48
48
  token,
49
49
  0
50
50
  );
51
- if ( potentialTypeAnnotation !== '' ) {
51
+ if ( potentialTypeAnnotation ) {
52
52
  jsdoc.tags.push( {
53
53
  tag: 'type',
54
54
  type: potentialTypeAnnotation,
@@ -397,7 +397,10 @@ function getFunctionToken( token ) {
397
397
 
398
398
  function getFunctionNameForError( declarationToken ) {
399
399
  let namedFunctionToken = declarationToken;
400
- if ( babelTypes.isExportNamedDeclaration( declarationToken ) ) {
400
+ if (
401
+ babelTypes.isExportNamedDeclaration( declarationToken ) ||
402
+ babelTypes.isExportDefaultDeclaration( declarationToken )
403
+ ) {
401
404
  namedFunctionToken = declarationToken.declaration;
402
405
  }
403
406
 
@@ -451,7 +454,7 @@ function getQualifiedObjectPatternTypeAnnotation( tag, paramType ) {
451
454
  * @param {CommentTag} tag The documented parameter.
452
455
  * @param {ASTNode} declarationToken The function the parameter is documented on.
453
456
  * @param {number} paramIndex The parameter index.
454
- * @return {null | string} The parameter's type annotation.
457
+ * @return {string?} The parameter's type annotation.
455
458
  */
456
459
  function getParamTypeAnnotation( tag, declarationToken, paramIndex ) {
457
460
  const functionToken = getFunctionToken( declarationToken );
@@ -469,58 +472,51 @@ function getParamTypeAnnotation( tag, declarationToken, paramIndex ) {
469
472
  );
470
473
  }
471
474
 
472
- const isQualifiedName = tag.name.includes( '.' );
475
+ if ( babelTypes.isAssignmentPattern( paramToken ) ) {
476
+ paramToken = paramToken.left;
477
+ }
473
478
 
474
- try {
475
- if ( babelTypes.isAssignmentPattern( paramToken ) ) {
476
- paramToken = paramToken.left;
477
- }
479
+ if (
480
+ ! paramToken.typeAnnotation ||
481
+ ! paramToken.typeAnnotation.typeAnnotation
482
+ ) {
483
+ return;
484
+ }
478
485
 
479
- const paramType = paramToken.typeAnnotation.typeAnnotation;
486
+ const paramType = paramToken.typeAnnotation.typeAnnotation;
487
+ const isQualifiedName = tag.name.includes( '.' );
480
488
 
481
- if (
482
- babelTypes.isIdentifier( paramToken ) ||
483
- babelTypes.isRestElement( paramToken ) ||
484
- ( ( babelTypes.isArrayPattern( paramToken ) ||
485
- babelTypes.isObjectPattern( paramToken ) ) &&
486
- ! isQualifiedName )
487
- ) {
488
- return getTypeAnnotation( paramType );
489
- } else if ( babelTypes.isArrayPattern( paramToken ) ) {
490
- return getQualifiedArrayPatternTypeAnnotation( tag, paramType );
491
- } else if ( babelTypes.isObjectPattern( paramToken ) ) {
492
- return getQualifiedObjectPatternTypeAnnotation( tag, paramType );
493
- }
494
- } catch ( e ) {
495
- throw new Error(
496
- `Could not find type for parameter '${
497
- tag.name
498
- }' in function '${ getFunctionNameForError( declarationToken ) }'.`
499
- );
489
+ if (
490
+ babelTypes.isIdentifier( paramToken ) ||
491
+ babelTypes.isRestElement( paramToken ) ||
492
+ ( ( babelTypes.isArrayPattern( paramToken ) ||
493
+ babelTypes.isObjectPattern( paramToken ) ) &&
494
+ ! isQualifiedName )
495
+ ) {
496
+ return getTypeAnnotation( paramType );
497
+ } else if ( babelTypes.isArrayPattern( paramToken ) ) {
498
+ return getQualifiedArrayPatternTypeAnnotation( tag, paramType );
499
+ } else if ( babelTypes.isObjectPattern( paramToken ) ) {
500
+ return getQualifiedObjectPatternTypeAnnotation( tag, paramType );
500
501
  }
501
502
  }
502
503
 
503
504
  /**
504
505
  * @param {ASTNode} declarationToken A function token.
505
- * @return {null | string} The function's return type annoation.
506
+ * @return {string?} The function's return type annotation.
506
507
  */
507
508
  function getReturnTypeAnnotation( declarationToken ) {
508
509
  const functionToken = getFunctionToken( declarationToken );
509
-
510
- try {
511
- return getTypeAnnotation( functionToken.returnType.typeAnnotation );
512
- } catch ( e ) {
513
- throw new Error(
514
- `Could not find return type for function '${ getFunctionNameForError(
515
- declarationToken
516
- ) }'.`
517
- );
510
+ if ( ! functionToken.returnType ) {
511
+ return;
518
512
  }
513
+
514
+ return getTypeAnnotation( functionToken.returnType.typeAnnotation );
519
515
  }
520
516
 
521
517
  /**
522
518
  * @param {ASTNode} declarationToken
523
- * @return {string} The type annotation for the variable.
519
+ * @return {string?} The type annotation for the variable.
524
520
  */
525
521
  function getVariableTypeAnnotation( declarationToken ) {
526
522
  let resolvedToken = declarationToken;
@@ -541,7 +537,6 @@ function getVariableTypeAnnotation( declarationToken ) {
541
537
  return getTypeAnnotation( resolvedToken.typeAnnotation.typeAnnotation );
542
538
  } catch ( e ) {
543
539
  // assume it's a fully undocumented variable, there's nothing we can do about that but fail silently.
544
- return '';
545
540
  }
546
541
  }
547
542
 
@@ -550,7 +545,7 @@ module.exports =
550
545
  * @param {CommentTag} tag A comment tag.
551
546
  * @param {ASTNode} token A function token.
552
547
  * @param {number | null} index The index of the parameter or `null` if not a param tag.
553
- * @return {null | string} The type annotation for the given tag or null if the tag has no type annotation.
548
+ * @return {[string]} The type annotation for the given tag or null if the tag has no type annotation.
554
549
  */
555
550
  function ( tag, token, index ) {
556
551
  // If the file is using JSDoc type annotations, use the JSDoc.
@@ -568,8 +563,5 @@ module.exports =
568
563
  case 'type': {
569
564
  return getVariableTypeAnnotation( token );
570
565
  }
571
- default: {
572
- return '';
573
- }
574
566
  }
575
567
  };
@@ -18,7 +18,7 @@ const formatTag = ( title, tags, formatter, docs ) => {
18
18
  docs.push( '\n' );
19
19
  docs.push( `*${ title }*` );
20
20
  docs.push( '\n' );
21
- docs.push( ...tags.map( formatter ) );
21
+ docs.push( ...tags.map( ( tag ) => `\n${ formatter( tag ) }` ) );
22
22
  }
23
23
  };
24
24
 
@@ -103,54 +103,88 @@ module.exports = (
103
103
  formatTag(
104
104
  'Related',
105
105
  getSymbolTagsByName( symbol, 'see', 'link' ),
106
- ( tag ) =>
107
- `\n- ${ tag.name.trim() }${
108
- tag.description ? ' ' : ''
109
- }${ tag.description.trim() }`,
106
+ ( tag ) => {
107
+ const name = tag.name.trim();
108
+ const desc = tag.description.trim();
109
+
110
+ // prettier-ignore
111
+ return desc
112
+ ? `- ${ name } ${ desc }`
113
+ : `- ${ name }`;
114
+ },
110
115
  docs
111
116
  );
112
117
  formatExamples( getSymbolTagsByName( symbol, 'example' ), docs );
113
118
  formatTag(
114
119
  'Type',
115
120
  getSymbolTagsByName( symbol, 'type' ),
116
- ( tag ) =>
117
- `\n- ${ getTypeOutput( tag ) }${ cleanSpaces(
118
- ` ${ tag.name } ${ tag.description }`
119
- ) }`,
121
+ ( tag ) => {
122
+ const type = tag.type && getTypeOutput( tag );
123
+ const desc = cleanSpaces(
124
+ `${ tag.name } ${ tag.description }`
125
+ );
126
+
127
+ // prettier-ignore
128
+ return type
129
+ ? `- ${ type }${ desc }`
130
+ : `- ${ desc }`;
131
+ },
120
132
  docs
121
133
  );
122
134
  formatTag(
123
135
  'Parameters',
124
136
  getSymbolTagsByName( symbol, 'param' ),
125
- ( tag ) =>
126
- `\n- *${ tag.name }* ${ getTypeOutput(
127
- tag
128
- ) }: ${ cleanSpaces( tag.description ) }`,
137
+ ( tag ) => {
138
+ const name = tag.name;
139
+ const type = tag.type && getTypeOutput( tag );
140
+ const desc = cleanSpaces( tag.description );
141
+
142
+ return type
143
+ ? `- *${ name }* ${ type }: ${ desc }`
144
+ : `- *${ name }* ${ desc }`;
145
+ },
129
146
  docs
130
147
  );
131
148
  formatTag(
132
149
  'Returns',
133
150
  getSymbolTagsByName( symbol, 'return' ),
134
151
  ( tag ) => {
135
- return `\n- ${ getTypeOutput( tag ) }: ${ cleanSpaces(
152
+ const type = tag.type && getTypeOutput( tag );
153
+ const desc = cleanSpaces(
136
154
  `${ tag.name } ${ tag.description }`
137
- ) }`;
155
+ );
156
+
157
+ // prettier-ignore
158
+ return type
159
+ ? `- ${ type }: ${ desc }`
160
+ : `- ${ desc }`;
138
161
  },
139
162
  docs
140
163
  );
141
164
  formatTag(
142
165
  'Type Definition',
143
166
  getSymbolTagsByName( symbol, 'typedef' ),
144
- ( tag ) => `\n- *${ tag.name }* ${ getTypeOutput( tag ) }`,
167
+ ( tag ) => {
168
+ const name = tag.name;
169
+ const type = getTypeOutput( tag );
170
+
171
+ return `- *${ name }* ${ type }`;
172
+ },
145
173
  docs
146
174
  );
147
175
  formatTag(
148
176
  'Properties',
149
177
  getSymbolTagsByName( symbol, 'property' ),
150
- ( tag ) =>
151
- `\n- *${ tag.name }* ${ getTypeOutput(
152
- tag
153
- ) }: ${ cleanSpaces( tag.description ) }`,
178
+ ( tag ) => {
179
+ const name = tag.name;
180
+ const type = tag.type && getTypeOutput( tag );
181
+ const desc = cleanSpaces( tag.description );
182
+
183
+ // prettier-ignore
184
+ return type
185
+ ? `- *${ name }* ${ type }: ${ desc }`
186
+ : `- *${ name }* ${ desc }`
187
+ },
154
188
  docs
155
189
  );
156
190
  docs.push( '\n' );
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@wordpress/docgen",
3
- "version": "1.18.2-next.33ec3857e2.0",
3
+ "version": "1.19.0",
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",
@@ -39,5 +39,5 @@
39
39
  "publishConfig": {
40
40
  "access": "public"
41
41
  },
42
- "gitHead": "51c7917ea7fac72953702f24d6daac87d99e7617"
42
+ "gitHead": "d95ccb9366e249133cdb1d7b25c382446b9ee502"
43
43
  }