react-docgen
Advanced tools
Comparing version 6.0.0-rc.8 to 6.0.0-rc.9
import { loadPartialConfig, parseSync } from '@babel/core'; | ||
import { extname } from 'path'; | ||
const TYPESCRIPT_EXTS = { | ||
'.cts': true, | ||
'.mts': true, | ||
'.ts': true, | ||
'.tsx': true, | ||
}; | ||
const TYPESCRIPT_EXTS = new Set(['.cts', '.mts', '.ts', '.tsx']); | ||
function getDefaultPlugins(options) { | ||
return [ | ||
'jsx', | ||
TYPESCRIPT_EXTS[extname(options.filename || '')] ? 'typescript' : 'flow', | ||
options.filename && TYPESCRIPT_EXTS.has(extname(options.filename)) | ||
? 'typescript' | ||
: 'flow', | ||
'asyncDoExpressions', | ||
@@ -14,0 +11,0 @@ 'decimal', |
@@ -12,2 +12,3 @@ export interface Documentation { | ||
name: string; | ||
description?: string; | ||
optional: boolean; | ||
@@ -17,2 +18,3 @@ type?: TypeDescriptor<FunctionSignatureType> | null; | ||
export interface MethodReturn { | ||
description?: string; | ||
type: TypeDescriptor<FunctionSignatureType> | undefined; | ||
@@ -19,0 +21,0 @@ } |
@@ -59,2 +59,3 @@ var __classPrivateFieldSet = (this && this.__classPrivateFieldSet) || function (receiver, state, value, kind, f) { | ||
for (const [key, value] of __classPrivateFieldGet(this, _DocumentationBuilder_data, "f")) { | ||
// @ts-expect-error custom handlers can add any properties to Documentation | ||
obj[key] = value; | ||
@@ -61,0 +62,0 @@ } |
@@ -7,6 +7,7 @@ import { getDocblock } from '../utils/docblock.js'; | ||
if (path.isClassDeclaration() || path.isClassExpression()) { | ||
const decorators = path.get('decorators'); | ||
// If we have a class declaration or expression, then the comment might be | ||
// attached to the last decorator instead as trailing comment. | ||
if (path.node.decorators && path.node.decorators.length > 0) { | ||
description = getDocblock(path.get('decorators')[path.node.decorators.length - 1], true); | ||
if (decorators && decorators.length > 0) { | ||
description = getDocblock(decorators[decorators.length - 1], true); | ||
} | ||
@@ -13,0 +14,0 @@ } |
import parseJsDoc from '../utils/parseJsDoc.js'; | ||
function removeEmpty(obj) { | ||
return Object.fromEntries(Object.entries(obj).filter(([, v]) => v != null)); | ||
} | ||
// Merges two objects ignoring null/undefined. | ||
@@ -8,9 +11,5 @@ function merge(obj1, obj2) { | ||
const merged = { | ||
...obj1, | ||
...removeEmpty(obj1 ?? {}), | ||
...removeEmpty(obj2 ?? {}), | ||
}; | ||
for (const prop in obj2) { | ||
if (obj2[prop] != null) { | ||
merged[prop] = obj2[prop]; | ||
} | ||
} | ||
return merged; | ||
@@ -17,0 +16,0 @@ } |
@@ -47,6 +47,9 @@ import getPropertyName from '../utils/getPropertyName.js'; | ||
function getStatelessPropsPath(componentDefinition) { | ||
let value = resolveToValue(componentDefinition); | ||
if (isReactForwardRefCall(value)) { | ||
value = resolveToValue(value.get('arguments')[0]); | ||
let value = componentDefinition; | ||
if (isReactForwardRefCall(componentDefinition)) { | ||
value = resolveToValue(componentDefinition.get('arguments')[0]); | ||
} | ||
if (!value.isFunction()) { | ||
return; | ||
} | ||
return value.get('params')[0]; | ||
@@ -108,3 +111,3 @@ } | ||
const defaultPropsHandler = function (documentation, componentDefinition) { | ||
let statelessProps = null; | ||
let statelessProps; | ||
const defaultPropsPath = getDefaultPropsPath(componentDefinition); | ||
@@ -111,0 +114,0 @@ /** |
import type { NodePath } from '@babel/traverse'; | ||
import type { Node } from '@babel/types'; | ||
type Predicate<T extends Node = Node> = (path: NodePath) => path is NodePath<T>; | ||
type Predicate<T extends NodePath> = (path: NodePath) => path is T; | ||
/** | ||
@@ -11,3 +10,3 @@ * This can be used in two ways | ||
*/ | ||
export default function findFunctionReturn<T extends Node = Node>(path: NodePath, predicate: Predicate<T>): NodePath<T> | undefined; | ||
export default function findFunctionReturn<T extends NodePath = NodePath>(path: NodePath, predicate: Predicate<T>): T | undefined; | ||
export {}; |
@@ -21,6 +21,13 @@ /** | ||
export function unwrapUtilityType(path) { | ||
while (isSupportedUtilityType(path)) { | ||
path = path.get('typeParameters').get('params')[0]; | ||
let resultPath = path; | ||
while (isSupportedUtilityType(resultPath)) { | ||
const typeParameters = resultPath.get('typeParameters'); | ||
if (!typeParameters.hasNode()) | ||
break; | ||
const firstParam = typeParameters.get('params')[0]; | ||
if (!firstParam) | ||
break; | ||
resultPath = firstParam; | ||
} | ||
return path; | ||
return resultPath; | ||
} |
@@ -41,3 +41,10 @@ import getPropertyName from './getPropertyName.js'; | ||
function handleKeysHelper(path) { | ||
let value = path.get('typeParameters').get('params')[0]; | ||
const typeParams = path.get('typeParameters'); | ||
if (!typeParams.hasNode()) { | ||
return null; | ||
} | ||
let value = typeParams.get('params')[0]; | ||
if (!value) { | ||
return null; | ||
} | ||
if (value.isTypeofTypeAnnotation()) { | ||
@@ -49,5 +56,5 @@ value = value.get('argument').get('id'); | ||
} | ||
const resolvedPath = resolveToValue(value); | ||
if (resolvedPath && | ||
(resolvedPath.isObjectExpression() || resolvedPath.isObjectTypeAnnotation())) { | ||
const resolvedPath = value.hasNode() ? resolveToValue(value) : value; | ||
if (resolvedPath.isObjectExpression() || | ||
resolvedPath.isObjectTypeAnnotation()) { | ||
const keys = resolveObjectToNameArray(resolvedPath, true); | ||
@@ -254,3 +261,4 @@ if (keys) { | ||
if (isTypeAlias) { | ||
if (visitedTypes[parent.node.id.name] === true) { | ||
const visitedType = visitedTypes[parent.node.id.name]; | ||
if (visitedType === true) { | ||
// if we are currently visiting this node then just return the name | ||
@@ -260,5 +268,5 @@ // as we are starting to endless loop | ||
} | ||
else if (typeof visitedTypes[parent.node.id.name] === 'object') { | ||
else if (typeof visitedType === 'object') { | ||
// if we already resolved the type simple return it | ||
return visitedTypes[parent.node.id.name]; | ||
return visitedType; | ||
} | ||
@@ -265,0 +273,0 @@ // mark the type as visited |
@@ -70,6 +70,9 @@ import { getDocblock } from './docblock.js'; | ||
const returnType = getTypeAnnotation(functionExpression.get('returnType')); | ||
if (returnType && returnType.isFlowType()) { | ||
if (!returnType) { | ||
return null; | ||
} | ||
if (returnType.isFlowType()) { | ||
return { type: getFlowType(returnType, null) }; | ||
} | ||
else if (returnType) { | ||
else if (returnType.isTSType()) { | ||
return { type: getTSType(returnType, null) }; | ||
@@ -76,0 +79,0 @@ } |
@@ -121,3 +121,4 @@ import { getDocblock } from '../utils/docblock.js'; | ||
if (argumentPath.isObjectExpression()) { | ||
let value = {}; | ||
const value = {}; | ||
let rawValue; | ||
argumentPath.get('properties').forEach((propertyPath) => { | ||
@@ -135,3 +136,3 @@ // We only handle ObjectProperty as there is nothing to handle for | ||
if (argument && isCyclicReference(argument, argumentPath)) { | ||
value = printValue(argument); | ||
rawValue = printValue(argument); | ||
return; | ||
@@ -148,3 +149,3 @@ } | ||
}); | ||
type.value = value; | ||
type.value = rawValue ?? value; | ||
} | ||
@@ -151,0 +152,0 @@ return type; |
@@ -35,2 +35,3 @@ import getPropertyName from './getPropertyName.js'; | ||
TSIndexedAccessType: handleTSIndexedAccessType, | ||
TSLiteralType: handleTSLiteralType, | ||
}; | ||
@@ -48,2 +49,9 @@ function handleTSQualifiedName(path) { | ||
} | ||
function handleTSLiteralType(path) { | ||
const literal = path.get('literal'); | ||
return { | ||
name: 'literal', | ||
value: printValue(literal), | ||
}; | ||
} | ||
function handleTSArrayType(path, typeParams) { | ||
@@ -340,9 +348,2 @@ return { | ||
} | ||
else if (path.isTSLiteralType()) { | ||
const literal = path.get('literal'); | ||
type = { | ||
name: 'literal', | ||
value: printValue(literal), | ||
}; | ||
} | ||
else if (node.type in namedTypes) { | ||
@@ -349,0 +350,0 @@ type = namedTypes[node.type](path, typeParams); |
import type { NodePath } from '@babel/traverse'; | ||
import type { FlowType } from '@babel/types'; | ||
import type { FlowType, Node, TSType } from '@babel/types'; | ||
/** | ||
@@ -7,2 +7,2 @@ * Gets the most inner valuable TypeAnnotation from path. If no TypeAnnotation | ||
*/ | ||
export default function getTypeAnnotation<T = FlowType>(path: NodePath): NodePath<T> | null; | ||
export default function getTypeAnnotation<T extends Node = FlowType | TSType>(path: NodePath<Node | null | undefined>): NodePath<T> | null; |
@@ -14,4 +14,3 @@ /** | ||
!resultPath.isTSType()); | ||
// @ts-ignore | ||
return resultPath; | ||
} |
@@ -7,5 +7,2 @@ import * as expressionTo from './expressionTo.js'; | ||
export default function isExportsOrModuleAssignment(path) { | ||
if (path.isExpressionStatement()) { | ||
path = path.get('expression'); | ||
} | ||
if (!path.isAssignmentExpression() || | ||
@@ -12,0 +9,0 @@ !path.get('left').isMemberExpression()) { |
import type { NodePath } from '@babel/traverse'; | ||
import type { CallExpression } from '@babel/types'; | ||
/** | ||
@@ -6,2 +7,4 @@ * Returns true if the expression is a function call of the form | ||
*/ | ||
export default function isReactBuiltinCall(path: NodePath, name: string): boolean; | ||
export default function isReactBuiltinCall(path: NodePath, name: string): path is NodePath<CallExpression & { | ||
__reactBuiltinTypeHint: true; | ||
}>; |
@@ -32,5 +32,2 @@ import isReactModuleName from './isReactModuleName.js'; | ||
export default function isReactBuiltinCall(path, name) { | ||
if (path.isExpressionStatement()) { | ||
path = path.get('expression'); | ||
} | ||
if (path.isCallExpression()) { | ||
@@ -37,0 +34,0 @@ const callee = path.get('callee'); |
import type { NodePath } from '@babel/traverse'; | ||
import type { CallExpression } from '@babel/types'; | ||
/** | ||
@@ -6,2 +7,4 @@ * Returns true if the expression is a function call of the form | ||
*/ | ||
export default function isReactChildrenElementCall(path: NodePath): boolean; | ||
export default function isReactChildrenElementCall(path: NodePath): path is NodePath<CallExpression & { | ||
__reactBuiltinTypeHint: true; | ||
}>; |
@@ -8,5 +8,2 @@ import isReactModuleName from './isReactModuleName.js'; | ||
export default function isReactChildrenElementCall(path) { | ||
if (path.isExpressionStatement()) { | ||
path = path.get('expression'); | ||
} | ||
if (!path.isCallExpression()) { | ||
@@ -13,0 +10,0 @@ return false; |
import type { NodePath } from '@babel/traverse'; | ||
import type { Expression, ExpressionStatement } from '@babel/types'; | ||
import type { CallExpression } from '@babel/types'; | ||
/** | ||
@@ -7,2 +7,4 @@ * Returns true if the expression is a function call of the form | ||
*/ | ||
export default function isReactCloneElementCall(path: NodePath<Expression | ExpressionStatement>): boolean; | ||
export default function isReactCloneElementCall(path: NodePath): path is NodePath<CallExpression & { | ||
__reactBuiltinTypeHint: true; | ||
}>; |
import type { NodePath } from '@babel/traverse'; | ||
import type { CallExpression } from '@babel/types'; | ||
/** | ||
@@ -10,2 +11,4 @@ * Returns true if the expression is a function call of the form | ||
*/ | ||
export default function isReactCreateClassCall(path: NodePath): boolean; | ||
export default function isReactCreateClassCall(path: NodePath): path is NodePath<CallExpression & { | ||
__reactBuiltinTypeHint: true; | ||
}>; |
@@ -11,5 +11,2 @@ import resolveToModule from './resolveToModule.js'; | ||
function isReactCreateClassCallModular(path) { | ||
if (path.isExpressionStatement()) { | ||
path = path.get('expression'); | ||
} | ||
if (!path.isCallExpression()) { | ||
@@ -30,4 +27,5 @@ return false; | ||
export default function isReactCreateClassCall(path) { | ||
return (isReactBuiltinCall(path, 'createClass') || | ||
return ((isReactBuiltinCall(path, 'createClass') && | ||
path.get('arguments').length === 1) || | ||
isReactCreateClassCallModular(path)); | ||
} |
import type { NodePath } from '@babel/traverse'; | ||
import type { Expression, ExpressionStatement } from '@babel/types'; | ||
import type { CallExpression } from '@babel/types'; | ||
/** | ||
@@ -7,2 +7,4 @@ * Returns true if the expression is a function call of the form | ||
*/ | ||
export default function isReactCreateElementCall(path: NodePath<Expression | ExpressionStatement>): boolean; | ||
export default function isReactCreateElementCall(path: NodePath): path is NodePath<CallExpression & { | ||
__reactBuiltinTypeHint: true; | ||
}>; |
import type { NodePath } from '@babel/traverse'; | ||
import type { CallExpression } from '@babel/types'; | ||
/** | ||
@@ -6,2 +7,4 @@ * Returns true if the expression is a function call of the form | ||
*/ | ||
export default function isReactForwardRefCall(path: NodePath): boolean; | ||
export default function isReactForwardRefCall(path: NodePath): path is NodePath<CallExpression & { | ||
__reactBuiltinTypeHint: true; | ||
}>; |
@@ -7,3 +7,4 @@ import isReactBuiltinCall from './isReactBuiltinCall.js'; | ||
export default function isReactForwardRefCall(path) { | ||
return isReactBuiltinCall(path, 'forwardRef'); | ||
return (isReactBuiltinCall(path, 'forwardRef') && | ||
path.get('arguments').length === 1); | ||
} |
{ | ||
"name": "react-docgen", | ||
"version": "6.0.0-rc.8", | ||
"version": "6.0.0-rc.9", | ||
"description": "A library to extract information from React components for documentation generation.", | ||
@@ -37,2 +37,3 @@ "repository": "reactjs/react-docgen", | ||
"@babel/types": "^7.18.9", | ||
"@types/babel__core": "^7.18.0", | ||
"@types/babel__traverse": "^7.18.0", | ||
@@ -39,0 +40,0 @@ "@types/doctrine": "^0.0.5", |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
223757
5639
10
+ Added@types/babel__core@^7.18.0
+ Added@types/babel__core@7.20.5(transitive)
+ Added@types/babel__generator@7.6.8(transitive)
+ Added@types/babel__template@7.4.4(transitive)