diff --git a/CHANGELOG.md b/CHANGELOG.md index a2bd660df..79f329ed3 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,7 @@ * [#50](https://github.com/rubocop-hq/rubocop-ast/pull/50): Support find pattern matching for Ruby 2.8 (3.0) parser. ([@koic][]) * [#55](https://github.com/rubocop-hq/rubocop-ast/pull/55): Add `ProcessedSource#line_with_comment?`. ([@marcandre][]) * [#63](https://github.com/rubocop-hq/rubocop-ast/pull/63): NodePattern now supports patterns as arguments to predicate and functions. ([@marcandre][]) +* [#64](https://github.com/rubocop-hq/rubocop-ast/pull/64): Add `Node#global_const?`. ([@marcandre][]) ### Bug fixes diff --git a/lib/rubocop/ast/node.rb b/lib/rubocop/ast/node.rb index 0ca94ed1e..5d05d28c6 100644 --- a/lib/rubocop/ast/node.rb +++ b/lib/rubocop/ast/node.rb @@ -313,8 +313,8 @@ def const_name def_node_matcher :defined_module0, <<~PATTERN {(class (const $_ $_) ...) (module (const $_ $_) ...) - (casgn $_ $_ (send (const nil? {:Class :Module}) :new ...)) - (casgn $_ $_ (block (send (const nil? {:Class :Module}) :new ...) ...))} + (casgn $_ $_ (send #global_const?({:Class :Module}) :new ...)) + (casgn $_ $_ (block (send #global_const?({:Class :Module}) :new ...) ...))} PATTERN private :defined_module0 @@ -496,16 +496,18 @@ def guard_clause? def_node_matcher :proc?, <<~PATTERN {(block (send nil? :proc) ...) - (block (send (const nil? :Proc) :new) ...) - (send (const nil? :Proc) :new)} + (block (send #global_const?(:Proc) :new) ...) + (send #global_const?(:Proc) :new)} PATTERN def_node_matcher :lambda?, '({block numblock} (send nil? :lambda) ...)' def_node_matcher :lambda_or_proc?, '{lambda? proc?}' + def_node_matcher :global_const?, '(const {nil? cbase} %1)' + def_node_matcher :class_constructor?, <<~PATTERN - { (send (const nil? {:Class :Module}) :new ...) - (block (send (const nil? {:Class :Module}) :new ...) ...)} + { (send #global_const?({:Class :Module}) :new ...) + (block (send #global_const?({:Class :Module}) :new ...) ...)} PATTERN # Some expressions are evaluated for their value, some for their side