Skip to content

Commit

Permalink
fix lint issues
Browse files Browse the repository at this point in the history
  • Loading branch information
ysmood committed Feb 19, 2024
1 parent 41de0e8 commit 0243a30
Show file tree
Hide file tree
Showing 122 changed files with 3,949 additions and 3,818 deletions.
106 changes: 99 additions & 7 deletions .golangci.yml
Expand Up @@ -2,19 +2,70 @@ run:
skip-dirs-use-default: false

linters:
enable:
- gofumpt
- revive
- gocyclo
- bodyclose
enable-all: true
disable:
- gochecknoinits
- paralleltest
- wrapcheck
- gosec
- gochecknoglobals
- musttag
- varnamelen
- wsl
- nonamedreturns
- tagliatelle
- nlreturn
- nakedret
- gomnd
- goerr113
- exhaustruct
- godox
- depguard
- testpackage
- exhaustive
- containedctx
- prealloc
- perfsprint
- ireturn
- contextcheck

gocyclo:
min-complexity: 15
# Deprecated ones:
- structcheck
- interfacer
- deadcode
- varcheck
- ifshort
- exhaustivestruct
- golint
- maligned
- nosnakecase
- scopelint

linters-settings:
cyclop:
max-complexity: 15
gocyclo:
min-complexity: 15
nestif:
min-complexity: 6
funlen:
lines: 120

issues:
exclude-use-default: false

exclude-rules:
- path: _test.go$
linters:
- lll
- funlen
- dupword
- goconst
- contextcheck
- errorlint
- testableexamples
- forcetypeassert

# To support old golang version
- path: lib/launcher/os_unix.go
source: "// \\+build !windows"
Expand All @@ -30,3 +81,44 @@ issues:
- source: 'io/ioutil'
linters:
- staticcheck

# Generated code
- path: lib/proto/
linters:
- lll
- gocritic
- dupword
- forcetypeassert
- path: lib/devices/list.go
linters:
- lll
- path: lib/js/helper.go
linters:
- lll

- path: /fixtures/
linters:
- forbidigo

- path: lib/examples/
linters:
- forbidigo
- noctx
- gocritic

- path: examples?_test.go$
linters:
- forbidigo
- noctx
- gocritic

- path: main.go$
linters:
- forbidigo
- noctx
- forcetypeassert
- lll

- path: lib/assets/
linters:
- lll
36 changes: 18 additions & 18 deletions browser.go
Expand Up @@ -23,7 +23,7 @@ import (
"github.com/ysmood/goob"
)

// Browser implements these interfaces
// Browser implements these interfaces.
var (
_ proto.Client = &Browser{}
_ proto.Contextable = &Browser{}
Expand Down Expand Up @@ -81,7 +81,7 @@ func New() *Browser {
}).WithPanic(utils.Panic)
}

