[SyntaxTree::ConstRef, NilClass, SyntaxTree::BodyStmt] I wasn't sure about our intentions, but I feel like it might be helpful to expect child_nodes to always return an array of node objects and nothing else so I opened this PR as a discussion with a potential solution. The solution I'm suggesting may look excessive but I end up choosing it versus calling .compact on the resulting array because I liked that we explicitly state which part of the class declaration is optional. Let me know if you have other suggestions! After the change: 3.0.3 :014 > SyntaxTree.parse("class A;end").child_nodes.first.body.first.child_nodes.map(&:class) => [SyntaxTree::ConstRef, SyntaxTree::BodyStmt] If we think that this is a reasonable change to implement, I can quickly change similar nodes like BodyStmt, for example: https://github.com/kddnewton/syntax_tree/blob/d738840feab02648899f08d00dc9fcf5f018f6a3/lib/syntax_tree.rb#L2850 I haven't covered it with tests because wasn't sure if that would be a correct change to do so let me know if I should add some. Thanks!">
Skip to content

Avoid returning nil values from ClassDeclaration#child_nodes #16

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

Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion lib/syntax_tree.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3698,7 +3698,11 @@ def initialize(constant:, superclass:, bodystmt:, location:, comments: [])
end

def child_nodes
[constant, superclass, bodystmt]
[].tap do |nodes|
nodes << constant
nodes << superclass if superclass
nodes << bodystmt
end
end

def format(q)
Expand Down