Skip to content

Commit 09f820e

Browse files
authored
fix: fix unstable error count (#133)
1 parent fa7da64 commit 09f820e

File tree

5 files changed

+171
-228
lines changed

5 files changed

+171
-228
lines changed

cmd/rslint/api.go

Lines changed: 3 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@ package main
33
import (
44
"fmt"
55
"os"
6-
"slices"
7-
"strings"
86
"sync"
97

108
"github.com/microsoft/typescript-go/shim/ast"
@@ -174,42 +172,6 @@ func (h *IPCHandler) HandleLint(req api.LintRequest) (*api.LintResponse, error)
174172
programs = append(programs, program)
175173
}
176174

177-
// Find source files from all programs
178-
files := []*ast.SourceFile{}
179-
180-
// If specific files are provided, use those
181-
if len(req.Files) > 0 {
182-
for _, filePath := range req.Files {
183-
absPath := tspath.ResolvePath(configDirectory, filePath)
184-
// Try to find the file in any of the programs
185-
for _, program := range programs {
186-
sourceFile := program.GetSourceFile(absPath)
187-
if sourceFile != nil {
188-
files = append(files, sourceFile)
189-
break // Found in this program, no need to check others
190-
}
191-
}
192-
}
193-
} else {
194-
// Otherwise use all source files from all programs
195-
for _, program := range programs {
196-
for _, file := range program.SourceFiles() {
197-
p := string(file.Path())
198-
if strings.Contains(p, "/node_modules/") {
199-
continue
200-
}
201-
// skip bundled files
202-
if strings.Contains(p, "bundled:") {
203-
continue
204-
}
205-
files = append(files, file)
206-
}
207-
}
208-
}
209-
slices.SortFunc(files, func(a *ast.SourceFile, b *ast.SourceFile) int {
210-
return len(b.Text()) - len(a.Text())
211-
})
212-
213175
// Collect diagnostics
214176
var diagnostics []api.Diagnostic
215177
var diagnosticsLock sync.Mutex
@@ -249,10 +211,10 @@ func (h *IPCHandler) HandleLint(req api.LintRequest) (*api.LintResponse, error)
249211
}
250212

