import ts from "typescript" ; import { Primitive , PropType } from "../analyze/extractPropTypes/types" ; import { ComponentInfo } from "../types" ; import { splitWords , upperCaseFirstLetter } from "../utils" ; function createLiteral ( value ) { if ( value === undefined ) { return ts . createIdentifier ( "undefined" ) ; } else if ( value === null ) { return ts . createNull ( ) ; } else if ( typeof value === "string" ) { return ts . createStringLiteral ( value ) ; } else if ( typeof value === "boolean" ) { return value ? ts . createTrue ( ) : ts . createFalse ( ) ; } else if ( typeof value === "number" ) { return ts . createNumericLiteral ( value + "" ) ; } else if ( Array . isArray ( value ) ) { return ts . createArrayLiteral ( value . map ( item => createLiteral ( item ) ) , false ) ; } throw new Error ( `Unrecognized value type ${ value }` ) ; } function createPropertyControlExpression ( prop ) { const createProp = ( propertyControlName , expr ) => ts . createPropertyAssignment ( propertyControlName , createLiteral ( expr ) ) ; const properties = [ ] ; if ( prop . type === "unsupported" ) { return undefined ; } properties . push ( createProp ( "title" , upperCaseFirstLetter ( splitWords ( prop . name ) . map ( x => x . toLowerCase ( ) ) . join ( " " ) ) ) ) ; properties . push ( ts . createPropertyAssignment ( ts . createIdentifier ( "type" ) , ts . createPropertyAccess ( ts . createIdentifier ( "ControlType" ) , ts . createIdentifier ( upperCaseFirstLetter ( prop . type ) ) ) ) ) ; if ( "defaultValue" in prop && typeof prop . defaultValue !== "undefined" && prop . defaultValue !== null ) { properties . push ( createProp ( "defaultValue" , prop . defaultValue ) ) ; } if ( prop . type === "enum" ) { properties . push ( createProp ( "options" , prop . possibleValues ) ) ; const optionTitles = prop . possibleValues . map ( val => splitWords ( val + "" ) . join ( " " ) ) ; properties . push ( createProp ( "optionTitles" , optionTitles ) ) ; } else if ( prop . type === "array" ) { } return ts . createObjectLiteral ( properties , true ) ; } /**
 * Returns an array of `ts.PropertyAssignment`'s
 *
 * Example:
 *
 * ```ts
 * //                              returns these properties as an array
 * //                          ↓                                          ↓
 * addPropertyControls(Button, { foo: {type: string}, bar: {type: string} })
 * ```
 */ export function createPropertyControlPropertyAssignments ( comp ) { return comp . propTypes . filter ( prop => prop . type !== "unsupported" ) . map ( prop => { return ts . createPropertyAssignment ( prop . name , createPropertyControlExpression ( prop ) ! ) ; } ) ; } /**
 * Creates a TS expression for the second argument of an `addPropertyControls` statement:
 *
 * ```ts
 * //                            returns this object
 * //                          ↓                     ↓
 * addPropertyControls(Button, { foo: {type: string} })
 * ```
 */ export function createPropertyControlObjectExpression ( comp ) { return ts . createObjectLiteral ( createPropertyControlPropertyAssignments ( comp ) , true ) ; }