feat(config): preserve lint/tool directives by default across supported languages

This commit is contained in:
2026-07-03 09:00:21 -05:00
parent 57bb1c4b62
commit 480aebfcd0
3 changed files with 173 additions and 22 deletions
+74 -22
View File
@@ -1,6 +1,7 @@
package config
import (
"fmt"
"os"
"path/filepath"
"strings"
@@ -22,11 +23,67 @@ func Default() *Config {
"XXX:",
"@ts-ignore",
"@ts-expect-error",
"@ts-nocheck",
"eslint-",
"prettier-ignore",
"biome-ignore",
"deno-lint-ignore",
"stylelint-",
"istanbul ignore",
"c8 ignore",
"v8 ignore",
"type: ignore",
"pylint:",
"mypy:",
"type: ignore",
"pyright:",
"ruff:",
"flake8:",
"noqa",
"nosec",
"noinspection",
"pragma: no cover",
"fmt: off",
"fmt: on",
"pyre-ignore",
"pyre-fixme",
"isort:",
"nolint",
"go:",
"lint:ignore",
"rubocop:",
"frozen_string_literal",
"NOLINT",
"clang-format",
"cppcheck-suppress",
"IWYU pragma",
"ReSharper",
"CHECKSTYLE",
"NOPMD",
"ktlint-disable",
"swiftlint:",
"ignore_for_file",
"phpstan-",
"psalm-",
"phpcs:",
"@phan",
"scalafix:",
"scalastyle:",
"luacheck:",
"selene:",
"shellcheck",
"hadolint",
"tflint",
"tfsec:",
"checkov:",
"yamllint",
"{-#",
"HLINT",
"credo:",
"solhint-",
"slither-disable",
"buf:lint",
"@generated",
"DO NOT EDIT",
},
ContextLines: 3,
}
@@ -189,27 +246,22 @@ func matchesWildcard(text, pattern string) bool {
}
func CreateExampleConfig() error {
content := `# Shush Configuration
# Patterns to preserve in comments (supports wildcards with *)
preserve = [
"TODO:",
"FIXME:",
"HACK:",
"XXX:",
"@ts-ignore",
"@ts-expect-error",
"eslint-",
"prettier-ignore",
"pylint:",
"mypy:",
"type: ignore",
"*IMPORTANT*", # Example wildcard: preserves any comment containing IMPORTANT
"*DEBUG*", # Example wildcard: preserves any comment containing DEBUG
]
def := Default()
# Number of context lines to show around changes in preview mode (default: 3)
context_lines = 3
`
var b strings.Builder
b.WriteString("# Shush Configuration\n")
b.WriteString("# Patterns to preserve in comments (supports wildcards with *)\n")
b.WriteString("# Defaults below cover common lint/tool directives for supported languages.\n")
b.WriteString("preserve = [\n")
for _, p := range def.Preserve {
fmt.Fprintf(&b, " %q,\n", p)
}
b.WriteString(" # Wildcards (*) preserve any comment containing the pattern:\n")
b.WriteString(" \"*IMPORTANT*\",\n")
b.WriteString(" \"*DEBUG*\",\n")
b.WriteString("]\n\n")
b.WriteString("# Number of context lines to show around changes in preview mode\n")
fmt.Fprintf(&b, "context_lines = %d\n", def.ContextLines)
return os.WriteFile(".shush.toml", []byte(content), 0644)
return os.WriteFile(".shush.toml", []byte(b.String()), 0644)
}
+57
View File
@@ -0,0 +1,57 @@
package config
import "testing"
func TestCreateExampleConfigRoundTrips(t *testing.T) {
dir := t.TempDir()
t.Chdir(dir)
if err := CreateExampleConfig(); err != nil {
t.Fatalf("create example config: %v", err)
}
cfg, err := loadFromFile(".shush.toml")
if err != nil {
t.Fatalf("parse generated config: %v", err)
}
def := Default()
have := make(map[string]bool, len(cfg.Preserve))
for _, p := range cfg.Preserve {
have[p] = true
}
for _, p := range def.Preserve {
if !have[p] {
t.Errorf("generated config missing default preserve pattern %q", p)
}
}
if cfg.ContextLines != def.ContextLines {
t.Errorf("context_lines = %d, want %d", cfg.ContextLines, def.ContextLines)
}
}
func TestShouldPreserveDirectives(t *testing.T) {
cfg := Default()
directives := []string{
"//nolint:errcheck",
"//go:build linux",
"# noqa: E501",
"# type: ignore",
"# rubocop:disable Style/Foo",
"# frozen_string_literal: true",
"{-# LANGUAGE OverloadedStrings #-}",
"// @phpstan-ignore-line",
"-- luacheck: ignore",
"// NOPMD",
"// ReSharper disable once Foo",
"// ignore_for_file: foo",
"# credo:disable-for-next-line",
"// solhint-disable-next-line",
"# yamllint disable-line",
}
for _, d := range directives {
if !cfg.ShouldPreserveComment(d) {
t.Errorf("directive not preserved by default: %q", d)
}
}
}