Skip to content

Commit

Permalink
Merge pull request #4 from timostamm/issue-3
Browse files Browse the repository at this point in the history
Drop const enums - fixes #3
  • Loading branch information
timostamm committed Sep 11, 2020
2 parents 77093f3 + 45cf668 commit 0689632
Show file tree
Hide file tree
Showing 8 changed files with 68 additions and 38 deletions.
4 changes: 2 additions & 2 deletions MANUAL.md
Original file line number Diff line number Diff line change
Expand Up @@ -1118,8 +1118,8 @@ several code generators:
| generator | version | optimize for | webpack output size |
|-------------------------|----------------:|-------------------|--------------------:|
| protobuf-ts | 1.0.1 | size | 40.627 b |
| protobuf-ts | 1.0.1 | speed | 69.189 b |
| protobuf-ts | 1.0.4 | size | 42,353 b |
| protobuf-ts | 1.0.4 | speed | 72,558 b |
| ts-proto | 1.26.0 | | 111.825 b |
| google-protobuf | 3.12.2 | | 396.934 b |
Expand Down
2 changes: 1 addition & 1 deletion packages/grpcweb-transport/src/grpc-web-format.ts
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ export function readGrpcWebResponseTrailer(data: Uint8Array): [GrpcStatusCode, s
* A grpc-frame type. Can be used to determine type of frame emitted by
* `readGrpcWebResponseBody()`.
*/
export const enum GrpcWebFrame { DATA = 0x00, TRAILER = 0x80 }
export enum GrpcWebFrame { DATA = 0x00, TRAILER = 0x80 }


/**
Expand Down
73 changes: 51 additions & 22 deletions packages/plugin/src/code-gen/field-info-generator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,11 @@ const scalarTypeMapping = {
export class FieldInfoGenerator {


// Use `2 /* NUMBER */` instead of `LongType.BIGINT`.
// Necessary for typescript compiler option "isolatedModules".
private inlineTypeEnums = true;


constructor(
private readonly registry: DescriptorRegistry,
private readonly imports: TypescriptImportManager,
Expand Down Expand Up @@ -81,10 +86,7 @@ export class FieldInfoGenerator {
if (fieldInfo.repeat !== undefined) {
properties.push(ts.createPropertyAssignment(
"repeat",
ts.createPropertyAccess(
ts.createIdentifier(this.imports.name('RepeatType', this.options.runtimeImportPath)),
repeatTypeMapping[fieldInfo.repeat],
)
this.createRepeatType(fieldInfo.repeat)
));
}

Expand All @@ -99,11 +101,11 @@ export class FieldInfoGenerator {
case "scalar":

// T: Scalar field type.
properties.push(ts.createPropertyAssignment("T", this.createScalarT(fieldInfo.T)));
properties.push(ts.createPropertyAssignment("T", this.createScalarType(fieldInfo.T)));

// L?: JavaScript long type
if (fieldInfo.L !== undefined) {
properties.push(ts.createPropertyAssignment("L", this.createScalarL(fieldInfo.L)));
properties.push(ts.createPropertyAssignment("L", this.createLongType(fieldInfo.L)));
}
break;

Expand All @@ -120,7 +122,7 @@ export class FieldInfoGenerator {

case "map":
// K: Map field key type.
properties.push(ts.createPropertyAssignment("K", this.createScalarT(fieldInfo.K)));
properties.push(ts.createPropertyAssignment("K", this.createScalarType(fieldInfo.K)));

// V: Map field value type.
properties.push(ts.createPropertyAssignment("V", this.createMapV(fieldInfo.V)));
Expand Down Expand Up @@ -162,7 +164,6 @@ export class FieldInfoGenerator {
return partial;
}


private createMessageT(type: rt.IMessageType<rt.UnknownMessage>): ts.ArrowFunction {
let descriptor = this.registry.resolveTypeName(type.typeName);
let generatedMessage = this.imports.type(descriptor);
Expand Down Expand Up @@ -192,24 +193,52 @@ export class FieldInfoGenerator {
}


private createScalarT(type: rt.ScalarType): ts.PropertyAccessExpression {
return ts.createPropertyAccess(
ts.createIdentifier(this.imports.name('ScalarType', this.options.runtimeImportPath)),
scalarTypeMapping[type],
)
private createRepeatType(type: rt.RepeatType): ts.Expression {
if (this.inlineTypeEnums) {
const expr = ts.createNumericLiteral(type.toString());
ts.addSyntheticTrailingComment(expr, ts.SyntaxKind.MultiLineCommentTrivia, `RepeatType.${repeatTypeMapping[type]}`);
return expr;
} else {
return ts.createPropertyAccess(
ts.createIdentifier(this.imports.name('RepeatType', this.options.runtimeImportPath)),
repeatTypeMapping[type],
);
}
}

private createScalarL(type: rt.LongType): ts.PropertyAccessExpression {
return ts.createPropertyAccess(
ts.createIdentifier(this.imports.name('LongType', this.options.runtimeImportPath)),
longTypeMapping[type],
)

private createScalarType(type: rt.ScalarType): ts.Expression {
if (this.inlineTypeEnums) {
const expr = ts.createNumericLiteral(type.toString());
ts.addSyntheticTrailingComment(expr, ts.SyntaxKind.MultiLineCommentTrivia, `ScalarType.${scalarTypeMapping[type]}`);
return expr;
} else {
return ts.createPropertyAccess(
ts.createIdentifier(this.imports.name('ScalarType', this.options.runtimeImportPath)),
scalarTypeMapping[type],
);
}
}


private createLongType(type: rt.LongType): ts.Expression {
if (this.inlineTypeEnums) {
const expr = ts.createNumericLiteral(type.toString());
ts.addSyntheticTrailingComment(expr, ts.SyntaxKind.MultiLineCommentTrivia, `LongType.${longTypeMapping[type]}`);
return expr;
} else {
return ts.createPropertyAccess(
ts.createIdentifier(this.imports.name('LongType', this.options.runtimeImportPath)),
longTypeMapping[type],
);
}
}


// V: Map field value type.
private createMapV(mapV: (rt.PartialFieldInfo & { kind: "map" })["V"]): ts.ObjectLiteralExpression {
let T: ts.PropertyAccessExpression | ts.ArrowFunction;
let L: ts.PropertyAccessExpression | undefined = undefined;
let T: ts.Expression;
let L: ts.Expression | undefined = undefined;
switch (mapV.kind) {
case "message":
T = this.createMessageT(mapV.T());
Expand All @@ -218,9 +247,9 @@ export class FieldInfoGenerator {
T = this.createEnumT(mapV.T());
break;
case "scalar":
T = this.createScalarT(mapV.T);
T = this.createScalarType(mapV.T);
if (mapV.L)
L = this.createScalarL(mapV.L);
L = this.createLongType(mapV.L);
break;
}
let properties: ts.ObjectLiteralElementLike[] = [
Expand Down
2 changes: 1 addition & 1 deletion packages/runtime-rpc/src/deferred.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export const enum DeferredState {
export enum DeferredState {
PENDING,
REJECTED,
RESOLVED
Expand Down
4 changes: 1 addition & 3 deletions packages/runtime/src/binary-format-contract.ts
Original file line number Diff line number Diff line change
Expand Up @@ -406,10 +406,8 @@ export interface IBinaryWriter {
* following value.
*
* See https://developers.google.com/protocol-buffers/docs/encoding#structure
*
* This is a const enum and cannot be used to lookup names.
*/
export const enum WireType {
export enum WireType {

/**
* Used for int32, int64, uint32, uint64, sint32, sint64, bool, enum
Expand Down
12 changes: 3 additions & 9 deletions packages/runtime/src/reflection-info.ts
Original file line number Diff line number Diff line change
Expand Up @@ -287,10 +287,8 @@ type fiPartialRules<T> = Omit<T, 'jsonName' | 'localName' | 'oneof' | 'repeat' |
* Scalar value types. This is a subset of field types declared by protobuf
* enum google.protobuf.FieldDescriptorProto.Type The types GROUP and MESSAGE
* are omitted, but the numerical values are identical.
*
* This is a const enum and cannot be used to lookup names.
*/
export const enum ScalarType {
export enum ScalarType {
// 0 is reserved for errors.
// Order is weird for historical reasons.
DOUBLE = 1,
Expand Down Expand Up @@ -340,10 +338,8 @@ export const enum ScalarType {
* uint64 my_field = 1 [jstype = JS_STRING];
* uint64 other_field = 2 [jstype = JS_NUMBER];
* ```
*
* This is a const enum and cannot be used to lookup names.
*/
export const enum LongType {
export enum LongType {

/**
* Use JavaScript `bigint`.
Expand Down Expand Up @@ -384,10 +380,8 @@ export const enum LongType {
* value for each element.
*
* `bytes` and `string` cannot be packed.
*
* This is a const enum and cannot be used to lookup names.
*/
export const enum RepeatType {
export enum RepeatType {

/**
* The field is not repeated.
Expand Down
2 changes: 2 additions & 0 deletions packages/test-generated/tsconfig.bigint.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
{
"extends": "./tsconfig.json",
"compilerOptions": {

// compiler requires this target for bigint
"target": "ES2020"
}
}
7 changes: 7 additions & 0 deletions packages/test-generated/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,13 @@
"module": "CommonJS",
"target": "ES2015",
"baseUrl": "./",


// activated for issue #3
// react sets this option, breaking const enum
"isolatedModules": true,


"strict": true,
"lib": [
"es2017",
Expand Down

0 comments on commit 0689632

Please sign in to comment.