Skip to content

Commit

Permalink
Refactors rewriter into its own file
Browse files Browse the repository at this point in the history
  • Loading branch information
bougyman committed Apr 11, 2024
1 parent abc9f6b commit 19cdc97
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 36 deletions.
44 changes: 8 additions & 36 deletions lib/namespacer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,42 +9,14 @@
module Rubyists
# Namespace for the namespacer tool
module Namespacer
# Do the AST rewriting with the Rewriter class
class Rewriter < Parser::TreeRewriter
attr_accessor(:namespaces)

def initialize(namespaces)
super()
@namespaces = namespaces
end

def on_module(node)
_on_module(node)
end

def on_class(node)
_on_module(node)
end

private

def wrap(ast)
# Recursively wrap the AST in the namespace modules
namespaces.split('::').reverse.inject(ast) do |current_ast, ns|
# Create a module node with the current namespace part and the current AST
Parser::AST::Node.new(:module, [Parser::AST::Node.new(:const, [nil, ns.to_sym]), current_ast])
end
end

def _on_module(node)
return unless node.location.column.zero?

(ast, comments) = Unparser.parse_with_comments(node.location.expression.source)
replace(node.location.expression, Unparser.unparse(wrap(ast), comments))
end
end

# Wrap some namespace around top-level AST nodes of 'module' or 'class' type
require_relative 'namespacer/rewriter'

# Wrap some namespace(s) around top-level AST nodes of 'module' or 'class' type
#
# @param string_or_io [String, IO] The source code to namespace
# @param namespaces [String] The namespace(s) to wrap around the top-level AST nodes
#
# @return [String] The source code with the namespace(s) wrapped around the top-level AST nodes
def self.namespace!(string_or_io, namespaces)
buffer = Parser::Source::Buffer.new("(#{namespaces})")
buffer.source = string_or_io.is_a?(IO) ? string_or_io.read : string_or_io
Expand Down
40 changes: 40 additions & 0 deletions lib/namespacer/rewriter.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
# frozen_string_literal: true

module Rubyists
module Namespacer
# Do the AST rewriting with the Rewriter class
class Rewriter < Parser::TreeRewriter
attr_accessor(:namespaces)

def initialize(namespaces)
super()
@namespaces = namespaces
end

def on_module(node)
_on_module(node)
end

def on_class(node)
_on_module(node)
end

private

def wrap(ast)
# Recursively wrap the AST in the namespace modules
namespaces.split('::').reverse.inject(ast) do |current_ast, ns|
# Create a module node with the current namespace part and the current AST
Parser::AST::Node.new(:module, [Parser::AST::Node.new(:const, [nil, ns.to_sym]), current_ast])
end
end

def _on_module(node)
return unless node.location.column.zero?

(ast, comments) = Unparser.parse_with_comments(node.location.expression.source)
replace(node.location.expression, Unparser.unparse(wrap(ast), comments))
end
end
end
end

0 comments on commit 19cdc97

Please sign in to comment.