Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle computed properties correctly and do not fail generation #340

Merged
merged 3 commits into from Apr 17, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 4 additions & 0 deletions .eslintrc.js
Expand Up @@ -27,5 +27,9 @@ module.exports = {
'no-unused-vars': 'off',
},
},
{
files: 'src/**/__tests__/*-test.js',
env: { jest: true },
},
],
};
21 changes: 21 additions & 0 deletions src/__tests__/__snapshots__/main-test.js.snap
Expand Up @@ -1027,3 +1027,24 @@ Object {
},
}
`;

exports[`main fixtures processes component "component_20.js" without errors 1`] = `
Object {
"description": "",
"displayName": "Button",
"methods": Array [],
"props": Object {
"@computed#children": Object {
"defaultValue": Object {
"computed": false,
"value": "\\"default\\"",
},
"description": "This is a test",
"required": false,
"type": Object {
"name": "string",
},
},
},
}
`;
17 changes: 17 additions & 0 deletions src/__tests__/fixtures/component_20.js
@@ -0,0 +1,17 @@
import React from 'react';
import PropTypes from 'prop-types';

const Button = () => (
<div></div>
);

Button.propTypes = {
/** This is a test */
[children]: PropTypes.string.isRequired,
};

Button.defaultProps = {
[children]: "default",
};

export default Button;
@@ -0,0 +1,25 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`componentMethodsHandler should handle and ignore computed methods 1`] = `
Array [
Object {
"docblock": "The foo method",
"modifiers": Array [],
"name": "@computed#foo",
"params": Array [
Object {
"name": "bar",
"optional": undefined,
"type": Object {
"name": "number",
},
},
],
"returns": Object {
"type": Object {
"name": "number",
},
},
},
]
`;
238 changes: 238 additions & 0 deletions src/handlers/__tests__/__snapshots__/defaultPropsHandler-test.js.snap
@@ -0,0 +1,238 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`defaultPropsHandler ClassDeclaration with static defaultProps should find prop default values that are imported variables 1`] = `
Object {
"foo": Object {
"defaultValue": Object {
"computed": true,
"value": "ImportedComponent",
},
},
}
`;

exports[`defaultPropsHandler ClassDeclaration with static defaultProps should find prop default values that are literals 1`] = `
Object {
"abc": Object {
"defaultValue": Object {
"computed": false,
"value": "{xyz: abc.def, 123: 42}",
},
},
"bar": Object {
"defaultValue": Object {
"computed": false,
"value": "42",
},
},
"baz": Object {
"defaultValue": Object {
"computed": false,
"value": "[\\"foo\\", \\"bar\\"]",
},
},
"foo": Object {
"defaultValue": Object {
"computed": false,
"value": "\\"bar\\"",
},
},
}
`;

exports[`defaultPropsHandler ClassExpression with static defaultProps should find prop default values that are literals 1`] = `
Object {
"abc": Object {
"defaultValue": Object {
"computed": false,
"value": "{xyz: abc.def, 123: 42}",
},
},
"bar": Object {
"defaultValue": Object {
"computed": false,
"value": "42",
},
},
"baz": Object {
"defaultValue": Object {
"computed": false,
"value": "[\\"foo\\", \\"bar\\"]",
},
},
"foo": Object {
"defaultValue": Object {
"computed": false,
"value": "\\"bar\\"",
},
},
}
`;

exports[`defaultPropsHandler Functional components with default params should find default props that are literals 1`] = `
Object {
"abc": Object {
"defaultValue": Object {
"computed": false,
"value": "{xyz: abc.def, 123: 42}",
},
},
"bar": Object {
"defaultValue": Object {
"computed": false,
"value": "42",
},
},
"baz": Object {
"defaultValue": Object {
"computed": false,
"value": "[\\"foo\\", \\"bar\\"]",
},
},
"foo": Object {
"defaultValue": Object {
"computed": false,
"value": "\\"bar\\"",
},
},
}
`;

exports[`defaultPropsHandler Functional components with default params should find prop default values that are imported variables 1`] = `
Object {
"foo": Object {
"defaultValue": Object {
"computed": true,
"value": "ImportedComponent",
},
},
}
`;

exports[`defaultPropsHandler Functional components with default params should override with defaultProps if available 1`] = `
Object {
"abc": Object {
"defaultValue": Object {
"computed": false,
"value": "{xyz: abc.def, 123: 42}",
},
},
"bar": Object {
"defaultValue": Object {
"computed": false,
"value": "42",
},
},
"baz": Object {
"defaultValue": Object {
"computed": false,
"value": "[\\"foo\\", \\"bar\\"]",
},
},
"foo": Object {
"defaultValue": Object {
"computed": false,
"value": "\\"bar\\"",
},
},
}
`;

exports[`defaultPropsHandler Functional components with default params should work with aliases 1`] = `
Object {
"abc": Object {
"defaultValue": Object {
"computed": false,
"value": "{xyz: abc.def, 123: 42}",
},
},
"bar": Object {
"defaultValue": Object {
"computed": false,
"value": "42",
},
},
"baz": Object {
"defaultValue": Object {
"computed": false,
"value": "[\\"foo\\", \\"bar\\"]",
},
},
"foo": Object {
"defaultValue": Object {
"computed": false,
"value": "\\"bar\\"",
},
},
}
`;

exports[`defaultPropsHandler Functional components with default params should work with no defaults 1`] = `Object {}`;

exports[`defaultPropsHandler ObjectExpression handles computed properties 1`] = `
Object {
"@computed#bar": Object {
"defaultValue": Object {
"computed": false,
"value": "42",
},
},
"foo": Object {
"defaultValue": Object {
"computed": false,
"value": "\\"bar\\"",
},
},
}
`;

exports[`defaultPropsHandler ObjectExpression ignores complex computed properties 1`] = `
Object {
"foo": Object {
"defaultValue": Object {
"computed": false,
"value": "\\"bar\\"",
},
},
}
`;

exports[`defaultPropsHandler ObjectExpression should find prop default values that are literals 1`] = `
Object {
"abc": Object {
"defaultValue": Object {
"computed": false,
"value": "{xyz: abc.def, 123: 42}",
},
},
"bar": Object {
"defaultValue": Object {
"computed": false,
"value": "42",
},
},
"baz": Object {
"defaultValue": Object {
"computed": false,
"value": "[\\"foo\\", \\"bar\\"]",
},
},
"foo": Object {
"defaultValue": Object {
"computed": false,
"value": "\\"bar\\"",
},
},
}
`;

exports[`defaultPropsHandler should only consider Property nodes, not e.g. spread properties 1`] = `
Object {
"bar": Object {
"defaultValue": Object {
"computed": false,
"value": "42",
},
},
}
`;
50 changes: 50 additions & 0 deletions src/handlers/__tests__/__snapshots__/flowTypeHandler-test.js.snap
@@ -1,5 +1,55 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`flowTypeHandler TypeAlias class definition for flow <0.53 ignores hash map entry 1`] = `
Object {
"bar": Object {
"description": "",
"flowType": Object {},
"required": false,
},
}
`;

exports[`flowTypeHandler TypeAlias class definition for flow >=0.53 with State ignores hash map entry 1`] = `
Object {
"bar": Object {
"description": "",
"flowType": Object {},
"required": false,
},
}
`;

exports[`flowTypeHandler TypeAlias class definition for flow >=0.53 without State ignores hash map entry 1`] = `
Object {
"bar": Object {
"description": "",
"flowType": Object {},
"required": false,
},
}
`;

exports[`flowTypeHandler TypeAlias class definition with inline props ignores hash map entry 1`] = `
Object {
"bar": Object {
"description": "",
"flowType": Object {},
"required": false,
},
}
`;

exports[`flowTypeHandler TypeAlias stateless component ignores hash map entry 1`] = `
Object {
"bar": Object {
"description": "",
"flowType": Object {},
"required": false,
},
}
`;

exports[`flowTypeHandler does support utility types inline 1`] = `
Object {
"foo": Object {
Expand Down