Skip to content

Commit b8681fb

Browse files
nathanmarksfkling
authored andcommitted
Check for import declaration type when getting default values, resolves #63 (#99)
1 parent 9325439 commit b8681fb

File tree

2 files changed

+27
-2
lines changed

2 files changed

+27
-2
lines changed

src/handlers/__tests__/defaultPropsHandler-test.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,27 @@ describe('defaultPropsHandler', () => {
8686
`;
8787
test(parse(src).get('body', 0));
8888
});
89+
90+
it('should find prop default values that are imported variables', () => {
91+
var src = `
92+
import ImportedComponent from './ImportedComponent';
93+
94+
class Foo {
95+
static defaultProps = {
96+
foo: ImportedComponent,
97+
};
98+
}
99+
`;
100+
defaultPropsHandler(documentation, parse(src).get('body', 1));
101+
expect(documentation.descriptors).toEqual({
102+
foo: {
103+
defaultValue: {
104+
value: 'ImportedComponent',
105+
computed: true,
106+
},
107+
},
108+
});
109+
});
89110
});
90111

91112
describe('ClassExpression with static defaultProps', () => {

src/handlers/defaultPropsHandler.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,12 @@ function getDefaultValue(path) {
2727
defaultValue = node.raw;
2828
} else {
2929
path = resolveToValue(path);
30-
node = path.node;
31-
defaultValue = printValue(path);
30+
if (types.ImportDeclaration.check(path.node)) {
31+
defaultValue = node.name;
32+
} else {
33+
node = path.node;
34+
defaultValue = printValue(path);
35+
}
3236
}
3337
if (typeof defaultValue !== 'undefined') {
3438
return {

0 commit comments

Comments
 (0)