| 84 | ); |
| 85 | |
| 86 | async function addInterfaceProperties(filePath: string, name: string) { |
| 87 | const outputFile = resolve(outputDir, `${name}.json`); |
| 88 | const json = JSON.parse( |
| 89 | await readFile(outputFile, "utf-8") |
| 90 | ) as ImperativeHandleMetadata; |
| 91 | |
| 92 | const sourceFile = ts.createSourceFile( |
| 93 | filePath, |
| 94 | await readFile(filePath, "utf-8"), |
| 95 | ts.ScriptTarget.Latest, |
| 96 | true |
| 97 | ); |
| 98 | const interfaceDeclaration = sourceFile.statements.find( |
| 99 | (statement): statement is ts.InterfaceDeclaration => |
| 100 | ts.isInterfaceDeclaration(statement) && statement.name.text === name |
| 101 | ); |
| 102 | if (!interfaceDeclaration) { |
| 103 | throw Error(`Interface ${name} not found in ${filePath}`); |
| 104 | } |
| 105 | |
| 106 | // Keep the declaration order |
| 107 | const methods: ImperativeHandleMetadata["methods"] = []; |
| 108 | for (const member of interfaceDeclaration.members) { |
| 109 | const memberName = member.name?.getText(sourceFile) ?? ""; |
| 110 | if (ts.isPropertySignature(member) || ts.isGetAccessor(member)) { |
| 111 | const comment = ts |
| 112 | .getJSDocCommentsAndTags(member) |
| 113 | .filter(ts.isJSDoc) |
| 114 | .map((jsDoc) => ts.getTextOfJSDocComment(jsDoc.comment) ?? "") |
| 115 | .join("\n\n"); |
| 116 | if (!comment) { |
| 117 | throw Error(`Missing JSDoc comment for ${name}.${memberName}`); |
| 118 | } |
| 119 | |
| 120 | methods.push({ |
| 121 | description: await parseDescription(comment), |
| 122 | html: await syntaxHighlight(member.getText(sourceFile), "TS"), |
| 123 | name: memberName |
| 124 | }); |
| 125 | } else { |
| 126 | const method = json.methods.find((method) => method.name === memberName); |
| 127 | if (method) { |
| 128 | methods.push(method); |
| 129 | } |
| 130 | } |
| 131 | } |
| 132 | json.methods = methods; |
| 133 | |
| 134 | await writeFile(outputFile, JSON.stringify(json, null, 2)); |
| 135 | } |