* Gets all class-like Dgeni documents from the given type. e.g. intersection types of * multiple classes will result in multiple Dgeni API documents for each class.
( type: ts.Type, baseDoc: ClassLikeExportDoc, exportSymbolsToDocsMap: Map<ts.Symbol, ClassLikeExportDoc>, )
| 59 | * multiple classes will result in multiple Dgeni API documents for each class. |
| 60 | */ |
| 61 | function getClassLikeDocsFromType( |
| 62 | type: ts.Type, |
| 63 | baseDoc: ClassLikeExportDoc, |
| 64 | exportSymbolsToDocsMap: Map<ts.Symbol, ClassLikeExportDoc>, |
| 65 | ): ClassLikeExportDoc[] { |
| 66 | let aliasSymbol: ts.Symbol | undefined = undefined; |
| 67 | let symbol: ts.Symbol = type.symbol; |
| 68 | const typeChecker = baseDoc.typeChecker; |
| 69 | |
| 70 | // Symbols can be aliases of the declaration symbol. e.g. in named import |
| 71 | // specifiers. We need to resolve the aliased symbol back to the declaration symbol. |
| 72 | if (symbol && (symbol.flags & ts.SymbolFlags.Alias) !== 0) { |
| 73 | aliasSymbol = symbol; |
| 74 | symbol = typeChecker.getAliasedSymbol(symbol); |
| 75 | } |
| 76 | |
| 77 | // Intersection types are commonly used in TypeScript mixins to express the |
| 78 | // class augmentation. e.g. "BaseClass & CanColor". |
| 79 | if (type.isIntersection()) { |
| 80 | return type.types.reduce( |
| 81 | (res, t) => [...res, ...getClassLikeDocsFromType(t, baseDoc, exportSymbolsToDocsMap)], |
| 82 | [] as ClassLikeExportDoc[], |
| 83 | ); |
| 84 | } else if (symbol) { |
| 85 | // If the given symbol has already been registered within Dgeni, we use the |
| 86 | // existing symbol instead of creating a new one. The dgeni typescript package |
| 87 | // keeps track of all exported symbols and their corresponding docs. See: |
| 88 | // dgeni-packages/blob/master/typescript/src/processors/linkInheritedDocs.ts |
| 89 | if (exportSymbolsToDocsMap.has(symbol)) { |
| 90 | return [exportSymbolsToDocsMap.get(symbol)!]; |
| 91 | } |
| 92 | let createdDoc: InheritanceCreatedClassLikeDoc | null = null; |
| 93 | if ((symbol.flags & ts.SymbolFlags.Class) !== 0) { |
| 94 | createdDoc = new ClassExportDoc(baseDoc.host, baseDoc.moduleDoc, symbol, aliasSymbol); |
| 95 | } else if ((symbol.flags & ts.SymbolFlags.Interface) !== 0) { |
| 96 | createdDoc = new InterfaceExportDoc(baseDoc.host, baseDoc.moduleDoc, symbol, aliasSymbol); |
| 97 | } |
| 98 | |
| 99 | if (createdDoc) { |
| 100 | // Mark the created document. This allows us to distinguish between documents which |
| 101 | // have been resolved by Dgeni automatically, and docs which are manually resolved. |
| 102 | createdDoc._inheritanceCreated = true; |
| 103 | // If a new document has been created, add it to the shared symbol. |
| 104 | exportSymbolsToDocsMap.set(aliasSymbol || symbol, createdDoc); |
| 105 | return [createdDoc]; |
| 106 | } |
| 107 | } |
| 108 | return []; |
| 109 | } |
no test coverage detected
searching dependent graphs…