Skip to content

Level nesting error when formatting many cascading elsif clauses. #491

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
davebenvenuti opened this issue Jan 24, 2025 · 0 comments
Open

Comments

@davebenvenuti
Copy link

davebenvenuti commented Jan 24, 2025

I noticed SyntaxTree.format has trouble with many (thousands) of subsequent elsif clauses. Here's an admittedly contrived example:

OS: macOS Sequoia 15.2
Ruby Version: ruby 3.3.4 (2024-07-09 revision be1089c8ec) [arm64-darwin23]
syntax_tree Version: 6.2.0

syntax_tree_nesting_error.rb

#!/usr/bin/env ruby

require "erb"
require "syntax_tree"

SyntaxTree.format(ERB.new(<<~RUBY, trim_mode: "-").result)
  def foo(x)
    if x == 0
      return :"0"
    <%- (1...5000).each do |i| -%>
    elsif x == <%= i %>
      return :"<%= i %>"
    <%- end -%>
    end
  end
RUBY

Output

ruby syntax_tree_nesting_error.rb
/Users/dave/.gem/ruby/3.3.4/gems/syntax_tree-6.2.0/lib/syntax_tree/parser.rb:2864:in `on_parse_error': nesting too deep (SyntaxTree::Parser::ParseError)
        from /Users/dave/.gem/ruby/3.3.4/gems/syntax_tree-6.2.0/lib/syntax_tree.rb:124:in `parse'
        from /Users/dave/.gem/ruby/3.3.4/gems/syntax_tree-6.2.0/lib/syntax_tree.rb:124:in `parse'
        from /Users/dave/.gem/ruby/3.3.4/gems/syntax_tree-6.2.0/lib/syntax_tree.rb:68:in `format'
        from syntax_tree_nesting_error.rb:6:in `<main>'

Interestingly, the same idea except with single-line conditionals works fine:

syntax_tree_single_line_conditionals.rb

#!/usr/bin/env ruby

require "erb"
require "syntax_tree"

SyntaxTree.format(ERB.new(<<~RUBY, trim_mode: "-").result)
  def foo(x)
    return :"0"
    <%- (1...5000).each do |i| -%>
    return :"<%= i %>" if x == <%= i %>
    <%- end -%>
  end
RUBY

This version runs without raising an error.

@davebenvenuti davebenvenuti changed the title Level-nesting error when formatting many cascading elsif clauses. Level nesting error when formatting many cascading elsif clauses. Jan 24, 2025
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

No branches or pull requests

1 participant