From 20d8a2dd93d3bbb6990594286f992e703fc4e334 Mon Sep 17 00:00:00 2001 From: dcodeIO Date: Fri, 30 Dec 2016 21:45:55 +0100 Subject: [PATCH] CLI: Added variable arguments support to tsd-jsdoc --- index.d.ts | 10 +++++----- lib/tsd-jsdoc/publish.js | 25 ++++++++++++++++--------- src/util/aspromise/index.d.ts | 2 +- src/util/codegen/index.d.ts | 4 ++-- src/util/codegen/test.ts | 4 ++++ src/util/eventemitter/index.d.ts | 2 +- 6 files changed, 29 insertions(+), 18 deletions(-) create mode 100644 src/util/codegen/test.ts diff --git a/index.d.ts b/index.d.ts index 9b942db3c..6b1a415da 100644 --- a/index.d.ts +++ b/index.d.ts @@ -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; @@ -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}. @@ -1911,7 +1911,7 @@ declare namespace protobuf { * @param {...*} params Function arguments * @returns {Promise<*>} Promisified function */ - function asPromise(fn: () => any, ctx: any, params: any): Promise; + function asPromise(fn: () => any, ctx: any, ...params: any[]): Promise; /** * A minimal base64 implementation for number arrays. @@ -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. @@ -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; } /** diff --git a/lib/tsd-jsdoc/publish.js b/lib/tsd-jsdoc/publish.js index 802318755..0a4948d3e 100644 --- a/lib/tsd-jsdoc/publish.js +++ b/lib/tsd-jsdoc/publish.js @@ -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 "); } @@ -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 }; @@ -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(", "); }); @@ -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) @@ -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 @@ -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, " = "); diff --git a/src/util/aspromise/index.d.ts b/src/util/aspromise/index.d.ts index 687e62217..5e73140ea 100644 --- a/src/util/aspromise/index.d.ts +++ b/src/util/aspromise/index.d.ts @@ -8,4 +8,4 @@ export = asPromise; * @param {...*} params Function arguments * @returns {Promise<*>} Promisified function */ -declare function asPromise(fn: () => any, ctx: any, params: any): Promise; +function asPromise(fn: () => any, ctx: any, ...params: any[]): Promise; diff --git a/src/util/codegen/index.d.ts b/src/util/codegen/index.d.ts index 3918ced15..9ea79b06a 100644 --- a/src/util/codegen/index.d.ts +++ b/src/util/codegen/index.d.ts @@ -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. @@ -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; diff --git a/src/util/codegen/test.ts b/src/util/codegen/test.ts new file mode 100644 index 000000000..e573524c6 --- /dev/null +++ b/src/util/codegen/test.ts @@ -0,0 +1,4 @@ +import codegen from "."; + +var cg = codegen("f", "a") + ("s", "a"); \ No newline at end of file diff --git a/src/util/eventemitter/index.d.ts b/src/util/eventemitter/index.d.ts index e6c8545a5..f17782385 100644 --- a/src/util/eventemitter/index.d.ts +++ b/src/util/eventemitter/index.d.ts @@ -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; }