MCPcopy
hub / github.com/shuding/nextra / generateDefinition

Function generateDefinition

packages/nextra/src/server/tsdoc/base.ts:69–174  ·  view source on GitHub ↗
({
  code,
  exportName = 'default',
  flattened = false
}: BaseArgs)

Source from the content-addressed store, hash-verified

67 * ```
68 */
69export 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.`

Callers 4

APIDocsFunction · 0.90
getReferenceFunction · 0.90
getReferenceFunction · 0.90
base.test.tsFile · 0.85

Calls 5

getCommentAndTagsFunction · 0.85
replaceJsDocLinksFunction · 0.85
getDocEntryFunction · 0.85
shouldFlattenTypeFunction · 0.85
getFormattedTextFunction · 0.85

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…