From 7c6d625d088ffcd48612e86b210d1232f7f8c85c Mon Sep 17 00:00:00 2001 From: Marc-Andre Lafortune Date: Thu, 24 Sep 2020 03:20:05 -0400 Subject: [PATCH] Add `Node#parent?` and `Node#root?` --- CHANGELOG.md | 4 ++++ lib/rubocop/ast/node.rb | 10 ++++++++++ 2 files changed, 14 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index b18d71945..bc48f559d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,10 @@ ## master (unreleased) +### New features + +* [#122](https://github.com/rubocop-hq/rubocop-ast/pull/122): Add `Node#parent?` and `Node#root?`. ([@marcandre][]) + ### Changes * [#121](https://github.com/rubocop-hq/rubocop-ast/pull/121): Update from `Parser::Ruby28` to `Parser::Ruby30` for Ruby 3.0 parser (experimental). ([@koic][]) diff --git a/lib/rubocop/ast/node.rb b/lib/rubocop/ast/node.rb index 3ab160625..49ab65475 100644 --- a/lib/rubocop/ast/node.rb +++ b/lib/rubocop/ast/node.rb @@ -92,6 +92,16 @@ def parent=(node) @mutable_attributes[:parent] = node end + # @return [Boolean] + def parent? + !!parent + end + + # @return [Boolean] + def root? + !parent + end + def complete! @mutable_attributes.freeze each_child_node(&:complete!)