|
| 1 | +export function sanitizeGodotNameForTs( |
| 2 | + name: string, |
| 3 | + type: "argument" | "property" |
| 4 | +): string { |
| 5 | + if ( |
| 6 | + name === "with" || |
| 7 | + name === "var" || |
| 8 | + name === "class" || |
| 9 | + name === "enum" || |
| 10 | + name === "default" || |
| 11 | + name === "in" |
| 12 | + ) { |
| 13 | + if (type === "argument") { |
| 14 | + return "_" + name |
| 15 | + } else { |
| 16 | + return `"${name}"` |
| 17 | + } |
| 18 | + } |
| 19 | + |
| 20 | + // for enum names in @GlobalScope |
| 21 | + name = name.replace(".", "_") |
| 22 | + |
| 23 | + // Bizarre case in SliderJoint3D.xml |
| 24 | + if (name.includes("/")) { |
| 25 | + if (type === "argument") { |
| 26 | + return name.replace("/", "_") |
| 27 | + } else { |
| 28 | + return `"${name}"` |
| 29 | + } |
| 30 | + } |
| 31 | + |
| 32 | + return name |
| 33 | +} |
| 34 | + |
| 35 | +export function godotTypeToTsType(godotType: string): string { |
| 36 | + if (godotType === "int") { |
| 37 | + return "int" |
| 38 | + } |
| 39 | + |
| 40 | + if (godotType === "float") { |
| 41 | + return "float" |
| 42 | + } |
| 43 | + |
| 44 | + if (godotType === "bool") { |
| 45 | + return "boolean" |
| 46 | + } |
| 47 | + |
| 48 | + if (godotType === "Array") { |
| 49 | + return "any[]" |
| 50 | + } |
| 51 | + |
| 52 | + if (godotType === "PackedScene") { |
| 53 | + return "PackedScene<any>" |
| 54 | + } |
| 55 | + |
| 56 | + if (godotType === "Variant") { |
| 57 | + return "any" |
| 58 | + } |
| 59 | + |
| 60 | + if (godotType === "String") { |
| 61 | + return "string" |
| 62 | + } |
| 63 | + |
| 64 | + if (godotType.startsWith("Transform2D")) { |
| 65 | + return "Transform2D" |
| 66 | + } |
| 67 | + |
| 68 | + if (godotType === "Dictionary") { |
| 69 | + return "Dictionary<any, any>" |
| 70 | + } |
| 71 | + |
| 72 | + if (godotType === "NodePath") { |
| 73 | + // TODO |
| 74 | + return "NodePathType" |
| 75 | + } |
| 76 | + |
| 77 | + if (godotType.match(/^[0-9]+$/)) { |
| 78 | + return "int" |
| 79 | + } |
| 80 | + |
| 81 | + return godotType |
| 82 | +} |
| 83 | + |
| 84 | +export function formatJsDoc(input: string): string { |
| 85 | + if (!input) { |
| 86 | + return `/** No documentation provided. */` |
| 87 | + } |
| 88 | + |
| 89 | + let lines = input.split("\n") |
| 90 | + |
| 91 | + if (lines.length === 1) { |
| 92 | + return `/** ${input} */` |
| 93 | + } |
| 94 | + |
| 95 | + const indentationLength = lines[1].length - lines[1].trimStart().length |
| 96 | + |
| 97 | + // All lines are indented except the first one for some reason. |
| 98 | + lines = [ |
| 99 | + lines[0], |
| 100 | + ...lines.slice(1).map((line) => line.slice(indentationLength)), |
| 101 | + ] |
| 102 | + |
| 103 | + lines = lines.filter((line) => line.trim() !== "") |
| 104 | + |
| 105 | + let result = "/**\n" |
| 106 | + |
| 107 | + let insideCodeBlock = false |
| 108 | + |
| 109 | + for (let line of lines) { |
| 110 | + if (line.includes("[codeblock]")) { |
| 111 | + result += " * @example \n" |
| 112 | + insideCodeBlock = true |
| 113 | + } |
| 114 | + |
| 115 | + if (line.includes("[/codeblock]")) { |
| 116 | + result += " * @summary \n" |
| 117 | + insideCodeBlock = false |
| 118 | + } |
| 119 | + |
| 120 | + if (line.includes("[codeblocks]")) { |
| 121 | + result += " * @example \n" |
| 122 | + insideCodeBlock = true |
| 123 | + } |
| 124 | + |
| 125 | + if (line.includes("[/codeblocks]")) { |
| 126 | + result += " * @summary \n" |
| 127 | + insideCodeBlock = false |
| 128 | + } |
| 129 | + |
| 130 | + line = line.replaceAll("[gdscript]", "") |
| 131 | + line = line.replaceAll("[/gdscript]", "") |
| 132 | + line = line.replaceAll("[csharp]", "") |
| 133 | + line = line.replaceAll("[/csharp]", "") |
| 134 | + line = line.replaceAll("[b]", "**") |
| 135 | + line = line.replaceAll("[/b]", "**") |
| 136 | + line = line.replaceAll("[i]", "**") |
| 137 | + line = line.replaceAll("[/i]", "**") |
| 138 | + line = line.replaceAll("[code]", "`") |
| 139 | + line = line.replaceAll("[/code]", "`") |
| 140 | + line = line.replaceAll("[codeblock]", "") |
| 141 | + line = line.replaceAll("[/codeblock]", "") |
| 142 | + line = line.replaceAll("[codeblocks]", "") |
| 143 | + line = line.replaceAll("[/codeblocks]", "") |
| 144 | + |
| 145 | + // This is the most fun edge case of all time - in RichTextLabel.xml |
| 146 | + line = line.replaceAll("*/", "") |
| 147 | + |
| 148 | + result += " * " + line + "\n" + (!insideCodeBlock ? " *\n" : "") |
| 149 | + } |
| 150 | + |
| 151 | + result += "*/" |
| 152 | + |
| 153 | + return result |
| 154 | +} |
0 commit comments