Skip to content

Commit

Permalink
+ ruby-[parse, rewrite]: add legacy switches (#699)
Browse files Browse the repository at this point in the history
  • Loading branch information
marcandre committed May 26, 2020
1 parent 521054b commit 996c820
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 2 deletions.
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

0 comments on commit 996c820

Please sign in to comment.