@wordpress/docgen 1.20.2 → 1.23.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/lib/get-type-annotation.js +55 -7
- package/lib/markdown/embed.js +3 -9
- package/package.json +2 -2
|
@@ -110,12 +110,10 @@ function getTypeLiteralPropertyTypeAnnotations( typeAnnotation ) {
|
|
|
110
110
|
* @param {babelTypes.TSTypeLiteral} typeAnnotation
|
|
111
111
|
*/
|
|
112
112
|
function getTypeLiteralTypeAnnotation( typeAnnotation ) {
|
|
113
|
-
const callProperties =
|
|
114
|
-
typeAnnotation
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
typeAnnotation
|
|
118
|
-
);
|
|
113
|
+
const callProperties =
|
|
114
|
+
getTypeLiteralCallSignatureDeclarationTypeAnnotations( typeAnnotation );
|
|
115
|
+
const indexers =
|
|
116
|
+
getTypeLiteralIndexSignatureTypeAnnotations( typeAnnotation );
|
|
119
117
|
const properties = getTypeLiteralPropertyTypeAnnotations( typeAnnotation );
|
|
120
118
|
|
|
121
119
|
return `{ ${ callProperties }${ properties }${ indexers }}`;
|
|
@@ -373,6 +371,56 @@ function getTypeAnnotation( typeAnnotation ) {
|
|
|
373
371
|
}
|
|
374
372
|
}
|
|
375
373
|
|
|
374
|
+
/**
|
|
375
|
+
* Extract wrapped selector functions to reach inside for parameter types.
|
|
376
|
+
*
|
|
377
|
+
* This function wasn't necessary until we started introducing more TypeScript code into
|
|
378
|
+
* the project. With parameter types fully in the JSDoc comments we always have a direct
|
|
379
|
+
* match between parameter name and the type. However, when working in TypeScript where
|
|
380
|
+
* we rely on the type annotations for the types we introduce a mismatch when wrapping
|
|
381
|
+
* functions.
|
|
382
|
+
*
|
|
383
|
+
* Example:
|
|
384
|
+
* export const getThings = createSelector( ( state ) => state.things, ( state ) => state.version );
|
|
385
|
+
*
|
|
386
|
+
* In this example we would document `state` but its type is buried inside of `createSelector`.
|
|
387
|
+
* Because this kind of scenario is tricky to properly parse without asking TypeScript directly
|
|
388
|
+
* to give us the actual type of `getThings` we're going to special-case the known instances
|
|
389
|
+
* of selector-wrapping to extract the inner function and re-connect the parameter types
|
|
390
|
+
* with their descriptions in the JSDoc comments.
|
|
391
|
+
*
|
|
392
|
+
* If we find more wrapper functions on selectors we should add them below following the
|
|
393
|
+
* example of `createSelector` and `createRegsitrySelector`.
|
|
394
|
+
*
|
|
395
|
+
* @param {ASTNode} token Contains either a function or a call to a function-wrapper.
|
|
396
|
+
*
|
|
397
|
+
* TODO: Remove the special-casing here once we're able to infer the types from TypeScript itself.
|
|
398
|
+
*/
|
|
399
|
+
function unwrapWrappedSelectors( token ) {
|
|
400
|
+
if ( babelTypes.isFunctionDeclaration( token ) ) {
|
|
401
|
+
return token;
|
|
402
|
+
}
|
|
403
|
+
|
|
404
|
+
if ( babelTypes.isArrowFunctionExpression( token ) ) {
|
|
405
|
+
return token;
|
|
406
|
+
}
|
|
407
|
+
|
|
408
|
+
if ( babelTypes.isCallExpression( token ) ) {
|
|
409
|
+
// createSelector( ( state, queryId ) => state.queries[ queryId ] );
|
|
410
|
+
// \--------------------------------------------/ CallExpression.arguments[0]
|
|
411
|
+
if ( token.callee.name === 'createSelector' ) {
|
|
412
|
+
return token.arguments[ 0 ];
|
|
413
|
+
}
|
|
414
|
+
|
|
415
|
+
// createRegistrySelector( ( selector ) => ( state, queryId ) => select( 'core/queries' ).get( queryId ) );
|
|
416
|
+
// \-----------------------------------------------------------/ CallExpression.arguments[0].body
|
|
417
|
+
// \---------------------------------------------------------------------------/ CallExpression.arguments[0]
|
|
418
|
+
if ( token.callee.name === 'createRegistrySelector' ) {
|
|
419
|
+
return token.arguments[ 0 ].body;
|
|
420
|
+
}
|
|
421
|
+
}
|
|
422
|
+
}
|
|
423
|
+
|
|
376
424
|
/**
|
|
377
425
|
* @param {ASTNode} token
|
|
378
426
|
* @return {babelTypes.ArrowFunctionExpression | babelTypes.FunctionDeclaration} The function token.
|
|
@@ -392,7 +440,7 @@ function getFunctionToken( token ) {
|
|
|
392
440
|
resolvedToken = resolvedToken.declarations[ 0 ].init;
|
|
393
441
|
}
|
|
394
442
|
|
|
395
|
-
return resolvedToken;
|
|
443
|
+
return unwrapWrappedSelectors( resolvedToken );
|
|
396
444
|
}
|
|
397
445
|
|
|
398
446
|
function getFunctionNameForError( declarationToken ) {
|
package/lib/markdown/embed.js
CHANGED
|
@@ -1,14 +1,8 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* External dependencies
|
|
3
|
-
*/
|
|
4
|
-
const { findLast } = require( 'lodash' );
|
|
5
|
-
|
|
6
1
|
const getHeadingIndex = ( ast, index ) => {
|
|
7
2
|
const astBeforeIndex = ast.children.slice( 0, index );
|
|
8
|
-
const lastHeading =
|
|
9
|
-
|
|
10
|
-
( node ) => node.type === 'heading'
|
|
11
|
-
);
|
|
3
|
+
const lastHeading = astBeforeIndex
|
|
4
|
+
.reverse()
|
|
5
|
+
.find( ( node ) => node.type === 'heading' );
|
|
12
6
|
return lastHeading ? lastHeading.depth : 1;
|
|
13
7
|
};
|
|
14
8
|
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@wordpress/docgen",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.23.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": "
|
|
42
|
+
"gitHead": "a80eeb62ec7cb1418b9915c277e084a29d6665e3"
|
|
43
43
|
}
|