|
| 1 | +// Copyright 2024 Oliver Eikemeier. All Rights Reserved. |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | +// |
| 15 | +// SPDX-License-Identifier: Apache-2.0 |
| 16 | + |
| 17 | +package analyzer |
| 18 | + |
| 19 | +import ( |
| 20 | + "log" |
| 21 | + |
| 22 | + "fillmore-labs.com/zerolint/pkg/internal/set" |
| 23 | + "fillmore-labs.com/zerolint/pkg/internal/visitor" |
| 24 | + "golang.org/x/tools/go/analysis" |
| 25 | +) |
| 26 | + |
| 27 | +// NewRun returns a configurable function for the Run field of [Analyzer]. |
| 28 | +func NewRun(opts ...Option) func(*analysis.Pass) (any, error) { |
| 29 | + option := options{ |
| 30 | + logger: log.Default(), |
| 31 | + } |
| 32 | + for _, opt := range opts { |
| 33 | + opt.apply(&option) |
| 34 | + } |
| 35 | + |
| 36 | + return func(pass *analysis.Pass) (any, error) { |
| 37 | + visitor.Run( |
| 38 | + option.logger, |
| 39 | + visitor.Visitor{ |
| 40 | + Pass: pass, |
| 41 | + Excludes: option.excludes, |
| 42 | + }, |
| 43 | + option.zeroTrace, |
| 44 | + option.basic, |
| 45 | + option.generated, |
| 46 | + ) |
| 47 | + |
| 48 | + return any(nil), nil |
| 49 | + } |
| 50 | +} |
| 51 | + |
| 52 | +// options defines configurable parameters for the linter. |
| 53 | +type options struct { |
| 54 | + logger *log.Logger |
| 55 | + excludes set.Set[string] |
| 56 | + zeroTrace bool |
| 57 | + basic bool |
| 58 | + generated bool |
| 59 | +} |
| 60 | + |
| 61 | +// Option defines configurations for [NewRun]. |
| 62 | +type Option interface { |
| 63 | + apply(opts *options) |
| 64 | +} |
| 65 | + |
| 66 | +// WithLogger is an [Option] to configure the used logger. |
| 67 | +func WithLogger(logger *log.Logger) Option { //nolint:ireturn |
| 68 | + return loggerOption{logger: logger} |
| 69 | +} |
| 70 | + |
| 71 | +type loggerOption struct { |
| 72 | + logger *log.Logger |
| 73 | +} |
| 74 | + |
| 75 | +func (o loggerOption) apply(opts *options) { |
| 76 | + opts.logger = o.logger |
| 77 | +} |
| 78 | + |
| 79 | +// WithExcludes is an [Option] to configure the excluded types. |
| 80 | +func WithExcludes(excludes []string) Option { //nolint:ireturn |
| 81 | + return excludesOption{excludes: excludes} |
| 82 | +} |
| 83 | + |
| 84 | +type excludesOption struct { |
| 85 | + excludes []string |
| 86 | +} |
| 87 | + |
| 88 | +func (o excludesOption) apply(opts *options) { |
| 89 | + if opts.excludes == nil { |
| 90 | + opts.excludes = set.New[string]() |
| 91 | + } |
| 92 | + for _, exclude := range o.excludes { |
| 93 | + opts.excludes.Insert(exclude) |
| 94 | + } |
| 95 | +} |
| 96 | + |
| 97 | +// WithZeroTrace is an [Option] to configure tracing of zero sized types. |
| 98 | +func WithZeroTrace(zeroTrace bool) Option { //nolint:ireturn |
| 99 | + return zeroTraceOption{zeroTrace: zeroTrace} |
| 100 | +} |
| 101 | + |
| 102 | +type zeroTraceOption struct { |
| 103 | + zeroTrace bool |
| 104 | +} |
| 105 | + |
| 106 | +func (o zeroTraceOption) apply(opts *options) { |
| 107 | + opts.zeroTrace = o.zeroTrace |
| 108 | +} |
| 109 | + |
| 110 | +// WithBasic is an [Option] to configure only basic linting. |
| 111 | +func WithBasic(basic bool) Option { //nolint:ireturn |
| 112 | + return basicOption{basic: basic} |
| 113 | +} |
| 114 | + |
| 115 | +type basicOption struct { |
| 116 | + basic bool |
| 117 | +} |
| 118 | + |
| 119 | +func (o basicOption) apply(opts *options) { |
| 120 | + opts.basic = o.basic |
| 121 | +} |
| 122 | + |
| 123 | +// WithGenerated is an [Option] to configure linting of generated files. |
| 124 | +func WithGenerated(generated bool) Option { //nolint:ireturn |
| 125 | + return generatedOption{generated: generated} |
| 126 | +} |
| 127 | + |
| 128 | +type generatedOption struct { |
| 129 | + generated bool |
| 130 | +} |
| 131 | + |
| 132 | +func (o generatedOption) apply(opts *options) { |
| 133 | + opts.generated = o.generated |
| 134 | +} |
0 commit comments