Skip to content

Commit 4f21c3a

Browse files
authored
chore: enable more lint rule (#148)
1 parent eabcccc commit 4f21c3a

File tree

10 files changed

+99
-45
lines changed

10 files changed

+99
-45
lines changed

.github/workflows/ci.yml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,11 @@ on:
66
pull_request:
77
types: [opened, synchronize]
88
workflow_dispatch:
9+
permissions:
10+
# Required: allow read access to the content for analysis.
11+
contents: read
12+
# Optional: allow read access to pull requests. Use with `only-new-issues` option.
13+
pull-requests: read
914

1015
env:
1116
GO_VERSION: '1.24.1'

.golangci.yml

Lines changed: 72 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,84 @@
1+
# yaml-language-server: $schema=https://golangci-lint.run/jsonschema/golangci.jsonschema.json
2+
13
version: '2'
4+
5+
run:
6+
allow-parallel-runners: true
7+
28
linters:
9+
default: none
10+
disable:
11+
- depguard
12+
- errcheck
13+
- paralleltest
314
enable:
4-
- govet # Go vet examiner
5-
- ineffassign # Detect ineffectual assignments
15+
# Enabled
16+
- asasalint
17+
- bidichk
18+
- bodyclose
19+
- canonicalheader
20+
- copyloopvar
21+
- depguard
22+
- durationcheck
23+
- errcheck
24+
- errchkjson
25+
- errname
26+
- errorlint
27+
- fatcontext
28+
- gocheckcompilerdirectives
29+
- goprintffuncname
30+
- govet
31+
- grouper
32+
- inamedparam
33+
- ineffassign
34+
- intrange
35+
- makezero
36+
- mirror
37+
- misspell
38+
- musttag
39+
- nakedret
40+
- nolintlint
41+
- paralleltest
42+
- perfsprint
43+
- predeclared
44+
- reassign
45+
- testableexamples
46+
- tparallel
47+
- unconvert
48+
- usestdlibvars
49+
- usetesting
650

7-
- unused # Check for unused constants, variables, functions and types
8-
- misspell # Finds commonly misspelled English words
9-
- unconvert # Remove unnecessary type conversions
51+
# Need to add headers
52+
# - goheader
53+
54+
# Check
55+
# - exhaustive
56+
# - gocritic
57+
# - gosec
58+
# - revive
59+
# - staticcheck
60+
# - testifylint
61+
# - unparam
62+
# - unused
63+
64+
settings:
65+
depguard:
66+
rules:
67+
main:
68+
deny:
69+
- pkg: 'encoding/json$'
70+
desc: 'Use "github.com/microsoft/typescript-go/internal/json" instead.'
1071

11-
disable:
12-
- errcheck # Check for unchecked errors
13-
- gosec # Inspects source code for security problems
14-
- staticcheck # It's a set of rules from staticcheck
1572
exclusions:
16-
generated: lax
73+
rules:
74+
- path: internal/fourslash/tests/gen/
75+
linters:
76+
- misspell
77+
1778
presets:
1879
- comments
19-
- common-false-positives
20-
- legacy
2180
- std-error-handling
22-
paths:
23-
- third_party$
24-
- builtin$
25-
- examples$
81+
2682
issues:
2783
max-issues-per-linter: 0
2884
max-same-issues: 0
29-
new: false
30-
formatters:
31-
enable:
32-
- gofmt
33-
- goimports
34-
exclusions:
35-
generated: lax
36-
paths:
37-
- third_party$
38-
- builtin$
39-
- examples$

cmd/rslint/api.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ func (h *IPCHandler) HandleLint(req api.LintRequest) (*api.LintResponse, error)
7878
// Get current directory
7979
currentDirectory, err := os.Getwd()
8080
if err != nil {
81-
return nil, fmt.Errorf("error getting current directory: %v", err)
81+
return nil, fmt.Errorf("error getting current directory: %w", err)
8282
}
8383
currentDirectory = tspath.NormalizePath(currentDirectory)
8484

@@ -167,7 +167,7 @@ func (h *IPCHandler) HandleLint(req api.LintRequest) (*api.LintResponse, error)
167167
for _, configFileName := range tsConfigs {
168168
program, err := utils.CreateProgram(false, fs, configDirectory, configFileName, host)
169169
if err != nil {
170-
return nil, fmt.Errorf("error creating TS program for %s: %v", configFileName, err)
170+
return nil, fmt.Errorf("error creating TS program for %s: %w", configFileName, err)
171171
}
172172
programs = append(programs, program)
173173
}
@@ -229,7 +229,7 @@ func (h *IPCHandler) HandleLint(req api.LintRequest) (*api.LintResponse, error)
229229
diagnosticCollector,
230230
)
231231
if err != nil {
232-
return nil, fmt.Errorf("error running linter: %v", err)
232+
return nil, fmt.Errorf("error running linter: %w", err)
233233
}
234234

