File
Blob: types/src/generator/parameter-names.ts
| 1 | // Copyright (c) 2022-2023 Cloudflare, Inc. |
| 2 | // Licensed under the Apache 2.0 license found in the LICENSE file or at: |
| 3 | // https://opensource.org/licenses/Apache-2.0 |
| 4 | |
| 5 | export type ParameterNamesData = Record< |
| 6 | /* fullyQualifiedParentName */ string, |
| 7 | Record</* functionName */ string, string[] | undefined> | undefined |
| 8 | >; |
| 9 | |
| 10 | let data: ParameterNamesData | undefined; |
| 11 | export function installParameterNames(newData: ParameterNamesData): void { |
| 12 | data = newData; |
| 13 | } |
| 14 | |
| 15 | const reservedKeywords = ["function", "number", "string"]; |
| 16 | |
| 17 | export function getParameterName( |
| 18 | fullyQualifiedParentName: string, |
| 19 | functionName: string, |
| 20 | index: number |
| 21 | ): string { |
| 22 | // `constructor` is a reserved property name |
| 23 | if (functionName === "constructor") functionName = `$${functionName}`; |
| 24 | const name = data?.[fullyQualifiedParentName]?.[functionName]?.[index]; |
| 25 | if (name === undefined) return `param${index}`; |
| 26 | if (reservedKeywords.includes(name)) return `$${name}`; |
| 27 | return name; |
| 28 | } |