Skip to content

Commit 1f75bfd

Browse files
BlaszNoviny
authored andcommitted
Fix default props with nested intersection types (#29)
* Add failing test when using default props with nested intersection prop types * Update getProp to handle nested intersection types
1 parent 1a1cb18 commit 1f75bfd

File tree

3 files changed

+98
-8
lines changed

3 files changed

+98
-8
lines changed

__snapshots__/test.js.snap

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2071,6 +2071,87 @@ Object {
20712071
}
20722072
`;
20732073

2074+
exports[`nested intersection type with default props 1`] = `
2075+
Object {
2076+
"classes": Array [
2077+
Object {
2078+
"kind": "intersection",
2079+
"name": Object {
2080+
"kind": "id",
2081+
"name": "Component",
2082+
"type": null,
2083+
},
2084+
"types": Array [
2085+
Object {
2086+
"kind": "generic",
2087+
"value": Object {
2088+
"kind": "intersection",
2089+
"types": Array [
2090+
Object {
2091+
"kind": "generic",
2092+
"value": Object {
2093+
"kind": "object",
2094+
"members": Array [
2095+
Object {
2096+
"default": Object {
2097+
"kind": "string",
2098+
"value": "baz",
2099+
},
2100+
"key": Object {
2101+
"kind": "id",
2102+
"name": "bar",
2103+
},
2104+
"kind": "property",
2105+
"optional": false,
2106+
"value": Object {
2107+
"kind": "string",
2108+
},
2109+
},
2110+
],
2111+
},
2112+
},
2113+
Object {
2114+
"kind": "object",
2115+
"members": Array [
2116+
Object {
2117+
"key": Object {
2118+
"kind": "id",
2119+
"name": "foo",
2120+
},
2121+
"kind": "property",
2122+
"optional": false,
2123+
"value": Object {
2124+
"kind": "string",
2125+
},
2126+
},
2127+
],
2128+
},
2129+
],
2130+
},
2131+
},
2132+
Object {
2133+
"kind": "object",
2134+
"members": Array [
2135+
Object {
2136+
"key": Object {
2137+
"kind": "id",
2138+
"name": "isDefaultChecked",
2139+
},
2140+
"kind": "property",
2141+
"optional": false,
2142+
"value": Object {
2143+
"kind": "boolean",
2144+
},
2145+
},
2146+
],
2147+
},
2148+
],
2149+
},
2150+
],
2151+
"kind": "program",
2152+
}
2153+
`;
2154+
20742155
exports[`new expression 1`] = `
20752156
Object {
20762157
"classes": Array [

index.js

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -58,16 +58,9 @@ const resolveFromGeneric = type => {
5858

5959
const getProp = (props, property) => {
6060
let prop;
61-
6261
if (props.kind === 'intersection') {
6362
props.types.forEach(pr => {
64-
if (pr.kind === 'generic') {
65-
if (pr.value.kind === 'object') {
66-
prop = getPropFromObject(pr.value, property) || prop;
67-
}
68-
} else if (pr.kind === 'object') {
69-
prop = getPropFromObject(pr, property) || prop;
70-
}
63+
prop = getProp(resolveFromGeneric(pr), property) || prop;
7164
});
7265
} else if (props.kind === 'object') {
7366
prop = getPropFromObject(props, property);

test.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -218,6 +218,22 @@ const TESTS = [
218218
}
219219
`
220220
},
221+
{
222+
name: 'nested intersection type with default props',
223+
typeSystem: 'flow',
224+
code: `
225+
type BaseProps = { bar: string }
226+
type Props = BaseProps & { foo: string }
227+
228+
class Component extends React.Component<Props & {
229+
isDefaultChecked: boolean,
230+
}> {
231+
static defaultProps = {
232+
bar: 'baz',
233+
}
234+
}
235+
`
236+
},
221237
{
222238
name: 'with spread in type annotation',
223239
typeSystem: 'flow',

0 commit comments

Comments
 (0)