235235
if diagnostics == nil {

cmd/rslint/cmd.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ func printDiagnostic(d rule.RuleDiagnostic, w *bufio.Writer, comparePathOptions
8888
case "jsonline":
8989
printDiagnosticJsonLine(d, w, comparePathOptions)
9090
default:
91-
panic(fmt.Sprintf("not supported format %s", format))
91+
panic("not supported format " + format)
9292
}
9393
}
9494

@@ -141,7 +141,8 @@ func printDiagnosticJsonLine(d rule.RuleDiagnostic, w *bufio.Writer, comparePath
141141
Error string `json:"error"`
142142
}
143143
errorObject := ErrorObject{Error: fmt.Sprintf("Failed to marshal diagnostic: %s", err)}
144-
errorBytes, _ := json.Marshal(errorObject) // Ignoring error since struct is simple
144+
145+
errorBytes,_ := json.Marshal(errorObject) //nolint:errchkjson
145146
w.Write(errorBytes)
146147
w.WriteByte('\n')
147148
return

cmd/rslint/lsp.go

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package main
33
import (
44
"context"
55
"encoding/json"
6+
"errors"
67
"fmt"
78
"io"
89
"log"
@@ -83,7 +84,7 @@ func (s *LSPServer) Handle(ctx context.Context, conn *jsonrpc2.Conn, req *jsonrp
8384
// Respond with method not found for unhandled methods
8485
return nil, &jsonrpc2.Error{
8586
Code: jsonrpc2.CodeMethodNotFound,
86-
Message: fmt.Sprintf("method not found: %s", req.Method),
87+
Message: "method not found: " + req.Method,
8788
}
8889
}
8990
}
@@ -411,7 +412,7 @@ type LintResponse struct {
411412

412413
func runLintWithPrograms(uri lsproto.DocumentUri, programs []*compiler.Program, rslintConfig config.RslintConfig) ([]rule.RuleDiagnostic, error) {
413414
if len(programs) == 0 {
414-
return nil, fmt.Errorf("no programs provided")
415+
return nil, errors.New("no programs provided")
415416
}
416417

417418
// Initialize rule registry with all available rules
@@ -439,7 +440,7 @@ func runLintWithPrograms(uri lsproto.DocumentUri, programs []*compiler.Program,
439440
diagnosticCollector,
440441
)
441442
if err != nil {
442-
return nil, fmt.Errorf("error running linter: %v", err)
443+
return nil, fmt.Errorf("error running linter: %w", err)
443444
}
444445

445446
if diagnostics == nil {
@@ -501,7 +502,7 @@ func createCodeActionFromRuleDiagnostic(ruleDiag rule.RuleDiagnostic, uri lsprot
501502
}
502503

503504
return &lsproto.CodeAction{
504-
Title: fmt.Sprintf("Fix: %s", ruleDiag.Message.Description),
505+
Title: "Fix: " + ruleDiag.Message.Description,
505506
Kind: ptrTo(lsproto.CodeActionKind("quickfix")),
506507
Edit: workspaceEdit,
507508
Diagnostics: &[]*lsproto.Diagnostic{lspDiagnostic},
@@ -554,7 +555,7 @@ func createCodeActionFromSuggestion(ruleDiag rule.RuleDiagnostic, suggestion rul
554555
}
555556

556557
return &lsproto.CodeAction{
557-
Title: fmt.Sprintf("Suggestion: %s", suggestion.Message.Description),
558+
Title: "Suggestion: " + suggestion.Message.Description,
558559
Kind: ptrTo(lsproto.CodeActionKind("quickfix")),
559560
Edit: workspaceEdit,
560561
Diagnostics: &[]*lsproto.Diagnostic{lspDiagnostic},

internal/api/api.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"bufio"
66
"encoding/binary"
77
"encoding/json"
8+
"errors"
89
"fmt"
910
"io"
1011
"os"
@@ -172,7 +173,7 @@ func (s *Service) Start() error {
172173
for {
173174
msg, err := s.readMessage()
174175
if err != nil {
175-
if err == io.EOF {
176+
if errors.Is(err, io.EOF) {
176177
return nil
177178
}
178179
return err

internal/config/cwd_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ func TestCwdHandling(t *testing.T) {
1414
if err != nil {
1515
t.Fatalf("Unable to get current working directory: %v", err)
1616
}
17-
defer os.Chdir(originalCwd) // Restore after test completes
17+
defer t.Chdir(originalCwd) // Restore after test completes
1818

1919
tests := []struct {
2020
name string

internal/config/loader.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package config
22

33
import (
4+
"errors"
45
"fmt"
56
"os"
67

@@ -38,7 +39,7 @@ func (loader *ConfigLoader) LoadRslintConfig(configPath string) (RslintConfig, s
3839
var config RslintConfig
3940
// Use JSONC parser to support comments and trailing commas
4041
if err := utils.ParseJSONC([]byte(data), &config); err != nil {
41-
return nil, "", fmt.Errorf("error parsing rslint config file %q: %v", configFileName, err)
42+
return nil, "", fmt.Errorf("error parsing rslint config file %q: %w", configFileName, err)
4243
}
4344

4445
// Update current directory to the config file's directory
@@ -57,7 +58,7 @@ func (loader *ConfigLoader) LoadDefaultRslintConfig() (RslintConfig, string, err
5758
}
5859
}
5960

60-
return nil, "", fmt.Errorf("no rslint config file found. Expected rslint.json or rslint.jsonc")
61+
return nil, "", errors.New("no rslint config file found. Expected rslint.json or rslint.jsonc")
6162
}
6263

6364
// LoadTsConfigsFromRslintConfig extracts and validates TypeScript configuration paths from rslint config
@@ -81,7 +82,7 @@ func (loader *ConfigLoader) LoadTsConfigsFromRslintConfig(rslintConfig RslintCon
8182
}
8283

8384
if len(tsConfigs) == 0 {
84-
return nil, fmt.Errorf("no TypeScript configuration found in rslint config")
85+
return nil, errors.New("no TypeScript configuration found in rslint config")
8586
}
8687

8788
return tsConfigs, nil

internal/rules/switch_exhaustiveness_check/switch_exhaustiveness_check.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package switch_exhaustiveness_check
22

33
import (
4-
"fmt"
54
"slices"
65

76
"github.com/microsoft/typescript-go/shim/ast"
@@ -26,7 +25,7 @@ func buildDangerousDefaultCaseMessage() rule.RuleMessage {
2625
func buildSwitchIsNotExhaustiveMessage(missingBranches string) rule.RuleMessage {
2726
return rule.RuleMessage{
2827
Id: "switchIsNotExhaustive",
29-
Description: fmt.Sprintf("Switch is not exhaustive"), // . Cases not matched: %v", missingBranches),
28+
Description: "Switch is not exhaustive", // . Cases not matched: %v", missingBranches),
3029
}
3130
}
3231

internal/utils/create_program.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package utils
22

33
import (
44
"context"
5+
"errors"
56
"fmt"
67

78
"github.com/microsoft/typescript-go/shim/bundled"
@@ -37,7 +38,7 @@ func CreateProgram(singleThreaded bool, fs vfs.FS, cwd string, tsconfigPath stri
3738
}
3839
program := compiler.NewProgram(opts)
3940
if program == nil {
40-
return nil, fmt.Errorf("couldn't create program")
41+
return nil, errors.New("couldn't create program")
4142
}
4243

4344
diagnostics := program.GetSyntacticDiagnostics(context.Background(), nil)

0 commit comments

Comments
 (0)