( componentName: string, props: ParsedProp[], interactiveState: InteractiveState[], )
| 133 | } |
| 134 | |
| 135 | function generateComponentJsx( |
| 136 | componentName: string, |
| 137 | props: ParsedProp[], |
| 138 | interactiveState: InteractiveState[], |
| 139 | ): string { |
| 140 | const stateMap = new Map<string, InteractiveState>(); |
| 141 | for (const s of interactiveState) { |
| 142 | stateMap.set(s.boolPropName, s); |
| 143 | stateMap.set(s.callbackPropName, s); |
| 144 | } |
| 145 | |
| 146 | const requiredProps = props.filter(p => !p.optional); |
| 147 | if (requiredProps.length === 0) { |
| 148 | return `<${componentName} />`; |
| 149 | } |
| 150 | |
| 151 | const propAssignments: string[] = []; |
| 152 | for (const prop of requiredProps) { |
| 153 | const state = stateMap.get(prop.name); |
| 154 | if (state && prop.isBoolean) { |
| 155 | propAssignments.push(`${prop.name}={this.state.${state.stateName}}`); |
| 156 | } else if (state && prop.isCallback) { |
| 157 | propAssignments.push( |
| 158 | `${prop.name}={this.${state.callbackPropName}Handler}`, |
| 159 | ); |
| 160 | } else { |
| 161 | propAssignments.push(`${prop.name}={${sampleValueForType(prop.typeString)}}`); |
| 162 | } |
| 163 | } |
| 164 | |
| 165 | if (propAssignments.length <= 2) { |
| 166 | return `<${componentName} ${propAssignments.join(' ')} />`; |
| 167 | } |
| 168 | |
| 169 | const indentedProps = propAssignments.map(p => ` ${p}`).join('\n'); |
| 170 | return `<${componentName}\n${indentedProps}\n />`; |
| 171 | } |
| 172 | |
| 173 | // --------------------------------------------------------------------------- |
| 174 | // BUILD.bazel |
no test coverage detected