Menu. It has a few sub-components. We are using the "Compound Components Pattern" to export these components. The Menu component is the parent component. Then we have Menu.Item, Menu.ItemSeparator, Menu.Panel, and Menu.Reference components. This is how the export looks like:Menu component is used in a few places. For example:Menu is used. For example:<Menu> component is referenced 325 times in 83 files. This is a large code refactor. It is tedious and error prone. Can we use LLMs to help us with this?Menu component to export the components using the new pattern. Then I ran tsc to produce a list of files with errors. Then I narrowed down to the files that mention Menu. Then for every file, I applied the ChatGPT generated suggestion. Let's go through this step by step.Menu componentMenu component:tsc to produce a list of files with errorsMenutsc output:Menu.createPrompt function is used to create the prompt for ChatGPT. It looks like this:normalizeResponse function is used to strip the ``` from the response. It looks like this:formatFile function is used to format the file using eslint. It is intentionally called after the writeFile function. This is because the LLM generated code sometimes is not syntactically correct (see Gotchas). However, the suggestions it generates might still be valuable. So we first write the file, then we format it. If it fails, we just ignore it.tsc againtsc again to see if there are any errors. There were none. We were done.```. I tried adding Do not wrap the output instruction and similar, but it didn't help. Sometimes it would even prepend text, e.g.normalizeResponse function to strip it.Do not truncate the response instruction and it seems to have dramatically reduced instances like this.Do not add comments instructions reduced this, but didn't eliminate it.Do not add comments instruction sometimes caused ChatGPT to strip existing comments, e.g.Posted Jun 29, 2023
I'v successfully utilized a LLM to automate and streamline the refactoring process, enhancing its efficiency and reliability.