Skip to content

Commit

Permalink
feat(scripts): api-doc add compatibility typescript 3 (#4732)
Browse files Browse the repository at this point in the history
  • Loading branch information
Domainv authored and valorkin committed Oct 24, 2018
1 parent 89e967f commit d5de6dd
Showing 1 changed file with 13 additions and 14 deletions.
27 changes: 13 additions & 14 deletions scripts/docs/api-doc.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
'use strict';

const ts = require('typescript');
const fs = require('fs');
const marked = require('marked');

const renderer = new marked.Renderer();
renderer.link = (href, title, text) => (`<a href=\"${href}\" target="_blank" title=\"${title}\">${text}</a>`);
marked.setOptions({gfm: true});

const getDescription = (symbol) => marked(
ts.displayPartsToString(symbol.getDocumentationComment()),
const getDescription = (symbol, typeChecker) => marked(
ts.displayPartsToString(symbol.getDocumentationComment(typeChecker)),
{renderer}
);

Expand All @@ -23,21 +22,21 @@ const ANGULAR_LIFECYCLE_METHODS = [
'ngAfterViewInit', 'ngAfterViewChecked', 'writeValue', 'registerOnChange', 'registerOnTouched', 'setDisabledState'
];

function isInternalMember(member) {
function isInternalMember(member, typeChecker) {
// todo: could be an issue, as for now lets skip members without a symbol
if (!member.symbol) {
return true;
}
const jsDoc = ts.displayPartsToString(member.symbol.getDocumentationComment());
const jsDoc = ts.displayPartsToString(member.symbol.getDocumentationComment(typeChecker));
return jsDoc.trim().length === 0 || jsDoc.indexOf('@internal') > -1;
}

function isAngularLifecycleHook(methodName) {
return ANGULAR_LIFECYCLE_METHODS.indexOf(methodName) >= 0;
}

function isPrivateOrInternal(member) {
return ((member.flags & ts.NodeFlags.Private) !== 0) || isInternalMember(member);
function isPrivateOrInternal(member, typeChecker) {
return ((member.flags & ts.NodeFlags.Private) !== 0) || isInternalMember(member, typeChecker);
}

class APIDocVisitor {
Expand Down Expand Up @@ -66,7 +65,7 @@ class APIDocVisitor {

visitInterfaceDeclaration(fileName, interfaceDeclaration) {
const symbol = this.program.getTypeChecker().getSymbolAtLocation(interfaceDeclaration.name);
const description = getDescription(symbol);
const description = getDescription(symbol, this.typeChecker);
const className = interfaceDeclaration.name.text;
const members = this.visitMembers(interfaceDeclaration.members);

Expand All @@ -81,7 +80,7 @@ class APIDocVisitor {

visitClassDeclaration(fileName, classDeclaration) {
const symbol = this.program.getTypeChecker().getSymbolAtLocation(classDeclaration.name);
const description = getDescription(symbol);
const description = getDescription(symbol, this.typeChecker);
const className = classDeclaration.name.text;
let directiveInfo, members;

Expand Down Expand Up @@ -165,7 +164,7 @@ class APIDocVisitor {
} else if (outDecorator) {
outputs.push(this.visitOutput(members[i], outDecorator));

} else if (!isPrivateOrInternal(members[i])) {
} else if (!isPrivateOrInternal(members[i], this.typeChecker)) {
if ((members[i].kind === ts.SyntaxKind.MethodDeclaration ||
members[i].kind === ts.SyntaxKind.MethodSignature) &&
!isAngularLifecycleHook(members[i].name.text)) {
Expand All @@ -188,7 +187,7 @@ class APIDocVisitor {
visitMethodDeclaration(method) {
return {
name: method.name.text,
description: getDescription(method.symbol),
description: getDescription(method.symbol, this.typeChecker),
args: method.parameters ? method.parameters.map((prop) => this.visitArgument(prop)) : [],
returnType: this.visitType(method.type)
}
Expand All @@ -204,7 +203,7 @@ class APIDocVisitor {
name: inArgs.length ? inArgs[0].text : property.name.text,
defaultValue: property.initializer ? this.stringifyDefaultValue(property.initializer) : undefined,
type: this.visitType(property),
description: getDescription(property.symbol)
description: getDescription(property.symbol, this.typeChecker)
};
}

Expand All @@ -222,7 +221,7 @@ class APIDocVisitor {
const outArgs = outDecorator.expression.arguments;
return {
name: outArgs.length ? outArgs[0].text : property.name.text,
description: getDescription(property.symbol)
description: getDescription(property.symbol, this.typeChecker)
};
}

Expand All @@ -231,7 +230,7 @@ class APIDocVisitor {
name: property.name.text,
defaultValue: property.initializer ? this.stringifyDefaultValue(property.initializer) : undefined,
type: this.visitType(property),
description: getDescription(property.symbol)
description: getDescription(property.symbol, this.typeChecker)
};
}

Expand Down

0 comments on commit d5de6dd

Please sign in to comment.