From 79fbbf48b8e4dc9c41dcbdef2b73c5f2608b0318 Mon Sep 17 00:00:00 2001 From: Endel Dreyer Date: Fri, 2 Dec 2016 16:01:47 +0100 Subject: [PATCH] improve TypeScript support. add simple test script. --- package.json | 3 ++- scripts/types.js | 5 ++++- src/inherits.js | 4 ++-- types/protobuf.js-test.ts | 14 +++++++++++++ types/protobuf.js.d.ts | 42 +++++++++++++++++++-------------------- 5 files changed, 43 insertions(+), 25 deletions(-) create mode 100644 types/protobuf.js-test.ts diff --git a/package.json b/package.json index 84a3c35c4..5b6867eb8 100644 --- a/package.json +++ b/package.json @@ -27,7 +27,7 @@ "build": "gulp", "docs": "jsdoc -c jsdoc.docs.json -R README.md", "pages": "node scripts/pages", - "types": "jsdoc -c jsdoc.types.json && node scripts/types.js", + "types": "jsdoc -c jsdoc.types.json && node scripts/types.js && tsc types/protobuf.js-test.ts --lib es2015 --noEmit", "lint": "eslint src", "test": "tape tests/*.js | tap-spec", "zuul": "zuul --ui tape --no-coverage --concurrency 1 -- tests/*.js", @@ -62,6 +62,7 @@ "tap-spec": "^4.1.1", "tape": "^4.6.3", "tsd-jsdoc": "dcodeIO/tsd-jsdoc", + "typescript": "^2.2.0-dev.20161202", "vinyl-buffer": "^1.0.0", "vinyl-fs": "^2.4.4", "vinyl-source-stream": "^1.1.0", diff --git a/scripts/types.js b/scripts/types.js index 3fbff41b2..e957aa5ab 100644 --- a/scripts/types.js +++ b/scripts/types.js @@ -18,6 +18,9 @@ var dts = fs.readFileSync(path.join(dir, "types.d.ts"), "utf8"); // Fix generic promises dts = dts.replace(/Promise\./g, "Promise"); +// Fix classes +dts = dts.replace(/\(\(\) => any\)/g, "any"); + // Fix multidimensional arrays var found; do { @@ -31,7 +34,7 @@ do { // Remove declare statements and wrap everything in a module dts = dts.replace(/\bdeclare\s/g, ""); dts = dts.replace(/^/mg, " "); -dts = header.join('\n')+"\ndeclare module protobuf {\n\n" + dts + "\n}\n"; +dts = header.join('\n')+"\ndeclare module \"protobufjs\" {\n\n" + dts + "\n}\n"; fs.writeFileSync(path.join(dir, "protobuf.js.d.ts"), dts); fs.unlinkSync(path.join(dir, "types.d.ts")); diff --git a/src/inherits.js b/src/inherits.js index 7ab44304f..2c2b19775 100644 --- a/src/inherits.js +++ b/src/inherits.js @@ -18,7 +18,7 @@ var _TypeError = util._TypeError; /** * Inherits a custom class from the message prototype of the specified message type. * @param {Function} clazz Inheriting class - * @param {Type} type Inherited message type + * @param {Type|ReflectionObject} type Inherited message type * @param {InheritanceOptions} [options] Inheritance options * @returns {Prototype} Created prototype */ @@ -40,7 +40,7 @@ function inherits(clazz, type, options) { */ var classProperties = { - + /** * Reference to the reflected type. * @name Class.$type diff --git a/types/protobuf.js-test.ts b/types/protobuf.js-test.ts new file mode 100644 index 000000000..4c9411210 --- /dev/null +++ b/types/protobuf.js-test.ts @@ -0,0 +1,14 @@ +/// +import * as protobuf from "protobufjs"; + +export const proto = {"nested":{"Hello":{"fields":{"value":{"rule":"required","type":"string","id":1}}}}}; + +const root = protobuf.Root.fromJSON(proto); + +export class Hello { + constructor (properties: any) { + protobuf.Prototype.call(this, properties); + } +} + +protobuf.inherits(Hello, root.lookup("Hello")); diff --git a/types/protobuf.js.d.ts b/types/protobuf.js.d.ts index 803ad9203..0bd362810 100644 --- a/types/protobuf.js.d.ts +++ b/types/protobuf.js.d.ts @@ -3,9 +3,9 @@ /* * protobuf.js v6.0.1 TypeScript definitions - * Generated Fri, 02 Dec 2016 12:20:52 UTC + * Generated Fri, 02 Dec 2016 15:01:02 UTC */ -declare module protobuf { +declare module "protobufjs" { /** * Provides common type definitions. @@ -306,7 +306,7 @@ declare module protobuf { * @returns {Promise|Object} A promise if callback has been omitted, otherwise the protobuf namespace * @throws {TypeError} If arguments are invalid */ - function load(filename: (string|string[]), root?: Root, callback?: (() => any)): (Promise|Object); + function load(filename: (string|string[]), root?: Root, callback?: any): (Promise|Object); /** * Options passed to {@link inherits}, modifying its behavior. @@ -324,11 +324,11 @@ declare module protobuf { /** * Inherits a custom class from the message prototype of the specified message type. * @param {Function} clazz Inheriting class - * @param {Type} type Inherited message type + * @param {Type|ReflectionObject} type Inherited message type * @param {InheritanceOptions} [options] Inheritance options * @returns {Prototype} Created prototype */ - function inherits(clazz: (() => any), type: Type, options?: InheritanceOptions): Prototype; + function inherits(clazz: any, type: (Type|ReflectionObject), options?: InheritanceOptions): Prototype; /** * This is not an actual type but stands as a reference for any constructor of a custom message class that you pass to the library. @@ -719,7 +719,7 @@ declare module protobuf { * @returns {Object} Prototype * @this ReflectionObject */ - static extend(constructor: (() => any)): Object; + static extend(constructor: any): Object; /** * Converts this reflection object to its JSON representation. @@ -1153,7 +1153,7 @@ declare module protobuf { * @returns {Promise|undefined} A promise if `callback` has been omitted * @throws {TypeError} If arguments are invalid */ - load(filename: (string|string[]), callback?: (() => any)): (Promise|undefined); + load(filename: (string|string[]), callback?: any): (Promise|undefined); } @@ -1215,7 +1215,7 @@ declare module protobuf { * @param {boolean} [responseDelimited=false] Whether response data is length delimited * @returns {Object} Runtime service */ - create(rpc: (() => any), requestDelimited?: boolean, responseDelimited?: boolean): Object; + create(rpc: any, requestDelimited?: boolean, responseDelimited?: boolean): Object; } @@ -1228,7 +1228,7 @@ declare module protobuf { * @param {function(?Error, Uint8Array=)} callback Node-style callback called with the error, if any, and the response data * @returns {undefined} */ - function RPCImpl(method: Method, requestData: Uint8Array, callback: (() => any)): undefined; + function RPCImpl(method: Method, requestData: Uint8Array, callback: any): undefined; /** * Handle object returned from {@link tokenize}. @@ -1240,11 +1240,11 @@ declare module protobuf { * @property {function(string, boolean=):boolean} skip Skips a token, returns its presence and advances or, if non-optional and not present, throws */ interface TokenizerHandle { - line: (() => any); - next: (() => any); - peek: (() => any); - push: (() => any); - skip: (() => any); + line: any; + next: any; + peek: any; + push: any; + skip: any; } @@ -1369,7 +1369,7 @@ declare module protobuf { * Defaults to use the internal constuctor. * @returns {Prototype} Message instance */ - create(properties?: Object, ctor?: (() => any)): Prototype; + create(properties?: Object, ctor?: any): Prototype; /** * Encodes a message of this type. @@ -1496,7 +1496,7 @@ declare module protobuf { * @param {Object|string[]} [scope] Function scope * @returns {function} A function to apply the scope manually when `scope` is an array, otherwise the generated function with scope applied */ - type CodegenEnder = (name?: string, scope?: (Object|string[])) => (() => any); + type CodegenEnder = (name?: string, scope?: (Object|string[])) => any; /** * Constructs new long bits. @@ -1610,14 +1610,14 @@ declare module protobuf { * If you assign any compatible buffer implementation to this property, the library will use it. * @type {?Function} */ - var Buffer: (() => any); + var Buffer: any; /** * Optional Long class to use. * If you assign any compatible long implementation to this property, the library will use it. * @type {?Function} */ - var Long: (() => any); + var Long: any; /** * Converts a number or long to an 8 characters long hash string. @@ -1680,7 +1680,7 @@ declare module protobuf { * @param {...*} params Function arguments * @returns {Promise<*>} Promisified function */ - function asPromise(fn: (() => any), ctx: Object, params: any): Promise; + function asPromise(fn: any, ctx: Object, params: any): Promise; /** * Fetches the contents of a file. @@ -1689,7 +1689,7 @@ declare module protobuf { * @param {function(?Error, string=)} [callback] Node-style callback * @returns {Promise|undefined} Promise if callback has been omitted */ - function fetch(path: string, callback?: (() => any)): (Promise|undefined); + function fetch(path: string, callback?: any): (Promise|undefined); /** * Tests if the specified path is absolute. @@ -1811,7 +1811,7 @@ declare module protobuf { * @param {number} val Value to write * @returns {Writer} `this` */ - push(fn: (() => any), len: number, val: number): Writer; + push(fn: any, len: number, val: number): Writer; /** * Writes a tag.