Skip to content

Commit 3518f67

Browse files
committed
defaultPropsHandler ignores non-Property nodes
react-docgen threw an error if the default props object literal contained a spread property.
1 parent 28eabbe commit 3518f67

File tree

4 files changed

+40
-11
lines changed

4 files changed

+40
-11
lines changed

bin/react-docgen.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,9 @@ function writeError(msg, path) {
6262
process.stderr.write('Error with path "' + path + '": ');
6363
}
6464
process.stderr.write(msg + '\n');
65+
if (msg instanceof Error) {
66+
process.stderr.write(msg.stack + '\n');
67+
}
6568
}
6669

6770
function exitWithError(error) {

src/handlers/__tests__/defaultPropsHandler-test.js

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ describe('defaultPropsHandler', () => {
7373
});
7474

7575
describe('ClassDeclaration with static defaultProps', () => {
76-
it.only('should find prop default values that are literals', () => {
76+
it('should find prop default values that are literals', () => {
7777
var src = `
7878
class Foo {
7979
static defaultProps = {
@@ -103,4 +103,28 @@ describe('defaultPropsHandler', () => {
103103
});
104104
});
105105

106+
it('should only consider Property nodes, not e.g. spread properties', () => {
107+
var src = `
108+
({
109+
getDefaultProps: function() {
110+
return {
111+
...Foo.bar,
112+
bar: 42,
113+
};
114+
}
115+
})
116+
`;
117+
let definition = parse(src).get('body', 0, 'expression');
118+
expect(() => defaultPropsHandler(documentation, definition))
119+
.not.toThrow();
120+
expect(documentation.descriptors).toEqual({
121+
bar: {
122+
defaultValue: {
123+
value: '42',
124+
computed: false,
125+
},
126+
},
127+
});
128+
});
129+
106130
});

src/handlers/defaultPropsHandler.js

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -68,14 +68,16 @@ export default function defaultPropsHandler(
6868
}
6969

7070
if (types.ObjectExpression.check(defaultPropsPath.node)) {
71-
defaultPropsPath.get('properties').each(function(propertyPath) {
72-
var propDescriptor = documentation.getPropDescriptor(
73-
getPropertyName(propertyPath)
74-
);
75-
var defaultValue = getDefaultValue(propertyPath.get('value'));
76-
if (defaultValue) {
77-
propDescriptor.defaultValue = defaultValue;
78-
}
79-
});
71+
defaultPropsPath.get('properties')
72+
.filter(propertyPath => types.Property.check(propertyPath.node))
73+
.forEach(function(propertyPath) {
74+
var propDescriptor = documentation.getPropDescriptor(
75+
getPropertyName(propertyPath)
76+
);
77+
var defaultValue = getDefaultValue(propertyPath.get('value'));
78+
if (defaultValue) {
79+
propDescriptor.defaultValue = defaultValue;
80+
}
81+
});
8082
}
8183
}

src/utils/__tests__/docblock-test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ describe('docblock', () => {
3838
});
3939
});
4040

41-
describe.only('getDocblock', () => {
41+
describe('getDocblock', () => {
4242
let comment = ['This is a docblock.', 'This is the second line.'];
4343
let source = [
4444
'/**',

0 commit comments

Comments
 (0)