Skip to content
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

filter comments outside of interface declaration (#50) #51

Merged
merged 2 commits into from Apr 12, 2023

Conversation

korylprince
Copy link
Contributor

This is a fix for Issue #50. It works by filtering comments outside of the interface declaration.

Arguably, this could be fixed in the ast package itself, as it's the ast package that assigns a comment at the end of the file to the last parsed node, but maybe that's expected behavior?

Copy link
Owner

@josharian josharian left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks.

Yes, this ought to be fixed upstream. See golang/go#21755. Feel free to pursue that instead of you'd prefer.

If you want to fix in impl, rather than reconstructing the comment groups here, it seems like it'd be easier to adjust flattenCommentMap (remove the panic, do the position checks inline).

This PR could also use a test.

@korylprince
Copy link
Contributor Author

It seems like there's some semi-recent activity on fixing this in go/ast. While that's definitely the desired approach, it's a bigger challenge than I'm able to take on at the moment. I'd definitely like to add a fix/workaround to impl, as I don't think that will take a huge amount of work.

In some more comprehensive testing, this PR isn't sufficient for all cases (it specifically fails with the // after Func2 comment below). Before working on this further, I'd like to establish what exactly you're targeting with comments.

Given the following file:

package pkg

// before Interface

// Interface doc
type Interface interface {
	// before Func

	// Func doc
	Func() // end Func

	// after Func

	// Func2 doc
	Func2() // end Func2

	// after Func2

} // end Interface

// after Interface

impl 't T' Interface currently outputs (only removing the panic in flattenCommentMap):

// before Func
// Func doc
// end Func
func (t T) Func() {
	panic("not implemented") // TODO: Implement
}

// after Func2
// after Interface
// after Func
// Func2 doc
// end Func2
func (t T) Func2() {
	panic("not implemented") // TODO: Implement
}

I would assert that the output should be:

// Func doc
// end Func
func (t T) Func() {
	panic("not implemented") // TODO: Implement
}

// Func2 doc
// end Func2
func (t T) Func2() {
	panic("not implemented") // TODO: Implement
}

For reference, go doc -all . outputs:

package pkg // import "test"


TYPES

type Interface interface {

	// Func doc
	Func() // end Func

	// Func2 doc
	Func2() // end Func2

} // end Interface
    Interface doc

In other words, only comments directly before (separated by exactly one newline) or after (separated by no newlines) a function should be output. Do you agree with that?

@josharian
Copy link
Owner

I'd be happy to mimic go doc. I'd also be happy to have just Func2 doc and not end Func2 if it simplifies things.

@josharian
Copy link
Owner

Or maybe we should just remove the panic. End-of-stuff comments are rare, and easy to delete if unwanted.

@korylprince korylprince force-pushed the panic-fix branch 2 times, most recently from 028b1fd to 848c16d Compare April 10, 2023 01:57
@korylprince
Copy link
Contributor Author

I've taken some time to dig into this issue further, particularly into how go doc creates its output.

It seems there's no good reason to mess with an ast.CommentMap in the first place. Each *ast.Field (e.g. each method of the interface) has a .Doc field that contains the correctly filtered doc comment that go doc uses.

I've changed this PR to change ast.CommentMap usage with *ast.Field.Doc. I've also included a test for free-floating comments.

If desired, the trailing comment (e.g. the // end Func from above) can also be accessed in *ast.Field.Comment, though I didn't include that in this PR. I'm not sure I've ever seen trailing comments on interface methods...

Copy link
Owner

@josharian josharian left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Very nice. Thank you.

@josharian josharian merged commit 30a6beb into josharian:main Apr 12, 2023
1 check passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants