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

+ ruby-[parse, rewrite]: add legacy switches #699

Merged
merged 1 commit into from May 26, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
23 changes: 21 additions & 2 deletions lib/parser/runner.rb
Expand Up @@ -14,9 +14,8 @@ def self.go(options)
end

def initialize
Parser::Builders::Default.modernize

@option_parser = OptionParser.new { |opts| setup_option_parsing(opts) }
@legacy = {}
@parser_class = nil
@parser = nil
@files = []
Expand All @@ -30,13 +29,16 @@ def initialize

def execute(options)
parse_options(options)
setup_builder_default
prepare_parser

process_all_input
end

private

LEGACY_MODES = %i[lambda procarg0 encoding index arg_inside_procarg0].freeze

def runner_name
raise NotImplementedError, "implement #{self.class}##{__callee__}"
end
Expand Down Expand Up @@ -126,6 +128,17 @@ def setup_option_parsing(opts)
@parser_class = Parser::RubyMotion
end

opts.on '--legacy', "Parse with all legacy modes" do
@legacy = Hash.new(true)
end

LEGACY_MODES.each do |mode|
opt_name = "--legacy-#{mode.to_s.gsub('_', '-')}"
opts.on opt_name, "Parse with legacy mode for emit_#{mode}" do
@legacy[mode] = true
end
end

opts.on '-w', '--warnings', 'Enable warnings' do |w|
@warnings = w
end
Expand Down Expand Up @@ -164,6 +177,12 @@ def parse_options(options)
end
end

def setup_builder_default
LEGACY_MODES.each do |mode|
Parser::Builders::Default.send(:"emit_#{mode}=", !@legacy[mode])
end
end

def prepare_parser
@parser = @parser_class.new

Expand Down
21 changes: 21 additions & 0 deletions test/test_runner_parse.rb
Expand Up @@ -18,6 +18,27 @@ def test_emit_ruby
's(:int, 123)'
end

def test_emit_modern_ruby
assert_prints ['-e', '->{}'],
'(lambda)'
assert_prints ['-e', 'self[1] = 2'],
'indexasgn'
end

def test_emit_legacy
assert_prints ['--legacy', '-e', '->{}'],
'(send nil :lambda)'
assert_prints ['--legacy', '-e', 'self[1] = 2'],
':[]='
end

def test_emit_legacy_lambda
assert_prints ['--legacy-lambda', '-e', '->{}'],
'(send nil :lambda)'
assert_prints ['--legacy-lambda', '-e', 'self[1] = 2'],
'indexasgn'
end

def test_emit_json
assert_prints ['--emit-json', '-e', '123'],
'["int",123]'
Expand Down