From 55d72bbc484926687925910c9689b57d222ccec7 Mon Sep 17 00:00:00 2001 From: Falk Harrison Date: Tue, 29 Sep 2026 10:46:31 +0200 Subject: [PATCH] fix(linter): check imports in standalone modules --- pkg/linter/imports_test.go | 63 ++++++++++++++++++++++++++++++++++++++ pkg/linter/linter.go | 10 ++++++ 2 files changed, 73 insertions(+) create mode 100644 pkg/linter/imports_test.go diff --git a/pkg/linter/imports_test.go b/pkg/linter/imports_test.go new file mode 100644 index 0000000..e03298f --- /dev/null +++ b/pkg/linter/imports_test.go @@ -0,0 +1,63 @@ +package linter + +import ( + "os" + "path/filepath" + "testing" +) + +func TestLintModuleImports(t *testing.T) { + for _, tc := range []struct { + name, content string + wantLine int + }{ + { + name: "standalone missing import", + content: "# Module\n\n", + wantLine: 4, + }, + { + name: "standalone valid import", + content: "# Dependency\n\n" + + "# Module\n\n", + }, + { + name: "ordinary cheat missing import", + content: "# Command\n```sh\necho hello\n```\n\n", + wantLine: 6, + }, + { + name: "exported cheat missing import reported once", + content: "# Command\n```sh\necho hello\n```\n\n", + wantLine: 7, + }, + } { + for _, mode := range []string{"file", "directory"} { + t.Run(tc.name+"/"+mode, func(t *testing.T) { + dir := t.TempDir() + path := filepath.Join(dir, "imports.md") + if err := os.WriteFile(path, []byte(tc.content), 0600); err != nil { + t.Fatal(err) + } + target := path + if mode == "directory" { + target = dir + } + findings, err := Lint(target) + if err != nil { + t.Fatal(err) + } + if tc.wantLine == 0 { + if len(findings) != 0 { + t.Fatalf("valid import produced findings: %+v", findings) + } + return + } + want := Finding{File: path, Line: tc.wantLine, Column: 8, Severity: SeverityError, Message: `import "missing" does not resolve to any exported module`} + if len(findings) != 1 || findings[0] != want { + t.Fatalf("Lint() = %+v, want [%+v]", findings, want) + } + }) + } + } +} diff --git a/pkg/linter/linter.go b/pkg/linter/linter.go index 9f0cf0b..4d557fd 100644 --- a/pkg/linter/linter.go +++ b/pkg/linter/linter.go @@ -276,9 +276,19 @@ func checkMissingChainSteps(name string, steps map[int]*parser.Cheat) []Finding func lintImports(index *parser.CheatIndex) []Finding { var findings []Finding + seen := make(map[*parser.Cheat]bool) for _, c := range index.Cheats { + seen[c] = true findings = append(findings, lintCheatImports(c, index)...) } + for _, module := range index.Modules { + for _, c := range module.Cheats { + if !seen[c] { + seen[c] = true + findings = append(findings, lintCheatImports(c, index)...) + } + } + } return findings }