generated from RebeccaStevens/template-typescript-node-package
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
1 parent
4dbaa8a
commit 8806846
Showing
8 changed files
with
240 additions
and
25 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
issuehunt: RebeccaStevens/typedoc-plugin-require-tags | ||
issuehunt: RebeccaStevens/typedoc-plugin-custom-validation | ||
ko_fi: rebeccastevens | ||
custom: https://github.com/RebeccaStevens/typedoc-plugin-require-tags/blob/main/DONATIONS.md | ||
custom: https://github.com/RebeccaStevens/typedoc-plugin-custom-validation/blob/main/DONATIONS.md |
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
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 |
---|---|---|
|
@@ -2,5 +2,6 @@ node_modules/ | |
|
||
/coverage/ | ||
/dist/ | ||
/docs/ | ||
|
||
*.log |
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,119 @@ | ||
/* eslint-disable eslint-comments/disable-enable-pair */ | ||
/* eslint-disable unicorn/no-empty-file */ | ||
import type { ProjectReflection, Reflection } from "typedoc"; | ||
import { Application, ParameterType, ReflectionKind } from "typedoc"; | ||
|
||
// Write me | ||
export type CustomValidationOptions = { | ||
byKind: ByKindEntry[]; | ||
}; | ||
|
||
export type ByKindEntry = { | ||
kinds: keyof typeof ReflectionKind | Array<keyof typeof ReflectionKind>; | ||
tags?: string | string[]; | ||
summary?: boolean; | ||
}; | ||
|
||
export function load(app: Readonly<Application>) { | ||
app.options.addDeclaration({ | ||
name: "customValidation", | ||
help: "The configuration object of the require-tags plugin.", | ||
type: ParameterType.Object, | ||
}); | ||
|
||
app.on( | ||
Application.EVENT_VALIDATE_PROJECT, | ||
(project: Readonly<ProjectReflection>) => { | ||
const customValidationOptions = app.options.getValue( | ||
"customValidation" | ||
) as CustomValidationOptions; | ||
|
||
let m_kinds = customValidationOptions.byKind | ||
.flatMap((by) => by.kinds) | ||
.map((kind) => ReflectionKind[kind]) | ||
.reduce((p, c) => p | c); | ||
|
||
const reflectionKindReplacements: Array< | ||
[oldKind: number, newKind: number] | ||
> = [ | ||
[ReflectionKind.FunctionOrMethod, ReflectionKind.CallSignature], | ||
[ReflectionKind.Constructor, ReflectionKind.ConstructorSignature], | ||
[ | ||
ReflectionKind.Accessor, | ||
ReflectionKind.GetSignature | ReflectionKind.SetSignature, | ||
], | ||
]; | ||
|
||
for (const [oldKind, newKind] of reflectionKindReplacements) { | ||
m_kinds = (m_kinds | newKind) & ~oldKind; | ||
} | ||
|
||
type Requirements = { tags: string[]; summary: boolean }; | ||
|
||
const requirementsByKind = new Map<number, Requirements>( | ||
customValidationOptions.byKind.flatMap(({ kinds, tags, summary }) => | ||
(Array.isArray(kinds) ? kinds : [kinds]).map( | ||
(kindString): [number, Requirements] => { | ||
const kind = ReflectionKind[kindString]; | ||
const realKind = | ||
reflectionKindReplacements.find( | ||
([oldKind]) => (oldKind & kind) !== 0 | ||
)?.[1] ?? kind; | ||
|
||
return [ | ||
realKind, | ||
{ | ||
tags: | ||
tags === undefined | ||
? [] | ||
: Array.isArray(tags) | ||
? tags | ||
: [tags], | ||
summary: summary ?? false, | ||
}, | ||
]; | ||
} | ||
) | ||
) | ||
); | ||
|
||
const reflections = project.getReflectionsByKind(m_kinds); | ||
const seen = new Set<Reflection>(); | ||
|
||
for (const reflection of reflections) { | ||
if (seen.has(reflection)) { | ||
continue; | ||
} | ||
seen.add(reflection); | ||
|
||
if (!reflection.hasComment() || reflection.comment!.isEmpty()) { | ||
app.logger.warn( | ||
`${reflection.getFriendlyFullName()} does not have any documentation.` | ||
); | ||
continue; | ||
} | ||
|
||
const requirements = requirementsByKind.get(reflection.kind); | ||
if (requirements !== undefined) { | ||
if ( | ||
requirements.summary && | ||
reflection.comment!.summary.length === 0 | ||
) { | ||
app.logger.warn( | ||
`${reflection.getFriendlyFullName()} does not have a summary.` | ||
); | ||
} | ||
|
||
for (const tagName of requirements.tags) { | ||
const tag: `@${string}` = tagName.startsWith("@") | ||
? (tagName as `@${string}`) | ||
: `@${tagName}`; | ||
|
||
if (reflection.comment!.getTags(tag).length === 0) { | ||
app.logger.warn( | ||
`${reflection.getFriendlyFullName()} does not have any ${tag} tags.` | ||
); | ||
} | ||
} | ||
} | ||
} | ||
} | ||
); | ||
} |