-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
feat: add godoclint linter #6062
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
+674
−0
Merged
Changes from all commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
a5552d3
chore: add `github.com/godoc-lint/[email protected]`
babakks 0907a1d
fix: add godoclint config type
babakks b5bb8b3
fix: add godoclint to JSON schema
babakks e6d7a5b
fix: add godoclint to next version config reference
babakks 3dc0816
feat: add godoclint to linters
babakks 49eddf5
test: add godoclint tests
babakks 8c6788a
review
ldez 6616bf1
fix: upgrade godoclint to v0.7.0
babakks f9ff8b2
review
ldez 5053586
fix: upgrade godoclint v0.8.0
babakks cad6711
review
ldez 23ab774
fix: upgrade to godoclint v0.9.0
babakks 21e4af2
fix: upgrade to godoclint v0.10.0
babakks 3922fe5
review: settings validation
ldez f9bb85f
review: use constants
ldez File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
package godoclint | ||
|
||
import ( | ||
"errors" | ||
"fmt" | ||
"slices" | ||
|
||
glcompose "github.com/godoc-lint/godoc-lint/pkg/compose" | ||
glconfig "github.com/godoc-lint/godoc-lint/pkg/config" | ||
"github.com/godoc-lint/godoc-lint/pkg/model" | ||
|
||
"github.com/golangci/golangci-lint/v2/pkg/config" | ||
"github.com/golangci/golangci-lint/v2/pkg/goanalysis" | ||
"github.com/golangci/golangci-lint/v2/pkg/golinters/internal" | ||
) | ||
|
||
func New(settings *config.GodoclintSettings) *goanalysis.Linter { | ||
var pcfg glconfig.PlainConfig | ||
|
||
if settings != nil { | ||
err := checkSettings(settings) | ||
if err != nil { | ||
internal.LinterLogger.Fatalf("godoclint: %v", err) | ||
} | ||
|
||
// The following options are explicitly ignored: they must be handled globally with exclusions or nolint directives. | ||
// - Include | ||
// - Exclude | ||
|
||
// The following options are explicitly ignored: these options cannot work as expected because the global configuration about tests. | ||
// - Options.MaxLenIncludeTests | ||
// - Options.PkgDocIncludeTests | ||
// - Options.SinglePkgDocIncludeTests | ||
// - Options.RequirePkgDocIncludeTests | ||
// - Options.RequireDocIncludeTests | ||
// - Options.StartWithNameIncludeTests | ||
// - Options.NoUnusedLinkIncludeTests | ||
|
||
pcfg = glconfig.PlainConfig{ | ||
Default: settings.Default, | ||
Enable: settings.Enable, | ||
Disable: settings.Disable, | ||
Options: &glconfig.PlainRuleOptions{ | ||
MaxLenLength: settings.Options.MaxLen.Length, | ||
MaxLenIncludeTests: pointer(true), | ||
PkgDocIncludeTests: pointer(false), | ||
SinglePkgDocIncludeTests: pointer(true), | ||
RequirePkgDocIncludeTests: pointer(false), | ||
RequireDocIncludeTests: pointer(true), | ||
RequireDocIgnoreExported: settings.Options.RequireDoc.IgnoreExported, | ||
RequireDocIgnoreUnexported: settings.Options.RequireDoc.IgnoreUnexported, | ||
StartWithNameIncludeTests: pointer(false), | ||
StartWithNameIncludeUnexported: settings.Options.StartWithName.IncludeUnexported, | ||
NoUnusedLinkIncludeTests: pointer(true), | ||
}, | ||
} | ||
} | ||
|
||
composition := glcompose.Compose(glcompose.CompositionConfig{ | ||
BaseDirPlainConfig: &pcfg, | ||
ExitFunc: func(_ int, err error) { | ||
internal.LinterLogger.Errorf("godoclint: %v", err) | ||
}, | ||
}) | ||
|
||
return goanalysis. | ||
NewLinterFromAnalyzer(composition.Analyzer.GetAnalyzer()). | ||
WithLoadMode(goanalysis.LoadModeSyntax) | ||
} | ||
|
||
func checkSettings(settings *config.GodoclintSettings) error { | ||
switch deref(settings.Default) { | ||
case string(model.DefaultSetAll): | ||
if len(settings.Enable) > 0 { | ||
return errors.New("cannot use 'enable' with 'default=all'") | ||
} | ||
|
||
case string(model.DefaultSetNone): | ||
if len(settings.Disable) > 0 { | ||
return errors.New("cannot use 'disable' with 'default=none'") | ||
} | ||
|
||
default: | ||
for _, rule := range settings.Enable { | ||
if slices.Contains(settings.Disable, rule) { | ||
return fmt.Errorf("a rule cannot be enabled and disabled at the same time: '%s'", rule) | ||
} | ||
} | ||
|
||
for _, rule := range settings.Disable { | ||
if slices.Contains(settings.Enable, rule) { | ||
return fmt.Errorf("a rule cannot be enabled and disabled at the same time: '%s'", rule) | ||
} | ||
} | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func pointer[T any](v T) *T { return &v } | ||
|
||
func deref[T any](v *T) T { | ||
if v == nil { | ||
var zero T | ||
return zero | ||
} | ||
|
||
return *v | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
package godoclint | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/golangci/golangci-lint/v2/test/testshared/integration" | ||
) | ||
|
||
func TestFromTestdata(t *testing.T) { | ||
integration.RunTestdata(t) | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
version: "2" | ||
|
||
linters: | ||
settings: | ||
godoclint: | ||
default: none | ||
enable: | ||
- pkg-doc | ||
- require-pkg-doc | ||
- start-with-name | ||
- require-doc | ||
- deprecated | ||
- max-len | ||
- no-unused-link | ||
options: | ||
start-with-name: | ||
include-unexported: true | ||
require-doc: | ||
ignore-exported: false | ||
ignore-unexported: false | ||
max-len: | ||
length: 127 |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.