Skip to content

Commit

Permalink
Handle computed properties correctly and do not fail generation (#340)
Browse files Browse the repository at this point in the history
* Handle computed properties correctly and do not fail generation

* Resolve identifiers and correctly use string/number literals

* Fix lint
  • Loading branch information
danez committed Apr 17, 2019
1 parent 8eca876 commit ff33fc8
Show file tree
Hide file tree
Showing 25 changed files with 824 additions and 89 deletions.
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

0 comments on commit ff33fc8

Please sign in to comment.