Skip to content

Commit d162074

Browse files
committed
Fix lint
1 parent 0ab8ab6 commit d162074

17 files changed

Lines changed: 138 additions & 68 deletions

eslint.config.mjs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,6 @@ export default tseslint.config([
8989
],
9090

9191
'@typescript-eslint/consistent-type-imports': 'error',
92-
'@typescript-eslint/no-non-null-assertion': 'off',
9392
'@typescript-eslint/sort-type-constituents': 'error',
9493
},
9594
},

packages/react-docgen-cli/src/commands/parse/options/loadReactDocgenPlugin.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,10 @@ export default async function loadReactDocgenPlugin<T>(
66
name: string,
77
builtins?: Record<string, T>,
88
): Promise<T> {
9-
if (builtins?.[input]) {
10-
return builtins[input]!;
9+
const builtin = builtins?.[input];
10+
11+
if (builtin !== undefined) {
12+
return builtin;
1113
}
1214

1315
const path = resolve(process.cwd(), input);

packages/react-docgen-cli/src/commands/parse/options/loadResolvers.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,5 +57,11 @@ export default async function loadResolvers(
5757
});
5858
}
5959

60-
return loadResolver(input[0]!);
60+
const [resolver] = input;
61+
62+
if (resolver === undefined) {
63+
return;
64+
}
65+
66+
return loadResolver(resolver);
6167
}

