Skip to content

Commit e791584

Browse files
committed
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.
1 parent 69c2d74 commit e791584

File tree

5 files changed

+33
-4
lines changed

5 files changed

+33
-4
lines changed

data/file.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ type File struct {
2626
TSFileName string
2727
// 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)
2828
PackageNonScalarType []Type
29+
// EnableStylingCheck enables the styling check for the given file
30+
EnableStylingCheck bool
2931
}
3032

3133
// StableDependencies are dependencies in a stable order.

generator/generator.go

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,17 +18,34 @@ import (
1818
// TypeScriptGRPCGatewayGenerator is the protobuf generator for typescript
1919
type TypeScriptGRPCGatewayGenerator struct {
2020
Registry *registry.Registry
21+
// EnableStylingCheck enables both eslint and tsc check for the generated code
22+
// This option will only turn on in integration test to ensure the readability in
23+
// the generated code.
24+
EnableStylingCheck bool
2125
}
2226

27+
const (
28+
// EnableStylingCheckOption is the option name for EnableStylingCheck
29+
EnableStylingCheckOption = "enable_styling_check"
30+
)
31+
2332
// New returns an initialised generator
2433
func New(paramsMap map[string]string) (*TypeScriptGRPCGatewayGenerator, error) {
2534
registry, err := registry.NewRegistry(paramsMap)
2635
if err != nil {
2736
return nil, errors.Wrap(err, "error instantiating a new registry")
2837
}
2938

39+
enableStylingCheck := false
40+
enableStylingCheckVal, ok := paramsMap[EnableStylingCheckOption]
41+
if ok {
42+
// default to true if not disabled specifi
43+
enableStylingCheck = enableStylingCheckVal == "true"
44+
}
45+
3046
return &TypeScriptGRPCGatewayGenerator{
31-
Registry: registry,
47+
Registry: registry,
48+
EnableStylingCheck: enableStylingCheck,
3249
}, nil
3350
}
3451

@@ -46,6 +63,7 @@ func (t *TypeScriptGRPCGatewayGenerator) Generate(req *plugin.CodeGeneratorReque
4663
needToGenerateFetchModule := false
4764
// feed fileData into rendering process
4865
for _, fileData := range filesData {
66+
fileData.EnableStylingCheck = t.EnableStylingCheck
4967
if !t.Registry.IsFileToGenerate(fileData.Name) {
5068
log.Debugf("file %s is not the file to generate, skipping", fileData.Name)
5169
continue
@@ -100,7 +118,7 @@ func (t *TypeScriptGRPCGatewayGenerator) generateFile(fileData *data.File, tmpl
100118
func (t *TypeScriptGRPCGatewayGenerator) generateFetchModule(tmpl *template.Template) (*plugin.CodeGeneratorResponse_File, error) {
101119
w := bytes.NewBufferString("")
102120
fileName := filepath.Join(t.Registry.FetchModuleDirectory, t.Registry.FetchModuleFilename)
103-
err := tmpl.Execute(w, nil)
121+
err := tmpl.Execute(w, &data.File{EnableStylingCheck: t.EnableStylingCheck})
104122
if err != nil {
105123
return nil, errors.Wrapf(err, "error generating fetch module at %s", fileName)
106124
}

generator/template.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,10 @@ export type {{.Name}} = {
6666
}
6767
{{end}}{{end}}
6868
69+
{{- if not .EnableStylingCheck}}
70+
/* eslint-disable */
71+
// @ts-nocheck
72+
{{- end}}
6973
/*
7074
* This file is a generated Typescript file for GRPC Gateway, DO NOT MODIFY
7175
*/
@@ -86,6 +90,10 @@ type OneOf<T> =
8690
`
8791

8892
const fetchTmpl = `
93+
{{- if not .EnableStylingCheck}}
94+
/* eslint-disable */
95+
// @ts-nocheck
96+
{{- end}}
8997
/*
9098
* This file is a generated Typescript file for GRPC Gateway, DO NOT MODIFY
9199
*/
Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#!/bin/bash
22
USE_PROTO_NAMES=${1:-"false"}
3+
ENABLE_STYLING_CHECK=${2:-"false"}
34
cd .. && go install && cd integration_tests && \
45
protoc -I . -I ../.. \
5-
--grpc-gateway-ts_out=logtostderr=true,use_proto_names=$USE_PROTO_NAMES,loglevel=debug:./ \
6+
--grpc-gateway-ts_out=logtostderr=true,use_proto_names=$USE_PROTO_NAMES,enable_styling_check=$ENABLE_STYLING_CHECK,loglevel=debug:./ \
67
service.proto msg.proto empty.proto

integration_tests/scripts/source.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
function runTest {
44
ORIG_NAME=${1:="false"}
55
CONFIG_NAME=${2:="karma.conf.js"}
6-
./scripts/gen-protos.sh $ORIG_NAME
6+
./scripts/gen-protos.sh $ORIG_NAME true
77
go run ./ -orig=$ORIG_NAME &
88
pid=$!
99

0 commit comments

Comments
 (0)