({
code,
exportName = 'default',
flattened = false
}: BaseArgs)
| 67 | * ``` |
| 68 | */ |
| 69 | export function generateDefinition({ |
| 70 | code, |
| 71 | exportName = 'default', |
| 72 | flattened = false |
| 73 | }: BaseArgs): GeneratedDefinition & (GeneratedType | GeneratedFunction) { |
| 74 | // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition -- Can't access at top level, fix File not found: /var/task/.../tsconfig.json |
| 75 | compilerObject ??= project.getTypeChecker().compilerObject |
| 76 | const sourceFile = project.createSourceFile(DEFAULT_FILENAME, code, { |
| 77 | overwrite: true |
| 78 | }) |
| 79 | const output: ExportedDeclarations[] = [] |
| 80 | for (const [key, declaration] of sourceFile.getExportedDeclarations()) { |
| 81 | if (key === exportName) output.push(...declaration) |
| 82 | } |
| 83 | const declaration = output[0] |
| 84 | if (!declaration) { |
| 85 | throw new Error(`Can't find "${exportName}" declaration`) |
| 86 | } |
| 87 | const declarationFilePath = declaration.getSourceFile().getFilePath() |
| 88 | const filePath = slash(path.relative(CWD, declarationFilePath)) |
| 89 | const symbol = declaration.getSymbolOrThrow() |
| 90 | const { comment, tags } = getCommentAndTags(declaration) |
| 91 | const description = ts.displayPartsToString(comment) |
| 92 | tags.returns &&= replaceJsDocLinks(tags.returns) |
| 93 | |
| 94 | const definition: GeneratedDefinition = { |
| 95 | // Skip adding `filePath` to snapshots on test env, since we have tests on Mac and on Windows, they fail |
| 96 | ...// process.env.NODE_ENV !== 'test' && |
| 97 | (filePath !== DEFAULT_FILENAME && { filePath }), |
| 98 | name: symbol.getName(), |
| 99 | ...(description && { description }), |
| 100 | ...(Object.keys(tags).length && { tags }) |
| 101 | } |
| 102 | |
| 103 | const declarationType = declaration.getType() |
| 104 | const callSignatures = declarationType.getCallSignatures() |
| 105 | const isFunction = callSignatures.length > 0 |
| 106 | |
| 107 | if (!isFunction) { |
| 108 | const entries = declarationType |
| 109 | .getProperties() |
| 110 | .flatMap(prop => |
| 111 | getDocEntry({ |
| 112 | symbol: prop, |
| 113 | declaration, |
| 114 | flattened |
| 115 | }) |
| 116 | ) |
| 117 | .filter(entry => !entry.tags || !('internal' in entry.tags)) |
| 118 | if (!entries.length) { |
| 119 | const typeName = declarationType.getText() |
| 120 | if (typeName === 'any') { |
| 121 | throw new Error( |
| 122 | 'Your type is resolved as "any", it seems like you have an issue in "generateDefinition.code" argument.' |
| 123 | ) |
| 124 | } |
| 125 | throw new Error( |
| 126 | `No properties found, check if your type "${typeName}" exist.` |
no test coverage detected
searching dependent graphs…