Skip to content

Commit fbbd399

Browse files
committed
more ts fixes
1 parent d29ebf1 commit fbbd399

File tree

4 files changed

+40
-14
lines changed

4 files changed

+40
-14
lines changed

packages/react-docgen/package.json

+1
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
"@babel/core": "^7.18.9",
3939
"@babel/traverse": "^7.18.9",
4040
"@babel/types": "^7.18.9",
41+
"@types/babel__core": "^7.18.0",
4142
"@types/babel__traverse": "^7.18.0",
4243
"@types/doctrine": "^0.0.5",
4344
"@types/resolve": "^1.20.2",

packages/react-docgen/src/babelParser.ts

+5-8
Original file line numberDiff line numberDiff line change
@@ -3,19 +3,16 @@ import { loadPartialConfig, parseSync } from '@babel/core';
33
import type { File } from '@babel/types';
44
import { extname } from 'path';
55

6-
const TYPESCRIPT_EXTS = {
7-
'.cts': true,
8-
'.mts': true,
9-
'.ts': true,
10-
'.tsx': true,
11-
};
6+
const TYPESCRIPT_EXTS = new Set(['.cts', '.mts', '.ts', '.tsx']);
127

138
function getDefaultPlugins(
149
options: TransformOptions,
1510
): NonNullable<ParserOptions['plugins']> {
1611
return [
1712
'jsx',
18-
TYPESCRIPT_EXTS[extname(options.filename || '')] ? 'typescript' : 'flow',
13+
options.filename && TYPESCRIPT_EXTS.has(extname(options.filename))
14+
? 'typescript'
15+
: 'flow',
1916
'asyncDoExpressions',
2017
'decimal',
2118
['decorators', { decoratorsBeforeExport: false }],
@@ -87,5 +84,5 @@ export default function babelParser(
8784
throw new Error('Unable to parse source code.');
8885
}
8986

90-
return ast;
87+
return ast as File;
9188
}

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

+6-4
Original file line numberDiff line numberDiff line change
@@ -391,7 +391,7 @@ function handleTypeofTypeAnnotation(
391391
return getFlowTypeWithResolvedTypes(path.get('argument'), typeParams);
392392
}
393393

394-
let visitedTypes = {};
394+
let visitedTypes: Record<string, TypeDescriptor | boolean> = {};
395395

396396
function getFlowTypeWithResolvedTypes(
397397
path: NodePath<FlowType>,
@@ -405,13 +405,15 @@ function getFlowTypeWithResolvedTypes(
405405
// When we see a TypeAlias mark it as visited so that the next
406406
// call of this function does not run into an endless loop
407407
if (isTypeAlias) {
408-
if (visitedTypes[parent.node.id.name] === true) {
408+
const visitedType = visitedTypes[parent.node.id.name];
409+
410+
if (visitedType === true) {
409411
// if we are currently visiting this node then just return the name
410412
// as we are starting to endless loop
411413
return { name: parent.node.id.name };
412-
} else if (typeof visitedTypes[parent.node.id.name] === 'object') {
414+
} else if (typeof visitedType === 'object') {
413415
// if we already resolved the type simple return it
414-
return visitedTypes[parent.node.id.name];
416+
return visitedType;
415417
}
416418
// mark the type as visited
417419
visitedTypes[parent.node.id.name] = true;

pnpm-lock.yaml

+28-2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)