Skip to content

Commit

Permalink
fix(typings): add quoted field names support
Browse files Browse the repository at this point in the history
  • Loading branch information
Heymdall committed Mar 28, 2019
1 parent a795f4e commit d987c1a
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 5 deletions.
6 changes: 4 additions & 2 deletions typings/__examples__/a.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,8 @@ export default class A extends React.Component {
* Even deeper documentation
*/
name: PropTypes.string,
size: PropTypes.number
size: PropTypes.number,
'quoted-field': PropTypes.string
})
}),
/**
Expand All @@ -76,7 +77,8 @@ export default class A extends React.Component {
* @returns {string|number}
*/
onClick: PropTypes.func,
onChange: PropTypes.func
onChange: PropTypes.func,
'9validPropName': PropTypes.number
};

render() {
Expand Down
4 changes: 3 additions & 1 deletion typings/__tests__/__snapshots__/index.tests.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@ export type AOptionalObjectWithShapeSubShapeShapeType = {
* Even deeper documentation
*/
name?: string;
size?: number
size?: number;
'quoted-field'?: string
};
export type AOptionalObjectWithShapeShapeType = {
color?: string;
Expand Down Expand Up @@ -101,6 +102,7 @@ export type AProps = DeepReadonlyObject<{
*/
onClick?: (stringParam?: string, count?: number, event?: React.MouseEvent<any>, anotherEvent?: React.KeyboardEvent<any>, element?: HTMLDivElement) => AOnClickReturnFieldType;
onChange?: Function;
'9validPropName'?: number;
}>;
Expand Down
13 changes: 11 additions & 2 deletions typings/stringify-component-definition.js
Original file line number Diff line number Diff line change
Expand Up @@ -117,11 +117,20 @@ function stringifyFunctionDefinition(type, componentName, propName, typeRefs, us
return `(${paramsTypes.join(', ')}) ${useArrowNotation ? '=>' : ':'} ${returnType}`;
}

function stringifyFieldName(fieldName) {
if (/^[A-z][A-z0-9]+$/.test(fieldName)) {
return fieldName;
}

return `'${fieldName}'`;
}

function stringifyField(fieldName, type, componentName, propName, typeRefs) {
const typeDescription = stringifyType(type, componentName, `${propName}${upperCamelCase(fieldName)}`, typeRefs);

return (
stringifyDescription(type.description, type.docblock) + // eslint-disable-line prefer-template
`${fieldName}${type.required ? '' : '?'}: ${typeDescription}`
`${stringifyFieldName(fieldName)}${type.required ? '' : '?'}: ${typeDescription}`
);
}

Expand Down Expand Up @@ -178,7 +187,7 @@ function stringifyComponentDefinition(info) {
const typeDef = stringifyType(type, info.displayName, propName, typeRefs);
const descriptionString = stringifyDescription(description, docblock);
return `${descriptionString}${propName}${required ? '' : '?'}: ${typeDef};\n`;
return `${descriptionString}${stringifyFieldName(propName)}${required ? '' : '?'}: ${typeDef};\n`;
}).join('')}
}>;
`
Expand Down

0 comments on commit d987c1a

Please sign in to comment.