From be81b95e95437d28957791de37cc2ced2c1edda7 Mon Sep 17 00:00:00 2001 From: Christian Winther Date: Fri, 9 Feb 2024 23:44:40 +0100 Subject: [PATCH] tune linters --- .golangci.yaml | 23 +++++++++++ cmd/disable/disable.go | 3 +- cmd/enable/enable.go | 3 +- cmd/groups/groups.go | 3 +- cmd/main.go | 10 ++--- cmd/set/set.go | 5 ++- cmd/value/value.go | 3 +- pkg/ast/assignment.go | 4 +- pkg/ast/document.go | 15 +++---- pkg/ast/shared.go | 2 +- pkg/file.go | 16 ++++---- pkg/render/handler_filters.go | 72 +++++++++++++++++----------------- pkg/render/handler_signal.go | 6 +-- pkg/render/output.go | 8 ++-- pkg/render/output_plain.go | 10 ++--- pkg/render/render.go | 8 ++-- pkg/render/render_formatter.go | 40 +++++++++---------- pkg/render/render_test.go | 4 +- pkg/render/settings_options.go | 20 +++++----- pkg/scanner/scanner.go | 22 +++++------ pkg/scanner/scanner_test.go | 6 +-- pkg/token/quote.go | 10 ++--- pkg/token/type.go | 10 ++--- pkg/tui/color.go | 8 ++-- pkg/tui/printer.go | 18 ++++----- pkg/validation/validation.go | 6 +-- 26 files changed, 182 insertions(+), 153 deletions(-) diff --git a/.golangci.yaml b/.golangci.yaml index 0b55270..aedc5ed 100644 --- a/.golangci.yaml +++ b/.golangci.yaml @@ -113,6 +113,7 @@ linters: # Whitespace Linter - Forces you to use empty lines! - wsl + - whitespace # A linter that detect the possibility to use variables/constants from the Go standard library. - usestdlibvars @@ -135,6 +136,17 @@ linters: # Align and sort struct tags consistently - tagalign + # Reports wrong mirror patterns of bytes/strings usage. + - mirror + + - dupl + - inamedparam + - perfsprint + - testifylint + - usestdlibvars + - varnamelen + # - gochecknoglobals + issues: exclude-rules: # ignoring this check from 'unused' since setID is indeed used, but @@ -204,6 +216,10 @@ linters-settings: excludes: [] exhaustive: + # Program elements to check for exhaustiveness. + check: + - switch + - map # Presence of "default" case in switch statements satisfies exhaustiveness, even if all enum members are not listed. # Default: false default-signifies-exhaustive: true @@ -211,6 +227,13 @@ linters-settings: unparam: check-exported: true + varnamelen: + check-return: true + check-type-param: true + ignore-names: + - tt + - err + tagalign: # Align and sort can be used together or separately. # diff --git a/cmd/disable/disable.go b/cmd/disable/disable.go index c9b231f..6be3d74 100644 --- a/cmd/disable/disable.go +++ b/cmd/disable/disable.go @@ -1,6 +1,7 @@ package disable import ( + "errors" "fmt" "github.com/jippi/dottie/pkg" @@ -15,7 +16,7 @@ var Command = &cobra.Command{ ValidArgsFunction: shared.NewCompleter().WithHandlers(render.FilterDisabledStatements).Get(), RunE: func(cmd *cobra.Command, args []string) error { if len(args) == 0 { - return fmt.Errorf("Missing required argument: KEY") + return errors.New("Missing required argument: KEY") } key := args[0] diff --git a/cmd/enable/enable.go b/cmd/enable/enable.go index fbf0349..313b025 100644 --- a/cmd/enable/enable.go +++ b/cmd/enable/enable.go @@ -1,6 +1,7 @@ package enable import ( + "errors" "fmt" "github.com/jippi/dottie/pkg" @@ -15,7 +16,7 @@ var Command = &cobra.Command{ ValidArgsFunction: shared.NewCompleter().WithHandlers(render.FilterActiveStatements).Get(), RunE: func(cmd *cobra.Command, args []string) error { if len(args) == 0 { - return fmt.Errorf("Missing required argument: KEY") + return errors.New("Missing required argument: KEY") } env, _, err := shared.Setup(cmd.Flags()) diff --git a/cmd/groups/groups.go b/cmd/groups/groups.go index cf5edcb..b67c7fc 100644 --- a/cmd/groups/groups.go +++ b/cmd/groups/groups.go @@ -1,6 +1,7 @@ package groups import ( + "errors" "fmt" "github.com/gosimple/slug" @@ -19,7 +20,7 @@ var Command = &cobra.Command{ groups := env.Groups if len(groups) == 0 { - return fmt.Errorf("No groups found") + return errors.New("No groups found") } fmt.Println("The following groups was found:") diff --git a/cmd/main.go b/cmd/main.go index 1af81a6..9550bd4 100644 --- a/cmd/main.go +++ b/cmd/main.go @@ -87,21 +87,21 @@ func indent(in string) string { func buildVersion() goversion.Info { return goversion.GetVersionInfo( // goversion.WithAppDetails("dottie", "Making .env file management easy", "https://github.com/jippi/dottie"), - func(i *goversion.Info) { + func(versionInfo *goversion.Info) { if commit != "" { - i.GitCommit = commit + versionInfo.GitCommit = commit } if treeState != "" { - i.GitTreeState = treeState + versionInfo.GitTreeState = treeState } if date != "" { - i.BuildDate = date + versionInfo.BuildDate = date } if version != "" { - i.GitVersion = version + versionInfo.GitVersion = version } }, ) diff --git a/cmd/set/set.go b/cmd/set/set.go index 36cc148..b6a638c 100644 --- a/cmd/set/set.go +++ b/cmd/set/set.go @@ -1,6 +1,7 @@ package set import ( + "errors" "fmt" "strings" @@ -25,7 +26,7 @@ func Command() *cobra.Command { } if len(args) == 0 { - return fmt.Errorf("Missing required argument: KEY=VALUE") + return errors.New("Missing required argument: KEY=VALUE") } comments, _ := cmd.Flags().GetStringArray("comment") @@ -40,7 +41,7 @@ func Command() *cobra.Command { for _, stringPair := range args { pairSlice := strings.SplitN(stringPair, "=", 2) if len(pairSlice) != 2 { - return fmt.Errorf("expected KEY=VALUE pair, missing '='") + return errors.New("expected KEY=VALUE pair, missing '='") } key := pairSlice[0] diff --git a/cmd/value/value.go b/cmd/value/value.go index ce503ef..c61fca8 100644 --- a/cmd/value/value.go +++ b/cmd/value/value.go @@ -1,6 +1,7 @@ package value import ( + "errors" "fmt" "github.com/jippi/dottie/pkg/cli/shared" @@ -14,7 +15,7 @@ var Command = &cobra.Command{ ValidArgsFunction: shared.NewCompleter().WithHandlers(render.FilterDisabledStatements).Get(), RunE: func(cmd *cobra.Command, args []string) error { if len(args) == 0 { - return fmt.Errorf("Missing required argument: KEY") + return errors.New("Missing required argument: KEY") } env, _, err := shared.Setup(cmd.Flags()) diff --git a/pkg/ast/assignment.go b/pkg/ast/assignment.go index f6ada76..1e6eb52 100644 --- a/pkg/ast/assignment.go +++ b/pkg/ast/assignment.go @@ -2,7 +2,7 @@ package ast import ( "bytes" - "fmt" + "errors" "reflect" "strings" @@ -85,7 +85,7 @@ func (a *Assignment) ValidationRules() string { func (a *Assignment) IsValid() error { if !a.Quote.Valid() { - return fmt.Errorf("invalid quote-style") + return errors.New("invalid quote-style") } return validator. diff --git a/pkg/ast/document.go b/pkg/ast/document.go index 05a3498..4edd6bd 100644 --- a/pkg/ast/document.go +++ b/pkg/ast/document.go @@ -2,6 +2,7 @@ package ast import ( + "errors" "fmt" "os" "reflect" @@ -81,17 +82,17 @@ func (d *Document) Get(name string) *Assignment { func (doc *Document) Interpolate(target *Assignment) (string, error) { if target == nil { - return "", fmt.Errorf("can't interpolate a nil assignment") + return "", errors.New("can't interpolate a nil assignment") } - lookup := func(in string) (string, bool) { + lookup := func(input string) (string, bool) { // Lookup in process environment - if val, ok := os.LookupEnv(in); ok { + if val, ok := os.LookupEnv(input); ok { return val, ok } // Search the currently available assignments in the document - result := doc.Get(in) + result := doc.Get(input) if result == nil { return "", false } @@ -159,14 +160,14 @@ func (doc *Document) Upsert(input *Assignment, options UpsertOptions) (*Assignme var res []Statement for _, stmt := range group.Statements { - x, ok := stmt.(*Assignment) + assignment, ok := stmt.(*Assignment) if !ok { res = append(res, stmt) continue } - if x.Name == before { + if assignment.Name == before { res = append(res, existing) } @@ -194,7 +195,7 @@ func (doc *Document) Upsert(input *Assignment, options UpsertOptions) (*Assignme if found { interpolated, err := doc.Interpolate(existing) if err != nil { - return nil, fmt.Errorf("could not interpolate variable") + return nil, errors.New("could not interpolate variable") } existing.Interpolated = interpolated diff --git a/pkg/ast/shared.go b/pkg/ast/shared.go index fc66f15..c1a2555 100644 --- a/pkg/ast/shared.go +++ b/pkg/ast/shared.go @@ -7,7 +7,7 @@ import ( // Statement represents syntax tree node of .env file statement (like: assignment or comment). type Statement interface { statementNode() - Is(Statement) bool + Is(statement Statement) bool Type() string } diff --git a/pkg/file.go b/pkg/file.go index 577a11d..4bbdee7 100644 --- a/pkg/file.go +++ b/pkg/file.go @@ -1,7 +1,7 @@ package pkg import ( - "fmt" + "errors" "io" "os" @@ -12,28 +12,28 @@ import ( ) func Load(filename string) (doc *ast.Document, err error) { - r, err := os.Open(filename) + file, err := os.Open(filename) if err != nil { return } - defer r.Close() + defer file.Close() - return Parse(r, filename) + return Parse(file, filename) } func Save(filename string, doc *ast.Document) error { - f, err := os.Create(filename) + file, err := os.Create(filename) if err != nil { return err } - defer f.Close() + defer file.Close() res := render.NewFormatter().Statement(doc) if res.IsEmpty() { - return fmt.Errorf("The rendered .env file is unexpectedly 0 bytes long - please report this as a bug (unless your file is empty)") + return errors.New("The rendered .env file is unexpectedly 0 bytes long - please report this as a bug (unless your file is empty)") } - _, err = f.WriteString(res.String()) + _, err = file.WriteString(res.String()) return err } diff --git a/pkg/render/handler_filters.go b/pkg/render/handler_filters.go index 862782a..e86be9b 100644 --- a/pkg/render/handler_filters.go +++ b/pkg/render/handler_filters.go @@ -7,95 +7,95 @@ import ( ) // FilterComments will filter out Comment statements if they aren't to be included -func FilterComments(hi *HandlerInput) HandlerSignal { +func FilterComments(input *HandlerInput) HandlerSignal { // Short circuit the filter if we allow comments - if hi.Settings.showComments { - return hi.Continue() + if input.Settings.showComments { + return input.Continue() } - switch hi.CurrentStatement.(type) { + switch input.CurrentStatement.(type) { case *ast.Comment: - if !hi.Settings.showComments { - return hi.Stop() + if !input.Settings.showComments { + return input.Stop() } } - return hi.Continue() + return input.Continue() } // FilterDisabledStatements will filter out Assignment Statements that are // disabled -func FilterDisabledStatements(hi *HandlerInput) HandlerSignal { +func FilterDisabledStatements(input *HandlerInput) HandlerSignal { // Short circuit the filter if we allow disabled statements - if hi.Settings.includeDisabled { - return hi.Continue() + if input.Settings.includeDisabled { + return input.Continue() } - switch statement := hi.CurrentStatement.(type) { + switch statement := input.CurrentStatement.(type) { case *ast.Assignment: - if !statement.Active && !hi.Settings.includeDisabled { - return hi.Stop() + if !statement.Active && !input.Settings.includeDisabled { + return input.Stop() } } - return hi.Continue() + return input.Continue() } // FilterActiveStatements will filter out Assignment Statements that are // *active* -func FilterActiveStatements(hi *HandlerInput) HandlerSignal { - switch statement := hi.CurrentStatement.(type) { +func FilterActiveStatements(input *HandlerInput) HandlerSignal { + switch statement := input.CurrentStatement.(type) { case *ast.Assignment: if statement.Active { - return hi.Stop() + return input.Stop() } } - return hi.Continue() + return input.Continue() } // FilterGroupName will filter out Statements that do not // belong to the required Group name -func FilterGroupName(hi *HandlerInput) HandlerSignal { +func FilterGroupName(input *HandlerInput) HandlerSignal { // Short circuit the filter if there is no Group name to filter on - if len(hi.Settings.filterGroup) == 0 { - return hi.Continue() + if len(input.Settings.filterGroup) == 0 { + return input.Continue() } - switch statement := hi.CurrentStatement.(type) { + switch statement := input.CurrentStatement.(type) { case *ast.Assignment: - if !statement.BelongsToGroup(hi.Settings.filterGroup) { - return hi.Stop() + if !statement.BelongsToGroup(input.Settings.filterGroup) { + return input.Stop() } case *ast.Group: - if !statement.BelongsToGroup(hi.Settings.filterGroup) { - return hi.Stop() + if !statement.BelongsToGroup(input.Settings.filterGroup) { + return input.Stop() } case *ast.Comment: - if !statement.BelongsToGroup(hi.Settings.filterGroup) { - return hi.Stop() + if !statement.BelongsToGroup(input.Settings.filterGroup) { + return input.Stop() } } - return hi.Continue() + return input.Continue() } // FilterKeyPrefix will filter out Assignment Statements that do not have the // configured (optional) key prefix. -func FilterKeyPrefix(hi *HandlerInput) HandlerSignal { +func FilterKeyPrefix(input *HandlerInput) HandlerSignal { // Short circuit the filter if there is no KeyPrefix to filter on - if len(hi.Settings.filterKeyPrefix) == 0 { - return hi.Continue() + if len(input.Settings.filterKeyPrefix) == 0 { + return input.Continue() } - switch statement := hi.CurrentStatement.(type) { + switch statement := input.CurrentStatement.(type) { case *ast.Assignment: - if !strings.HasPrefix(statement.Name, hi.Settings.filterKeyPrefix) { - return hi.Stop() + if !strings.HasPrefix(statement.Name, input.Settings.filterKeyPrefix) { + return input.Stop() } } - return hi.Continue() + return input.Continue() } diff --git a/pkg/render/handler_signal.go b/pkg/render/handler_signal.go index 0c042b8..5351332 100644 --- a/pkg/render/handler_signal.go +++ b/pkg/render/handler_signal.go @@ -16,11 +16,11 @@ var signals = []string{ // String returns the string corresponding to the Handler Signal. func (hs HandlerSignal) String() string { - s := "" + str := "" if int(hs) < len(signals) { - s = signals[hs] + str = signals[hs] } - return s + return str } diff --git a/pkg/render/output.go b/pkg/render/output.go index ab757b9..93c425c 100644 --- a/pkg/render/output.go +++ b/pkg/render/output.go @@ -3,8 +3,8 @@ package render import "github.com/jippi/dottie/pkg/ast" type Output interface { - GroupBanner(*ast.Group, Settings) *Lines - Assignment(*ast.Assignment, Settings) *Lines - Comment(*ast.Comment, Settings) *Lines - Newline(*ast.Newline, Settings) *Lines + GroupBanner(group *ast.Group, settings Settings) *Lines + Assignment(assignment *ast.Assignment, settings Settings) *Lines + Comment(comment *ast.Comment, settings Settings) *Lines + Newline(newline *ast.Newline, settings Settings) *Lines } diff --git a/pkg/render/output_plain.go b/pkg/render/output_plain.go index f2e9cfa..aa1db4d 100644 --- a/pkg/render/output_plain.go +++ b/pkg/render/output_plain.go @@ -21,20 +21,20 @@ func (PlainOutput) GroupBanner(group *ast.Group, settings Settings) *Lines { return out } -func (PlainOutput) Assignment(a *ast.Assignment, settings Settings) *Lines { +func (PlainOutput) Assignment(assignment *ast.Assignment, settings Settings) *Lines { var buf bytes.Buffer - if !a.Active { + if !assignment.Active { buf.WriteString("#") } - val := a.Literal + val := assignment.Literal if settings.useInterpolatedValues { - val = a.Interpolated + val = assignment.Interpolated } - buf.WriteString(fmt.Sprintf("%s=%s%s%s", a.Name, a.Quote, val, a.Quote)) + buf.WriteString(fmt.Sprintf("%s=%s%s%s", assignment.Name, assignment.Quote, val, assignment.Quote)) return NewLinesCollection().Add(buf.String()) } diff --git a/pkg/render/render.go b/pkg/render/render.go index ccb4196..74f7d65 100644 --- a/pkg/render/render.go +++ b/pkg/render/render.go @@ -60,10 +60,10 @@ func NewUnfilteredRenderer(settings Settings, additionalHandlers ...Handler) *Re // It's responsible for delegating statements to handlers, calling the right // Output functions and track the ordering of Statements being rendered func (r *Renderer) Statement(currentStatement any) *Lines { - hi := r.newHandlerInput(currentStatement) + handlerInput := r.newHandlerInput(currentStatement) for _, handler := range r.handlers { - status := handler(hi) + status := handler(handlerInput) switch status { // Stop processing the statement and return nothing @@ -72,7 +72,7 @@ func (r *Renderer) Statement(currentStatement any) *Lines { // Stop processing the statement and return the value from the handler case Return: - if hi.ReturnValue.IsEmpty() { + if handlerInput.ReturnValue.IsEmpty() { return nil } @@ -85,7 +85,7 @@ func (r *Renderer) Statement(currentStatement any) *Lines { r.PreviousStatement = prev } - return hi.ReturnValue + return handlerInput.ReturnValue // Continue to next handler (or default behavior if we run out of handlers) case Continue: diff --git a/pkg/render/render_formatter.go b/pkg/render/render_formatter.go index dc9c8cb..fd1558e 100644 --- a/pkg/render/render_formatter.go +++ b/pkg/render/render_formatter.go @@ -20,45 +20,45 @@ func NewFormatter() *Renderer { // FormatterHandler is responsible for formatting an .env file according // to our opinionated style. -func FormatterHandler(hi *HandlerInput) HandlerSignal { - switch statement := hi.CurrentStatement.(type) { +func FormatterHandler(input *HandlerInput) HandlerSignal { + switch statement := input.CurrentStatement.(type) { case *ast.Newline: - if !hi.Settings.showBlankLines { - return hi.Stop() + if !input.Settings.showBlankLines { + return input.Stop() } - if hi.PreviousStatement == nil { - return hi.Return(NewLinesCollection().Newline("FormatterHandler::Newline (PreviousStatement is nil)")) + if input.PreviousStatement == nil { + return input.Return(NewLinesCollection().Newline("FormatterHandler::Newline (PreviousStatement is nil)")) } - if hi.PreviousStatement.Is(&ast.Comment{}) { - return hi.Return(NewLinesCollection().Newline("FormatterHandler::Newline (retain newlines around stand-alone comments)", hi.PreviousStatement.Type())) + if input.PreviousStatement.Is(&ast.Comment{}) { + return input.Return(NewLinesCollection().Newline("FormatterHandler::Newline (retain newlines around stand-alone comments)", input.PreviousStatement.Type())) } // Ignore all existing newlines when doing formatting as // we will be injecting these ourself in other places. - return hi.Stop() + return input.Stop() case *ast.Group: - output := hi.Renderer.group(statement) + output := input.Renderer.group(statement) if output.IsEmpty() { - return hi.Stop() + return input.Stop() } buf := NewLinesCollection() - if hi.Settings.showBlankLines && hi.PreviousStatement != nil && !hi.PreviousStatement.Is(&ast.Newline{}) { - buf.Newline("FormatterHandler::Group:before", hi.PreviousStatement.Type()) + if input.Settings.showBlankLines && input.PreviousStatement != nil && !input.PreviousStatement.Is(&ast.Newline{}) { + buf.Newline("FormatterHandler::Group:before", input.PreviousStatement.Type()) } buf.Append(output) - return hi.Return(buf) + return input.Return(buf) case *ast.Assignment: - output := hi.Renderer.assignment(statement) + output := input.Renderer.assignment(statement) if output.IsEmpty() { - return hi.Stop() + return input.Stop() } buf := NewLinesCollection() @@ -67,14 +67,14 @@ func FormatterHandler(hi *HandlerInput) HandlerSignal { // be allowed to cuddle (without newline between them) or not. // // Statements are only allow cuddle if both have no comments - if hi.Settings.showBlankLines && statement.Is(hi.PreviousStatement) && (statement.HasComments() || assignmentHasComments(hi.PreviousStatement)) { - buf.Newline("FormatterHandler::Assignment:Comments", hi.PreviousStatement.Type()) + if input.Settings.showBlankLines && statement.Is(input.PreviousStatement) && (statement.HasComments() || assignmentHasComments(input.PreviousStatement)) { + buf.Newline("FormatterHandler::Assignment:Comments", input.PreviousStatement.Type()) } - return hi.Return(buf.Append(output)) + return input.Return(buf.Append(output)) } - return hi.Continue() + return input.Continue() } // assignmentHasComments checks if the Statement is an Assignment diff --git a/pkg/render/render_test.go b/pkg/render/render_test.go index 60de2ce..c59d460 100644 --- a/pkg/render/render_test.go +++ b/pkg/render/render_test.go @@ -15,7 +15,7 @@ import ( func TestFormatter(t *testing.T) { t.Parallel() - g := goldie.New( + golden := goldie.New( t, goldie.WithFixtureDir("test-fixtures/formatter"), goldie.WithNameSuffix(".golden.env"), @@ -63,7 +63,7 @@ func TestFormatter(t *testing.T) { env, err := pkg.Load(tt.filename) require.NoError(t, err) - g.Assert(t, tt.name, []byte(render.NewFormatter().Statement(env).String())) + golden.Assert(t, tt.name, []byte(render.NewFormatter().Statement(env).String())) }) } } diff --git a/pkg/render/settings_options.go b/pkg/render/settings_options.go index 893cfaa..074e1f0 100644 --- a/pkg/render/settings_options.go +++ b/pkg/render/settings_options.go @@ -49,13 +49,13 @@ func WithGroupBanners(b bool) SettingsOption { } } -func WithFormattedOutput(b bool) SettingsOption { +func WithFormattedOutput(boolean bool) SettingsOption { return func(s *Settings) { - s.formatOutput = b - s.showComments = b - s.ShowGroupBanners = b - s.showColors = b - s.showBlankLines = b + s.formatOutput = boolean + s.showComments = boolean + s.ShowGroupBanners = boolean + s.showColors = boolean + s.showBlankLines = boolean } } @@ -72,16 +72,16 @@ func WithOutputter(o Output) SettingsOption { } func WithOutputType(t OutputType) SettingsOption { - return func(s *Settings) { + return func(settings *Settings) { switch t { case Plain: - s.outputter = PlainOutput{} + settings.outputter = PlainOutput{} case Colorized: - s.outputter = ColorizedOutput{} + settings.outputter = ColorizedOutput{} case CompletionKeyOnly: - s.outputter = CompletionOutputKeys{} + settings.outputter = CompletionOutputKeys{} default: panic("Invalid outputter type") diff --git a/pkg/scanner/scanner.go b/pkg/scanner/scanner.go index 27a310d..127df14 100644 --- a/pkg/scanner/scanner.go +++ b/pkg/scanner/scanner.go @@ -34,18 +34,18 @@ type Scanner struct { // New returns new Scanner. func New(input string) *Scanner { - s := &Scanner{ + scanner := &Scanner{ input: input, lineNumber: 1, } - s.next() + scanner.next() - if s.rune == bom { - s.next() // ignore BOM at the beginning of the file + if scanner.rune == bom { + scanner.next() // ignore BOM at the beginning of the file } - return s + return scanner } // NextToken scans the next token and returns the token position, the token, and its literal string @@ -343,23 +343,23 @@ func (s *Scanner) prev() rune { // Reads a single Unicode character and returns the rune and its width in bytes. func (s *Scanner) scanRune(offset int) (rune, int) { - r := rune(s.input[offset]) + runeVal := rune(s.input[offset]) width := 1 switch { - case r >= utf8.RuneSelf: + case runeVal >= utf8.RuneSelf: // not ASCII - r, width = utf8.DecodeRune([]byte(s.input[offset:])) - if r == utf8.RuneError && width == 1 { + runeVal, width = utf8.DecodeRuneInString(s.input[offset:]) + if runeVal == utf8.RuneError && width == 1 { panic("illegal UTF-8 encoding on position " + strconv.Itoa(offset)) } - if r == bom && s.offset > 0 { + if runeVal == bom && s.offset > 0 { panic("illegal byte order mark on position " + strconv.Itoa(offset)) } } - return r, width + return runeVal, width } func (s *Scanner) peek(length int) string { diff --git a/pkg/scanner/scanner_test.go b/pkg/scanner/scanner_test.go index 735afc6..a0ad745 100644 --- a/pkg/scanner/scanner_test.go +++ b/pkg/scanner/scanner_test.go @@ -372,11 +372,11 @@ func TestScanner_NextToken(t *testing.T) { sc := scanner.New(tt.input) - i := 0 + counter := 0 for { actual := sc.NextToken() - expected := tt.expected[i] + expected := tt.expected[counter] assert.Equal(t, expected.Type, actual.Type) assert.Equal(t, expected.Literal, actual.Literal) @@ -385,7 +385,7 @@ func TestScanner_NextToken(t *testing.T) { break } - i++ + counter++ } }) } diff --git a/pkg/token/quote.go b/pkg/token/quote.go index 51c75b9..8d9e369 100644 --- a/pkg/token/quote.go +++ b/pkg/token/quote.go @@ -34,17 +34,17 @@ func (qt Quote) String() string { return "" } - s := "" + str := "" if int(qt) < len(quotes) { - s = string(quotes[qt]) + str = string(quotes[qt]) } - if s == "" { - s = "quote(" + string(qt.Rune()) + ")" + if str == "" { + str = "quote(" + string(qt.Rune()) + ")" } - return s + return str } func QuoteFromString(in string) Quote { diff --git a/pkg/token/type.go b/pkg/token/type.go index 1bbc90b..150a44b 100644 --- a/pkg/token/type.go +++ b/pkg/token/type.go @@ -56,15 +56,15 @@ var tokens = []string{ // String returns the string corresponding to the token. func (t Type) String() string { - s := "" + str := "" if int(t) < len(tokens) { - s = tokens[t] + str = tokens[t] } - if s == "" { - s = "token(" + strconv.Itoa(int(t)) + ")" + if str == "" { + str = "token(" + strconv.Itoa(int(t)) + ")" } - return s + return str } diff --git a/pkg/tui/color.go b/pkg/tui/color.go index dc4500a..0c2a7d1 100644 --- a/pkg/tui/color.go +++ b/pkg/tui/color.go @@ -27,18 +27,18 @@ type Color struct { } func NewColor(config ColorConfig) Color { - c := Color{ + color := Color{ Text: config.Text.AdaptiveColor(), TextEmphasis: config.TextEmphasis.AdaptiveColor(), Background: config.Background.AdaptiveColor(), Border: config.Border.AdaptiveColor(), } - if len(c.Text.Dark) == 0 { - c.Text.Dark = c.TextEmphasis.Dark + if len(color.Text.Dark) == 0 { + color.Text.Dark = color.TextEmphasis.Dark } - return c + return color } func (c Color) Printer(renderer *lipgloss.Renderer, options ...PrinterOption) Print { diff --git a/pkg/tui/printer.go b/pkg/tui/printer.go index 1f10d4c..5764511 100644 --- a/pkg/tui/printer.go +++ b/pkg/tui/printer.go @@ -104,7 +104,7 @@ type Printer interface { Style() lipgloss.Style // ApplyTextStyle returns a new copy of [StylePrint] instance with the [Style] based on the callback changes - ApplyStyle(StyleChanger) Print + ApplyStyle(changer StyleChanger) Print // WrapMode returns the configured [WrapMode] WrapMode() promptkit.WrapMode @@ -138,14 +138,14 @@ func NewPrinter(color Color, renderer *lipgloss.Renderer, options ...PrinterOpti WithWrapMode(nil), }, options...) - p := &Print{} + printer := &Print{} for _, option := range options { - option(p) + option(printer) } - p.boxStyles = p.color.BoxStyles(p.renderer.NewStyle(), p.renderer.NewStyle()) + printer.boxStyles = printer.color.BoxStyles(printer.renderer.NewStyle(), printer.renderer.NewStyle()) - return *p + return *printer } // ----------------------------------------------------- @@ -378,16 +378,16 @@ func WithBoxStyle(style Box) PrinterOption { } func WithEmphasis(b bool) PrinterOption { - return func(p *Print) { - p.textEmphasis = b + return func(printer *Print) { + printer.textEmphasis = b if b { - p.textStyle = p.color.TextEmphasisStyle(p.renderer.NewStyle()) + printer.textStyle = printer.color.TextEmphasisStyle(printer.renderer.NewStyle()) return } - p.textStyle = p.color.TextStyle(p.renderer.NewStyle()) + printer.textStyle = printer.color.TextStyle(printer.renderer.NewStyle()) } } diff --git a/pkg/validation/validation.go b/pkg/validation/validation.go index b1c6d0c..82fa6ae 100644 --- a/pkg/validation/validation.go +++ b/pkg/validation/validation.go @@ -17,7 +17,7 @@ func NewError(assignment *ast.Assignment, err error) ValidationError { } } -func Validate(d *ast.Document) []ValidationError { +func Validate(doc *ast.Document) []ValidationError { data := map[string]any{} rules := map[string]any{} @@ -27,7 +27,7 @@ func Validate(d *ast.Document) []ValidationError { // so this slice tracks that fieldOrder := []string{} - for _, assignment := range d.Assignments() { + for _, assignment := range doc.Assignments() { if !assignment.Active { continue } @@ -51,7 +51,7 @@ func Validate(d *ast.Document) []ValidationError { if err, ok := errors[field]; ok { result = append(result, ValidationError{ Error: err, - Assignment: d.Get(field), + Assignment: doc.Get(field), }) } }