Skip to content

Add naming spike #18

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 55 additions & 0 deletions lib/naming_normalizers/base.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
module NamingNormalizers
class Base < Parser::TreeRewriter
include Mandate

def initialize(code)
@code = code
@mapping = {}
end

def call
buffer = Parser::Source::Buffer.new('', source: code)
builder = RuboCop::AST::Builder.new
ast = Parser::CurrentRuby.new(builder).parse(buffer)
rewrite(buffer, ast)
end

def on_op_asgn(node)
# p "opasgn"
#node.pry
super
end

def on_casgn(node)
# p "casgn"
#node.pry
super
end

def on_defs(node)
# p "defs"
#node.pry
super
end

def on_numblock(node)
# p "numblock"
#node.pry
super
end

def handler_missing(node)
case node.type
# noop
when true, :str, :nil, :int
super
else
p "handler_missing"
#node.pry
end
end

private
attr_reader :code, :mapping
end
end
60 changes: 60 additions & 0 deletions lib/naming_normalizers/method_locals.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
module NamingNormalizers
class MethodLocals < Base

def initialize(code)
super(code)

@method_name = nil
@methods_vars = {}
end

%i[
on_argument
on_const
on_var
on_vasgn
].each do |method_name|
define_method method_name do |node|
replace_loc_name(node)
super(node)
end
end

def on_def(node)
@method_name = node.loc.name.source
@methods_vars[@method_name] = {}

super(node)
end

def on_end(node)
node.pry
end

def handler_missing(node)
super(node)
end

def replace_loc_name(node)
replace(node.loc.name, placeholder_for(node.loc.name.source))
end

def replace_loc_selector(node)
replace(node.loc.selector, placeholder_for(node.loc.selector.source))
end

def placeholder_for(token)
# Return if we're outside of a method
return token unless instance_variable_defined?("@method_name") && @method_name

# Get the vars for this method_naem
meth_vars = @methods_vars[@method_name]

# Return if we have one
return meth_vars[token] if meth_vars.key?(token)

# Else get out of here
meth_vars[token] = "#{@method_name}_#{meth_vars.length}"
end
end
end
114 changes: 114 additions & 0 deletions lib/normalizers/method_internal_naming_normalising.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
require 'parser/current'

# class Normalize < Parser::AST::Processor
class NamingNormalizers::MethodInternals < Parser::TreeRewriter
include Mandate

def initialize(code, mapping)
@original_code = code
@mapping = mapping
end

def call
buffer = Parser::Source::Buffer.new('', source: original_code)
builder = RuboCop::AST::Builder.new
ast = Parser::CurrentRuby.new(builder).parse(buffer)
rewrite(buffer, ast)
end

%i[
on_argument
on_const
on_var
on_vasgn
].each do |method_name|
define_method method_name do |node|
replace_loc_name(node)
super(node)
end
end

def on_def(node)
# node.pry
p node.loc.name.source
# @method_scope = metho
replace_loc_name(node)
super(node)
end

def on_send(node)
# node.pry
if !node.enumerator_method? &&
!node.operator_method?
replace_loc_selector(node)
end

super
end

def process_regular_node
p "HERE?"
super
end

def on_op_asgn(node)
# p "opasgn"
#node.pry
super
end

def on_casgn(node)
# p "casgn"
#node.pry
super
end

def on_defs(node)
# p "defs"
#node.pry
super
end

def on_numblock(node)
# p "numblock"
#node.pry
super
end

def handler_missing(node)
case node.type
# noop
when true, :str, :nil, :int
super
else
p "handler_missing"
#node.pry
end
end

private
attr_reader :original_code, :mapping

def replace_loc_name(node)
p node.type
placeholder = case node.type
when :def
# node.pry
placeholder_for(:method, node.loc.name.source)
else
placeholder_for(:misc, node.loc.name.source)
end

replace(node.loc.name, placeholder)
end

def replace_loc_selector(node)
replace(node.loc.selector, placeholder_for(node.loc.selector.source))
end

def placeholder_for(type, token)
key = "#{type}_#{token}"
mapping.key?(key) ? mapping[key] : key
end
end

33 changes: 25 additions & 8 deletions lib/normalizers/naming_normalizer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ def call
%i[
on_argument
on_const
on_def
on_var
on_vasgn
].each do |method_name|
Expand All @@ -29,6 +28,14 @@ def call
end
end

def on_def(node)
# node.pry
p node.loc.name.source
# @method_scope = metho
replace_loc_name(node)
super(node)
end

def on_send(node)
# node.pry
if !node.enumerator_method? &&
Expand All @@ -44,25 +51,25 @@ def process_regular_node
end

def on_op_asgn(node)
p "opasgn"
# p "opasgn"
#node.pry
super
end

def on_casgn(node)
p "casgn"
# p "casgn"
#node.pry
super
end

def on_defs(node)
p "defs"
# p "defs"
#node.pry
super
end

def on_numblock(node)
p "numblock"
# p "numblock"
#node.pry
super
end
Expand All @@ -82,14 +89,24 @@ def handler_missing(node)
attr_reader :original_code, :mapping

def replace_loc_name(node)
replace(node.loc.name, placeholder_for(node.loc.name.source))
p node.type
placeholder = case node.type
when :def
# node.pry
placeholder_for(:method, node.loc.name.source)
else
placeholder_for(:misc, node.loc.name.source)
end

replace(node.loc.name, placeholder)
end

def replace_loc_selector(node)
replace(node.loc.selector, placeholder_for(node.loc.selector.source))
end

def placeholder_for(token)
mapping.key?(token) ? mapping[token] : token
def placeholder_for(type, token)
key = "#{type}_#{token}"
mapping.key?(key) ? mapping[key] : key
end
end
4 changes: 3 additions & 1 deletion lib/representer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,16 @@
require 'parser/current'
require 'pathname'

require_relative 'naming_normalizers/base'
require_relative 'naming_normalizers/method_locals'
require_relative 'normalizers/naming_normalizer'

require_relative 'generate_mapping'

require_relative 'representation'
require_relative 'represent_solution'

#require 'pry'
require 'pry'

module Representer
def self.generate(exercise_slug, solution_path, output_path)
Expand Down
Loading