-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
151 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
const td = require("typedoc"); | ||
|
||
/** @param {td.Application} app */ | ||
exports.load = function (app) { | ||
app.converter.on( | ||
td.Converter.EVENT_CREATE_DECLARATION, | ||
function (context, reflection) { | ||
expandObjectLikeTypes(context, reflection, app); | ||
}, | ||
); | ||
}; | ||
|
||
/** | ||
* The reflection types affected by this plugin. | ||
*/ | ||
const TYPES_TO_EXPAND = ["mapped", "intersection", "reference"]; | ||
|
||
/** | ||
* @param {td.Context} context | ||
* @param {td.DeclarationReflection} reflection | ||
* @param {td.Application} app | ||
*/ | ||
function expandObjectLikeTypes(context, reflection, app) { | ||
const hasInternalModule = | ||
app.options.getDeclaration("internalModule") !== undefined; | ||
const internalModule = hasInternalModule | ||
? app.options.getValue("internalModule") || "<internal>" | ||
: "<internal>"; | ||
|
||
if ( | ||
reflection.kind !== td.ReflectionKind.TypeAlias || | ||
!reflection.type?.type || | ||
!TYPES_TO_EXPAND.includes(reflection.type.type) || | ||
reflection.parent.originalName === internalModule | ||
) | ||
return; | ||
|
||
const symbol = context.project.getSymbolFromReflection(reflection); | ||
const declaration = symbol?.declarations?.[0]; | ||
|
||
if (!symbol || !declaration) return; // Will probably never happen in practice | ||
const reflectionType = context.checker.getDeclaredTypeOfSymbol(symbol); | ||
|
||
const typeRefl = context | ||
.withScope(reflection) | ||
.createDeclarationReflection( | ||
td.ReflectionKind.TypeLiteral, | ||
undefined, | ||
undefined, | ||
"__type", | ||
); | ||
context.finalizeDeclarationReflection(typeRefl); | ||
const typeContext = context.withScope(typeRefl); | ||
|
||
for (const propertySymbol of reflectionType.getProperties()) { | ||
const propertyType = | ||
propertySymbol && | ||
context.checker.getTypeOfSymbolAtLocation(propertySymbol, declaration); | ||
const resolvedReflection = resolvePropertyReflection( | ||
context, | ||
reflectionType, | ||
propertySymbol, | ||
); | ||
|
||
const element = typeContext.createDeclarationReflection( | ||
td.ReflectionKind.Property, | ||
undefined, | ||
undefined, | ||
propertySymbol.name, | ||
); | ||
|
||
if (resolvedReflection) { | ||
element.comment = resolvedReflection.comment; | ||
element.flags = resolvedReflection.flags; | ||
element.sources = resolvedReflection.sources; | ||
|
||
if (resolvedReflection instanceof td.DeclarationReflection) { | ||
element.defaultValue = resolvedReflection.defaultValue; | ||
} | ||
} | ||
|
||
element.type = typeContext.converter.convertType(typeContext, propertyType); | ||
typeContext.finalizeDeclarationReflection(element); | ||
} | ||
|
||
reflection.type = new td.ReflectionType(typeRefl); | ||
} | ||
|
||
/** | ||
* @param {td.Context} context | ||
* @param {td.TypeScript.Type} objectType | ||
* @param {td.TypeScript.Symbol} propertySymbol | ||
*/ | ||
function resolvePropertyReflection(context, objectType, propertySymbol) { | ||
const resolvedType = context.checker.getPropertyOfType( | ||
objectType, | ||
propertySymbol.name, | ||
); | ||
const resolvedDeclaration = resolvedType?.declarations?.[0]; | ||
const resolvedSymbol = | ||
resolvedDeclaration && context.getSymbolAtLocation(resolvedDeclaration); | ||
|
||
return ( | ||
resolvedSymbol && context.project.getReflectionFromSymbol(resolvedSymbol) | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
const { Converter } = require("typedoc"); | ||
const { | ||
iterateProjectComments, | ||
} = require("../node_modules/typedoc-plugin-include-example/src/iterate-project-comments"); | ||
const { | ||
findExample, | ||
} = require("../node_modules/typedoc-plugin-include-example/src/find-example"); | ||
|
||
function includeExample(comment, example) { | ||
comment.summary.push({ | ||
kind: "code", | ||
text: `\n\n### Example\n\`\`\`ts\n${example}\`\`\``, | ||
}); | ||
|
||
comment.blockTags = comment.blockTags.filter( | ||
(tag) => tag.tag !== "@includeExample", | ||
); | ||
} | ||
|
||
function processComments(context) { | ||
for (const comment of iterateProjectComments(context)) { | ||
const example = findExample(comment); | ||
|
||
if (example !== null) { | ||
const exampleWithPackageImport = example.replace( | ||
/import {(.+)} from "\.\.\/\.\.\/src\/index"/g, | ||
'import {$1} from "clarifai-nodejs"', | ||
); | ||
includeExample(comment, exampleWithPackageImport); | ||
} | ||
} | ||
} | ||
|
||
exports.load = function (application) { | ||
application.converter.on(Converter.EVENT_RESOLVE_BEGIN, processComments); | ||
}; |