matryer / moq

Interface mocking tool for go generate
http://bit.ly/meetmoq
MIT License
1.96k stars 126 forks source link

Add -write-pkg-comment flag #202

Closed samherrmann closed 1 year ago

samherrmann commented 1 year ago

Based on Go's code review comments guide, packages should have a comment [1]. This rule is also enforced by staticcheck [2], which is the default lint tool in the Go extension for VSCode. The -write-pkg-comment flag is particularly useful when combined with the -pkg flag where the defined package may only contain generated mock files. Additionally, the -write-pkg-comment flag also exists in mockgen [3].

References

  1. https://github.com/golang/go/wiki/CodeReviewComments#package-comments
  2. https://staticcheck.dev/docs/checks/#ST1000
  3. https://github.com/uber-go/mock/blob/v0.2.0/mockgen/mockgen.go#L62
sudo-suhas commented 1 year ago

I don't think the package comment adds much value. Generated code is usually exempted from certain code quality checks. If the file written by moq is not being recognised as generated code, we should look to fix that instead.

samherrmann commented 1 year ago

I gave this a bit more thought and at the end I agree with you. If staticcheck is the linting tool being used, then users can drop a staticcheck.conf file in the folder/package containing the mocks and tell it to ignore the package. Alternatively, if users really wanted the generated code to be checked by the linting tool, then for the package comment case users can drop a separate Go file in the folder that contains nothing but the package name and the comment. E.g.:

mocks/mocks.go (human created file):

// Package mocks is a generated Moq package.
package mocks

mocks/foo.go (Moq generated file):

// Code generated by moq; DO NOT EDIT.
// github.com/matryer/moq

package mocks

// ...