Skip to content

Commit 5f2bbdb

Browse files
MarshallOfSoundNoviny
authored andcommitted
feat: add support for TSIndexedAccessType and fix TSQualifiedName for imported types (#31)
* feat: add support for TSIndexedAccessType and fix TSQualifiedName for imported types * test: add test for TSIndexedAccessType
1 parent a3f2002 commit 5f2bbdb

File tree

3 files changed

+49
-1
lines changed

3 files changed

+49
-1
lines changed

__snapshots__/test.js.snap

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4093,6 +4093,26 @@ Object {
40934093
}
40944094
`;
40954095

4096+
exports[`typescript indexed type 1`] = `
4097+
Object {
4098+
"classes": Array [
4099+
Object {
4100+
"kind": "generic",
4101+
"name": Object {
4102+
"kind": "id",
4103+
"name": "Component",
4104+
"type": null,
4105+
},
4106+
"value": Object {
4107+
"kind": "object",
4108+
"name": "MyType['props']",
4109+
},
4110+
},
4111+
],
4112+
"kind": "program",
4113+
}
4114+
`;
4115+
40964116
exports[`unary operator 1`] = `
40974117
Object {
40984118
"classes": Array [

index.js

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -826,6 +826,23 @@ converters.NullableTypeAnnotation = (path, context) /*: K.Nullable*/ => {
826826
};
827827
};
828828

829+
converters.TSIndexedAccessType = (path, context) => {
830+
const type = convert(path.get('objectType'), context);
831+
const indexKey = path.get('indexType').node.literal.value;
832+
833+
if (type.kind === 'generic') {
834+
return {
835+
kind: 'generic',
836+
value: {
837+
kind: type.value.kind,
838+
name: `${type.value.name || type.value.referenceIdName}['${indexKey}']`
839+
}
840+
};
841+
} else {
842+
throw new Error(`Unsupported TSIndexedAccessType kind: ${type.kind}`)
843+
}
844+
}
845+
829846
converters.TSStringKeyword = (path) /*: K.String */ => {
830847
return { kind: 'string' };
831848
};
@@ -971,7 +988,7 @@ converters.TSQualifiedName = (path, context) /*: K.Id */ => {
971988

972989
return {
973990
kind: 'id',
974-
name: `${left.name}.${right.name}`
991+
name: `${left.name || left.referenceIdName}.${right.name}`
975992
};
976993
};
977994

test.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1109,6 +1109,17 @@ const TESTS = [
11091109
class Component extends React.Component<RecursiveType> {}
11101110
`
11111111
},
1112+
{
1113+
name: 'typescript indexed type',
1114+
typeSystem: 'typescript',
1115+
code: `
1116+
type MyType = {
1117+
props: RecursiveType
1118+
}
1119+
1120+
class Component extends React.Component<MyType['props']> {}
1121+
`
1122+
},
11121123
];
11131124

11141125
for (let testCase /*: TestCase */ of TESTS) {

0 commit comments

Comments
 (0)