Skip to content

Commit

Permalink
Merge pull request #52 from join-com/join-14379
Browse files Browse the repository at this point in the history
[JOIN-14379] Fix: runtime error on redundant imports
  • Loading branch information
castarco authored Oct 18, 2021
2 parents 8ed6800 + 51a431d commit a1b9e0a
Show file tree
Hide file tree
Showing 23 changed files with 1,912 additions and 24 deletions.
17 changes: 13 additions & 4 deletions build.sh
Original file line number Diff line number Diff line change
@@ -1,10 +1,19 @@
#!/bin/sh

# Flags
COMMIT_HASH="$(git rev-parse --short HEAD)"
BUILD_TIME="$(date +%s)"

COMMIT_FLAG="-X 'github.com/join-com/protoc-gen-ts/version.BuildCommit=${COMMIT_HASH}'"
TIME_FLAG="-X 'github.com/join-com/protoc-gen-ts/version.BuildTime=${BUILD_TIME}'"

GO_LD_FLAGS="${COMMIT_FLAG} ${TIME_FLAG}"

# Building binary
env GOOS=darwin GOARCH=amd64 go build -o ./dist/protoc-gen-tsx.darwin.amd64
env GOOS=darwin GOARCH=arm64 go build -o ./dist/protoc-gen-tsx.darwin.arm64
env GOOS=linux GOARCH=amd64 go build -o ./dist/protoc-gen-tsx.linux.amd64
env GOOS=linux GOARCH=arm64 go build -o ./dist/protoc-gen-tsx.linux.arm64
env GOOS=darwin GOARCH=amd64 go build -ldflags="${GO_LD_FLAGS}" -o ./dist/protoc-gen-tsx.darwin.amd64
env GOOS=darwin GOARCH=arm64 go build -ldflags="${GO_LD_FLAGS}" -o ./dist/protoc-gen-tsx.darwin.arm64
env GOOS=linux GOARCH=amd64 go build -ldflags="${GO_LD_FLAGS}" -o ./dist/protoc-gen-tsx.linux.amd64
env GOOS=linux GOARCH=arm64 go build -ldflags="${GO_LD_FLAGS}" -o ./dist/protoc-gen-tsx.linux.arm64

rm -f ./dist/protoc-gen-tsx;

