Skip to content

Commit

Permalink
correctly write doclets at es6 class constructors
Browse files Browse the repository at this point in the history
  • Loading branch information
HackbrettXXX committed Apr 23, 2019
1 parent fa8a767 commit 312578e
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 19 deletions.
33 changes: 22 additions & 11 deletions src/Emitter.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,22 @@
import * as ts from 'typescript';
import { Dictionary } from './Dictionary';
import { warn } from './logger';
import { assertNever } from './assert_never';
import {Dictionary} from './Dictionary';
import {warn} from './logger';
import {assertNever} from './assert_never';
import {
createClass,
createClassMember,
createClassMethod, createConstructor,
createEnum,
createFunction,
createClassMethod,
createInterfaceMethod,
createInterface,
createClassMember,
createInterfaceMember,
createNamespaceMember,
createInterfaceMethod,
createModule,
createNamespace,
createNamespaceMember,
createTypedef,
createEnum,
} from './create_helpers';
import {createFunctionParams} from "./type_resolve_helpers";

interface IDocletTreeNode
{
Expand All @@ -41,10 +42,15 @@ function isEnum(doclet: TDoclet)

function shouldMoveOutOfClass(doclet: TDoclet)
{
return isClassLike(doclet)
return !isConstructor(doclet)
&& (isClassLike(doclet)
|| isModuleLike(doclet)
|| isEnum(doclet)
|| doclet.kind === 'typedef';
|| doclet.kind === 'typedef');
}

function isConstructor(doclet: TDoclet) {
return doclet.kind === "class" && doclet.name === doclet.memberof
}

export class Emitter
Expand Down Expand Up @@ -238,7 +244,12 @@ export class Emitter
switch (node.doclet.kind)
{
case 'class':
return createClass(node.doclet, children);
if (isConstructor(node.doclet)) {
// constructor in es6 classes with own doclet
return createConstructor(node.doclet)
} else {
return createClass(node.doclet, children);
}

case 'constant':
case 'member':
Expand Down
31 changes: 24 additions & 7 deletions src/create_helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -253,15 +253,9 @@ export function createEnum(doclet: IMemberDoclet): ts.EnumDeclaration

export function createClassMember(doclet: IMemberDoclet): ts.PropertyDeclaration
{
const mods: ts.Modifier[] = [];
const type = resolveType(doclet.type, doclet);

if (doclet.access === 'private')
mods.push(ts.createModifier(ts.SyntaxKind.PrivateKeyword));
else if (doclet.access === 'protected')
mods.push(ts.createModifier(ts.SyntaxKind.ProtectedKeyword));
else if (doclet.access === 'public')
mods.push(ts.createModifier(ts.SyntaxKind.PublicKeyword));
const mods: ts.Modifier[] = getAccessModifiers(doclet);

if (doclet.scope === 'static')
mods.push(ts.createModifier(ts.SyntaxKind.StaticKeyword));
Expand All @@ -279,6 +273,29 @@ export function createClassMember(doclet: IMemberDoclet): ts.PropertyDeclaration
));
}

function getAccessModifiers(doclet: IMemberDoclet | IClassDoclet) {
const mods: ts.Modifier[] = [];

if (doclet.access === 'private' || doclet.access === 'package')
mods.push(ts.createModifier(ts.SyntaxKind.PrivateKeyword));
else if (doclet.access === 'protected')
mods.push(ts.createModifier(ts.SyntaxKind.ProtectedKeyword));
else if (doclet.access === 'public')
mods.push(ts.createModifier(ts.SyntaxKind.PublicKeyword));

return mods
}

export function createConstructor(doclet: IClassDoclet) {
const params = createFunctionParams(doclet);
return handleComment(doclet, ts.createConstructor(
undefined, // decorators
getAccessModifiers(doclet), // modifiers
params, // parameters
undefined // body
))
}

export function createInterfaceMember(doclet: IMemberDoclet): ts.PropertySignature
{
const mods: ts.Modifier[] = [];
Expand Down
2 changes: 1 addition & 1 deletion src/typings/jsdoc.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ declare interface IDocletBase {
tags?: IDocletTag[];
memberof?: string;
see?: string;
access?: ('public' | 'private' | 'protected');
access?: ('public' | 'private' | 'protected' | 'package');
examples?: string;
deprecated?: string;
defaultvalue?: string;
Expand Down

0 comments on commit 312578e

Please sign in to comment.