Skip to content

Commit 4b8b721

Browse files
committed
fix: Handle ObjectTypeSpreadProperties which are not resolvable
1 parent 3e6ed53 commit 4b8b721

File tree

3 files changed

+49
-8
lines changed

3 files changed

+49
-8
lines changed

src/utils/__tests__/__snapshots__/getFlowType-test.ts.snap

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,3 +120,29 @@ Object {
120120
"type": "object",
121121
}
122122
`;
123+
124+
exports[`getFlowType handles unresolved ObjectTypeSpreadProperty 1`] = `
125+
Object {
126+
"name": "signature",
127+
"raw": "{| apple: string, banana: string, ...MyType |}",
128+
"signature": Object {
129+
"properties": Array [
130+
Object {
131+
"key": "apple",
132+
"value": Object {
133+
"name": "string",
134+
"required": true,
135+
},
136+
},
137+
Object {
138+
"key": "banana",
139+
"value": Object {
140+
"name": "string",
141+
"required": true,
142+
},
143+
},
144+
],
145+
},
146+
"type": "object",
147+
}
148+
`;

src/utils/__tests__/getFlowType-test.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1296,6 +1296,18 @@ describe('getFlowType', () => {
12961296
expect(getFlowType(typePath, null, mockImporter)).toMatchSnapshot();
12971297
});
12981298

1299+
it('handles unresolved ObjectTypeSpreadProperty', () => {
1300+
const typePath = statement(`
1301+
var x: {| apple: string, banana: string, ...MyType |} = 2;
1302+
`)
1303+
.get('declarations', 0)
1304+
.get('id')
1305+
.get('typeAnnotation')
1306+
.get('typeAnnotation');
1307+
1308+
expect(getFlowType(typePath, null, mockImporter)).toMatchSnapshot();
1309+
});
1310+
12991311
it('handles nested ObjectTypeSpreadProperty', () => {
13001312
const typePath = statement(`
13011313
var x: {| apple: string, banana: string, ...BreakfastFruits |} = 2;

src/utils/getFlowType.ts

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -221,18 +221,21 @@ function handleObjectTypeAnnotation(
221221
});
222222
} else if (t.ObjectTypeSpreadProperty.check(param.node)) {
223223
let spreadObject = resolveToValue(param.get('argument'), importer);
224-
if (t.GenericTypeAnnotation.check(spreadObject.value)) {
224+
if (t.GenericTypeAnnotation.check(spreadObject.node)) {
225225
const typeAlias = resolveToValue(spreadObject.get('id'), importer);
226-
if (t.ObjectTypeAnnotation.check(typeAlias.get('right').value)) {
226+
if (t.ObjectTypeAnnotation.check(typeAlias.get('right').node)) {
227227
spreadObject = resolveToValue(typeAlias.get('right'), importer);
228228
}
229229
}
230-
const props = handleObjectTypeAnnotation(
231-
spreadObject,
232-
typeParams,
233-
importer,
234-
) as ObjectSignatureType;
235-
type.signature.properties.push(...props.signature.properties);
230+
231+
if (t.ObjectTypeAnnotation.check(spreadObject.node)) {
232+
const props = handleObjectTypeAnnotation(
233+
spreadObject,
234+
typeParams,
235+
importer,
236+
) as ObjectSignatureType;
237+
type.signature.properties.push(...props.signature.properties);
238+
}
236239
}
237240
});
238241

0 commit comments

Comments
 (0)