// Incognito creates a new incognito browser
// Incognito creates a new incognito browser.
func (b *Browser) Incognito() (*Browser, error) {
res, err := proto.TargetCreateBrowserContext{}.Call(b)
if err != nil {
Expand All @@ -100,31 +100,31 @@ func (b *Browser) ControlURL(url string) *Browser {
return b
}

// SlowMotion set the delay for each control action, such as the simulation of the human inputs
// SlowMotion set the delay for each control action, such as the simulation of the human inputs.
func (b *Browser) SlowMotion(delay time.Duration) *Browser {
b.slowMotion = delay
return b
}

// Trace enables/disables the visual tracing of the input actions on the page
// Trace enables/disables the visual tracing of the input actions on the page.
func (b *Browser) Trace(enable bool) *Browser {
b.trace = enable
return b
}

// Monitor address to listen if not empty. Shortcut for [Browser.ServeMonitor]
// Monitor address to listen if not empty. Shortcut for [Browser.ServeMonitor].
func (b *Browser) Monitor(url string) *Browser {
b.monitor = url
return b
}

// Logger overrides the default log functions for tracing
// Logger overrides the default log functions for tracing.
func (b *Browser) Logger(l utils.Logger) *Browser {
b.logger = l
return b
}

// Client set the cdp client
// Client set the cdp client.
func (b *Browser) Client(c CDPClient) *Browser {
b.client = c
return b
Expand All @@ -138,7 +138,7 @@ func (b *Browser) DefaultDevice(d devices.Device) *Browser {
return b
}

// NoDefaultDevice is the same as [Browser.DefaultDevice](devices.Clear)
// NoDefaultDevice is the same as [Browser.DefaultDevice](devices.Clear).
func (b *Browser) NoDefaultDevice() *Browser {
return b.DefaultDevice(devices.Clear)
}
Expand Down Expand Up @@ -174,7 +174,7 @@ func (b *Browser) Connect() error {
return proto.TargetSetDiscoverTargets{Discover: true}.Call(b)
}

// Close the browser
// Close the browser.
func (b *Browser) Close() error {
if b.BrowserContextID == "" {
return proto.BrowserClose{}.Call(b)
Expand Down Expand Up @@ -213,7 +213,7 @@ func (b *Browser) Page(opts proto.TargetCreateTarget) (p *Page, err error) {
return
}

// Pages retrieves all visible pages
// Pages retrieves all visible pages.
func (b *Browser) Pages() (Pages, error) {
list, err := proto.TargetGetTargets{}.Call(b)
if err != nil {
Expand Down Expand Up @@ -247,7 +247,7 @@ func (b *Browser) Call(ctx context.Context, sessionID, methodName string, params
return
}

// PageFromSession is used for low-level debugging
// PageFromSession is used for low-level debugging.
func (b *Browser) PageFromSession(sessionID proto.TargetSessionID) *Page {
sessionCtx, cancel := context.WithCancel(b.ctx)
return &Page{
Expand Down Expand Up @@ -358,15 +358,15 @@ func (b *Browser) eachEvent(sessionID proto.TargetSessionID, callbacks ...interf
for _, cb := range callbacks {
cbVal := reflect.ValueOf(cb)
eType := cbVal.Type().In(0)
name := reflect.New(eType.Elem()).Interface().(proto.Event).ProtoEvent()
name := reflect.New(eType.Elem()).Interface().(proto.Event).ProtoEvent() //nolint: forcetypeassert
cbMap[name] = cbVal

// Only enabled domains will emit events to cdp client.
// We enable the domains for the event types if it's not enabled.
// We restore the domains to their previous states after the wait ends.
domain, _ := proto.ParseMethodName(name)
if req := proto.GetType(domain + ".enable"); req != nil {
enable := reflect.New(req).Interface().(proto.Request)
enable := reflect.New(req).Interface().(proto.Request) //nolint: forcetypeassert
restores = append(restores, b.EnableDomain(sessionID, enable))
}
}
Expand Down Expand Up @@ -394,7 +394,7 @@ func (b *Browser) eachEvent(sessionID proto.TargetSessionID, callbacks ...interf

if cbVal, has := cbMap[msg.Method]; has {
e := reflect.New(proto.GetType(msg.Method))
msg.Load(e.Interface().(proto.Event))
msg.Load(e.Interface().(proto.Event)) //nolint: forcetypeassert
args := []reflect.Value{e}
if cbVal.Type().NumIn() == 2 {
args = append(args, reflect.ValueOf(msg.SessionID))
Expand All @@ -410,7 +410,7 @@ func (b *Browser) eachEvent(sessionID proto.TargetSessionID, callbacks ...interf
}
}

// Event of the browser
// Event of the browser.
func (b *Browser) Event() <-chan *Message {
src := b.event.Subscribe(b.ctx)
dst := make(chan *Message)
Expand All @@ -427,7 +427,7 @@ func (b *Browser) Event() <-chan *Message {
select {
case <-b.ctx.Done():
return
case dst <- e.(*Message):
case dst <- e.(*Message): //nolint: forcetypeassert
}
}
}
Expand Down Expand Up @@ -476,7 +476,7 @@ func (b *Browser) IgnoreCertErrors(enable bool) error {
return proto.SecuritySetIgnoreCertificateErrors{Ignore: enable}.Call(b)
}

// GetCookies from the browser
// GetCookies from the browser.
func (b *Browser) GetCookies() ([]*proto.NetworkCookie, error) {
res, err := proto.StorageGetCookies{BrowserContextID: b.BrowserContextID}.Call(b)
if err != nil {
Expand Down Expand Up @@ -537,7 +537,7 @@ func (b *Browser) WaitDownload(dir string) func() (info *proto.PageDownloadWillB
}
}

// Version info of the browser
// Version info of the browser.
func (b *Browser) Version() (*proto.BrowserGetVersionResult, error) {
return proto.BrowserGetVersion{}.Call(b)
}
18 changes: 9 additions & 9 deletions browser_test.go
Expand Up @@ -50,7 +50,7 @@ func TestDefaultDevice(t *testing.T) {
ua := ""

s := g.Serve()
s.Mux.HandleFunc("/t", func(rw http.ResponseWriter, r *http.Request) {
s.Mux.HandleFunc("/t", func(_ http.ResponseWriter, r *http.Request) {
ua = r.Header.Get("User-Agent")
})

Expand Down Expand Up @@ -153,7 +153,7 @@ func TestBrowserWaitEvent(t *testing.T) {
g.page.MustNavigate(g.blank())
wait()

wait = g.browser.EachEvent(func(e *proto.PageFrameNavigated, id proto.TargetSessionID) bool {
wait = g.browser.EachEvent(func(_ *proto.PageFrameNavigated, _ proto.TargetSessionID) bool {
return true
})
g.page.MustNavigate(g.blank())
Expand Down Expand Up @@ -201,7 +201,7 @@ func TestBlockingNavigation(t *testing.T) {
s := g.Serve()
pause := g.Context()

s.Mux.HandleFunc("/a", func(w http.ResponseWriter, r *http.Request) {
s.Mux.HandleFunc("/a", func(_ http.ResponseWriter, _ *http.Request) {
<-pause.Done()
})
s.Route("/b", ".html", `<html>ok</html>`)
Expand All @@ -226,7 +226,7 @@ func TestResolveBlocking(t *testing.T) {

pause := g.Context()

s.Mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
s.Mux.HandleFunc("/", func(_ http.ResponseWriter, _ *http.Request) {
<-pause.Done()
})

Expand All @@ -248,14 +248,14 @@ func TestTestTry(t *testing.T) {
g.Nil(rod.Try(func() {}))

err := rod.Try(func() { panic(1) })
var errVal *rod.ErrTry
var errVal *rod.TryError
g.True(errors.As(err, &errVal))
g.Is(err, &rod.ErrTry{})
g.Is(err, &rod.TryError{})
g.Eq(errVal.Unwrap().Error(), "1")
g.Eq(1, errVal.Value)
g.Has(errVal.Error(), "error value: 1\ngoroutine")

errVal = rod.Try(func() { panic(errors.New("t")) }).(*rod.ErrTry)
errVal = rod.Try(func() { panic(errors.New("t")) }).(*rod.TryError)
g.Eq(errVal.Unwrap().Error(), "t")
}

Expand Down Expand Up @@ -405,7 +405,7 @@ func TestStreamReader(t *testing.T) {

r := rod.NewStreamReader(g.page, "")

g.mc.stub(1, proto.IORead{}, func(send StubSend) (gson.JSON, error) {
g.mc.stub(1, proto.IORead{}, func(_ StubSend) (gson.JSON, error) {
return gson.New(proto.IOReadResult{
Data: "test",
}), nil
Expand All @@ -418,7 +418,7 @@ func TestStreamReader(t *testing.T) {
_, err := r.Read(nil)
g.Err(err)

g.mc.stub(1, proto.IORead{}, func(send StubSend) (gson.JSON, error) {
g.mc.stub(1, proto.IORead{}, func(_ StubSend) (gson.JSON, error) {
return gson.New(proto.IOReadResult{
Base64Encoded: true,
Data: "@",
Expand Down

0 comments on commit 0243a30

Please sign in to comment.