ruby-next-core 0.14.1 → 0.15.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: c8f084701e3c87e432518f6367d883b9524dbc002d77352793b912504a63e8d0
4
- data.tar.gz: bc44011f4183dd2133f259f07dc7a40c4be5dbd4cf2f85e35efb525463011ace
3
+ metadata.gz: 28083aad6fa1b32e00c885533e4c9c732a2401057f9ed6a939cd78ff3b1b58bb
4
+ data.tar.gz: 55734dd6e38699ff7e03c2723633ecd063f8fd97fe8878ca553772dbdac08ab3
5
5
  SHA512:
6
- metadata.gz: eabbb2bc0ccfa86f033eabce8001d208638a3995c7f112c29ed0e5f3142597bc60d87112899e7d383b09739c7c4cbe2eff06d10c58df7577cf12f80d4cb19c4d
7
- data.tar.gz: 000c76da160d8dba1231121b7e71b335da2d9775240bfc7a9fb2201ca55d6ac366f47f1c918a3c5ebb46abdcc90857b0c3a9d221455ed6eff68965f5b8f905e9
6
+ metadata.gz: 7d42096d6a616470a402493894d5a7e65b7f08fd935dc8acfd0b91bf66016030edc93fd9c742aeed2fe8653dceb51c7b08c669e48ed1ff11373202cd738c9abc
7
+ data.tar.gz: a5740b48cb70283a3848d9415205baf7d17f4ee859562937b7f597bea0de43edba09f59bead4418284b395e6132cec92222b93cb1c1dc0c985adc3eda7b36986
data/CHANGELOG.md CHANGED
@@ -2,6 +2,20 @@
2
2
 
3
3
  ## master
4
4
 
5
+ ## 0.15.0 (2022-03-21)
6
+
7
+ - Support IRB ([@palkan][])
8
+
9
+ - Create empty `.rbnext` folder during `nextify` if nothing to transpile. ([@palkan][])
10
+
11
+ This would prevent from auto-transpiling a library every time when no files should be transpiled.
12
+
13
+ - Auto-transpile using the current Ruby version. ([@palkan][])
14
+
15
+ - Support Pry. ([@baygeldin][])
16
+
17
+ - Add `rescue/ensure/else` within block rewriter for Ruby < 2.5. ([@fargelus][])
18
+
5
19
  ## 0.14.1 (2022-01-21)
6
20
 
7
21
  - Fix nested find patterns transpiling. ([@palkan][])
