-
-
Notifications
You must be signed in to change notification settings - Fork 297
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
resolve TODOS and fix handling of some TypeScript typeof types (#747)
- Loading branch information
Showing
10 changed files
with
213 additions
and
63 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
--- | ||
'react-docgen': patch | ||
--- | ||
|
||
Handle `typeof import('...')` and `typeof MyType.property` correctly in | ||
TypeScript |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
41 changes: 41 additions & 0 deletions
41
packages/react-docgen/src/utils/__tests__/__snapshots__/getTypeParameters-test.ts.snap
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
// Vitest Snapshot v1 | ||
|
||
exports[`getTypeParameters > Flow > detects simple type 1`] = ` | ||
{ | ||
"T": Node { | ||
"id": Node { | ||
"name": "T", | ||
"type": "Identifier", | ||
}, | ||
"type": "GenericTypeAnnotation", | ||
"typeParameters": null, | ||
}, | ||
} | ||
`; | ||
|
||
exports[`getTypeParameters > TypeScript > detects default 1`] = ` | ||
{ | ||
"R": Node { | ||
"type": "TSStringKeyword", | ||
}, | ||
"T": Node { | ||
"type": "TSTypeReference", | ||
"typeName": Node { | ||
"name": "T", | ||
"type": "Identifier", | ||
}, | ||
}, | ||
} | ||
`; | ||
|
||
exports[`getTypeParameters > TypeScript > detects simple type 1`] = ` | ||
{ | ||
"T": Node { | ||
"type": "TSTypeReference", | ||
"typeName": Node { | ||
"name": "T", | ||
"type": "Identifier", | ||
}, | ||
}, | ||
} | ||
`; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
61 changes: 61 additions & 0 deletions
61
packages/react-docgen/src/utils/__tests__/getTypeParameters-test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
import type { | ||
TSTypeAliasDeclaration, | ||
TSTypeParameterDeclaration, | ||
TSTypeParameterInstantiation, | ||
TypeAlias, | ||
TypeParameterDeclaration, | ||
TypeParameterInstantiation, | ||
} from '@babel/types'; | ||
import { parse, parseTypescript } from '../../../tests/utils'; | ||
import getTypeParameters from '../getTypeParameters.js'; | ||
import { describe, expect, test } from 'vitest'; | ||
import type { NodePath } from '@babel/traverse'; | ||
|
||
describe('getTypeParameters', () => { | ||
describe('TypeScript', () => { | ||
test('detects simple type', () => { | ||
const path = | ||
parseTypescript.statement<TSTypeAliasDeclaration>('type x<T> = y<T>'); | ||
|
||
expect( | ||
getTypeParameters( | ||
path.get('typeParameters') as NodePath<TSTypeParameterDeclaration>, | ||
path | ||
.get('typeAnnotation') | ||
.get('typeParameters') as NodePath<TSTypeParameterInstantiation>, | ||
null, | ||
), | ||
).toMatchSnapshot(); | ||
}); | ||
test('detects default', () => { | ||
const path = parseTypescript.statement<TSTypeAliasDeclaration>( | ||
'type x<T, R = string> = y<T>;', | ||
); | ||
|
||
expect( | ||
getTypeParameters( | ||
path.get('typeParameters') as NodePath<TSTypeParameterDeclaration>, | ||
path | ||
.get('typeAnnotation') | ||
.get('typeParameters') as NodePath<TSTypeParameterInstantiation>, | ||
null, | ||
), | ||
).toMatchSnapshot(); | ||
}); | ||
}); | ||
describe('Flow', () => { | ||
test('detects simple type', () => { | ||
const path = parse.statement<TypeAlias>('type x<T> = y<T>'); | ||
|
||
expect( | ||
getTypeParameters( | ||
path.get('typeParameters') as NodePath<TypeParameterDeclaration>, | ||
path | ||
.get('right') | ||
.get('typeParameters') as NodePath<TypeParameterInstantiation>, | ||
null, | ||
), | ||
).toMatchSnapshot(); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters