Skip to content

Commit

Permalink
Breaking: Replaced Buffer and Long types with interfaces and removed …
Browse files Browse the repository at this point in the history
…stubs; CLI: Further TypeScript definition improvements
  • Loading branch information
dcodeIO committed Apr 16, 2017
1 parent 22f907c commit ff85800
Show file tree
Hide file tree
Showing 10 changed files with 103 additions and 212 deletions.
7 changes: 1 addition & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -465,12 +465,7 @@ Note that the service API is meant for clients. Implementing a server-side endpo

The library ships with its own [type definitions](https://github.com/dcodeIO/protobuf.js/blob/master/index.d.ts) and modern editors like [Visual Studio Code](https://code.visualstudio.com/) will automatically detect and use them for code completion.

**Note** that the npm package depends on [@types/node](https://www.npmjs.com/package/@types/node) because of `Buffer` and [@types/long](https://www.npmjs.com/package/@types/long) because of `Long`. If you are not building for node and/or not using long.js and want to exclude their full type definitions manually for whatever reason, there are two stubs available that can be referenced instead of the respective full type definition:

```ts
/// <reference path="./node_modules/protobufjs/stub-long.d.ts" />
/// <reference path="./node_modules/protobufjs/stub-node.d.ts" />
```
The npm package depends on [@types/node](https://www.npmjs.com/package/@types/node) because of `Buffer` and [@types/long](https://www.npmjs.com/package/@types/long) because of `Long`. If you are not building for node and/or not using long.js, it should be safe to exclude them manually.

#### Using the JS API

Expand Down
7 changes: 4 additions & 3 deletions cli/lib/tsd-jsdoc/publish.js
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ function writeln() {
indentWritten = false;
}

var skipTagsRe = /@(type|memberof|name|exports|interface|extends|implements|constructor|function|template|property|this|typedef|variation|example|returns \{undefined})[^@$]*/g,
var skipTagsRe = /@(type|tstype|const|memberof|name|exports|interface|extends|implements|constructor|function|template|property|this|typedef|variation|example|returns \{undefined})[^@$]*/g,
skipTypeRe = /(@[^ ]+) \{[^\s$]+}/g;

// writes a comment
Expand Down Expand Up @@ -468,7 +468,7 @@ function handleClass(element, parent) {
// class-compatible members
var incompatible = [];
getChildrenOf(element).forEach(function(child) {
if (isClassLike(child) || element.kind === "module" || element.kind === "typedef") {
if (isClassLike(child) || child.kind === "module" || child.kind === "typedef" || child.isEnum) {
incompatible.push(child);
return;
}
Expand Down Expand Up @@ -515,7 +515,8 @@ function handleMember(element, parent) {

} else {

if (isClassLike(parent)) {
var inClass = isClassLike(parent);
if (inClass) {
write(element.access || "public", " ");
if (element.scope === "static")
write("static ");
Expand Down
36 changes: 8 additions & 28 deletions ext/descriptor/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ export interface IFieldDescriptorProto {
defaultValue?: string;
oneofIndex?: number;
jsonName?: any;
options?: IFieldOptionsProperties;
options?: IFieldOptions;
}

type IFieldDescriptorProtoLabel = number;
Expand Down Expand Up @@ -138,17 +138,9 @@ export const FileDescriptorSet: $protobuf.Type;

export const FileDescriptorProto: $protobuf.Type;

export const DescriptorProto: $protobuf.Type;
export const DescriptorProto: $protobuf.Type & { ExtensionRange: $protobuf.Type, ReservedRange: $protobuf.Type};

export const DescriptorProtoExtensionRange: $protobuf.Type;

export const DescriptorProtoReservedRange: $protobuf.Type;

export const FieldDescriptorProto: $protobuf.Type;

export const FieldDescriptorProtoLabel: $protobuf.Enum;

export const FieldDescriptorProtoType: $protobuf.Enum;
export const FieldDescriptorProto: $protobuf.Type & { Label: $protobuf.Enum, Type: $protobuf.Enum};

export const OneofDescriptorProto: $protobuf.Type;

Expand All @@ -160,17 +152,11 @@ export const EnumValueDescriptorProto: $protobuf.Type;

export const MethodDescriptorProto: $protobuf.Type;

export const FileOptions: $protobuf.Type;

export const FileOptionsOptimizeMode: $protobuf.Enum;
export const FileOptions: $protobuf.Type & { OptimizeMode: $protobuf.Enum};

export const MessageOptions: $protobuf.Type;

export const FieldOptions: $protobuf.Type;

export const FieldOptionsCType: $protobuf.Enum;

export const FieldOptionsJSType: $protobuf.Enum;
export const FieldOptions: $protobuf.Type & { CType: $protobuf.Enum, JSType: $protobuf.Enum};

export const OneofOptions: $protobuf.Type;

Expand All @@ -182,14 +168,8 @@ export const ServiceOptions: $protobuf.Type;

export const MethodOptions: $protobuf.Type;

export const UninterpretedOption: $protobuf.Type;

export const UninterpretedOptionNamePart: $protobuf.Type;

export const SourceCodeInfo: $protobuf.Type;

export const SourceCodeInfoLocation: $protobuf.Type;
export const UninterpretedOption: $protobuf.Type & { NamePart: $protobuf.Type};

export const GeneratedCodeInfo: $protobuf.Type;
export const SourceCodeInfo: $protobuf.Type & { Location: $protobuf.Type};

export const GeneratedCodeInfoAnnotation: $protobuf.Type;
export const GeneratedCodeInfo: $protobuf.Type & { Annotation: $protobuf.Type};
Expand Down
133 changes: 35 additions & 98 deletions ext/descriptor/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -313,7 +313,7 @@ Type.prototype.toDescriptor = function toDescriptor(syntax) {
* @property {string} [defaultValue] Literal default value
* @property {number} [oneofIndex] Oneof index if part of a oneof
* @property {*} [jsonName] Not supported
* @property {IFieldOptionsProperties} [options] Field options
* @property {IFieldOptions} [options] Field options
*/

/**
Expand Down Expand Up @@ -874,54 +874,28 @@ function underScore(str) {
* Reflected descriptor proto.
* @name DescriptorProto
* @type {Type}
* @property {Type} ExtensionRange
* @property {Type} ReservedRange
* @const
* @tstype $protobuf.Type
*/

/**
* Reflected descriptor proto extension range.
* @name DescriptorProtoExtensionRange
* @type {Type}
* @const
* @tstype $protobuf.Type
* @tstype $protobuf.Type & {
* ExtensionRange: $protobuf.Type,
* ReservedRange: $protobuf.Type
* }
*/
exports.DescriptorProtoExtensionRange = exports.DescriptorProto.ExtensionRange;

/**
* Reflected descriptor proto reserved range.
* @name DescriptorProtoReservedRange
* @type {Type}
* @const
* @tstype $protobuf.Type
*/
exports.DescriptorProtoReservedRange = exports.DescriptorProto.ReservedRange;

/**
* Reflected field descriptor proto.
* @name FieldDescriptorProto
* @type {Type}
* @property {Enum} Label
* @property {Enum} Type
* @const
* @tstype $protobuf.Type
* @tstype $protobuf.Type & {
* Label: $protobuf.Enum,
* Type: $protobuf.Enum
* }
*/

/**
* Reflected field descriptor proto label.
* @name FieldDescriptorProtoLabel
* @type {Enum}
* @const
* @tstype $protobuf.Enum
*/
exports.FieldDescriptorProtoLabel = exports.FieldDescriptorProto.Label;

/**
* Reflected field descriptor proto type.
* @name FieldDescriptorProtoType
* @type {Enum}
* @const
* @tstype $protobuf.Enum
*/
exports.FieldDescriptorProtoType = exports.FieldDescriptorProto.Type;

/**
* Reflected oneof descriptor proto.
* @name OneofDescriptorProto
Expand Down Expand Up @@ -966,19 +940,13 @@ exports.FieldDescriptorProtoType = exports.FieldDescriptorProto.Type;
* Reflected file options.
* @name FileOptions
* @type {Type}
* @property {Enum} OptimizeMode
* @const
* @tstype $protobuf.Type
* @tstype $protobuf.Type & {
* OptimizeMode: $protobuf.Enum
* }
*/

/**
* Reflected file options optimize mode.
* @name FileOptionsOptimizeMode
* @type {Type}
* @const
* @tstype $protobuf.Enum
*/
exports.FileOptionsOptimizeMode = exports.FileOptions.OptimizeMode;

/**
* Reflected message options.
* @name MessageOptions
Expand All @@ -991,27 +959,14 @@ exports.FileOptionsOptimizeMode = exports.FileOptions.OptimizeMode;
* Reflected field options.
* @name FieldOptions
* @type {Type}
* @property {Enum} CType
* @property {Enum} JSType
* @const
* @tstype $protobuf.Type
*/

/**
* Reflected field options c-type.
* @name FieldOptionsCType
* @type {Enum}
* @const
* @tstype $protobuf.Enum
*/
exports.FieldOptionsCType = exports.FieldOptions.CType;

/**
* Reflected field options js-type.
* @name FieldOptionsJSType
* @type {Enum}
* @const
* @tstype $protobuf.Enum
* @tstype $protobuf.Type & {
* CType: $protobuf.Enum,
* JSType: $protobuf.Enum
* }
*/
exports.FieldOptionsJSType = exports.FieldOptions.JSType;

/**
* Reflected oneof options.
Expand Down Expand Up @@ -1057,49 +1012,31 @@ exports.FieldOptionsJSType = exports.FieldOptions.JSType;
* Reflected uninterpretet option.
* @name UninterpretedOption
* @type {Type}
* @property {Type} NamePart
* @const
* @tstype $protobuf.Type
*/

/**
* Reflected uninterpreted option name part.
* @name UninterpretedOptionNamePart
* @type {Type}
* @const
* @tstype $protobuf.Type
* @tstype $protobuf.Type & {
* NamePart: $protobuf.Type
* }
*/
exports.UninterpretedOptionNamePart = exports.UninterpretedOption.NamePart;

/**
* Reflected source code info.
* @name SourceCodeInfo
* @type {Type}
* @property {Type} Location
* @const
* @tstype $protobuf.Type
*/

/**
* Reflected source code info location.
* @name SourceCodeInfoLocation
* @type {Type}
* @const
* @tstype $protobuf.Type
* @tstype $protobuf.Type & {
* Location: $protobuf.Type
* }
*/
exports.SourceCodeInfoLocation = exports.SourceCodeInfo.Location;

/**
* Reflected generated code info.
* @name GeneratedCodeInfo
* @type {Type}
* @property {Type} Annotation
* @const
* @tstype $protobuf.Type
*/

/**
* Reflected generated code info annotation.
* @name GeneratedCodeInfoAnnotation
* @type {Type}
* @const
* @tstype $protobuf.Type
* @tstype $protobuf.Type & {
* Annotation: $protobuf.Type
* }
*/
exports.GeneratedCodeInfoAnnotation = exports.GeneratedCodeInfo.Annotation;
Loading

0 comments on commit ff85800

Please sign in to comment.