Skip to content

Commit 24d4701

Browse files
committed
Add an option to supress Parser gem compatibility warnings
1 parent 8a70019 commit 24d4701

5 files changed

Lines changed: 40 additions & 13 deletions

File tree

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
* Only two adapters are now available: SSH adapter, and local temp file adapter. All other adapters are deprecated.
2424
* Cache directory is now a [global configuration option](README.md#configuration), rather than an argument to the ExecutionContext.
2525
* The default cache location is now in the .remote_ruby/cache, relative to the working directory.
26+
* Added a possibility to suppress [Parser warnings](https://github.com/whitequark/parser#compatibility-with-ruby-mri).
2627
* Added [changelog](#CHANGELOG.md)
2728

2829
### Migration from v0.3

README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,11 @@ RemoteRuby.configure do |c|
144144
# `dump_code` is set to `true` in the ExecutionContext.
145145
# By default code is saved to .remote_ruby/code (relative to the working directory).
146146
c.code_dir = File.join(Dir.pwd, '.remote_ruby/code')
147+
148+
# Set to true if you don't want to see warnings about parser gem compatibility with
149+
# current Ruby version.
150+
# False by default.
151+
c.suppress_parser_warnings = false
147152
end
148153
```
149154

lib/remote_ruby.rb

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ module RemoteRuby
1313

1414
class << self
1515
attr_reader :plugins, :ignored_types
16-
attr_accessor :cache_dir, :code_dir
16+
attr_accessor :cache_dir, :code_dir, :suppress_parser_warnings
1717

1818
def root(*params)
1919
root_dir = ::Gem::Specification.find_by_name('remote_ruby').gem_dir
@@ -63,5 +63,7 @@ def configure
6363

6464
config.ignore_types RemoteRuby::ExecutionContext
6565

66+
config.suppress_parser_warnings = false
67+
6668
config.register_plugin(:rails, RemoteRuby::RailsPlugin)
6769
end

lib/remote_ruby/parser_factory.rb

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
# frozen_string_literal: true
2+
3+
module RemoteRuby
4+
# Serves to dynamically require the parser gem and configure it.
5+
# This is done in a separate module to have a possibility
6+
# to suppress parser gem warnings about Ruby compatibility.
7+
module ParserFactory
8+
def self.require_parser
9+
begin
10+
prev = $VERBOSE
11+
$VERBOSE = nil if RemoteRuby.suppress_parser_warnings
12+
require 'parser/current'
13+
require 'unparser'
14+
ensure
15+
$VERBOSE = prev
16+
end
17+
18+
# Opt-in to most recent AST format
19+
Parser::Builders::Default.emit_lambda = true
20+
Parser::Builders::Default.emit_procarg0 = true
21+
Parser::Builders::Default.emit_encoding = true
22+
Parser::Builders::Default.emit_index = true
23+
Parser::Builders::Default.emit_arg_inside_procarg0 = true
24+
Parser::Builders::Default.emit_forward_arg = true
25+
Parser::Builders::Default.emit_kwargs = true
26+
Parser::Builders::Default.emit_match_pattern = true
27+
end
28+
end
29+
end

lib/remote_ruby/source_extractor.rb

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,14 @@
11
# frozen_string_literal: true
22

33
require 'method_source'
4-
require 'parser/current'
5-
require 'unparser'
6-
7-
# Opt-in to most recent AST format
8-
Parser::Builders::Default.emit_lambda = true
9-
Parser::Builders::Default.emit_procarg0 = true
10-
Parser::Builders::Default.emit_encoding = true
11-
Parser::Builders::Default.emit_index = true
12-
Parser::Builders::Default.emit_arg_inside_procarg0 = true
13-
Parser::Builders::Default.emit_forward_arg = true
14-
Parser::Builders::Default.emit_kwargs = true
15-
Parser::Builders::Default.emit_match_pattern = true
4+
require 'remote_ruby/parser_factory'
165

176
module RemoteRuby
187
# Receives a block and extracts Ruby code (as a string) with this block's
198
# source
209
class SourceExtractor
2110
def extract(&block)
11+
RemoteRuby::ParserFactory.require_parser
2212
ast = Parser::CurrentRuby.parse(block.source)
2313
block_node = find_block(ast)
2414

0 commit comments

Comments
 (0)