Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

check for value before using methods #400

Merged
merged 3 commits into from
May 28, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions src/__tests__/__snapshots__/main-test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -1801,6 +1801,24 @@ Object {
}
`;

exports[`main fixtures processes component "component_41.tsx" without errors 1`] = `
Object {
"description": "",
"displayName": "MyComponent",
"methods": Array [],
"props": Object {
"value": Object {
"description": "String value of a number",
"required": false,
"tsType": Object {
"name": "STRING_VALS[number]",
"raw": "typeof STRING_VALS[number]",
},
},
},
}
`;

exports[`main fixtures processes component "flow-export-type.js" without errors 1`] = `
Object {
"description": "This is a Flow class component",
Expand Down
24 changes: 24 additions & 0 deletions src/__tests__/fixtures/component_41.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import React from 'react';

export const STRING_VALS = [
'one',
'two',
'three'
];

interface IProps {
/**
* String value of a number
*/
value?: typeof STRING_VALS[number];
}

const MyComponent = (props: IProps) => {
return (
<div>
{props.value}
</div>
);
}

export default MyComponent;
20 changes: 20 additions & 0 deletions src/utils/__tests__/getTSType-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -753,6 +753,26 @@ describe('getTSType', () => {
});
});

it('resolves indexed access of array', () => {
const typePath = statement(`
var x: typeof STRING_VALS[number];

const STRING_VALS = [
'one',
'two',
'three'
];
`)
.get('declarations', 0)
.get('id')
.get('typeAnnotation')
.get('typeAnnotation');
expect(getTSType(typePath, null, noopImporter)).toEqual({
name: 'STRING_VALS[number]',
raw: 'typeof STRING_VALS[number]',
});
});

it('can resolve indexed access to imported type', () => {
const typePath = statement(`
var x: A["x"] = 2;
Expand Down
6 changes: 4 additions & 2 deletions src/utils/getTSType.ts
Original file line number Diff line number Diff line change
Expand Up @@ -391,12 +391,14 @@ function handleTSIndexedAccessType(
// We only get the signature if the objectType is a type (vs interface)
if (!objectType.signature)
return {
name: `${objectType.name}[${indexType.value.toString()}]`,
name: `${objectType.name}[${
indexType.value ? indexType.value.toString() : indexType.name
}]`,
raw: printValue(path),
};
const resolvedType = objectType.signature.properties.find(p => {
// indexType.value = "'foo'"
return p.key === indexType.value.replace(/['"]+/g, '');
return indexType.value && p.key === indexType.value.replace(/['"]+/g, '');
});
if (!resolvedType) {
return { name: 'unknown' };
Expand Down