Expand Down
24 changes: 24 additions & 0 deletions internal/generator/runner_generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"github.com/iancoleman/strcase"
"github.com/join-com/protoc-gen-ts/internal/join_proto"
"github.com/join-com/protoc-gen-ts/internal/utils"
"github.com/join-com/protoc-gen-ts/version"
"google.golang.org/protobuf/compiler/protogen"
"google.golang.org/protobuf/types/descriptorpb"
)
Expand All @@ -20,6 +21,7 @@ func (r *Runner) generateTypescriptFile(protoFile *protogen.File, generatedFileS
// TODO: Generate comment with version, in order to improve traceability & debugging experience
generatedFileStream.P(
"// GENERATED CODE -- DO NOT EDIT!\n",
"// GENERATOR VERSION: "+version.MajorVersion+"."+version.MinorVersion+"."+version.PatchVersion+"."+version.BuildCommit+"."+version.BuildTime+"\n",
"/* eslint-disable @typescript-eslint/no-non-null-assertion */\n",
)

Expand Down Expand Up @@ -121,6 +123,8 @@ func (r *Runner) generateTypescriptNamespace(generatedFileStream *protogen.Gener
)
r.indentLevel += 2

r.generateTypescriptClassDecoratorDefinition(generatedFileStream, protoFile)

// This interface is namespace-private, as it's being replicated for every generated file
r.P(
generatedFileStream,
Expand All @@ -142,6 +146,26 @@ func (r *Runner) generateTypescriptNamespace(generatedFileStream *protogen.Gener
r.P(generatedFileStream, "\n}")
}

func (r *Runner) generateTypescriptClassDecoratorDefinition(generatedFileStream *protogen.GeneratedFile, protoFile *protogen.File) {
r.P(
generatedFileStream,
"const registerGrpcClass = <T extends protobufjs.Message<T>>(",
" typeName: string",
"): protobufjs.TypeDecorator<T> => {",
" if (protobufjs.util.decorateRoot.get(typeName) != null) {",
" // eslint-disable-next-line @typescript-eslint/ban-types",
" return (",
" // eslint-disable-next-line @typescript-eslint/no-unused-vars",
" _: protobufjs.Constructor<T>",
" ): void => {",
" // Do nothing",
" }",
" }",
" return protobufjs.Type.d(typeName)",
"}",
)
}

func (r *Runner) generateTypescriptFlavors(generatedFileStream *protogen.GeneratedFile, protoFile *protogen.File) {
flavorBaseTypesMap := make(map[string]string)
flavorDeclarations := make([]string, 0)
Expand Down
2 changes: 1 addition & 1 deletion internal/generator/runner_generate_class.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ func (r *Runner) generateTypescriptMessageClass(generatedFileStream *protogen.Ge
}
r.P(
generatedFileStream,
"@protobufjs.Type.d('"+strings.Replace(r.currentPackage, ".", "_", -1)+"_"+className+"')",
"@registerGrpcClass('"+strings.Replace(r.currentPackage, ".", "_", -1)+"_"+className+"')",
"export class "+className+" extends protobufjs.Message<"+className+"> implements "+implementedInterfaces+" {\n",
)
r.indentLevel += 2
Expand Down
1 change: 1 addition & 0 deletions tests/.eslintignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
generated/
generatedLegacy/
generatedRedundant/
node_modules/
19 changes: 17 additions & 2 deletions tests/__tests__/generated/Flavors.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
// GENERATED CODE -- DO NOT EDIT!
// GENERATOR VERSION: 2.1.0.c2c6dc6.1634553468
/* eslint-disable @typescript-eslint/no-non-null-assertion */

import * as joinGRPC from '@join-com/grpc'
Expand All @@ -10,6 +11,20 @@ import { grpc } from '@join-com/grpc'

// eslint-disable-next-line @typescript-eslint/no-namespace
export namespace Flavors {
const registerGrpcClass = <T extends protobufjs.Message<T>>(
typeName: string
): protobufjs.TypeDecorator<T> => {
if (protobufjs.util.decorateRoot.get(typeName) != null) {
// eslint-disable-next-line @typescript-eslint/ban-types
return (
// eslint-disable-next-line @typescript-eslint/no-unused-vars
_: protobufjs.Constructor<T>
): void => {
// Do nothing
}
}
return protobufjs.Type.d(typeName)
}
interface ConvertibleTo<T> {
asInterface(): T
}
Expand All @@ -27,7 +42,7 @@ export namespace Flavors {
emails: Email[]
}

@protobufjs.Type.d('flavors_UserProfile')
@registerGrpcClass('flavors_UserProfile')
export class UserProfile
extends protobufjs.Message<UserProfile>
implements ConvertibleTo<IUserProfile>, IUserProfile
Expand Down Expand Up @@ -100,7 +115,7 @@ export namespace Flavors {
}
}

@protobufjs.Type.d('flavors_UserRequest')
@registerGrpcClass('flavors_UserRequest')
export class UserRequest
extends protobufjs.Message<UserRequest>
implements ConvertibleTo<IUserRequest>, IUserRequest
Expand Down
23 changes: 19 additions & 4 deletions tests/__tests__/generated/Regressions.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,25 @@
// GENERATED CODE -- DO NOT EDIT!
// GENERATOR VERSION: 2.1.0.c2c6dc6.1634553468
/* eslint-disable @typescript-eslint/no-non-null-assertion */

import * as protobufjs from 'protobufjs/light'

// eslint-disable-next-line @typescript-eslint/no-namespace
export namespace Regressions {
const registerGrpcClass = <T extends protobufjs.Message<T>>(
typeName: string
): protobufjs.TypeDecorator<T> => {
if (protobufjs.util.decorateRoot.get(typeName) != null) {
// eslint-disable-next-line @typescript-eslint/ban-types
return (
// eslint-disable-next-line @typescript-eslint/no-unused-vars
_: protobufjs.Constructor<T>
): void => {
// Do nothing
}
}
return protobufjs.Type.d(typeName)
}
interface ConvertibleTo<T> {
asInterface(): T
}
Expand Down Expand Up @@ -39,7 +54,7 @@ export namespace Regressions {
/**
* @deprecated
*/
@protobufjs.Type.d('regressions_DeprecatedMessageWithDeprecatedField')
@registerGrpcClass('regressions_DeprecatedMessageWithDeprecatedField')
export class DeprecatedMessageWithDeprecatedField
extends protobufjs.Message<DeprecatedMessageWithDeprecatedField>
implements
Expand Down Expand Up @@ -96,7 +111,7 @@ export namespace Regressions {
}
}

@protobufjs.Type.d('regressions_MessageWithDeprecatedField')
@registerGrpcClass('regressions_MessageWithDeprecatedField')
export class MessageWithDeprecatedField
extends protobufjs.Message<MessageWithDeprecatedField>
implements
Expand Down Expand Up @@ -148,7 +163,7 @@ export namespace Regressions {
}
}

@protobufjs.Type.d('regressions_Reg01Inner')
@registerGrpcClass('regressions_Reg01Inner')
export class Reg01Inner
extends protobufjs.Message<Reg01Inner>
implements ConvertibleTo<IReg01Inner>, IReg01Inner
Expand Down Expand Up @@ -189,7 +204,7 @@ export namespace Regressions {
}
}

@protobufjs.Type.d('regressions_Reg01Outer')
@registerGrpcClass('regressions_Reg01Outer')
export class Reg01Outer
extends protobufjs.Message<Reg01Outer>
implements ConvertibleTo<IReg01Outer>, IReg01Outer
Expand Down
27 changes: 21 additions & 6 deletions tests/__tests__/generated/Test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
// GENERATED CODE -- DO NOT EDIT!
// GENERATOR VERSION: 2.1.0.c2c6dc6.1634553468
/* eslint-disable @typescript-eslint/no-non-null-assertion */

import * as joinGRPC from '@join-com/grpc'
Expand All @@ -13,6 +14,20 @@ import { grpc } from '@join-com/grpc'

// eslint-disable-next-line @typescript-eslint/no-namespace
export namespace Foo {
const registerGrpcClass = <T extends protobufjs.Message<T>>(
typeName: string
): protobufjs.TypeDecorator<T> => {
if (protobufjs.util.decorateRoot.get(typeName) != null) {
// eslint-disable-next-line @typescript-eslint/ban-types
return (
// eslint-disable-next-line @typescript-eslint/no-unused-vars
_: protobufjs.Constructor<T>
): void => {
// Do nothing
}
}
return protobufjs.Type.d(typeName)
}
interface ConvertibleTo<T> {
asInterface(): T
}
Expand Down Expand Up @@ -101,7 +116,7 @@ export namespace Foo {
optionalField?: number
}

@protobufjs.Type.d('foo_CustomOptionsTest')
@registerGrpcClass('foo_CustomOptionsTest')
export class CustomOptionsTest
extends protobufjs.Message<CustomOptionsTest>
implements ConvertibleTo<ICustomOptionsTest>
Expand Down Expand Up @@ -182,7 +197,7 @@ export namespace Foo {
/**
* @deprecated
*/
@protobufjs.Type.d('foo_Nested')
@registerGrpcClass('foo_Nested')
export class Nested
extends protobufjs.Message<Nested>
implements ConvertibleTo<INested>, INested
Expand Down Expand Up @@ -223,7 +238,7 @@ export namespace Foo {
}
}

@protobufjs.Type.d('foo_Request')
@registerGrpcClass('foo_Request')
export class Request
extends protobufjs.Message<Request>
implements ConvertibleTo<IRequest>, IRequest
Expand Down Expand Up @@ -264,7 +279,7 @@ export namespace Foo {
}
}

@protobufjs.Type.d('foo_RequiredPropertiesTest')
@registerGrpcClass('foo_RequiredPropertiesTest')
export class RequiredPropertiesTest
extends protobufjs.Message<RequiredPropertiesTest>
implements ConvertibleTo<IRequiredPropertiesTest>, IRequiredPropertiesTest
Expand Down Expand Up @@ -335,7 +350,7 @@ export namespace Foo {
}
}

@protobufjs.Type.d('foo_Test')
@registerGrpcClass('foo_Test')
export class Test
extends protobufjs.Message<Test>
implements ConvertibleTo<ITest>
Expand Down Expand Up @@ -535,7 +550,7 @@ export namespace Foo {
}
}

