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
+42
View File
@@ -115,3 +115,45 @@ func TestStripComments(t *testing.T) {
})
}
}
func TestStripCommentsPreservesDirectives(t *testing.T) {
def := config.Default()
cases := []struct {
name string
file string
src string
}{
{"go nolint", "a.go", "x := 1 //nolint:errcheck\n"},
{"go build directive", "a.go", "//go:build linux\npackage main\n"},
{"python noqa", "a.py", "x = 1 # noqa: E501\n"},
{"python type ignore", "a.py", "y = 2 # type: ignore\n"},
{"python nosec", "a.py", "p = 1 # nosec B105\n"},
{"js eslint disable", "a.js", "const a = 1 // eslint-disable-line\n"},
{"ts expect error", "a.ts", "const b = 1 // @ts-expect-error\n"},
{"c clang nolint", "a.c", "int x; // NOLINT(cert-err58-cpp)\n"},
{"shell shellcheck", "a.sh", "y=1 # shellcheck disable=SC2086\n"},
{"ruby rubocop", "a.rb", "x = 1 # rubocop:disable Style/Foo\n"},
{"ruby frozen string", "a.rb", "# frozen_string_literal: true\nx = 1\n"},
{"haskell pragma", "a.hs", "{-# LANGUAGE OverloadedStrings #-}\nmodule M where\n"},
{"php phpstan", "a.php", "<?php\n$x = 1; // @phpstan-ignore-line\n"},
{"lua luacheck", "a.lua", "local x = 1 -- luacheck: ignore\n"},
{"java nopmd", "a.java", "int x = 1; // NOPMD\n"},
{"csharp resharper", "a.cs", "int x = 1; // ReSharper disable once Foo\n"},
{"dart ignore for file", "a.dart", "// ignore_for_file: foo\nvar x = 1;\n"},
{"elixir credo", "a.ex", "x = 1 # credo:disable-for-next-line\n"},
{"solidity solhint", "a.sol", "uint x; // solhint-disable-next-line\n"},
{"kotlin ktlint", "a.kt", "val x = 1 // ktlint-disable\n"},
{"yaml yamllint", "a.yml", "x: 1 # yamllint disable-line\n"},
}
for _, c := range cases {
t.Run(c.name, func(t *testing.T) {
out, _, err := StripComments([]byte(c.src), c.file, def, Options{})
if err != nil {
t.Fatalf("err: %v", err)
}
if string(out) != c.src {
t.Errorf("directive not preserved:\ngot %q\nwant %q", out, c.src)
}
})
}
}