@capacitor/docgen
Advanced tools
Comparing version 0.0.11 to 0.0.12
@@ -12,2 +12,3 @@ "use strict"; | ||
const markdown_1 = require("./markdown"); | ||
const parse_1 = require("./parse"); | ||
const readFile = util_1.promisify(fs_1.default.readFile); | ||
@@ -87,6 +88,9 @@ const writeFile = util_1.promisify(fs_1.default.writeFile); | ||
if (data.interfaces.length > 0) { | ||
o.push(`* [Interfaces](#interfaces)`); | ||
o.push(`* [Interfaces](#${parse_1.slugify('Interfaces')})`); | ||
} | ||
if (data.typeAliases.length > 0) { | ||
o.push(`* [Type Aliases](#${parse_1.slugify('Type Aliases')})`); | ||
} | ||
if (data.enums.length > 0) { | ||
o.push(`* [Enums](#enums)`); | ||
o.push(`* [Enums](#${parse_1.slugify('Enums')})`); | ||
} | ||
@@ -113,2 +117,10 @@ return o.join('\n').trim(); | ||
} | ||
if (data.typeAliases.length > 0) { | ||
o.push(`### Type Aliases`); | ||
o.push(``); | ||
data.typeAliases.forEach(i => { | ||
o.push(typeAliasTable(data, i)); | ||
}); | ||
o.push(``); | ||
} | ||
if (data.enums.length > 0) { | ||
@@ -209,2 +221,17 @@ o.push(`### Enums`); | ||
} | ||
function typeAliasTable(data, t) { | ||
const o = []; | ||
o.push(``); | ||
o.push(`#### ${t.name}`); | ||
o.push(``); | ||
if (t.docs) { | ||
o.push(formatting_1.formatDescription(data, t.docs)); | ||
o.push(``); | ||
} | ||
o.push(`${t.types.map(ty => { | ||
return formatting_1.formatType(data, ty.text).formatted; | ||
})}`); | ||
o.push(``); | ||
return o.join(`\n`); | ||
} | ||
function enumTable(data, i) { | ||
@@ -211,0 +238,0 @@ const o = []; |
@@ -9,1 +9,2 @@ import type { DocsData, DocsParseOptions } from './types'; | ||
export declare function parse(opts: DocsParseOptions): (api: string) => DocsData; | ||
export declare function slugify(id: string): string; |
@@ -6,3 +6,3 @@ "use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.parse = void 0; | ||
exports.slugify = exports.parse = void 0; | ||
const typescript_1 = __importDefault(require("typescript")); | ||
@@ -24,4 +24,5 @@ const transpile_1 = require("./transpile"); | ||
const enums = []; | ||
const typeAliases = []; | ||
tsSourceFiles.forEach(tsSourceFile => { | ||
parseSourceFile(tsSourceFile, typeChecker, interfaces, enums); | ||
parseSourceFile(tsSourceFile, typeChecker, interfaces, typeAliases, enums); | ||
}); | ||
@@ -34,5 +35,6 @@ return (api) => { | ||
enums: [], | ||
typeAliases: [], | ||
}; | ||
if (apiInterface) { | ||
collectInterfaces(data, apiInterface, interfaces, enums); | ||
collectInterfaces(data, apiInterface, interfaces, typeAliases, enums); | ||
} | ||
@@ -43,3 +45,3 @@ return data; | ||
exports.parse = parse; | ||
function collectInterfaces(data, i, interfaces, enums) { | ||
function collectInterfaces(data, i, interfaces, typeAliases, enums) { | ||
var _a; | ||
@@ -51,25 +53,31 @@ if (i.name !== ((_a = data.api) === null || _a === void 0 ? void 0 : _a.name) && | ||
i.methods.forEach(m => { | ||
collectUsed(data, m.complexTypes, interfaces, enums); | ||
collectUsed(data, m.complexTypes, interfaces, typeAliases, enums); | ||
}); | ||
i.properties.forEach(p => { | ||
collectUsed(data, p.complexTypes, interfaces, enums); | ||
collectUsed(data, p.complexTypes, interfaces, typeAliases, enums); | ||
}); | ||
} | ||
function collectUsed(data, complexTypes, interfaces, enums) { | ||
function collectUsed(data, complexTypes, interfaces, typeAliases, enums) { | ||
complexTypes.forEach(typeName => { | ||
const fi = interfaces.find(i => i.name === typeName); | ||
if (fi && !data.interfaces.some(i => i.name === fi.name)) { | ||
collectInterfaces(data, fi, interfaces, enums); | ||
collectInterfaces(data, fi, interfaces, typeAliases, enums); | ||
} | ||
const ei = enums.find(i => i.name === typeName); | ||
if (ei) { | ||
if (!data.enums.some(en => en.name === ei.name)) { | ||
data.enums.push(ei); | ||
} | ||
const ei = enums.find(en => en.name === typeName); | ||
if (ei && !data.enums.some(en => en.name === ei.name)) { | ||
data.enums.push(ei); | ||
} | ||
const ti = typeAliases.find(ty => ty.name === typeName); | ||
if (ti && !data.typeAliases.some(ty => ty.name === ti.name)) { | ||
data.typeAliases.push(ti); | ||
ti.types.forEach(type => { | ||
collectUsed(data, type.complexTypes, interfaces, typeAliases, enums); | ||
}); | ||
} | ||
}); | ||
} | ||
function parseSourceFile(tsSourceFile, typeChecker, interfaces, enums) { | ||
function parseSourceFile(tsSourceFile, typeChecker, interfaces, typeAliases, enums) { | ||
const statements = tsSourceFile.statements; | ||
const interfaceDeclarations = statements.filter(typescript_1.default.isInterfaceDeclaration); | ||
const typeAliasDeclarations = statements.filter(typescript_1.default.isTypeAliasDeclaration); | ||
const enumDeclarations = statements.filter(typescript_1.default.isEnumDeclaration); | ||
@@ -82,2 +90,5 @@ interfaceDeclarations.forEach(interfaceDeclaration => { | ||
}); | ||
typeAliasDeclarations.forEach(typeAliasDeclaration => { | ||
typeAliases.push(getTypeAlias(typeChecker, typeAliasDeclaration)); | ||
}); | ||
} | ||
@@ -136,2 +147,51 @@ function getInterface(typeChecker, node) { | ||
} | ||
function getTypeAlias(typeChecker, node) { | ||
const symbol = typeChecker.getSymbolAtLocation(node.name); | ||
const docs = symbol ? serializeSymbol(typeChecker, symbol) : null; | ||
const typeAliasName = node.name.text; | ||
const typeAlias = { | ||
name: typeAliasName, | ||
slug: slugify(typeAliasName), | ||
docs: (docs === null || docs === void 0 ? void 0 : docs.docs) || '', | ||
types: [], | ||
}; | ||
if (node.type) { | ||
if (typescript_1.default.isFunctionTypeNode(node.type)) { | ||
const signature = typeChecker.getSignatureFromDeclaration(node.type); | ||
if (signature) { | ||
const referencedTypes = new Set(getAllTypeReferences(node.type)); | ||
referencedTypes.delete('Promise'); | ||
const signatureString = typeChecker.signatureToString(signature); | ||
typeAlias.types = [ | ||
{ | ||
text: signatureString, | ||
complexTypes: Array.from(referencedTypes), | ||
}, | ||
]; | ||
} | ||
} | ||
else if (typescript_1.default.isUnionTypeNode(node.type) && node.type.types) { | ||
typeAlias.types = node.type.types.map(t => { | ||
const referencedTypes = new Set(getAllTypeReferences(t)); | ||
referencedTypes.delete('Promise'); | ||
const typeRef = { | ||
text: t.getText(), | ||
complexTypes: Array.from(referencedTypes), | ||
}; | ||
return typeRef; | ||
}); | ||
} | ||
else if (typeof node.type.getText === 'function') { | ||
const referencedTypes = new Set(getAllTypeReferences(node.type)); | ||
referencedTypes.delete('Promise'); | ||
typeAlias.types = [ | ||
{ | ||
text: node.type.getText(), | ||
complexTypes: Array.from(referencedTypes), | ||
}, | ||
]; | ||
} | ||
} | ||
return typeAlias; | ||
} | ||
function getInterfaceMethod(typeChecker, methodSignature) { | ||
@@ -144,2 +204,6 @@ const flags = typescript_1.default.TypeFormatFlags.WriteArrowStyleSignature | | ||
} | ||
const tags = signature.getJsDocTags(); | ||
if (tags.some(t => t.name === 'hidden')) { | ||
return null; | ||
} | ||
const returnType = typeChecker.getReturnTypeOfSignature(signature); | ||
@@ -169,3 +233,3 @@ const returnTypeNode = typeChecker.typeToTypeNode(returnType, methodSignature, typescript_1.default.NodeBuilderFlags.NoTruncation | typescript_1.default.NodeBuilderFlags.NoTypeReduction); | ||
returns: returnString, | ||
tags: signature.getJsDocTags(), | ||
tags, | ||
docs: typescript_1.default.displayPartsToString(signature.getDocumentationComment(typeChecker)), | ||
@@ -176,5 +240,2 @@ complexTypes: Array.from(referencedTypes), | ||
m.slug = slugify(formatting_1.formatMethodSignature(m)); | ||
if (m.tags.some(t => t.name === 'hidden')) { | ||
return null; | ||
} | ||
return m; | ||
@@ -240,3 +301,3 @@ } | ||
} | ||
return Array.from(referencedTypes); | ||
return referencedTypes; | ||
} | ||
@@ -255,1 +316,2 @@ function getEntityName(entity) { | ||
} | ||
exports.slugify = slugify; |
export interface DocsData { | ||
api: DocsInterface | null; | ||
interfaces: DocsInterface[]; | ||
typeAliases: DocsTypeAlias[]; | ||
enums: DocsEnum[]; | ||
@@ -19,2 +20,12 @@ } | ||
} | ||
export interface DocsTypeAlias { | ||
name: string; | ||
slug: string; | ||
docs: string; | ||
types: DocsTypeAliasReference[]; | ||
} | ||
export interface DocsTypeAliasReference { | ||
text: string; | ||
complexTypes: string[]; | ||
} | ||
export interface DocsEnumMember { | ||
@@ -21,0 +32,0 @@ name: string; |
{ | ||
"name": "@capacitor/docgen", | ||
"version": "0.0.11", | ||
"version": "0.0.12", | ||
"description": "Docs Readme Markdown and JSON Generator for Capacitor Plugins", | ||
@@ -5,0 +5,0 @@ "keywords": [ |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
46108
1205