@protobufjs.Type.d('foo_BigWrapper')
@registerGrpcClass('foo_BigWrapper')
export class BigWrapper
extends protobufjs.Message<BigWrapper>
implements ConvertibleTo<IBigWrapper>
Expand Down
17 changes: 16 additions & 1 deletion tests/__tests__/generated/common/Common.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,25 @@
// GENERATED CODE -- DO NOT EDIT!
// GENERATOR VERSION: 2.1.0.c2c6dc6.1634553468
/* eslint-disable @typescript-eslint/no-non-null-assertion */

import * as protobufjs from 'protobufjs/light'

// eslint-disable-next-line @typescript-eslint/no-namespace
export namespace Common {
const registerGrpcClass = <T extends protobufjs.Message<T>>(
typeName: string
): protobufjs.TypeDecorator<T> => {
if (protobufjs.util.decorateRoot.get(typeName) != null) {
// eslint-disable-next-line @typescript-eslint/ban-types
return (
// eslint-disable-next-line @typescript-eslint/no-unused-vars
_: protobufjs.Constructor<T>
): void => {
// Do nothing
}
}
return protobufjs.Type.d(typeName)
}
interface ConvertibleTo<T> {
asInterface(): T
}
Expand All @@ -17,7 +32,7 @@ export namespace Common {
latsName?: string
}

@protobufjs.Type.d('common_OtherPkgMessage')
@registerGrpcClass('common_OtherPkgMessage')
export class OtherPkgMessage
extends protobufjs.Message<OtherPkgMessage>
implements ConvertibleTo<IOtherPkgMessage>, IOtherPkgMessage
Expand Down
17 changes: 16 additions & 1 deletion tests/__tests__/generated/common/Extra.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
// GENERATED CODE -- DO NOT EDIT!
// GENERATOR VERSION: 2.1.0.c2c6dc6.1634553468
/* eslint-disable @typescript-eslint/no-non-null-assertion */

import * as protobufjs from 'protobufjs/light'
Expand All @@ -7,6 +8,20 @@ import { GoogleProtobuf } from '../google/protobuf/Timestamp'

// eslint-disable-next-line @typescript-eslint/no-namespace
export namespace Common {
const registerGrpcClass = <T extends protobufjs.Message<T>>(
typeName: string
): protobufjs.TypeDecorator<T> => {
if (protobufjs.util.decorateRoot.get(typeName) != null) {
// eslint-disable-next-line @typescript-eslint/ban-types
return (
// eslint-disable-next-line @typescript-eslint/no-unused-vars
_: protobufjs.Constructor<T>
): void => {
// Do nothing
}
}
return protobufjs.Type.d(typeName)
}
interface ConvertibleTo<T> {
asInterface(): T
}
Expand All @@ -20,7 +35,7 @@ export namespace Common {
birthDate?: Date
}

@protobufjs.Type.d('common_ExtraPkgMessage')
@registerGrpcClass('common_ExtraPkgMessage')
export class ExtraPkgMessage
extends protobufjs.Message<ExtraPkgMessage>
implements ConvertibleTo<IExtraPkgMessage>
Expand Down
17 changes: 16 additions & 1 deletion tests/__tests__/generated/google/protobuf/Empty.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,32 @@
// GENERATED CODE -- DO NOT EDIT!
// GENERATOR VERSION: 2.1.0.c2c6dc6.1634553468
/* eslint-disable @typescript-eslint/no-non-null-assertion */

import * as protobufjs from 'protobufjs/light'

// eslint-disable-next-line @typescript-eslint/no-namespace
export namespace GoogleProtobuf {
const registerGrpcClass = <T extends protobufjs.Message<T>>(
typeName: string
): protobufjs.TypeDecorator<T> => {
if (protobufjs.util.decorateRoot.get(typeName) != null) {
// eslint-disable-next-line @typescript-eslint/ban-types
return (
// eslint-disable-next-line @typescript-eslint/no-unused-vars
_: protobufjs.Constructor<T>
): void => {
// Do nothing
}
}
return protobufjs.Type.d(typeName)
}
interface ConvertibleTo<T> {
asInterface(): T
}

export interface IEmpty {}

@protobufjs.Type.d('google_protobuf_Empty')
@registerGrpcClass('google_protobuf_Empty')
export class Empty
extends protobufjs.Message<Empty>
implements ConvertibleTo<IEmpty>, IEmpty
Expand Down
Loading

0 comments on commit a1b9e0a

Please sign in to comment.