Skip to content

Commit 458f24a

Browse files
committed
Fix typescript to not use recast and fix flow types
1 parent f5b2121 commit 458f24a

File tree

5 files changed

+66
-55
lines changed

5 files changed

+66
-55
lines changed

.eslintrc.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ module.exports = {
1616
globals: {
1717
ASTNode: true,
1818
NodePath: true,
19+
$Exact: true,
1920
},
2021
overrides: [
2122
{

src/types.js

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -45,10 +45,12 @@ export type FlowBaseType = {
4545
alias?: string,
4646
};
4747

48-
export type FlowSimpleType = FlowBaseType & {|
49-
name: string,
50-
raw?: string,
51-
|};
48+
export type FlowSimpleType = $Exact<
49+
FlowBaseType & {
50+
name: string,
51+
raw?: string,
52+
},
53+
>;
5254

5355
export type FlowLiteralType = FlowBaseType & {
5456
name: 'literal',
@@ -63,7 +65,7 @@ export type FlowElementsType = FlowBaseType & {
6365

6466
export type FlowFunctionArgumentType = {
6567
name: string,
66-
type: FlowTypeDescriptor,
68+
type?: FlowTypeDescriptor,
6769
rest?: boolean,
6870
};
6971

@@ -77,6 +79,17 @@ export type FlowFunctionSignatureType = FlowBaseType & {
7779
},
7880
};
7981

82+
export type TSFunctionSignatureType = FlowBaseType & {
83+
name: 'signature',
84+
type: 'function',
85+
raw: string,
86+
signature: {
87+
arguments: Array<FlowFunctionArgumentType>,
88+
return: FlowTypeDescriptor,
89+
this?: FlowTypeDescriptor,
90+
},
91+
};
92+
8093
export type FlowObjectSignatureType = FlowBaseType & {
8194
name: 'signature',
8295
type: 'object',
@@ -100,6 +113,7 @@ export type FlowTypeDescriptor =
100113
export type PropDescriptor = {
101114
type?: PropTypeDescriptor,
102115
flowType?: FlowTypeDescriptor,
116+
tsType?: FlowTypeDescriptor,
103117
required?: boolean,
104118
defaultValue?: any,
105119
description?: string,

src/utils/__tests__/getTSType-test.js

Lines changed: 14 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -6,32 +6,23 @@
66
*
77
*/
88

9-
/* global jest, describe, beforeEach, it, expect */
9+
import { expression as expr, statement as stmt } from '../../../tests/utils';
10+
import getTSType from '../getTSType';
1011

11-
jest.disableAutomock();
12-
13-
describe('getTSType', () => {
14-
let expression, statement;
15-
let getTSType;
16-
17-
beforeEach(() => {
18-
getTSType = require('../getTSType').default;
19-
const {
20-
expression: expr,
21-
statement: stmt,
22-
} = require('../../../tests/utils');
23-
expression = code =>
24-
expr(code, undefined, {
25-
filename: 'test.ts',
26-
babelrc: false,
27-
});
28-
statement = code =>
29-
stmt(code, undefined, {
30-
filename: 'test.ts',
31-
babelrc: false,
32-
});
12+
function expression(code) {
13+
return expr(code, {
14+
filename: 'test.ts',
15+
babelrc: false,
16+
});
17+
}
18+
function statement(code) {
19+
return stmt(code, {
20+
filename: 'test.ts',
21+
babelrc: false,
3322
});
23+
}
3424

25+
describe('getTSType', () => {
3526
it('detects simple types', () => {
3627
const simplePropTypes = [
3728
'string',

src/utils/getFlowType.js

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,11 @@ import getTypeParameters, {
1717
type TypeParameters,
1818
} from '../utils/getTypeParameters';
1919
import type {
20-
FlowTypeDescriptor,
2120
FlowElementsType,
2221
FlowFunctionSignatureType,
2322
FlowObjectSignatureType,
23+
FlowSimpleType,
24+
FlowTypeDescriptor,
2425
} from '../types';
2526

2627
const { namedTypes: t } = types;
@@ -199,7 +200,7 @@ function handleObjectTypeAnnotation(
199200
return type;
200201
}
201202

202-
function handleInterfaceDeclaration(path: NodePath): FlowElementsType {
203+
function handleInterfaceDeclaration(path: NodePath): FlowSimpleType {
203204
// Interfaces are handled like references which would be documented separately,
204205
// rather than inlined like type aliases.
205206
return {
@@ -268,7 +269,7 @@ function handleFunctionTypeAnnotation(
268269
name: param.node.name ? param.node.name.name : '',
269270
type: typeAnnotation
270271
? getFlowTypeWithResolvedTypes(typeAnnotation, typeParams)
271-
: null,
272+
: undefined,
272273
});
273274
});
274275

@@ -280,7 +281,7 @@ function handleFunctionTypeAnnotation(
280281
name: rest.node.name ? rest.node.name.name : '',
281282
type: typeAnnotation
282283
? getFlowTypeWithResolvedTypes(typeAnnotation, typeParams)
283-
: null,
284+
: undefined,
284285
rest: true,
285286
});
286287
}

src/utils/getTSType.js

Lines changed: 27 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -6,26 +6,26 @@
66
*
77
* @flow
88
*/
9+
10+
import types from 'ast-types';
911
import getPropertyName from './getPropertyName';
1012
import printValue from './printValue';
11-
import recast from 'recast';
1213
import getTypeAnnotation from '../utils/getTypeAnnotation';
1314
import resolveToValue from '../utils/resolveToValue';
1415
import { resolveObjectToNameArray } from '../utils/resolveObjectKeysToArray';
1516
import getTypeParameters, {
1617
type TypeParameters,
1718
} from '../utils/getTypeParameters';
1819
import type {
19-
FlowTypeDescriptor,
2020
FlowElementsType,
21-
FlowFunctionSignatureType,
2221
FlowFunctionArgumentType,
2322
FlowObjectSignatureType,
23+
FlowSimpleType,
24+
FlowTypeDescriptor,
25+
TSFunctionSignatureType,
2426
} from '../types';
2527

26-
const {
27-
types: { namedTypes: types },
28-
} = recast;
28+
const { namedTypes: t } = types;
2929

3030
const tsTypes = {
3131
TSAnyKeyword: 'any',
@@ -71,7 +71,7 @@ function handleTSTypeReference(
7171
typeParams: ?TypeParameters,
7272
): ?FlowTypeDescriptor {
7373
let type: FlowTypeDescriptor;
74-
if (types.TSQualifiedName.check(path.node.typeName)) {
74+
if (t.TSQualifiedName.check(path.node.typeName)) {
7575
const typeName = path.get('typeName');
7676

7777
if (typeName.node.left.name === 'React') {
@@ -144,19 +144,23 @@ function handleTSTypeLiteral(
144144

145145
path.get('members').each(param => {
146146
if (
147-
types.TSPropertySignature.check(param.node) ||
148-
types.TSMethodSignature.check(param.node)
147+
t.TSPropertySignature.check(param.node) ||
148+
t.TSMethodSignature.check(param.node)
149149
) {
150+
const propName = getPropertyName(param);
151+
if (!propName) {
152+
return;
153+
}
150154
type.signature.properties.push({
151-
key: getPropertyName(param),
155+
key: propName,
152156
value: getTSTypeWithRequirements(
153157
param.get('typeAnnotation'),
154158
typeParams,
155159
),
156160
});
157-
} else if (types.TSCallSignatureDeclaration.check(param.node)) {
161+
} else if (t.TSCallSignatureDeclaration.check(param.node)) {
158162
type.signature.constructor = handleTSFunctionType(param, typeParams);
159-
} else if (types.TSIndexSignature.check(param.node)) {
163+
} else if (t.TSIndexSignature.check(param.node)) {
160164
type.signature.properties.push({
161165
key: getTSTypeWithResolvedTypes(
162166
param
@@ -176,7 +180,7 @@ function handleTSTypeLiteral(
176180
return type;
177181
}
178182

179-
function handleTSInterfaceDeclaration(path: NodePath): FlowElementsType {
183+
function handleTSInterfaceDeclaration(path: NodePath): FlowSimpleType {
180184
// Interfaces are handled like references which would be documented separately,
181185
// rather than inlined like type aliases.
182186
return {
@@ -213,8 +217,8 @@ function handleTSIntersectionType(
213217
function handleTSFunctionType(
214218
path: NodePath,
215219
typeParams: ?TypeParameters,
216-
): FlowFunctionSignatureType {
217-
const type: FlowFunctionSignatureType = {
220+
): TSFunctionSignatureType {
221+
const type: TSFunctionSignatureType = {
218222
name: 'signature',
219223
type: 'function',
220224
raw: printValue(path),
@@ -233,7 +237,7 @@ function handleTSFunctionType(
233237
name: param.node.name || '',
234238
type: typeAnnotation
235239
? getTSTypeWithResolvedTypes(typeAnnotation, typeParams)
236-
: null,
240+
: undefined,
237241
};
238242

239243
if (param.node.name === 'this') {
@@ -284,13 +288,13 @@ function handleTSTypeQuery(
284288
return { name: path.node.exprName.name };
285289
}
286290

287-
function handleTSTypeOperator(path: NodePath): FlowTypeDescriptor {
291+
function handleTSTypeOperator(path: NodePath): ?FlowTypeDescriptor {
288292
if (path.node.operator !== 'keyof') {
289293
return null;
290294
}
291295

292296
let value = path.get('typeAnnotation');
293-
if (types.TSTypeQuery.check(value.node)) {
297+
if (t.TSTypeQuery.check(value.node)) {
294298
value = value.get('exprName');
295299
} else if (value.node.id) {
296300
value = value.get('id');
@@ -299,8 +303,8 @@ function handleTSTypeOperator(path: NodePath): FlowTypeDescriptor {
299303
const resolvedPath = resolveToValue(value);
300304
if (
301305
resolvedPath &&
302-
(types.ObjectExpression.check(resolvedPath.node) ||
303-
types.TSTypeLiteral.check(resolvedPath.node))
306+
(t.ObjectExpression.check(resolvedPath.node) ||
307+
t.TSTypeLiteral.check(resolvedPath.node))
304308
) {
305309
const keys = resolveObjectToNameArray(resolvedPath, true);
306310

@@ -320,13 +324,13 @@ function getTSTypeWithResolvedTypes(
320324
path: NodePath,
321325
typeParams: ?TypeParameters,
322326
): FlowTypeDescriptor {
323-
if (types.TSTypeAnnotation.check(path.node)) {
327+
if (t.TSTypeAnnotation.check(path.node)) {
324328
path = path.get('typeAnnotation');
325329
}
326330

327331
const node = path.node;
328332
let type: ?FlowTypeDescriptor;
329-
const isTypeAlias = types.TSTypeAliasDeclaration.check(path.parentPath.node);
333+
const isTypeAlias = t.TSTypeAliasDeclaration.check(path.parentPath.node);
330334

331335
// When we see a typealias mark it as visited so that the next
332336
// call of this function does not run into an endless loop
@@ -345,7 +349,7 @@ function getTSTypeWithResolvedTypes(
345349

346350
if (node.type in tsTypes) {
347351
type = { name: tsTypes[node.type] };
348-
} else if (types.TSLiteralType.check(node)) {
352+
} else if (t.TSLiteralType.check(node)) {
349353
type = {
350354
name: 'literal',
351355
value: node.literal.raw || `${node.literal.value}`,

0 commit comments

Comments
 (0)