Skip to content

Commit

Permalink
defaultPropsHandler ignores non-Property nodes
Browse files Browse the repository at this point in the history
react-docgen threw an error if the default props object literal
contained a spread property.
  • Loading branch information
fkling committed Sep 4, 2015
1 parent 28eabbe commit 3518f67
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 11 deletions.
3 changes: 3 additions & 0 deletions bin/react-docgen.js
Expand Up @@ -62,6 +62,9 @@ function writeError(msg, path) {
process.stderr.write('Error with path "' + path + '": ');
}
process.stderr.write(msg + '\n');
if (msg instanceof Error) {
process.stderr.write(msg.stack + '\n');
}
}

function exitWithError(error) {
Expand Down
26 changes: 25 additions & 1 deletion src/handlers/__tests__/defaultPropsHandler-test.js
Expand Up @@ -73,7 +73,7 @@ describe('defaultPropsHandler', () => {
});

describe('ClassDeclaration with static defaultProps', () => {
it.only('should find prop default values that are literals', () => {
it('should find prop default values that are literals', () => {
var src = `
class Foo {
static defaultProps = {
Expand Down Expand Up @@ -103,4 +103,28 @@ describe('defaultPropsHandler', () => {
});
});

it('should only consider Property nodes, not e.g. spread properties', () => {
var src = `
({
getDefaultProps: function() {
return {
...Foo.bar,
bar: 42,
};
}
})
`;
let definition = parse(src).get('body', 0, 'expression');
expect(() => defaultPropsHandler(documentation, definition))
.not.toThrow();
expect(documentation.descriptors).toEqual({
bar: {
defaultValue: {
value: '42',
computed: false,
},
},
});
});

});
20 changes: 11 additions & 9 deletions src/handlers/defaultPropsHandler.js
Expand Up @@ -68,14 +68,16 @@ export default function defaultPropsHandler(
}

if (types.ObjectExpression.check(defaultPropsPath.node)) {
defaultPropsPath.get('properties').each(function(propertyPath) {
var propDescriptor = documentation.getPropDescriptor(
getPropertyName(propertyPath)
);
var defaultValue = getDefaultValue(propertyPath.get('value'));
if (defaultValue) {
propDescriptor.defaultValue = defaultValue;
}
});
defaultPropsPath.get('properties')
.filter(propertyPath => types.Property.check(propertyPath.node))
.forEach(function(propertyPath) {
var propDescriptor = documentation.getPropDescriptor(
getPropertyName(propertyPath)
);
var defaultValue = getDefaultValue(propertyPath.get('value'));
if (defaultValue) {
propDescriptor.defaultValue = defaultValue;
}
});
}
}
2 changes: 1 addition & 1 deletion src/utils/__tests__/docblock-test.js
Expand Up @@ -38,7 +38,7 @@ describe('docblock', () => {
});
});

describe.only('getDocblock', () => {
describe('getDocblock', () => {
let comment = ['This is a docblock.', 'This is the second line.'];
let source = [
'/**',
Expand Down

0 comments on commit 3518f67

Please sign in to comment.