Skip to content

Commit 20d8a2d

Browse files
committed
CLI: Added variable arguments support to tsd-jsdoc
1 parent 40074bb commit 20d8a2d

File tree

6 files changed

+29
-18
lines changed

6 files changed

+29
-18
lines changed

index.d.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
// $> pbts --name protobuf --out index.d.ts src
2-
// Generated Fri, 30 Dec 2016 16:29:57 UTC
2+
// Generated Fri, 30 Dec 2016 20:40:44 UTC
33

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

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

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

19621962
/**
19631963
* Constructs a new event emitter instance.
@@ -1998,7 +1998,7 @@ declare namespace protobuf {
19981998
* @param {...*} args Arguments
19991999
* @returns {util.EventEmitter} `this`
20002000
*/
2001-
emit(evt: string, args: any): util.EventEmitter;
2001+
emit(evt: string, ...args: any[]): util.EventEmitter;
20022002
}
20032003

20042004
/**

lib/tsd-jsdoc/publish.js

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -219,9 +219,9 @@ function getTypeOf(element) {
219219
}
220220

221221
// begins writing the definition of the specified element
222-
function begin(element) {
222+
function begin(element, noDeclare) {
223223
writeComment(element.comment);
224-
if (element.scope === "global" && !options.module)
224+
if (element.scope === "global" && !options.module && !noDeclare)
225225
write("declare ");
226226
}
227227

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

241241
// parameter types
242242
if (element.params)
243-
element.params.forEach(function(param, i) {
243+
element.params.forEach(function(param) {
244244
var path = param.name.split(/\./g);
245245
if (path.length === 1)
246246
params[param.name] = {
247247
type: getTypeOf(param),
248+
variable: param.variable === true,
248249
optional: param.optional === true,
249250
defaultValue: param.defaultvalue // TODO
250251
};
@@ -254,8 +255,13 @@ function writeFunctionSignature(element, isConstructor, isTypeDef) {
254255

255256
var paramNames = Object.keys(params);
256257
paramNames.forEach(function(name, i) {
257-
var param = params[name];
258-
write(name, param.optional ? "?: " : ": ", param.type);
258+
var param = params[name];
259+
var type = param.type;
260+
if (param.variable) {
261+
name = "..." + name;
262+
type = param.type.charAt(0) === "(" ? "any[]" : param.type + "[]";
263+
}
264+
write(name, param.optional ? "?: " : ": ", type);
259265
if (i < paramNames.length - 1)
260266
write(", ");
261267
});
@@ -332,8 +338,9 @@ function notAModuleReference(ref) {
332338

333339
// handles a class or class-like
334340
function handleClass(element, parent) {
335-
begin(element);
336-
if (isInterface(element))
341+
var is_interface = isInterface(element);
342+
begin(element, is_interface);
343+
if (is_interface)
337344
write("interface ");
338345
else {
339346
if (element.virtual)
@@ -363,7 +370,7 @@ function handleClass(element, parent) {
363370
++indent;
364371

365372
// constructor
366-
if (!isInterface(element) && !element.virtual)
373+
if (!is_interface && !element.virtual)
367374
handleFunction(element, parent, true);
368375

369376
// members except inner classes
@@ -467,7 +474,7 @@ function handleTypeDef(element, parent) {
467474
writeInterface(element);
468475
}
469476
} else {
470-
begin(element);
477+
begin(element, true);
471478
if (element.access)
472479
write(element.access, " ");
473480
write("type ", element.name, " = ");

src/util/aspromise/index.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,4 @@ export = asPromise;
88
* @param {...*} params Function arguments
99
* @returns {Promise<*>} Promisified function
1010
*/
11-
declare function asPromise(fn: () => any, ctx: any, params: any): Promise<any>;
11+
function asPromise(fn: () => any, ctx: any, ...params: any[]): Promise<any>;

src/util/codegen/index.d.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ export = codegen;
1010
* @property {function(string=):string} str Stringifies the so far generated function source.
1111
* @property {function(string=, Object=):function} eof Ends generation and builds the function whilst applying a scope.
1212
*/
13-
type Codegen = (format: string, args: any) => Codegen;
13+
type Codegen = (format: string, ...args: any[]) => Codegen;
1414

1515
/**
1616
* A closure for generating functions programmatically.
@@ -22,4 +22,4 @@ type Codegen = (format: string, args: any) => Codegen;
2222
* @property {boolean} supported Whether code generation is supported by the environment.
2323
* @property {boolean} verbose=false When set to true, codegen will log generated code to console. Useful for debugging.
2424
*/
25-
declare function codegen(params: string): Codegen;
25+
declare function codegen(...params: string[]): Codegen;

src/util/codegen/test.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
import codegen from ".";
2+
3+
var cg = codegen("f", "a")
4+
("s", "a");

src/util/eventemitter/index.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,5 +39,5 @@ declare class EventEmitter {
3939
* @param {...*} args Arguments
4040
* @returns {util.EventEmitter} `this`
4141
*/
42-
emit(evt: string, args: any): EventEmitter;
42+
emit(evt: string, ...args: any[]): EventEmitter;
4343
}

0 commit comments

Comments
 (0)