Fix DocumentedPublicApis crash on non-Node left siblings - #102
Open
armandrius wants to merge 2 commits into
Open
Fix DocumentedPublicApis crash on non-Node left siblings#102armandrius wants to merge 2 commits into
armandrius wants to merge 2 commits into
Conversation
An argument-less block's empty args node has no source range, and a modifier other than private_class_method leaves a Symbol as the sibling. Both raised NoMethodError instead of reporting an offense.
A documentation comment above `private def foo` attaches to the enclosing send, not to the definition, so the cop flagged documented methods. Route every Symbol sibling through the branch that already handled private_class_method.
conwayje
approved these changes
Jul 28, 2026
Author
|
Thanks for the review. Both workflow runs on this PR are still sitting at I ran the equivalent gates locally on every Ruby version the
|
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.
Fixes #101
Summary
Two shapes of method definition made
Packs/DocumentedPublicApisraiseNoMethodErrorrather than report an offense. Both come from one assumption innode_is_sorbet_signature?, that a left sibling which is notnilis an AST node with source.A method that is the leading statement of an argument-less block has the block's own empty
argsnode as its sibling. That node has no source range, soNode#sourcereturnsnilandnil.include?raises.Struct.new(:a) do,Data.define(:a) doandclass_eval doall hit it.A method passed to a modifier other than
private_class_methodhas aSymbolas its sibling, andSymbolhas no#source.The first commit guards against both. The second widens the existing
private_class_methodbranch to anySymbolsibling, because otherwise the first commit converts theprivate defcrash into a false positive:documentation_comment?looks up comments attached to the node it is handed, and forprivate def foothe comment attaches to the enclosing send, so a documented method gets flagged. That is the same reason #56 special-casedprivate_class_methodin the first place.The second commit is independently droppable if you would rather take only the crash fix.
Motivation
Found on a pack whose public API is a set of
Data.define(...) do ... endvalue objects with instance methods. The cop cannot evaluate any file using that idiom.Behaviour
Widening the branch to any
Symbolis a superset of the old condition, so every input that took the old branch still takes it. The only newly routed inputs are the ones that used to raise. All 11 pre-existing examples keep byte-identical offense ranges.One genuinely new behaviour worth flagging: a method defined on the right-hand side of an assignment now consults the comment above the assignment instead of raising, because a
casgnalso leaves aSymbolsibling.Tests
Nine new examples. Five cover methods inside blocks: argument-less and undocumented, argument-less and documented, a sig with documentation above it, a sig with no documentation, and a block that declares parameters as a regression guard. Four mirror the existing
private_class_methodmatrix usingprivate def.Notes
manual/is unchanged. The class docstring was not touched, andtasks/cop_documentation.rakerenders only the docstring and its examples rather than method-body comments, soCI=true VERIFYING_DOCUMENTATION=true bundle exec rake generate_cops_documentationpasses with no diff.Verified locally: 90 examples and 0 failures at 100 percent line and branch coverage,
srb tcclean,rubocopclean.mainhas moved on from the releasedv0.0.45, so this needs a release before affected consumers can pick it up.cc @alexevanczuk (wrote the
private_class_methodhandling in #56) and @dduugg