Permalink
Browse files

improve TypeScript support. add simple test script.

  • Loading branch information...
endel committed Dec 2, 2016
1 parent 96fa07a commit 79fbbf48b8e4dc9c41dcbdef2b73c5f2608b0318
Showing with 43 additions and 25 deletions.
  1. +2 −1 package.json
  2. +4 −1 scripts/types.js
  3. +2 −2 src/inherits.js
  4. +14 −0 types/protobuf.js-test.ts
  5. +21 −21 types/protobuf.js.d.ts
@@ -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",
@@ -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"));
@@ -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
@@ -0,0 +1,14 @@
///<reference path="./protobuf.js.d.ts" />
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"));
@@ -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<Root>|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<Root>|Object);
function load(filename: (string|string[]), root?: Root, callback?: any): (Promise<Root>|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<Root>|undefined} A promise if `callback` has been omitted
* @throws {TypeError} If arguments are invalid
*/
load(filename: (string|string[]), callback?: (() => any)): (Promise<Root>|undefined);
load(filename: (string|string[]), callback?: any): (Promise<Root>|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<any>;
function asPromise(fn: any, ctx: Object, params: any): Promise<any>;
/**
* Fetches the contents of a file.
@@ -1689,7 +1689,7 @@ declare module protobuf {
* @param {function(?Error, string=)} [callback] Node-style callback
* @returns {Promise<string>|undefined} Promise if callback has been omitted
*/
function fetch(path: string, callback?: (() => any)): (Promise<string>|undefined);
function fetch(path: string, callback?: any): (Promise<string>|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.

0 comments on commit 79fbbf4

Please sign in to comment.