Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

correctly write doclets at es6 class constructors #74

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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';
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fix these whitespace changes please.

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";
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: spaces between braces


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) {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: Newline before braces, don't use K&R style braces.

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