251213
// Run linter
252-
err = linter.RunLinter(
214+
lintedFilesCount, err := linter.RunLinter(
253215
programs,
254216
false, // Don't use single-threaded mode for IPC
255-
files,
217+
nil,
256218
func(sourceFile *ast.SourceFile) []linter.ConfiguredRule {
257219
return utils.Map(rulesWithOptions, func(r RuleWithOption) linter.ConfiguredRule {
258220

@@ -277,7 +239,7 @@ func (h *IPCHandler) HandleLint(req api.LintRequest) (*api.LintResponse, error)
277239
return &api.LintResponse{
278240
Diagnostics: diagnostics,
279241
ErrorCount: errorsCount,
280-
FileCount: len(files),
242+
FileCount: int(lintedFilesCount),
281243
RuleCount: len(rulesWithOptions),
282244
}, nil
283245
}

cmd/rslint/cmd.go

Lines changed: 5 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,7 @@ import (
1010
"runtime"
1111
"runtime/pprof"
1212
"runtime/trace"
13-
"slices"
1413
"strconv"
15-
"strings"
1614
"sync"
1715
"time"
1816
"unicode"
@@ -432,23 +430,6 @@ func runCMD() int {
432430

433431
}
434432

435-
files := []*ast.SourceFile{}
436-
for _, program := range programs {
437-
cwdPath := string(tspath.ToPath("", currentDirectory, program.Host().FS().UseCaseSensitiveFileNames()).EnsureTrailingDirectorySeparator())
438-
for _, file := range program.SourceFiles() {
439-
p := string(file.Path())
440-
if strings.Contains(p, "/node_modules/") {
441-
continue
442-
}
443-
if _, matched := strings.CutPrefix(p, cwdPath); matched {
444-
files = append(files, file)
445-
}
446-
}
447-
slices.SortFunc(files, func(a *ast.SourceFile, b *ast.SourceFile) int {
448-
return len(b.Text()) - len(a.Text())
449-
})
450-
}
451-
452433
var wg sync.WaitGroup
453434

454435
diagnosticsChan := make(chan rule.RuleDiagnostic, 4096)
@@ -494,10 +475,10 @@ func runCMD() int {
494475
}
495476
}()
496477

497-
err = linter.RunLinter(
478+
lintedfileCount, err := linter.RunLinter(
498479
programs,
499480
singleThreaded,
500-
files,
481+
nil,
501482
func(sourceFile *ast.SourceFile) []linter.ConfiguredRule {
502483
activeRules := rslintconfig.GlobalRuleRegistry.GetEnabledRules(rslintConfig, sourceFile.FileName())
503484
return activeRules
@@ -572,7 +553,7 @@ func runCMD() int {
572553
}
573554

574555
filesText := "files"
575-
if len(files) == 1 {
556+
if lintedfileCount == 1 {
576557
filesText = "file"
577558
}
578559
threadsCount := 1
@@ -593,7 +574,7 @@ func runCMD() int {
593574
warningsColorFunc("%d", warningsCount),
594575
warningsText,
595576
colors.DimText(""),
596-
colors.BoldText("%d", len(files)),
577+
colors.BoldText("%d", lintedfileCount),
597578
filesText,
598579
colors.BoldText("%v", time.Since(timeBefore).Round(time.Millisecond)),
599580
colors.BoldText("%d", threadsCount),
@@ -610,7 +591,7 @@ func runCMD() int {
610591
warningsColorFunc("%d", warningsCount),
611592
warningsText,
612593
colors.DimText(""),
613-
colors.BoldText("%d", len(files)),
594+
colors.BoldText("%d", lintedfileCount),
614595
filesText,
615596
colors.BoldText("%v", time.Since(timeBefore).Round(time.Millisecond)),
616597
colors.BoldText("%d", threadsCount),

cmd/rslint/lsp.go

Lines changed: 2 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -352,29 +352,6 @@ func runLintWithPrograms(uri lsproto.DocumentUri, programs []*compiler.Program,
352352
// Initialize rule registry with all available rules
353353
config.RegisterAllTypeSriptEslintPluginRules()
354354

355-
// Find all source files from all programs, prioritizing the file matching the URI
356-
var targetFile *ast.SourceFile
357-
uriPath := uriToPath(string(uri))
358-
359-
for _, program := range programs {
360-
for _, file := range program.SourceFiles() {
361-
362-
if file.FileName() == uriPath {
363-
targetFile = file
364-
}
365-
}
366-
}
367-
368-
// If we found a target file, prioritize it, otherwise use all files
369-
var files []*ast.SourceFile
370-
if targetFile != nil {
371-
files = []*ast.SourceFile{targetFile}
372-
} else {
373-
log.Printf("Target file not found for URI %s, processing all files", uri)
374-
}
375-
376-
log.Printf("Processing %d files from %d programs", len(files), len(programs))
377-
378355
// Collect diagnostics
379356
var diagnostics []rule.RuleDiagnostic
380357
var diagnosticsLock sync.Mutex
@@ -385,12 +362,11 @@ func runLintWithPrograms(uri lsproto.DocumentUri, programs []*compiler.Program,
385362
defer diagnosticsLock.Unlock()
386363
diagnostics = append(diagnostics, d)
387364
}
388-
389365
// Run linter with all programs using rule registry
390-
err := linter.RunLinter(
366+
_, err := linter.RunLinter(
391367
programs,
392368
false, // Don't use single-threaded mode for LSP
393-
files,
369+
nil,
394370
func(sourceFile *ast.SourceFile) []linter.ConfiguredRule {
395371
activeRules := config.GlobalRuleRegistry.GetEnabledRules(rslintConfig, sourceFile.FileName())
396372
return activeRules

0 commit comments

Comments
 (0)