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

Rewrite code case #49

Open
rainmantsr opened this issue Apr 3, 2018 · 3 comments
Open

Rewrite code case #49

rainmantsr opened this issue Apr 3, 2018 · 3 comments

Comments

@rainmantsr
Copy link

I am using jruby-parser to rewrite code, but with some code is has some problems:
This:
require 'jruby-parser'

root = JRubyParser.parse( %q{
def log(severity)
case severity
when Severity::DEBUG
@logging_services.debug(msg.to_s)
when Severity::INFO
@logging_services.info(msg.to_s)
when Severity::WARNING
@logging_services.warn(msg.to_s)
else
@logging_services.error(msg.to_s)
end
end
} )

root.to_source

produces:
def log severity
case severity[
when Severity
@logging_services.debug(msg.to_s)
when Severity
@logging_services.info(msg.to_s)
when Severity
@logging_services.warn(msg.to_s)
else
@logging_services.error(msg.to_s),
when Severity
@logging_services.info(msg.to_s)
when Severity
@logging_services.warn(msg.to_s)
else
@logging_services.error(msg.to_s),
when Severity
@logging_services.warn(msg.to_s)
else
@logging_services.error(msg.to_s)]
end
end

Can I do something to avoid this behaviour?

@enebo
Copy link
Member

enebo commented Apr 6, 2018

rewriter definitely is broken in this case. It looks like two bugs in fact. All whens get the same 'else' added and also each case is only calling the first part of the constant (also referred to as Colon2 in this case).

@chirejaszy
Copy link

src/org/jrubyparser/ast/RootNode.java

@chirejaszy
Copy link

require 'jruby-parser'

root = JRubyParser.parse("b = foo(1)")
fcall = root.find_node(:fcall)
fcall.name = 'bar'
fcall.args[0] = true

Write out the new source

root.to_source # b = bar(true)

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

3 participants