From b30e198739e472e5551c0b64a6bca8d8655ef75e 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 53373b830..800063d23 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][]) + ## 0.4.2 (2020-09-18) ### Bug fixes 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!)