Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 16 additions & 10 deletions cli/lib/tsd-jsdoc/publish.js
Original file line number Diff line number Diff line change
Expand Up @@ -276,21 +276,27 @@ function getTypeOf(element) {
// Ensure upper case Object for map expressions below
name = name.replace(/\bobject\b/g, "Object");

// Convert innermost generic types first so Array.<Object.<...>>
// and Object.<string,Array.<...>> are both handled correctly
var prevName;
do {
prevName = name;
name = name.replace(/\bObject\.?<([^,<>]*), *([^<>]*)>/gi, function($0, $1, $2) {
return "{ [k: " + $1 + "]: " + $2 + " }";
});
name = name.replace(/\bArray\.?<([^<>]*)>/gi, function($0, $1) {
return $1 + "[]";
});
name = name.replace(/\b(?!Object|Array)([\w$]+)\.<([^<>]*)>/gi, function($0, $1, $2) {
return $1 + "<" + $2 + ">";
});
} while (name !== prevName);

// Correct Something.<Something> to Something<Something>
name = replaceRecursive(name, /\b(?!Object|Array)([\w$]+)\.<([^>]*)>/gi, function($0, $1, $2) {
return $1 + "<" + $2 + ">";
});

// Replace Array.<string> with string[]
name = replaceRecursive(name, /\bArray\.?<([^>]*)>/gi, function($0, $1) {
return $1 + "[]";
});

// Replace Object.<string,number> with { [k: string]: number }
name = replaceRecursive(name, /\bObject\.?<([^,]*), *([^>]*)>/gi, function($0, $1, $2) {
return "{ [k: " + $1 + "]: " + $2 + " }";
});

// Replace functions (there are no signatures) with Function
name = name.replace(/\bfunction(?:\(\))?\b/g, "Function");

Expand Down
38 changes: 19 additions & 19 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -261,6 +261,15 @@ export class Field extends FieldBase {
*/
constructor(name: string, id: number, type: string, rule?: (string|{ [k: string]: any }), extend?: (string|{ [k: string]: any }), options?: { [k: string]: any });

/**
* Field decorator (TypeScript).
* @param fieldId Field id
* @param fieldType Field type
* @param [fieldRule="optional"] Field rule
* @returns Decorator function
*/
public static d<T extends Message<T>>(fieldId: number, fieldType: (Constructor<T>|string), fieldRule?: ("optional"|"required"|"repeated")): FieldDecorator;

/**
* Constructs a field from a field descriptor.
* @param name Field name
Expand Down Expand Up @@ -297,15 +306,6 @@ export class Field extends FieldBase {
* @returns Decorator function
*/
public static d<T extends number | number[] | Long | Long[] | string | string[] | boolean | boolean[] | Uint8Array | Uint8Array[] | Buffer | Buffer[]>(fieldId: number, fieldType: ("double"|"float"|"int32"|"uint32"|"sint32"|"fixed32"|"sfixed32"|"int64"|"uint64"|"sint64"|"fixed64"|"sfixed64"|"string"|"bool"|"bytes"|object), fieldRule?: ("optional"|"required"|"repeated"), defaultValue?: T): FieldDecorator;

/**
* Field decorator (TypeScript).
* @param fieldId Field id
* @param fieldType Field type
* @param [fieldRule="optional"] Field rule
* @returns Decorator function
*/
public static d<T extends Message<T>>(fieldId: number, fieldType: (Constructor<T>|string), fieldRule?: ("optional"|"required"|"repeated")): FieldDecorator;
}

/** Base class of all reflected message fields. This is not an actual class but here for the sake of having consistent type definitions. */
Expand Down Expand Up @@ -539,15 +539,15 @@ export class Message<T extends object = object> {
*/
constructor(properties?: Properties<T>);

/** Unknown fields preserved while decoding */
public $unknowns?: Uint8Array[];

/** Reference to the reflected type. */
public static readonly $type: Type;

/** Reference to the reflected type. */
public readonly $type: Type;

/** Unknown fields preserved while decoding. */
public $unknowns?: Uint8Array[];

/**
* Creates a new message of this type using the specified properties.
* @param [properties] Properties to set
Expand Down Expand Up @@ -627,9 +627,9 @@ export class Method extends ReflectionObject {
* @param [responseStream] Whether the response is streamed
* @param [options] Declared options
* @param [comment] The comment for this method
* @param [parsedOptions] Declared options, properly parsed into an object
* @param [parsedOptions] Declared options, properly parsed into objects
*/
constructor(name: string, type: (string|undefined), requestType: string, responseType: string, requestStream?: (boolean|{ [k: string]: any }), responseStream?: (boolean|{ [k: string]: any }), options?: { [k: string]: any }, comment?: string, parsedOptions?: { [k: string]: any });
constructor(name: string, type: (string|undefined), requestType: string, responseType: string, requestStream?: (boolean|{ [k: string]: any }), responseStream?: (boolean|{ [k: string]: any }), options?: { [k: string]: any }, comment?: string, parsedOptions?: { [k: string]: any }[]);

/** Method type. */
public type: string;
Expand All @@ -655,8 +655,8 @@ export class Method extends ReflectionObject {
/** Comment for this method */
public comment: (string|null);

/** Options properly parsed into an object */
public parsedOptions: any;
/** Options properly parsed into objects */
public parsedOptions?: { [k: string]: any }[];

/**
* Constructs a method from a method descriptor.
Expand Down Expand Up @@ -699,8 +699,8 @@ export interface IMethod {
/** Method comments */
comment: string;

/** Method options properly parsed into an object */
parsedOptions?: { [k: string]: any };
/** Method options properly parsed into objects */
parsedOptions?: { [k: string]: any }[];
}

/** Reflected namespace. */
Expand Down Expand Up @@ -901,7 +901,7 @@ export abstract class ReflectionObject {
public options?: { [k: string]: any };

/** Parsed Options. */
public parsedOptions?: { [k: string]: any[] };
public parsedOptions?: { [k: string]: any }[];

/** Unique name within its namespace. */
public name: string;
Expand Down
7 changes: 4 additions & 3 deletions src/method.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ var util = require("./util");
* @param {boolean|Object.<string,*>} [responseStream] Whether the response is streamed
* @param {Object.<string,*>} [options] Declared options
* @param {string} [comment] The comment for this method
* @param {Object.<string,*>} [parsedOptions] Declared options, properly parsed into an object
* @param {Array.<Object.<string,*>>} [parsedOptions] Declared options, properly parsed into objects
*/
function Method(name, type, requestType, responseType, requestStream, responseStream, options, comment, parsedOptions) {

Expand Down Expand Up @@ -96,7 +96,8 @@ function Method(name, type, requestType, responseType, requestStream, responseSt
this.comment = comment;

/**
* Options properly parsed into an object
* Options properly parsed into objects
* @type {Array.<Object.<string,*>>|undefined}
*/
this.parsedOptions = parsedOptions;
}
Expand All @@ -111,7 +112,7 @@ function Method(name, type, requestType, responseType, requestStream, responseSt
* @property {boolean} [responseStream=false] Whether responses are streamed
* @property {Object.<string,*>} [options] Method options
* @property {string} comment Method comments
* @property {Object.<string,*>} [parsedOptions] Method options properly parsed into an object
* @property {Array.<Object.<string,*>>} [parsedOptions] Method options properly parsed into objects
*/

/**
Expand Down
4 changes: 4 additions & 0 deletions tests/comp_typescript.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ var __decorate = (this && this.__decorate) || function (decorators, target, key,
var __metadata = (this && this.__metadata) || function (k, v) {
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
};
var _a, _b;
exports.__esModule = true;
exports.AwesomeMessage = exports.AwesomeSubMessage = exports.AwesomeEnum = exports.Hello = void 0;
var __1 = require("..");
Expand All @@ -41,6 +42,9 @@ var root = __1.Root.fromJSON({
});
var HelloReflected = root.lookupType("Hello");
HelloReflected.create({ value: "hi" });
var parsedOptionValue = (_a = HelloReflected.parsedOptions) === null || _a === void 0 ? void 0 : _a[0]["(custom_option)"];
var reflectedMethod = new __1.Method("Call", undefined, "Hello", "Hello", false, false, undefined, undefined, [{ option: 1 }]);
var parsedMethodOptionValue = (_b = reflectedMethod.parsedOptions) === null || _b === void 0 ? void 0 : _b[0].option;
// Custom classes
var Hello = /** @class */ (function (_super) {
__extends(Hello, _super);
Expand Down
6 changes: 5 additions & 1 deletion tests/comp_typescript.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// test currently consists only of not throwing

import { Root, Message, Type, Field, MapField, OneOf } from "..";
import { Root, Message, Method, Type, Field, MapField, OneOf } from "..";

// Reflection
const root = Root.fromJSON({
Expand All @@ -20,6 +20,10 @@ const HelloReflected = root.lookupType("Hello");

HelloReflected.create({ value: "hi" });

const parsedOptionValue: number | undefined = HelloReflected.parsedOptions?.[0]["(custom_option)"];
const reflectedMethod = new Method("Call", undefined, "Hello", "Hello", false, false, undefined, undefined, [{ option: 1 }]);
const parsedMethodOptionValue: number | undefined = reflectedMethod.parsedOptions?.[0].option;

// Custom classes

export class Hello extends Message<Hello> {
Expand Down