packages/react-docgen/src/handlers/componentDocblockHandler.ts

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,11 @@ function getDocblockFromComponent(path: NodePath): string | null {
1919
// If we have a class declaration or expression, then the comment might be
2020
// attached to the last decorator instead as trailing comment.
2121
if (decorators && decorators.length > 0) {
22-
description = getDocblock(decorators[decorators.length - 1]!, true);
22+
const lastDecorator = decorators[decorators.length - 1];
23+
24+
if (lastDecorator) {
25+
description = getDocblock(lastDecorator, true);
26+
}
2327
}
2428
}
2529
if (description == null) {
@@ -41,9 +45,10 @@ function getDocblockFromComponent(path: NodePath): string | null {
4145
}
4246
}
4347
if (!description) {
44-
const searchPath = isReactForwardRefCall(path)
45-
? path.get('arguments')[0]!
46-
: path;
48+
const [forwardRefArgument] = isReactForwardRefCall(path)
49+
? path.get('arguments')
50+
: [];
51+
const searchPath = forwardRefArgument ?? path;
4752
const inner = resolveToValue(searchPath);
4853

4954
if (inner.node !== path.node) {

packages/react-docgen/src/handlers/componentMethodsHandler.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -141,9 +141,13 @@ function findStatelessComponentBody(
141141
return body;
142142
}
143143
} else if (isReactForwardRefCall(componentDefinition)) {
144-
const inner = resolveToValue(componentDefinition.get('arguments')[0]!);
144+
const [forwardRefArgument] = componentDefinition.get('arguments');
145145

146-
return findStatelessComponentBody(inner);
146+
if (forwardRefArgument) {
147+
const inner = resolveToValue(forwardRefArgument);
148+
149+
return findStatelessComponentBody(inner);
150+
}
147151
}
148152

149153
return undefined;

packages/react-docgen/src/handlers/defaultPropsHandler.ts

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ import type { Handler } from './index.js';
2323

2424
function getDefaultValue(path: NodePath): DefaultValueDescriptor | null {
2525
let defaultValue: string | undefined;
26-
let resolvedPath = path;
2726
let valuePath = path;
2827

2928
if (path.isBooleanLiteral()) {
@@ -33,6 +32,8 @@ function getDefaultValue(path: NodePath): DefaultValueDescriptor | null {
3332
} else if (path.isLiteral()) {
3433
defaultValue = path.node.extra?.raw as string;
3534
} else {
35+
let resolvedPath: NodePath;
36+
3637
if (path.isAssignmentPattern()) {
3738
resolvedPath = resolveToValue(path.get('right'));
3839
} else {
@@ -64,7 +65,13 @@ function getStatelessPropsPath(
6465
let value: NodePath = componentDefinition;
6566

6667
if (isReactForwardRefCall(componentDefinition)) {
67-
value = resolveToValue(componentDefinition.get('arguments')[0]!);
68+
const [forwardRefArgument] = componentDefinition.get('arguments');
69+
70+
if (!forwardRefArgument) {
71+
return undefined;
72+
}
73+
74+
value = resolveToValue(forwardRefArgument);
6875
}
6976

7077
if (!value.isFunction()) {

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

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,11 @@ export function getDocblock(path: NodePath, trailing = false): string | null {
3535
}
3636

3737
if (comments.length > 0) {
38-
return parseDocblock(comments[comments.length - 1]!.value);
38+
const lastComment = comments[comments.length - 1];
39+
40+
if (lastComment) {
41+
return parseDocblock(lastComment.value);
42+
}
3943
}
4044

4145
return null;
@@ -50,7 +54,11 @@ export function getDoclets(str: string): Record<string, string> {
5054
let match: RegExpExecArray | null;
5155

5256
while ((match = DOCLET_PATTERN.exec(str))) {
53-
doclets[match[1]!] = match[2] || true;
57+
const [, name, value] = match;
58+
59+
if (name) {
60+
doclets[name] = value || true;
61+
}
5462
}
5563

5664
return doclets;

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

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,13 @@ function resolveComponentDefinition(
2424
): NodePath<ComponentNode> | null {
2525
if (isReactCreateClassCall(definition)) {
2626
// return argument
27-
const resolvedPath = resolveToValue(definition.get('arguments')[0]!);
27+
const [argument] = definition.get('arguments');
28+
29+
if (!argument) {
30+
return null;
31+
}
32+
33+
const resolvedPath = resolveToValue(argument);
2834

2935
if (resolvedPath.isObjectExpression()) {
3036
return resolvedPath;

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

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -182,15 +182,13 @@ function handleGenericTypeAnnotation(
182182
);
183183
}
184184

185-
if (
186-
typeParams &&
187-
typeParams[type.name] &&
188-
typeParams[type.name]!.isGenericTypeAnnotation()
189-
) {
185+
const resolvedTypeParam = typeParams?.[type.name];
186+
187+
if (resolvedTypeParam?.isGenericTypeAnnotation()) {
190188
return type;
191189
}
192190

193-
if (typeParams && typeParams[type.name]) {
191+
if (resolvedTypeParam) {
194192
type = getFlowTypeWithResolvedTypes(
195193
resolvedPath as NodePath<FlowType>,
196194
typeParams,
@@ -482,8 +480,10 @@ function getFlowTypeWithResolvedTypes(
482480
visitedTypes[parent.node.id.name] = true;
483481
}
484482

485-
if (path.node.type in flowTypes) {
486-
type = { name: flowTypes[path.node.type]! };
483+
const primitiveFlowType = flowTypes[path.node.type];
484+
485+
if (primitiveFlowType) {
486+
type = { name: primitiveFlowType };
487487
} else if (path.node.type in flowLiteralTypes) {
488488
type = {
489489
name: 'literal',
@@ -499,8 +499,12 @@ function getFlowTypeWithResolvedTypes(
499499
).node.value
500500
}`,
501501
};
502-
} else if (path.node.type in namedTypes) {
503-
type = namedTypes[path.node.type]!(path, typeParams);
502+
} else {
503+
const typeHandler = namedTypes[path.node.type];
504+
505+
if (typeHandler) {
506+
type = typeHandler(path, typeParams);
507+
}
504508
}
505509

506510
if (!type) {

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

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,13 @@ function resolveName(path: NodePath): string | undefined {
1818
);
1919
}
2020
// VariableDeclarator always has at least one declaration, hence the non-null-assertion
21-
const id = declarations[0]!.get('id');
21+
const [declaration] = declarations;
22+
23+
if (!declaration) {
24+
return;
25+
}
26+
27+
const id = declaration.get('id');
2228

2329
if (id.isIdentifier()) {
2430
return id.node.name;

0 commit comments

Comments
 (0)