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 all commits
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
28 changes: 20 additions & 8 deletions src/Emitter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,18 @@ import { warn } from './logger';
import { assertNever } from './assert_never';
import {
createClass,
createFunction,
createClassMember,
createClassMethod,
createInterfaceMethod,
createConstructor,
createEnum,
createFunction,
createInterface,
createClassMember,
createInterfaceMember,
createNamespaceMember,
createInterfaceMethod,
createModule,
createNamespace,
createNamespaceMember,
createTypedef,
createEnum,
} from './create_helpers';

interface IDocletTreeNode
Expand All @@ -41,10 +42,16 @@ 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 +245,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
33 changes: 26 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,31 @@ 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
55 changes: 55 additions & 0 deletions test/expected/constructors.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/**
* Class documentation.
*/
declare class MyClass {

/**
* Constructor documentation.
* @public
* @param {number} a
* @param {string} b
*/
public constructor(a: number, b: string);
}

/**
* Class documentation.
*/
declare class MyClass2 {

/**
* Constructor documentation.
* @protected
* @param {number} a
* @param {string} b
*/
protected constructor(a: number, b: string);
}

/**
* Class documentation.
*/
declare class MyClass3 {

/**
* Constructor documentation.
* @package
* @param {number} a
* @param {string} b
*/
private constructor(a: number, b: string);
}

/**
* Class documentation.
*/
declare class MyClass4 {

/**
* Constructor documentation.
* @private
* @param {number} a
* @param {string} b
*/
private constructor(a: number, b: string);
}
55 changes: 55 additions & 0 deletions test/fixtures/constructors.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/**
* Class documentation.
*/
class MyClass {

/**
* Constructor documentation.
* @public
* @param {number} a
* @param {string} b
*/
constructor(a, b) {}
}

/**
* Class documentation.
*/
class MyClass2 {

/**
* Constructor documentation.
* @protected
* @param {number} a
* @param {string} b
*/
constructor(a, b) {}
}

/**
* Class documentation.
*/
class MyClass3 {

/**
* Constructor documentation.
* @package
* @param {number} a
* @param {string} b
*/
constructor(a, b) {}
}

/**
* Class documentation.
*/
class MyClass4 {

/**
* Constructor documentation.
* @private
* @param {number} a
* @param {string} b
*/
constructor(a, b) {}
}
7 changes: 7 additions & 0 deletions test/specs/constructors.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { expectJsDoc } from '../lib';

suite('Constructor Checks', () => {
test('All', () => {
expectJsDoc('constructors');
});
});