diff --git a/Makefile b/Makefile index 530dd33950..c3a90ab2c7 100644 --- a/Makefile +++ b/Makefile @@ -51,6 +51,10 @@ test: $(MAKE) -C ./k6 go-test go test ./cmd/... ./pkg/... -timeout 1m30s +.PHONY: lint-translation-keys +lint-translation-keys: + go run ./devtools/gotemplatelinter --ignore-rule indentation --ignore-rule eol-at-eof ./resources/authgear/templates/en/web/authflowv2 + .PHONY: lint lint: golangci-lint run ./cmd/... ./pkg/... --timeout 7m @@ -66,7 +70,7 @@ lint: -go run ./devtools/importlinter worker api lib util >> .make-lint-expect 2>&1 -go run ./devtools/bandimportlinter ./pkg ./cmd >> .make-lint-expect 2>&1 git diff --exit-code .make-lint-expect > /dev/null 2>&1 - go run ./devtools/gotemplatelinter ./resources/authgear/templates/en/web/authflowv2 + go run ./devtools/gotemplatelinter --ignore-rule translation-key ./resources/authgear/templates/en/web/authflowv2 .PHONY: fmt fmt: diff --git a/devtools/gotemplatelinter/args_flags.go b/devtools/gotemplatelinter/args_flags.go new file mode 100644 index 0000000000..4f87a29013 --- /dev/null +++ b/devtools/gotemplatelinter/args_flags.go @@ -0,0 +1,39 @@ +package main + +import ( + "flag" + "strings" +) + +type ArgsFlags struct { + Paths []string + RulesToIgnore []string +} + +func ParseArgsFlags() ArgsFlags { + var rulesToIgnore RulesToIgnoreFlags + flag.Var(&rulesToIgnore, "ignore-rule", "rules to ignore, repeat this flag to ignore more rules") + + flag.Parse() + + paths := flag.Args() + + return ArgsFlags{ + Paths: paths, + RulesToIgnore: rulesToIgnore, + } +} + +type RulesToIgnoreFlags []string + +func (rs *RulesToIgnoreFlags) String() string { + if rs == nil { + return "" + } + return strings.Join(*rs, ",") +} + +func (rs *RulesToIgnoreFlags) Set(value string) error { + *rs = append(*rs, value) + return nil +} diff --git a/devtools/gotemplatelinter/final_newline_rule.go b/devtools/gotemplatelinter/final_newline_rule.go index c8e6012caf..3be71173e8 100644 --- a/devtools/gotemplatelinter/final_newline_rule.go +++ b/devtools/gotemplatelinter/final_newline_rule.go @@ -4,9 +4,11 @@ import ( "strings" ) -type FinalNewlineRule struct{} +type EOLAtEOFRule struct{} -func (r FinalNewlineRule) Check(content string, path string) LintViolations { +var _ Rule = EOLAtEOFRule{} + +func (r EOLAtEOFRule) Check(content string, path string) LintViolations { var violations LintViolations lines := strings.Split(content, "\n") if len(content) > 0 && !strings.HasSuffix(content, "\n") { @@ -21,3 +23,7 @@ func (r FinalNewlineRule) Check(content string, path string) LintViolations { } return violations } + +func (r EOLAtEOFRule) Key() string { + return "eol-at-eof" +} diff --git a/devtools/gotemplatelinter/final_newline_rule_test.go b/devtools/gotemplatelinter/final_newline_rule_test.go index c0ac10c30c..008c8b539f 100644 --- a/devtools/gotemplatelinter/final_newline_rule_test.go +++ b/devtools/gotemplatelinter/final_newline_rule_test.go @@ -6,10 +6,10 @@ import ( . "github.com/smartystreets/goconvey/convey" ) -func TestFinalNewlineRule(t *testing.T) { +func TestEOLAtEOFRule(t *testing.T) { Convey("Given a empty file", t, func() { html := `` - violations := FinalNewlineRule{}.Check(html, "test.html") + violations := EOLAtEOFRule{}.Check(html, "test.html") Convey("Then I expect to see a lint violation", func() { So(len(violations), ShouldEqual, 0) }) @@ -17,7 +17,7 @@ func TestFinalNewlineRule(t *testing.T) { Convey("Given a file with a single line", t, func() { html := `
Test
` - violations := FinalNewlineRule{}.Check(html, "test.html") + violations := EOLAtEOFRule{}.Check(html, "test.html") Convey("Then I expect to see a lint violation", func() { So(len(violations), ShouldEqual, 1) So(violations[0].Path, ShouldEqual, "test.html") @@ -30,7 +30,7 @@ func TestFinalNewlineRule(t *testing.T) { Convey("Given a file with a single line and a newline", t, func() { html := `Test
` - violations := FinalNewlineRule{}.Check(html, "test.html") + violations := EOLAtEOFRule{}.Check(html, "test.html") Convey("Then I expect to see no violation", func() { So(len(violations), ShouldEqual, 0) }) @@ -39,7 +39,7 @@ func TestFinalNewlineRule(t *testing.T) { Convey("Given a file with a single character and a newline", t, func() { html := `A ` - violations := FinalNewlineRule{}.Check(html, "test.html") + violations := EOLAtEOFRule{}.Check(html, "test.html") Convey("Then I expect to see no violation", func() { So(len(violations), ShouldEqual, 0) }) @@ -55,7 +55,7 @@ func TestFinalNewlineRule(t *testing.T) {Test