Skip to content

Commit

Permalink
improve TypeScript support. add simple test script.
Browse files Browse the repository at this point in the history
  • Loading branch information
endel committed Dec 2, 2016
1 parent 96fa07a commit 79fbbf4
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 25 deletions.
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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",
Expand Down
5 changes: 4 additions & 1 deletion scripts/types.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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"));
4 changes: 2 additions & 2 deletions src/inherits.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand All @@ -40,7 +40,7 @@ function inherits(clazz, type, options) {
*/

var classProperties = {

/**
* Reference to the reflected type.
* @name Class.$type
Expand Down
14 changes: 14 additions & 0 deletions types/protobuf.js-test.ts
Original file line number Diff line number Diff line change
@@ -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"));
42 changes: 21 additions & 21 deletions types/protobuf.js.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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.
Expand All @@ -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.
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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);

}

Expand Down Expand Up @@ -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;

}

Expand All @@ -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}.
Expand All @@ -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;
}


Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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.
Expand All @@ -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.
Expand Down Expand Up @@ -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.
Expand Down

0 comments on commit 79fbbf4

Please sign in to comment.