summaryrefslogtreecommitdiff
path: root/rubocop/node_pattern_helper.rb
diff options
context:
space:
mode:
Diffstat (limited to 'rubocop/node_pattern_helper.rb')
-rw-r--r--rubocop/node_pattern_helper.rb14
1 files changed, 14 insertions, 0 deletions
diff --git a/rubocop/node_pattern_helper.rb b/rubocop/node_pattern_helper.rb
new file mode 100644
index 00000000000..ecd4560763c
--- /dev/null
+++ b/rubocop/node_pattern_helper.rb
@@ -0,0 +1,14 @@
+# frozen_string_literal: true
+
+module RuboCop
+ module NodePatternHelper
+ # Returns a nested `(const ...)` node pattern for a full qualified +name+.
+ #
+ # @examples
+ # const_pattern 'Foo::Bar' # => (const (const {nil? cbase} :Foo) :Bar)
+ # const_pattern 'Foo::Bar', parent: ':Baz' # => (const (const :Baz :Foo) :Bar)
+ def const_pattern(name, parent: '{nil? cbase}')
+ name.split('::').inject(parent) { |memo, name_part| "(const #{memo} :#{name_part})" }
+ end
+ end
+end