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

support static members/methods in an interface #95

Merged
Merged
Show file tree
Hide file tree
Changes from 3 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
43 changes: 34 additions & 9 deletions src/Emitter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -143,13 +143,13 @@ export class Emitter

if (extras.length)
{
const interfaceLongname = this._getInterfaceKey(doclet.longname);
interfaceMerge = this._treeNodes[interfaceLongname] = {
const longname = this._getInterfaceKey(doclet.longname);
interfaceMerge = this._treeNodes[longname] = {
doclet: {
kind: 'interface',
name: doclet.name,
scope: doclet.scope,
longname: interfaceLongname,
longname: longname,
augments: extras,
memberof: doclet.memberof,
},
Expand All @@ -158,6 +158,31 @@ export class Emitter
}
}

let namespaceMerge: IDocletTreeNode | null = null;

// Generate an namespace of the same name as the interface/mixin to perform
// a namespace merge containing any static children (ex members and functions).
if (doclet.kind === 'interface' || doclet.kind === 'mixin')
{
const staticChildren = docs.filter(d => (d as IDocletBase).memberof === doclet.longname && (d as IDocletBase).scope === 'static');
if (staticChildren.length)
{
const longname = this._getNamespaceKey(doclet.longname);
namespaceMerge = this._treeNodes[longname] = {
doclet: {
kind: 'namespace',
name: doclet.name,
scope: doclet.scope,
longname: longname,
memberof: doclet.memberof,
},
children: [],
};

staticChildren.forEach(c => (c as IDocletBase).memberof = longname);
}
}

if (doclet.memberof)
{
const parent = this._treeNodes[doclet.memberof];
Expand All @@ -175,8 +200,8 @@ export class Emitter
{
const mod = this._getOrCreateClassNamespace(parent);

if (interfaceMerge)
mod.children.push(interfaceMerge);
if (interfaceMerge) mod.children.push(interfaceMerge);
if (namespaceMerge) mod.children.push(namespaceMerge);

mod.children.push(obj);
}
Expand All @@ -192,17 +217,17 @@ export class Emitter

if (!isParentEnum)
{
if (interfaceMerge)
parent.children.push(interfaceMerge);
if (interfaceMerge) parent.children.push(interfaceMerge);
if (namespaceMerge) parent.children.push(namespaceMerge);

parent.children.push(obj);
}
}
}
else
{
if (interfaceMerge)
this._treeRoots.push(interfaceMerge);
if (interfaceMerge) this._treeRoots.push(interfaceMerge);
if (namespaceMerge) this._treeRoots.push(namespaceMerge);
englercj marked this conversation as resolved.
Show resolved Hide resolved

this._treeRoots.push(obj);
}
Expand Down
39 changes: 39 additions & 0 deletions test/expected/interface_all.d.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,48 @@
declare namespace Color {
/**
* @function Color.staticMethod1
*/
function staticMethod1(): void;
/**
* @function
* @static
*/
function staticMethod2(): void;
/**
* @member {Number} Color.staticMember1
*/
var staticMember1: number;
/**
* @member {Boolean}
* @static
*/
var staticMember2: boolean;
/**
* @type {String}
* @name Color.staticMember3
*/
var staticMember3: string;
/**
* @type {Object}
* @static
*/
var staticMember4: any;
}

/**
* Interface for classes that represent a color.
*
* @interface
*/
declare interface Color {
/**
* @function
*/
instanceMethod(): void;
/**
* @member {Number}
*/
instanceMember: number;
/**
* Get the color as an array of red, green, and blue values, represented as
* decimal numbers between 0 and 1.
Expand Down
46 changes: 44 additions & 2 deletions test/fixtures/interface_all.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,50 @@
*/
function Color() {}

/**
* @function Color.staticMethod1
*/
Color.staticMethod1 = function() {};

/**
* @function
* @static
*/
Color.staticMethod2 = function() {};

/**
* @member {Number} Color.staticMember1
*/
Color.staticMember1 = 1;

/**
* @member {Boolean}
* @static
*/
Color.staticMember2 = true;

/**
* @type {String}
* @name Color.staticMember3
*/
Color.staticMember3 = "foobar";

/**
* @type {Object}
* @static
*/
Color.staticMember4 = {};

/**
* @function
*/
Color.prototype.instanceMethod = function() {};

/**
* @member {Number}
*/
Color.prototype.instanceMember = 5;

/**
* Get the color as an array of red, green, and blue values, represented as
* decimal numbers between 0 and 1.
Expand All @@ -15,5 +59,3 @@ function Color() {}
Color.prototype.rgb = function() {
throw new Error('not implemented');
};