From 805c7c05184ddf116cf45f308d9e00bdac1e2a8c Mon Sep 17 00:00:00 2001 From: Marc-Andre Lafortune Date: Mon, 13 Jul 2020 11:05:22 -0400 Subject: [PATCH 1/2] Fix alignment --- lib/rubocop/ast/node_pattern.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/rubocop/ast/node_pattern.rb b/lib/rubocop/ast/node_pattern.rb index ab8994dee..c3ff2f278 100644 --- a/lib/rubocop/ast/node_pattern.rb +++ b/lib/rubocop/ast/node_pattern.rb @@ -135,8 +135,8 @@ class Compiler SEPARATORS = /\s+/.freeze ONLY_SEPARATOR = /\A#{SEPARATORS}\Z/.freeze - TOKENS = Regexp.union(META, PARAM_CONST, KEYWORD_NAME, PARAM_NUMBER, NUMBER, - METHOD_NAME, SYMBOL, STRING) + TOKENS = Regexp.union(META, PARAM_CONST, KEYWORD_NAME, PARAM_NUMBER, NUMBER, + METHOD_NAME, SYMBOL, STRING) TOKEN = /\G(?:#{SEPARATORS}|#{TOKENS}|.)/.freeze From 4038a79d29daf0d3611b9e2b21c8f0dc38aa59e2 Mon Sep 17 00:00:00 2001 From: Marc-Andre Lafortune Date: Sat, 11 Jul 2020 17:50:08 -0400 Subject: [PATCH 2/2] Add Node#global_const? --- CHANGELOG.md | 1 + lib/rubocop/ast/node.rb | 14 ++++++++------ 2 files changed, 9 insertions(+), 6 deletions(-) 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