From e79158450253410b0cb7bc99920517acaa8b1be7 Mon Sep 17 00:00:00 2001 From: Yun Lai Date: Mon, 24 May 2021 11:26:20 +1000 Subject: [PATCH] Add option enable_styling_check to toggle styling check, the option is false by default. Making it an option to make sure the generated code disablees styling check by default, while leaving it on in the CI to make sure the generated code has some good level of readability. --- data/file.go | 2 ++ generator/generator.go | 22 ++++++++++++++++++++-- generator/template.go | 8 ++++++++ integration_tests/scripts/gen-protos.sh | 3 ++- integration_tests/scripts/source.sh | 2 +- 5 files changed, 33 insertions(+), 4 deletions(-) diff --git a/data/file.go b/data/file.go index 7abb3f9..0299ea1 100644 --- a/data/file.go +++ b/data/file.go @@ -26,6 +26,8 @@ type File struct { TSFileName string // PackageNonScalarType stores the type inside the same packages within the file, which will be used to figure out external dependencies inside the same package (different files) PackageNonScalarType []Type + // EnableStylingCheck enables the styling check for the given file + EnableStylingCheck bool } // StableDependencies are dependencies in a stable order. diff --git a/generator/generator.go b/generator/generator.go index 9dd3762..f366ee6 100644 --- a/generator/generator.go +++ b/generator/generator.go @@ -18,8 +18,17 @@ import ( // TypeScriptGRPCGatewayGenerator is the protobuf generator for typescript type TypeScriptGRPCGatewayGenerator struct { Registry *registry.Registry + // EnableStylingCheck enables both eslint and tsc check for the generated code + // This option will only turn on in integration test to ensure the readability in + // the generated code. + EnableStylingCheck bool } +const ( + // EnableStylingCheckOption is the option name for EnableStylingCheck + EnableStylingCheckOption = "enable_styling_check" +) + // New returns an initialised generator func New(paramsMap map[string]string) (*TypeScriptGRPCGatewayGenerator, error) { registry, err := registry.NewRegistry(paramsMap) @@ -27,8 +36,16 @@ func New(paramsMap map[string]string) (*TypeScriptGRPCGatewayGenerator, error) { return nil, errors.Wrap(err, "error instantiating a new registry") } + enableStylingCheck := false + enableStylingCheckVal, ok := paramsMap[EnableStylingCheckOption] + if ok { + // default to true if not disabled specifi + enableStylingCheck = enableStylingCheckVal == "true" + } + return &TypeScriptGRPCGatewayGenerator{ - Registry: registry, + Registry: registry, + EnableStylingCheck: enableStylingCheck, }, nil } @@ -46,6 +63,7 @@ func (t *TypeScriptGRPCGatewayGenerator) Generate(req *plugin.CodeGeneratorReque needToGenerateFetchModule := false // feed fileData into rendering process for _, fileData := range filesData { + fileData.EnableStylingCheck = t.EnableStylingCheck if !t.Registry.IsFileToGenerate(fileData.Name) { log.Debugf("file %s is not the file to generate, skipping", fileData.Name) continue @@ -100,7 +118,7 @@ func (t *TypeScriptGRPCGatewayGenerator) generateFile(fileData *data.File, tmpl func (t *TypeScriptGRPCGatewayGenerator) generateFetchModule(tmpl *template.Template) (*plugin.CodeGeneratorResponse_File, error) { w := bytes.NewBufferString("") fileName := filepath.Join(t.Registry.FetchModuleDirectory, t.Registry.FetchModuleFilename) - err := tmpl.Execute(w, nil) + err := tmpl.Execute(w, &data.File{EnableStylingCheck: t.EnableStylingCheck}) if err != nil { return nil, errors.Wrapf(err, "error generating fetch module at %s", fileName) } diff --git a/generator/template.go b/generator/template.go index bf02c98..439f799 100644 --- a/generator/template.go +++ b/generator/template.go @@ -66,6 +66,10 @@ export type {{.Name}} = { } {{end}}{{end}} +{{- if not .EnableStylingCheck}} +/* eslint-disable */ +// @ts-nocheck +{{- end}} /* * This file is a generated Typescript file for GRPC Gateway, DO NOT MODIFY */ @@ -86,6 +90,10 @@ type OneOf = ` const fetchTmpl = ` +{{- if not .EnableStylingCheck}} +/* eslint-disable */ +// @ts-nocheck +{{- end}} /* * This file is a generated Typescript file for GRPC Gateway, DO NOT MODIFY */ diff --git a/integration_tests/scripts/gen-protos.sh b/integration_tests/scripts/gen-protos.sh index 165bbd5..c354c31 100755 --- a/integration_tests/scripts/gen-protos.sh +++ b/integration_tests/scripts/gen-protos.sh @@ -1,6 +1,7 @@ #!/bin/bash USE_PROTO_NAMES=${1:-"false"} +ENABLE_STYLING_CHECK=${2:-"false"} cd .. && go install && cd integration_tests && \ protoc -I . -I ../.. \ - --grpc-gateway-ts_out=logtostderr=true,use_proto_names=$USE_PROTO_NAMES,loglevel=debug:./ \ + --grpc-gateway-ts_out=logtostderr=true,use_proto_names=$USE_PROTO_NAMES,enable_styling_check=$ENABLE_STYLING_CHECK,loglevel=debug:./ \ service.proto msg.proto empty.proto \ No newline at end of file diff --git a/integration_tests/scripts/source.sh b/integration_tests/scripts/source.sh index 78c689c..eabb1c6 100755 --- a/integration_tests/scripts/source.sh +++ b/integration_tests/scripts/source.sh @@ -3,7 +3,7 @@ function runTest { ORIG_NAME=${1:="false"} CONFIG_NAME=${2:="karma.conf.js"} - ./scripts/gen-protos.sh $ORIG_NAME + ./scripts/gen-protos.sh $ORIG_NAME true go run ./ -orig=$ORIG_NAME & pid=$!