Skip to content
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

NoMethodError: undefined method `type' #141

Closed
catmando opened this issue Mar 13, 2020 · 4 comments
Closed

NoMethodError: undefined method `type' #141

catmando opened this issue Mar 13, 2020 · 4 comments
Assignees
Labels

Comments

@catmando
Copy link

The following AST

s(:def, :wait,
  s(:args,
    s(:arg, :seconds)),
  s(:block,
    s(:send,
      s(:send,
        s(:const, nil, :Promise), :new), :tap),
    s(:args,
      s(:procarg0, :p)),
    s(:block,
      s(:send, nil, :after,
        s(:lvar, :seconds)),
      s(:args),
      s(:send,
        s(:lvar, :p), :resolve,
        s(:lvar, :seconds)))))

throws this error:

NoMethodError: undefined method `type' for :p:Symbol

on version >= '0.4.6' and `0.4.7. (0.4.5 works fine.)

The precise code is as follows:

Unparser.unparse Parser::CurrentRuby.parse(
  "def wait(seconds)\nPromise.new.tap { |p| after(seconds) { p.resolve(seconds) }}\nend"
)

Parser::CurrentRuby == Parser::Ruby26

(parser v 2.6.5.0)

@mbj
Copy link
Owner

mbj commented Mar 15, 2020

@catmando BTW I can only reproduce this via your script if I enable the modern Ruby AST:

Parser::Builders::Default.emit_lambda   = true
Parser::Builders::Default.emit_procarg0 = true
Parser::Builders::Default.emit_encoding = true
Parser::Builders::Default.emit_index    = true

@mbj
Copy link
Owner

mbj commented Mar 15, 2020

@catmando And more: The modern Ruby AST documentation is incomplete, which I just corrected with a pull request to parser.

To get your example parsing you need to switch on: emit_arg_inside_procarg0. Which than gets you to the actual modern AST format.

Or you use Unaprser.parse which uses a correctly configured parser internally.

@mbj
Copy link
Owner

mbj commented Mar 15, 2020

@catmando I'm closing this as I'm confident your issue will be resolved once you activate the modern AST format. Please reopen if its not the case.

@mbj mbj closed this as completed Mar 15, 2020
@mbj mbj added the bug label Mar 15, 2020
@mbj mbj self-assigned this Mar 15, 2020
@catmando
Copy link
Author

@mbj Thanks! I can't remember why, but I needed modern AST. However adding emit_arg_inside_procarg0 = true seems to make everything good. Thanks so much for finding that.

Also thanks for this Gem. Its an absolute key component of a gem called hyper-spec. Hyper-spec lets you write rspec expectations that will run on the client (using the Opal -> JS transpiler)

So you can write a spec like this:

  it "can load isomorphic code using the isomorphic method" do
    isomorphic do  # this executes on both the client and the server
      def factorial(n)
        n==1 ? 1 : n * factorial(n-1)
      end
    end
    expect_evaluate_ruby do 
      factorial(5)  # this runs on the client in the browser (or headless browser)
    end.to eq(factorial(5))
  end

we use parser to parse the code and extract the code in the blocks, then use unparser to get in back into ruby, then the opal compiler to translate it to javascript.

We have 1000's of specs written like this, and it lets us code everything in ruby, so a huge thanks to you.

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

No branches or pull requests

2 participants