| 151 | } |
| 152 | |
| 153 | export class FunctionCall implements PythonCode { |
| 154 | private args: PythonCode[]; |
| 155 | public name: string; |
| 156 | public multiLine: boolean; |
| 157 | |
| 158 | constructor( |
| 159 | name: string, |
| 160 | args: PythonCode[] | Record<string, PythonCode>, |
| 161 | multiLine = false, |
| 162 | ) { |
| 163 | this.name = name; |
| 164 | this.multiLine = multiLine; |
| 165 | this.args = objectToArgs(args); |
| 166 | } |
| 167 | |
| 168 | toCode(): string { |
| 169 | if (this.multiLine) { |
| 170 | if (this.args.length === 0) { |
| 171 | return `${this.name}()`; |
| 172 | } |
| 173 | if (this.args.length === 1) { |
| 174 | return `${this.name}(${this.args[0].toCode()})`; |
| 175 | } |
| 176 | return `${this.name}(${indentList(this.args)})`; |
| 177 | } |
| 178 | return `${this.name}(${this.args.map(asString).join(", ")})`; |
| 179 | } |
| 180 | |
| 181 | addArg(...args: PythonCode[]): FunctionCall { |
| 182 | return new FunctionCall(this.name, [...this.args, ...args], this.multiLine); |
| 183 | } |
| 184 | |
| 185 | chain( |
| 186 | name: string, |
| 187 | args: PythonCode[] | Record<string, PythonCode>, |
| 188 | ): FunctionCall { |
| 189 | args = objectToArgs(args); |
| 190 | |
| 191 | // If the function call is multi-line, we need to add a newline to the name |
| 192 | const fullName = this.multiLine |
| 193 | ? `${this.toCode()}\n.${name}` |
| 194 | : `${this.toCode()}.${name}`; |
| 195 | |
| 196 | return new FunctionCall(fullName, args, this.multiLine); |
| 197 | } |
| 198 | } |
| 199 | |
| 200 | function objectToArgs( |
| 201 | obj: Record<string, PythonCode> | PythonCode[], |
nothing calls this directly
no outgoing calls
no test coverage detected
searching dependent graphs…