Skip to content

Commit

Permalink
fix: Handle ObjectTypeSpreadProperties which are not resolvable
Browse files Browse the repository at this point in the history
  • Loading branch information
danez committed May 28, 2022
1 parent 3e6ed53 commit 4b8b721
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 8 deletions.
26 changes: 26 additions & 0 deletions src/utils/__tests__/__snapshots__/getFlowType-test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -120,3 +120,29 @@ Object {
"type": "object",
}
`;

exports[`getFlowType handles unresolved ObjectTypeSpreadProperty 1`] = `
Object {
"name": "signature",
"raw": "{| apple: string, banana: string, ...MyType |}",
"signature": Object {
"properties": Array [
Object {
"key": "apple",
"value": Object {
"name": "string",
"required": true,
},
},
Object {
"key": "banana",
"value": Object {
"name": "string",
"required": true,
},
},
],
},
"type": "object",
}
`;
12 changes: 12 additions & 0 deletions src/utils/__tests__/getFlowType-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1296,6 +1296,18 @@ describe('getFlowType', () => {
expect(getFlowType(typePath, null, mockImporter)).toMatchSnapshot();
});

it('handles unresolved ObjectTypeSpreadProperty', () => {
const typePath = statement(`
var x: {| apple: string, banana: string, ...MyType |} = 2;
`)
.get('declarations', 0)
.get('id')
.get('typeAnnotation')
.get('typeAnnotation');

expect(getFlowType(typePath, null, mockImporter)).toMatchSnapshot();
});

it('handles nested ObjectTypeSpreadProperty', () => {
const typePath = statement(`
var x: {| apple: string, banana: string, ...BreakfastFruits |} = 2;
Expand Down
19 changes: 11 additions & 8 deletions src/utils/getFlowType.ts
Original file line number Diff line number Diff line change
Expand Up @@ -221,18 +221,21 @@ function handleObjectTypeAnnotation(
});
} else if (t.ObjectTypeSpreadProperty.check(param.node)) {
let spreadObject = resolveToValue(param.get('argument'), importer);
if (t.GenericTypeAnnotation.check(spreadObject.value)) {
if (t.GenericTypeAnnotation.check(spreadObject.node)) {
const typeAlias = resolveToValue(spreadObject.get('id'), importer);
if (t.ObjectTypeAnnotation.check(typeAlias.get('right').value)) {
if (t.ObjectTypeAnnotation.check(typeAlias.get('right').node)) {
spreadObject = resolveToValue(typeAlias.get('right'), importer);
}
}
const props = handleObjectTypeAnnotation(
spreadObject,
typeParams,
importer,
) as ObjectSignatureType;
type.signature.properties.push(...props.signature.properties);

if (t.ObjectTypeAnnotation.check(spreadObject.node)) {
const props = handleObjectTypeAnnotation(
spreadObject,
typeParams,
importer,
) as ObjectSignatureType;
type.signature.properties.push(...props.signature.properties);
}
}
});

Expand Down

0 comments on commit 4b8b721

Please sign in to comment.