Skip to content

Bug formating code, possibly? #297

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

Closed
stoivo opened this issue Feb 8, 2023 · 5 comments · Fixed by #300
Closed

Bug formating code, possibly? #297

stoivo opened this issue Feb 8, 2023 · 5 comments · Fixed by #300

Comments

@stoivo
Copy link

stoivo commented Feb 8, 2023

I think I have found a bug. I think this code should work.

require 'bundler/inline'

gemfile do
  source 'https://rubygems.org'
  gem 'syntax_tree', '5.3.0'
end

require "syntax_tree"

source = <<~RUBY
with_options bookkeeping_account_number: 12 do
  validates :bookkeeping_number
end
RUBY

program = SyntaxTree.parse(source)
program => { statements: { body: [binary] } }

formatter = SyntaxTree::Formatter.new(source, [])
binary.format(formatter)

formatter.flush
formatter.output.join

right now I get

untitled2.rb:26: warning: One-line pattern matching is experimental, and the behavior may change in future versions of Ruby!                                                                                                                                                                                                               
/Users/simon/.asdf/installs/ruby/3.0.5/lib/ruby/gems/3.0.0/gems/syntax_tree-5.3.0/lib/syntax_tree/node.rb:4374:in `forced_do_end_bounds?': undefined method `call' for nil:NilClass (NoMethodError)                                                                                                                                        
        from /Users/simon/.asdf/installs/ruby/3.0.5/lib/ruby/gems/3.0.0/gems/syntax_tree-5.3.0/lib/syntax_tree/node.rb:4308:in `format'                                                                                                                                                                                                    
        from /Users/simon/.asdf/installs/ruby/3.0.5/lib/ruby/gems/3.0.0/gems/syntax_tree-5.3.0/lib/syntax_tree/formatter.rb:168:in `format'                                                                                                                                                                                                
        from /Users/simon/.asdf/installs/ruby/3.0.5/lib/ruby/gems/3.0.0/gems/syntax_tree-5.3.0/lib/syntax_tree/node.rb:3434:in `format'                                                                                                                                                                                                    
        from untitled2.rb:29:in `<main>'   
@kddnewton
Copy link
Member

Yeah, generally we don't really test formatting inner nodes of the tree, just from the top down. But I can see this use case, so it'll be fixed by #300.

@stoivo
Copy link
Author

stoivo commented Feb 8, 2023

Just so you know use it to see if you changed any assosiasions on any of our models. If you did it would require us to update some doc, sometimes. So I find all the calls to belongs_to, has_one or has_many and copy out just those calles like this.

class AssosiationVisitor < SyntaxTree::Visitor
  attr_reader :assosiations

  def initialize
    @assosiations = []
  end

  visit_method def visit_command(node)
    if ASSOSIATION_RELATIONS.member?(node.message.value)
      formatter = SyntaxTree::Formatter.new(node, [])
      node.format(formatter)

      formatter.flush
      @assosiations.push(formatter.output.join)
    end

    super
  end
end

def find_assisiations(source)
  visitor = AssosiationVisitor.new
  visitor.visit(SyntaxTree.parse(source))
  visitor.assosiations
end

@kddnewton
Copy link
Member

kddnewton commented Feb 8, 2023

Nice that's cool!

A small thing, you can move a couple of things around in there and it might be a little more ergonomic.

class AssosiationVisitor < SyntaxTree::Visitor
  attr_reader :assosiations

  def initialize
    @assosiations = []
  end

  visit_method def visit_command(node)
    if ASSOSIATION_RELATIONS.member?(node.message.value)
      formatter = SyntaxTree::Formatter.new(node, [])
      node.format(formatter)

      formatter.flush
      @assosiations.push(formatter.output.join)
    end

    super
  end

  def visit_program(node)
    assosiations
  end
end

def find_assisiations(source)
  SyntaxTree.parse(source).accept(AssosiationVisitor.new)
end

@stoivo
Copy link
Author

stoivo commented Feb 11, 2023

It should be ... right?

  def visit_program(node)
    super
    assosiations
  end

@kddnewton
Copy link
Member

Ahh yeah, there should be a super in there, whoops

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants