Skip to content

Commit

Permalink
CLI: Added variable arguments support to tsd-jsdoc
Browse files Browse the repository at this point in the history
  • Loading branch information
dcodeIO committed Dec 30, 2016
1 parent 40074bb commit 20d8a2d
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 18 deletions.
10 changes: 5 additions & 5 deletions index.d.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// $> pbts --name protobuf --out index.d.ts src
// Generated Fri, 30 Dec 2016 16:29:57 UTC
// Generated Fri, 30 Dec 2016 20:40:44 UTC

export = protobuf;
export as namespace protobuf;
Expand Down Expand Up @@ -1851,7 +1851,7 @@ declare namespace protobuf {
* @property {function(string=):string} str Stringifies the so far generated function source.
* @property {function(string=, Object=):function} eof Ends generation and builds the function whilst applying a scope.
*/
type Codegen = (format: string, args: any) => Codegen;
type Codegen = (format: string, ...args: any[]) => Codegen;

/**
* Node-style callback as used by {@link util.fetch}.
Expand Down Expand Up @@ -1911,7 +1911,7 @@ declare namespace protobuf {
* @param {...*} params Function arguments
* @returns {Promise<*>} Promisified function
*/
function asPromise(fn: () => any, ctx: any, params: any): Promise<any>;
function asPromise(fn: () => any, ctx: any, ...params: any[]): Promise<any>;

/**
* A minimal base64 implementation for number arrays.
Expand Down Expand Up @@ -1957,7 +1957,7 @@ declare namespace protobuf {
* @property {boolean} supported Whether code generation is supported by the environment.
* @property {boolean} verbose=false When set to true, codegen will log generated code to console. Useful for debugging.
*/
function codegen(params: string): Codegen;
function codegen(...params: string[]): Codegen;

/**
* Constructs a new event emitter instance.
Expand Down Expand Up @@ -1998,7 +1998,7 @@ declare namespace protobuf {
* @param {...*} args Arguments
* @returns {util.EventEmitter} `this`
*/
emit(evt: string, args: any): util.EventEmitter;
emit(evt: string, ...args: any[]): util.EventEmitter;
}

/**
Expand Down
25 changes: 16 additions & 9 deletions lib/tsd-jsdoc/publish.js
Original file line number Diff line number Diff line change
Expand Up @@ -219,9 +219,9 @@ function getTypeOf(element) {
}

// begins writing the definition of the specified element
function begin(element) {
function begin(element, noDeclare) {
writeComment(element.comment);
if (element.scope === "global" && !options.module)
if (element.scope === "global" && !options.module && !noDeclare)
write("declare ");
}

Expand All @@ -240,11 +240,12 @@ function writeFunctionSignature(element, isConstructor, isTypeDef) {

// parameter types
if (element.params)
element.params.forEach(function(param, i) {
element.params.forEach(function(param) {
var path = param.name.split(/\./g);
if (path.length === 1)
params[param.name] = {
type: getTypeOf(param),
variable: param.variable === true,
optional: param.optional === true,
defaultValue: param.defaultvalue // TODO
};
Expand All @@ -254,8 +255,13 @@ function writeFunctionSignature(element, isConstructor, isTypeDef) {

var paramNames = Object.keys(params);
paramNames.forEach(function(name, i) {
var param = params[name];
write(name, param.optional ? "?: " : ": ", param.type);
var param = params[name];
var type = param.type;
if (param.variable) {
name = "..." + name;
type = param.type.charAt(0) === "(" ? "any[]" : param.type + "[]";
}
write(name, param.optional ? "?: " : ": ", type);
if (i < paramNames.length - 1)
write(", ");
});
Expand Down Expand Up @@ -332,8 +338,9 @@ function notAModuleReference(ref) {

// handles a class or class-like
function handleClass(element, parent) {
begin(element);
if (isInterface(element))
var is_interface = isInterface(element);
begin(element, is_interface);
if (is_interface)
write("interface ");
else {
if (element.virtual)
Expand Down Expand Up @@ -363,7 +370,7 @@ function handleClass(element, parent) {
++indent;

// constructor
if (!isInterface(element) && !element.virtual)
if (!is_interface && !element.virtual)
handleFunction(element, parent, true);

// members except inner classes
Expand Down Expand Up @@ -467,7 +474,7 @@ function handleTypeDef(element, parent) {
writeInterface(element);
}
} else {
begin(element);
begin(element, true);
if (element.access)
write(element.access, " ");
write("type ", element.name, " = ");
Expand Down
2 changes: 1 addition & 1 deletion src/util/aspromise/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,4 @@ export = asPromise;
* @param {...*} params Function arguments
* @returns {Promise<*>} Promisified function
*/
declare function asPromise(fn: () => any, ctx: any, params: any): Promise<any>;
function asPromise(fn: () => any, ctx: any, ...params: any[]): Promise<any>;
4 changes: 2 additions & 2 deletions src/util/codegen/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ export = codegen;
* @property {function(string=):string} str Stringifies the so far generated function source.
* @property {function(string=, Object=):function} eof Ends generation and builds the function whilst applying a scope.
*/
type Codegen = (format: string, args: any) => Codegen;
type Codegen = (format: string, ...args: any[]) => Codegen;

/**
* A closure for generating functions programmatically.
Expand All @@ -22,4 +22,4 @@ type Codegen = (format: string, args: any) => Codegen;
* @property {boolean} supported Whether code generation is supported by the environment.
* @property {boolean} verbose=false When set to true, codegen will log generated code to console. Useful for debugging.
*/
declare function codegen(params: string): Codegen;
declare function codegen(...params: string[]): Codegen;
4 changes: 4 additions & 0 deletions src/util/codegen/test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import codegen from ".";

var cg = codegen("f", "a")
("s", "a");
2 changes: 1 addition & 1 deletion src/util/eventemitter/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,5 +39,5 @@ declare class EventEmitter {
* @param {...*} args Arguments
* @returns {util.EventEmitter} `this`
*/
emit(evt: string, args: any): EventEmitter;
emit(evt: string, ...args: any[]): EventEmitter;
}

0 comments on commit 20d8a2d

Please sign in to comment.