Repository navigation
fix(go): a type declared on its own keeps its doc comment - #2438
Merged
Merged
Conversation
tree-sitter-go puts the comment above `type Foo struct{}` before the
`type_declaration`, but the node is made from the spec inside it, so both
extractors found no docstring for an ungrouped struct, interface, defined
type or alias. A type inside a `type ( ... )` group kept its own comment,
which sits beside it in the parentheses.
Go's docstring is now read from the declaration when the declaration
holds one spec and nothing precedes that spec inside it. The leading
comment of a group with several members stays the group's.
Reading from the declaration exposed comments that trail the line above:
`const _ = proto.GoGoProtoPackageIsVersion3 // please upgrade the proto
package` would have become `type MetricType int32`'s doc. For every Go
declaration, a comment that begins after code on its line, and one that
begins on the line such a comment ends, is now left out, as go/parser
reads them. Seven function docstrings on etcd and prometheus lose one.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…rasekhar-072d56 # Conflicts: # CHANGELOG.md
#2417 now extracts `type A = B`, so the test covers an ungrouped alias and an alias that is a member of a commented group. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Problem
A Go type declared on its own lost its doc comment in both extractors, while one inside a
type ( … )group kept it:tree-sitter-go wraps every type in a
type_declaration, and the comment is that declaration's previous sibling. The node is made from the spec inside it (atype_spec, or atype_aliassince #2417), and the spec's only predecessor is thetypekeyword. So the docstring walk found nothing for any ungrouped struct, interface, defined type or alias.Fix
getDeclarationWrapperinlanguages/go.ts,declaration_wrapperingo.rs). The climb happens when thetype_declarationholds exactly one spec (type_specortype_alias) and nothing precedes that spec inside it.// X …abovetype ( Lone int )) takes the comment above it, as go doc does, unless its member has a comment of its own. None of the three repos below has such a group.docstringSkipsTrailingCommentsextractor option, set for Go;preceding_docstring_skipping_trailingindocstring.rs).const _ = proto.GoGoProtoPackageIsVersion3 // please upgrade the proto packagewould have becometype MetricType int32's doc.BenchmarkSize←var GlobalTotal uint64 // Encourage the compiler….Validation
Indexes of etcd, prometheus and gin built on main (
ed199e60, aliases included) and on this branch, with the default kernel route and withCODEGRAPH_KERNEL=0:kernel-parity.mjs --lang goshows 0 diffs on all three repos, with the same deferral counts as main (etcd 40/1105, prometheus 0/736, gin 0/99).Spot-check
// ==== TextUnmarshaler tests START ====, the/*** CONTENT NEGOTIATION ***/banner aboveNegotiate, prometheus's// Filters in this file expect ….ReadTxpicks up an orphaned copy ofBucket.IsSafeRangeBucket's doc.Overlap
type A = Bis indexed and links to the type it names #2417 (aliases) merged while this was in progress; this branch is synced with it. An alias counts as a group member, sotype ( A int; B = A )doesn't hand the group comment toA, and an ungrouped alias keeps its doc. The test covers both.getPrecedingDocstring.Tests
__tests__/go-type-doc-comments.test.ts: kernel and wasm, LF and CRLF. On main it fails with 10 wrong values: 8 types have no doc (7 declared on their own, an alias among them, plus a group of one), andKelvin(a group member) andNext(a function) take the trailing comment of the line above.docstring::tests::skips_the_comments_that_trail_code.__tests__/fixtures/kernel-parity/torture.gogains a documented ungrouped type, a commented group and a trailing comment.mcp-status-freshness,mcp-writer-lock,mpeg-ts-not-typescript) passed when their files ran alone. The fifth, the Python#1820case infunction-ref.test.ts, passed in 151 s once its in-file 60 s limit was raised locally (not committed). None of them involves docstrings.🤖 Generated with Claude Code