Skip to content

Commit 305db70

Browse files
committed
Add more return types
1 parent d1744d4 commit 305db70

File tree

10 files changed

+76
-51
lines changed

10 files changed

+76
-51
lines changed

packages/react-docgen-cli/src/commands/parse/command.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ const defaultIgnoreGlobs = [
2323
const defaultHandlers = Object.keys(builtinHandlers);
2424
const defaultResolvers = ['find-exported-component'];
2525

26-
function collect(value: string, previous: string[]) {
26+
function collect(value: string, previous: string[]): string[] {
2727
if (
2828
!previous ||
2929
previous === defaultIgnoreGlobs ||

packages/react-docgen-cli/src/commands/parse/output/outputError.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
import { relative } from 'path';
22
import chalk from 'chalk';
33

4-
function isReactDocgenError(error: NodeJS.ErrnoException) {
5-
return error instanceof Error && error.code?.startsWith('ERR_REACTDOCGEN');
4+
function isReactDocgenError(error: NodeJS.ErrnoException): boolean {
5+
return Boolean(
6+
error instanceof Error && error.code?.startsWith('ERR_REACTDOCGEN'),
7+
);
68
}
79

810
function outputReactDocgenError(
@@ -38,7 +40,7 @@ export default function outputError(
3840
error: Error,
3941
filePath: string,
4042
options: { failOnWarning: boolean },
41-
) {
43+
): boolean {
4244
if (isReactDocgenError(error)) {
4345
return outputReactDocgenError(error, filePath, options);
4446
} else {

packages/react-docgen-cli/src/commands/parse/output/outputResult.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import type { Documentation } from 'react-docgen';
44
export default async function outputResult(
55
documentation: Record<string, Documentation[]>,
66
{ pretty = false, output }: { pretty: boolean; output: string | undefined },
7-
) {
7+
): Promise<void> {
88
const result = JSON.stringify(
99
documentation,
1010
undefined,

packages/react-docgen-cli/tests/integration/utils/withFixture.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { rm, stat } from 'fs/promises';
22
import { dirname, join } from 'path';
3-
import type { ExecaError } from 'execa';
3+
import type { ExecaError, ExecaReturnValue } from 'execa';
44
import { execaNode } from 'execa';
55
import copy from 'cpy';
66
import { temporaryDirectory } from 'tempy';
@@ -22,7 +22,9 @@ export default async function withFixture(
2222
): Promise<void> {
2323
const tempDir = temporaryDirectory();
2424

25-
async function run(args: readonly string[]) {
25+
async function run(
26+
args: readonly string[],
27+
): Promise<ExecaError | ExecaReturnValue<string>> {
2628
try {
2729
return await execaNode(cliBinary, args, {
2830
cwd: tempDir,

packages/react-docgen/src/resolver/FindAllDefinitionsResolver.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ interface TraverseState {
1919
foundDefinitions: Set<ComponentNodePath>;
2020
}
2121

22-
function classVisitor(path: NodePath, state: TraverseState) {
22+
function classVisitor(path: NodePath, state: TraverseState): void {
2323
if (isReactComponentClass(path)) {
2424
normalizeClassDefinition(path);
2525
state.foundDefinitions.add(path);
@@ -36,7 +36,7 @@ function statelessVisitor(
3636
| ObjectMethod
3737
>,
3838
state: TraverseState,
39-
) {
39+
): void {
4040
if (isStatelessComponent(path)) {
4141
state.foundDefinitions.add(path);
4242
}

packages/react-docgen/src/utils/getMemberValuePath.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,8 @@ type SupportedNodes =
2727

2828
const postprocessPropTypes = (
2929
path: NodePath<ClassMethod | Expression | ObjectMethod>,
30-
) => (path.isFunction() ? resolveFunctionDefinitionToReturnValue(path) : path);
30+
): NodePath<ClassMethod | Expression | ObjectMethod> | null =>
31+
path.isFunction() ? resolveFunctionDefinitionToReturnValue(path) : path;
3132

3233
const POSTPROCESS_MEMBERS = new Map([['propTypes', postprocessPropTypes]]);
3334

packages/react-docgen/src/utils/getPropType.ts

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,10 @@ function getPropTypeOneOfType(
9595
return type;
9696
}
9797

98-
function getPropTypeArrayOf(type: PropTypeDescriptor, argumentPath: NodePath) {
98+
function getPropTypeArrayOf(
99+
type: PropTypeDescriptor,
100+
argumentPath: NodePath,
101+
): PropTypeDescriptor {
99102
const docs = getDocblock(argumentPath);
100103

101104
if (docs) {
@@ -112,7 +115,10 @@ function getPropTypeArrayOf(type: PropTypeDescriptor, argumentPath: NodePath) {
112115
return type;
113116
}
114117

115-
function getPropTypeObjectOf(type: PropTypeDescriptor, argumentPath: NodePath) {
118+
function getPropTypeObjectOf(
119+
type: PropTypeDescriptor,
120+
argumentPath: NodePath,
121+
): PropTypeDescriptor {
116122
const docs = getDocblock(argumentPath);
117123

118124
if (docs) {
@@ -155,7 +161,10 @@ function isCyclicReference(
155161
/**
156162
* Handles shape and exact prop types
157163
*/
158-
function getPropTypeShapish(type: PropTypeDescriptor, argumentPath: NodePath) {
164+
function getPropTypeShapish(
165+
type: PropTypeDescriptor,
166+
argumentPath: NodePath,
167+
): PropTypeDescriptor {
159168
if (!argumentPath.isObjectExpression()) {
160169
argumentPath = resolveToValue(argumentPath);
161170
}
@@ -254,7 +263,7 @@ function callPropTypeHandler(
254263
name: PropTypeDescriptor['name'],
255264
handler: PropTypeHandler,
256265
argumentPath: NodePath | undefined,
257-
) {
266+
): PropTypeDescriptor {
258267
let type: PropTypeDescriptor = { name };
259268

260269
if (argumentPath) {

packages/react-docgen/src/utils/getTypeFromReactComponent.ts

Lines changed: 41 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -92,18 +92,14 @@ export function applyToTypeProperties(
9292
.get('members')
9393
.forEach((propertyPath) => callback(propertyPath, typeParams));
9494
} else if (path.isInterfaceDeclaration()) {
95-
if (path.node.extends) {
96-
applyExtends(documentation, path, callback, typeParams);
97-
}
95+
applyExtends(documentation, path, callback, typeParams);
9896

9997
path
10098
.get('body')
10199
.get('properties')
102100
.forEach((propertyPath) => callback(propertyPath, typeParams));
103101
} else if (path.isTSInterfaceDeclaration()) {
104-
if (path.node.extends) {
105-
applyExtends(documentation, path, callback, typeParams);
106-
}
102+
applyExtends(documentation, path, callback, typeParams);
107103

108104
path
109105
.get('body')
@@ -133,36 +129,47 @@ function applyExtends(
133129
path: NodePath<InterfaceDeclaration | TSInterfaceDeclaration>,
134130
callback: (propertyPath: NodePath, params: TypeParameters | null) => void,
135131
typeParams: TypeParameters | null,
136-
) {
137-
(
138-
path.get('extends') as Array<
139-
NodePath<InterfaceExtends | TSExpressionWithTypeArguments>
140-
>
141-
).forEach((extendsPath) => {
142-
const resolvedPath = resolveGenericTypeAnnotation(extendsPath);
143-
144-
if (resolvedPath) {
145-
if (
146-
resolvedPath.has('typeParameters') &&
147-
extendsPath.node.typeParameters
148-
) {
149-
typeParams = getTypeParameters(
150-
resolvedPath.get('typeParameters') as NodePath<
151-
TSTypeParameterDeclaration | TypeParameterDeclaration
152-
>,
153-
extendsPath.get('typeParameters') as NodePath<
154-
TSTypeParameterInstantiation | TypeParameterInstantiation
155-
>,
132+
): void {
133+
const classExtends = path.get('extends');
134+
135+
if (!Array.isArray(classExtends)) {
136+
return;
137+
}
138+
139+
classExtends.forEach(
140+
(
141+
extendsPath: NodePath<InterfaceExtends | TSExpressionWithTypeArguments>,
142+
) => {
143+
const resolvedPath = resolveGenericTypeAnnotation(extendsPath);
144+
145+
if (resolvedPath) {
146+
if (
147+
resolvedPath.has('typeParameters') &&
148+
extendsPath.node.typeParameters
149+
) {
150+
typeParams = getTypeParameters(
151+
resolvedPath.get('typeParameters') as NodePath<
152+
TSTypeParameterDeclaration | TypeParameterDeclaration
153+
>,
154+
extendsPath.get('typeParameters') as NodePath<
155+
TSTypeParameterInstantiation | TypeParameterInstantiation
156+
>,
157+
typeParams,
158+
);
159+
}
160+
applyToTypeProperties(
161+
documentation,
162+
resolvedPath,
163+
callback,
156164
typeParams,
157165
);
158-
}
159-
applyToTypeProperties(documentation, resolvedPath, callback, typeParams);
160-
} else {
161-
const idPath = getTypeIdentifier(extendsPath);
166+
} else {
167+
const idPath = getTypeIdentifier(extendsPath);
162168

163-
if (idPath && idPath.isIdentifier()) {
164-
documentation.addComposes(idPath.node.name);
169+
if (idPath && idPath.isIdentifier()) {
170+
documentation.addComposes(idPath.node.name);
171+
}
165172
}
166-
}
167-
});
173+
},
174+
);
168175
}

packages/react-docgen/src/utils/ts-types/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,7 @@ export default function initialize(scopeClass: typeof BaseScope): void {
162162
scopeClass.prototype.registerDeclaration = registerDeclaration;
163163

164164
scopeClass.prototype._realCrawl = scopeClass.prototype.crawl;
165-
scopeClass.prototype.crawl = function (this: BaseScope) {
165+
scopeClass.prototype.crawl = function (this: BaseScope): void {
166166
this.typeBindings = Object.create(null);
167167
this._realCrawl();
168168
};

packages/react-docgen/tests/NodePathSerializer.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,16 @@ import { NodePath } from '@babel/traverse';
22
import { removePropertiesDeep } from '@babel/types';
33
import type { expect } from 'vitest';
44

5-
function removeUndefinedProperties(node) {
5+
function removeUndefinedProperties(
6+
node: Record<string, unknown>,
7+
): Record<string, unknown> {
68
for (const key of Object.keys(node)) {
79
if (node[key] === undefined) {
810
delete node[key];
911
} else if (node[key] === Object(node[key])) {
10-
node[key] = removeUndefinedProperties(node[key]);
12+
node[key] = removeUndefinedProperties(
13+
node[key] as Record<string, unknown>,
14+
);
1115
}
1216
}
1317

0 commit comments

Comments
 (0)