Skip to content

Commit

Permalink
Reorganize node classes into new AST namespace
Browse files Browse the repository at this point in the history
This change introduces a new `RuboCop::AST` namespace, and moves the
following classes into it:

 - `Node`
 - `Sexp`
 - `ArrayNode`, `HashNode`, `IfNode`, `ConditionalNode`, `ModifierNode`,
   `UntilNode`, `WhileNode`

It also renames the `ast_node` directory to `ast`, and `ast_node.rb` to
`node.rb`.
  • Loading branch information
Drenmi authored and bbatsov committed Jan 5, 2017
1 parent 0d01d29 commit 48f1637
Show file tree
Hide file tree
Showing 36 changed files with 97 additions and 715 deletions.
23 changes: 12 additions & 11 deletions lib/rubocop.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,17 +19,18 @@
require 'rubocop/name_similarity'
require 'rubocop/node_pattern'
require 'rubocop/string_interpreter'
require 'rubocop/ast_node/sexp'
require 'rubocop/ast_node'
require 'rubocop/ast_node/node_extensions/conditional_node'
require 'rubocop/ast_node/node_extensions/modifier_node'
require 'rubocop/ast_node/node_extensions/array_node'
require 'rubocop/ast_node/node_extensions/hash_node'
require 'rubocop/ast_node/node_extensions/if_node'
require 'rubocop/ast_node/node_extensions/until_node'
require 'rubocop/ast_node/node_extensions/while_node'
require 'rubocop/ast_node/builder'
require 'rubocop/ast_node/traversal'
require 'rubocop/ast/sexp'
require 'rubocop/ast/node'
require 'rubocop/ast/node/mixin/conditional_node'
require 'rubocop/ast/node/mixin/modifier_node'
require 'rubocop/ast/node/array_node'
require 'rubocop/ast/node/case_node'
require 'rubocop/ast/node/hash_node'
require 'rubocop/ast/node/if_node'
require 'rubocop/ast/node/until_node'
require 'rubocop/ast/node/while_node'
require 'rubocop/ast/builder'
require 'rubocop/ast/traversal'
require 'rubocop/error'
require 'rubocop/warning'

Expand Down
14 changes: 7 additions & 7 deletions lib/rubocop/ast_node/builder.rb → lib/rubocop/ast/builder.rb
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
# frozen_string_literal: true

module RuboCop
class Node
module AST
# `RuboCop::Builder` is an AST builder that is utilized to let `Parser`
# generate ASTs with {RuboCop::Node}.
# generate ASTs with {RuboCop::AST::Node}.
#
# @example
# buffer = Parser::Source::Buffer.new('(string)')
Expand All @@ -28,11 +28,11 @@ def string_value(token)

def node_map(type)
case type
when :array then RuboCop::NodeExtension::ArrayNode
when :hash then RuboCop::NodeExtension::HashNode
when :if then RuboCop::NodeExtension::IfNode
when :until, :until_post then RuboCop::NodeExtension::UntilNode
when :while, :while_post then RuboCop::NodeExtension::WhileNode
when :array then ArrayNode
when :hash then HashNode
when :if then IfNode
when :until, :until_post then UntilNode
when :while, :while_post then WhileNode
else Node
end
end
Expand Down
5 changes: 3 additions & 2 deletions lib/rubocop/ast/node.rb
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ def sibling_index
def each_ancestor(*types, &block)
return to_enum(__method__, *types) unless block_given?

visit_ancestors_with_types(types, &block)
visit_ancestors(types, &block)

self
end
Expand Down Expand Up @@ -422,6 +422,7 @@ def binary_operation?

def chained?
return false unless argument?

receiver, _method_name, *_args = *parent
equal?(receiver)
end
Expand Down Expand Up @@ -523,7 +524,7 @@ def visit_descendants(types, &block)

private

def visit_ancestors_with_types(types)
def visit_ancestors(types)
last_node = self

while (current_node = last_node.parent)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
# frozen_string_literal: true

module RuboCop
module NodeExtension
module AST
# A node extension for `array` nodes.
class ArrayNode < RuboCop::Node
class ArrayNode < Node
PERCENT_LITERAL_TYPES = {
string: /^%[wW]/,
symbol: /^%[iI]/
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
# frozen_string_literal: true

module RuboCop
module NodeExtension
module AST
# A node extension for `hash` nodes.
class HashNode < RuboCop::Node
class HashNode < Node
def pairs
each_pair.to_a
end
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
# frozen_string_literal: true

module RuboCop
module NodeExtension
module AST
# A node extension for `if` nodes.
class IfNode < RuboCop::Node
class IfNode < Node
include ConditionalNode
include ModifierNode

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# frozen_string_literal: true

module RuboCop
module NodeExtension
module AST
# Common functionality for nodes that have conditions:
# `if`, `while`, `until`, `case`
module ConditionalNode
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# frozen_string_literal: true

module RuboCop
module NodeExtension
module AST
# Common functionality for nodes that can be used as modifiers:
# `if`, `while`, `until`
module ModifierNode
Expand Down
4 changes: 0 additions & 4 deletions lib/rubocop/ast/node/until_node.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,6 @@ def keyword
'until'
end

def inverse_keyword
'while'
end

def do?
loc.begin && loc.begin.is?('do')
end
Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,20 @@
# frozen_string_literal: true

module RuboCop
module NodeExtension
module AST
# A node extension for `until` nodes.
class UntilNode < RuboCop::Node
class UntilNode < Node
include ConditionalNode
include ModifierNode

def keyword
'until'
end

def inverse_keyword
'while'
end

def do?
loc.begin && loc.begin.is?('do')
end
Expand Down
4 changes: 0 additions & 4 deletions lib/rubocop/ast/node/while_node.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,6 @@ def keyword
'while'
end

def inverse_keyword
'until'
end

def do?
loc.begin && loc.begin.is?('do')
end
Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,20 @@
# frozen_string_literal: true

module RuboCop
module NodeExtension
module AST
# A node extension for `while` nodes.
class WhileNode < RuboCop::Node
class WhileNode < Node
include ConditionalNode
include ModifierNode

def keyword
'while'
end

def inverse_keyword
'until'
end

def do?
loc.begin && loc.begin.is?('do')
end
Expand Down
16 changes: 16 additions & 0 deletions lib/rubocop/ast/sexp.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# frozen_string_literal: true

module RuboCop
module AST
# This module provides a shorthand method to create a {Node} like
# `Parser::AST::Sexp`.
#
# @see http://rubydoc.info/gems/ast/AST/Sexp
module Sexp
# Creates a {Node} with type `type` and children `children`.
def s(type, *children)
Node.new(type, children)
end
end
end
end
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# frozen_string_literal: true

module RuboCop
class Node
module AST
# Provides methods for traversing an AST.
# Does not transform an AST; for that, use Parser::AST::Processor.
# Override methods to perform custom processing. Remember to call `super`
Expand Down
Loading

0 comments on commit 48f1637

Please sign in to comment.