data/README.md CHANGED
@@ -71,6 +71,8 @@ _Please, submit a PR to add your project to the list!_
71
71
  - [`ruby -ruby-next`](#uby-next)
72
72
  - [Logging & Debugging](#logging-and-debugging)
73
73
  - [RuboCop](#rubocop)
74
+ - [Using with IRB](#irb)
75
+ - [Using with Pry](#pry)
74
76
  - [Using with EOL Rubies](#using-with-eol-rubies)
75
77
  - [Proposed & edge features](#proposed-and-edge-features)
76
78
  - [Known limitations](#known-limitations)
@@ -454,6 +456,38 @@ AllCops:
454
456
 
455
457
  **NOTE:** you need `ruby-next` gem available in the environment where you run RuboCop (having `ruby-next-core` is not enough).
456
458
 
459
+ ## IRB
460
+
461
+ Ruby Next supports IRB. In order to enable edge Ruby features for your REPL, add the following line to your `.irbrc`:
462
+
463
+ ```ruby
464
+ require "ruby-next/irb"
465
+ ```
466
+
467
+ Alternatively, you can require it at startup:
468
+
469
+ ```sh
470
+ irb -r ruby-next/irb
471
+ # or
472
+ irb -ruby-next/irb
473
+ ```
474
+
475
+ ## Pry
476
+
477
+ Ruby Next supports Pry. In order to enable edge Ruby features for your REPL, add the following line to your `.pryrc`:
478
+
479
+ ```ruby
480
+ require "ruby-next/pry"
481
+ ```
482
+
483
+ Alternatively, you can require it at startup:
484
+
485
+ ```sh
486
+ pry -r ruby-next/pry
487
+ # or
488
+ pry -ruby-next/pry
489
+ ```
490
+
457
491
  ## Using with EOL Rubies
458
492
 
459
493
  We currently provide support for Ruby 2.2, 2.3 and 2.4.
@@ -186,6 +186,9 @@ module RubyNext
186
186
  require "ruby-next/language/rewriters/2.3/safe_navigation"
187
187
  rewriters << Rewriters::SafeNavigation
188
188
 
189
+ require "ruby-next/language/rewriters/2.5/rescue_within_block"
190
+ rewriters << Rewriters::RescueWithinBlock
191
+
189
192
  require "ruby-next/language/rewriters/2.7/args_forward"
190
193
  rewriters << Rewriters::ArgsForward
191
194
 
@@ -24,6 +24,8 @@ module RubyNext
24
24
  contents = File.read(path)
25
25
  transpile path, contents
26
26
  end
27
+
28
+ ensure_rbnext!
27
29
  end
28
30
 
29
31
  def parse!(args)
@@ -185,6 +187,17 @@ module RubyNext
185
187
  FileUtils.rm_r(next_dir_path)
186
188
  end
187
189
 
190
+ def ensure_rbnext!
191
+ return if CLI.dry_run? || stdout?
192
+
193
+ return if File.directory?(next_dir_path)
194
+
195
+ return if next_dir_path.end_with?(".rb")
196
+
197
+ FileUtils.mkdir_p next_dir_path
198
+ File.write(File.join(next_dir_path, ".keep"), "")
199
+ end
200
+
188
201
  def next_dir_path
189
202
  @next_dir_path ||= (out_path || File.join(lib_path, RUBY_NEXT_DIR))
190
203
  end
@@ -65,7 +65,7 @@ module RubyNext
65
65
  end
66
66
 
67
67
  class << self
68
- # Returns true if the syntax is supported
68
+ # Returns true if the syntax is not supported
69
69
  # by the current Ruby (performs syntax check, not version check)
70
70
  def unsupported_syntax?
71
71
  save_verbose, $VERBOSE = $VERBOSE, nil
@@ -24,6 +24,8 @@ module RubyNext
24
24
  contents = File.read(path)
25
25
  transpile path, contents
26
26
  end
27
+
28
+ ensure_rbnext!
27
29
  end
28
30
 
29
31
  def parse!(args)
@@ -185,6 +187,17 @@ module RubyNext
185
187
  FileUtils.rm_r(next_dir_path)
186
188
  end
187
189
 
190
+ def ensure_rbnext!
191
+ return if CLI.dry_run? || stdout?
192
+
193
+ return if File.directory?(next_dir_path)
194
+
195
+ return if next_dir_path.end_with?(".rb")
196
+
197
+ FileUtils.mkdir_p next_dir_path
198
+ File.write(File.join(next_dir_path, ".keep"), "")
199
+ end
200
+
188
201
  def next_dir_path
189
202
  @next_dir_path ||= (out_path || File.join(lib_path, RUBY_NEXT_DIR))
190
203
  end
@@ -0,0 +1,24 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "ruby-next"
4
+ # Include RubyNext into TOPLEVEL_BINDING for polyfills to work
5
+ eval("using RubyNext", TOPLEVEL_BINDING, __FILE__, __LINE__)
6
+
7
+ require "ruby-next/language"
8
+
9
+ # IRB extension to transpile code before evaluating
10
+ module RubyNext
11
+ module IRBExt
12
+ def evaluate(context, statements, *args)
13
+ new_statements = ::RubyNext::Language.transform(
14
+ statements,
15
+ rewriters: ::RubyNext::Language.current_rewriters,
16
+ using: false
17
+ )
18
+
19
+ super(context, new_statements, *args)
20
+ end
21
+ end
22
+ end
23
+
24
+ IRB::WorkSpace.prepend(RubyNext::IRBExt)
@@ -0,0 +1,39 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RubyNext
4
+ module Language
5
+ module Rewriters
6
+ class RescueWithinBlock < Base
7
+ NAME = "rescue-within-block"
8
+ SYNTAX_PROBE = "lambda do
9
+ raise 'err'
10
+ rescue
11
+ $! # => #<RuntimeError: err>
12
+ end.call"
13
+
14
+ MIN_SUPPORTED_VERSION = Gem::Version.new("2.5.0")
15
+
16
+ def on_block(block_node)
17
+ exception_node = block_node.children.find do |node|
18
+ node.type == :rescue || node.type == :ensure
19
+ end
20
+
21
+ return unless exception_node
22
+
23
+ context.track! self
24
+
25
+ insert_before(exception_node.loc.expression, "begin;")
26
+ insert_after(exception_node.loc.expression, ";end")
27
+
28
+ new_childrens = block_node.children.map do |child|
29
+ next s(:kwbegin, exception_node) if child == exception_node
30
+
31
+ child
32
+ end
33
+
34
+ block_node.updated(:block, new_childrens)
35
+ end
36
+ end
37
+ end
38
+ end
39
+ end
@@ -66,7 +66,7 @@ module RubyNext
66
66
  end
67
67
 
68
68
  class << self
69
- # Returns true if the syntax is supported
69
+ # Returns true if the syntax is not supported
70
70
  # by the current Ruby (performs syntax check, not version check)
71
71
  def unsupported_syntax?
72
72
  save_verbose, $VERBOSE = $VERBOSE, nil
@@ -12,7 +12,7 @@ module RubyNext
12
12
  return if File.directory?(target_dir)
13
13
 
14
14
  Dir.chdir(root_dir) do
15
- unless system("bundle exec ruby-next nextify ./#{lib_dir} -o #{target_dir} > /dev/null 2>&1")
15
+ unless system("bundle exec ruby-next nextify ./#{lib_dir} -o #{target_dir} --min-version=#{RUBY_VERSION} > /dev/null 2>&1")
16
16
  RubyNext.warn "Traspiled files are missing in: #{target_dir}. \n" \
17
17
  "Make sure you have gem 'ruby-next' in your Gemfile to auto-transpile the required files from source on load. " \
18
18
  "Otherwise the code from #{root_dir} may not work correctly."
@@ -186,6 +186,9 @@ module RubyNext
186
186
  require "ruby-next/language/rewriters/2.3/safe_navigation"
187
187
  rewriters << Rewriters::SafeNavigation
188
188
 
189
+ require "ruby-next/language/rewriters/2.5/rescue_within_block"
190
+ rewriters << Rewriters::RescueWithinBlock
191
+
189
192
  require "ruby-next/language/rewriters/2.7/args_forward"
190
193
  rewriters << Rewriters::ArgsForward
191
194
 
@@ -0,0 +1,90 @@
1
+ # frozen_string_literal: true
2
+
3
+ # This file contains patches for Pry integration.
4
+ # It's supposed to be required inside .pryrc files.
5
+
6
+ require "ruby-next/language/setup"
7
+ require "ruby-next/language/runtime"
8
+
9
+ # Enables refinements by injecting "using RubyNext"
10
+ # before Pry copies and memorizes the TOPLEVEL_BINDING.
11
+ Pry.singleton_class.prepend(Module.new do
12
+ def toplevel_binding
13
+ unless defined?(@_using_injected) && @_using_injected
14
+ orig_binding = super
15
+
16
+ # Copy TOPLEVEL_BINDING without local variables.
17
+ TOPLEVEL_BINDING.eval <<-RUBY
18
+ using RubyNext
19
+
20
+ def self.__pry__
21
+ binding
22
+ end
23
+
24
+ Pry.toplevel_binding = __pry__
25
+
26
+ class << self; undef __pry__; end
27
+ RUBY
28
+
29
+ # Inject local variables from the original binding.
30
+ orig_binding.local_variables.each do |var|
31
+ value = orig_binding.local_variable_get(var)
32
+ @toplevel_binding.local_variable_set(var, value)
33
+ end
34
+
35
+ @_using_injected = true
36
+ end
37
+
38
+ super
39
+ end
40
+ end)
41
+
42
+ # Enables edge Ruby syntax by transpiling the code
43
+ # before it's evaluated in the context of a binding.
44
+ Pry.prepend(Module.new do
45
+ def current_binding
46
+ super.tap do |obj|
47
+ next if obj.respond_to?(:__nextified__)
48
+
49
+ obj.instance_eval do
50
+ def eval(code, *args)
51
+ new_code = ::RubyNext::Language::Runtime.transform(code, using: false)
52
+
53
+ super(new_code, *args)
54
+ end
55
+
56
+ def __nextified__
57
+ end
58
+ end
59
+ end
60
+ end
61
+ end)
62
+
63
+ # Enables edge Ruby syntax for multi-line input.
64
+ Pry::Code.singleton_class.prepend(Module.new do
65
+ def complete_expression?(str)
66
+ silence_stderr do
67
+ ::Parser::RubyNext.parse(str)
68
+ end
69
+
70
+ true
71
+ rescue Parser::SyntaxError => ex
72
+ case ex.message
73
+ when /unexpected token \$end/
74
+ false
75
+ else
76
+ true
77
+ end
78
+ end
79
+
80
+ private
81
+
82
+ def silence_stderr
83
+ stderr = StringIO.new
84
+ orig_stderr, $stderr = $stderr, stderr
85
+
86
+ yield
87
+
88
+ $stderr = orig_stderr
89
+ end
90
+ end)
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module RubyNext
4
- VERSION = "0.14.1"
4
+ VERSION = "0.15.0"
5
5
  end
@@ -0,0 +1,3 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "ruby-next/irb"
@@ -0,0 +1,3 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "ruby-next/pry"
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ruby-next-core
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.14.1
4
+ version: 0.15.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Vladimir Dementyev
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2022-01-21 00:00:00.000000000 Z
11
+ date: 2022-03-21 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: ruby-next-parser
@@ -103,6 +103,7 @@ files:
103
103
  - lib/ruby-next/core/time/floor.rb
104
104
  - lib/ruby-next/core/unboundmethod/bind_call.rb
105
105
  - lib/ruby-next/core_ext.rb
106
+ - lib/ruby-next/irb.rb
106
107
  - lib/ruby-next/language.rb
107
108
  - lib/ruby-next/language/bootsnap.rb
108
109
  - lib/ruby-next/language/eval.rb
@@ -112,6 +113,7 @@ files:
112
113
  - lib/ruby-next/language/rewriters/2.3/safe_navigation.rb
113
114
  - lib/ruby-next/language/rewriters/2.3/squiggly_heredoc.rb
114
115
  - lib/ruby-next/language/rewriters/2.4/dir.rb
116
+ - lib/ruby-next/language/rewriters/2.5/rescue_within_block.rb
115
117
  - lib/ruby-next/language/rewriters/2.6/endless_range.rb
116
118
  - lib/ruby-next/language/rewriters/2.7/args_forward.rb
117
119
  - lib/ruby-next/language/rewriters/2.7/numbered_params.rb
@@ -136,11 +138,14 @@ files:
136
138
  - lib/ruby-next/language/setup.rb
137
139
  - lib/ruby-next/language/unparser.rb
138
140
  - lib/ruby-next/logging.rb
141
+ - lib/ruby-next/pry.rb
139
142
  - lib/ruby-next/rubocop.rb
140
143
  - lib/ruby-next/setup_self.rb
141
144
  - lib/ruby-next/utils.rb
142
145
  - lib/ruby-next/version.rb
143
146
  - lib/uby-next.rb
147
+ - lib/uby-next/irb.rb
148
+ - lib/uby-next/pry.rb
144
149
  homepage: http://github.com/palkan/ruby-next
145
150
  licenses:
146
151
  - MIT