ruby-next-core 0.15.2 → 1.0.0.rc.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (59) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +32 -0
  3. data/README.md +118 -48
  4. data/bin/mspec +11 -0
  5. data/lib/.rbnext/2.1/ruby-next/commands/nextify.rb +295 -0
  6. data/lib/.rbnext/2.1/ruby-next/core.rb +10 -2
  7. data/lib/.rbnext/2.1/ruby-next/language.rb +54 -10
  8. data/lib/.rbnext/2.3/ruby-next/commands/nextify.rb +82 -2
  9. data/lib/.rbnext/2.3/ruby-next/config.rb +79 -0
  10. data/lib/.rbnext/2.3/ruby-next/core/data.rb +159 -0
  11. data/lib/.rbnext/2.3/ruby-next/language/rewriters/2.7/pattern_matching.rb +44 -9
  12. data/lib/.rbnext/2.3/ruby-next/language/rewriters/base.rb +6 -32
  13. data/lib/.rbnext/2.3/ruby-next/utils.rb +3 -22
  14. data/lib/.rbnext/2.6/ruby-next/core/data.rb +159 -0
  15. data/lib/.rbnext/2.7/ruby-next/core/data.rb +159 -0
  16. data/lib/.rbnext/2.7/ruby-next/core.rb +10 -2
  17. data/lib/.rbnext/2.7/ruby-next/language/paco_parsers/string_literals.rb +109 -0
  18. data/lib/.rbnext/2.7/ruby-next/language/rewriters/2.7/pattern_matching.rb +44 -9
  19. data/lib/.rbnext/2.7/ruby-next/language/rewriters/text.rb +132 -0
  20. data/lib/.rbnext/3.2/ruby-next/commands/base.rb +55 -0
  21. data/lib/.rbnext/3.2/ruby-next/language/rewriters/2.7/pattern_matching.rb +1095 -0
  22. data/lib/.rbnext/3.2/ruby-next/rubocop.rb +210 -0
  23. data/lib/ruby-next/commands/nextify.rb +84 -2
  24. data/lib/ruby-next/config.rb +27 -0
  25. data/lib/ruby-next/core/data.rb +159 -0
  26. data/lib/ruby-next/core/matchdata/deconstruct.rb +9 -0
  27. data/lib/ruby-next/core/matchdata/deconstruct_keys.rb +20 -0
  28. data/lib/ruby-next/core/matchdata/named_captures.rb +11 -0
  29. data/lib/ruby-next/core/refinement/import.rb +44 -36
  30. data/lib/ruby-next/core/time/deconstruct_keys.rb +30 -0
  31. data/lib/ruby-next/core.rb +10 -2
  32. data/lib/ruby-next/irb.rb +2 -2
  33. data/lib/ruby-next/language/bootsnap.rb +2 -25
  34. data/lib/ruby-next/language/paco_parser.rb +7 -0
  35. data/lib/ruby-next/language/paco_parsers/base.rb +47 -0
  36. data/lib/ruby-next/language/paco_parsers/comments.rb +26 -0
  37. data/lib/ruby-next/language/paco_parsers/string_literals.rb +109 -0
  38. data/lib/ruby-next/language/parser.rb +24 -2
  39. data/lib/ruby-next/language/rewriters/2.7/pattern_matching.rb +42 -7
  40. data/lib/ruby-next/language/rewriters/3.0/args_forward_leading.rb +2 -2
  41. data/lib/ruby-next/language/rewriters/3.1/oneline_pattern_parensless.rb +1 -1
  42. data/lib/ruby-next/language/rewriters/3.2/anonymous_restargs.rb +104 -0
  43. data/lib/ruby-next/language/rewriters/abstract.rb +57 -0
  44. data/lib/ruby-next/language/rewriters/base.rb +6 -32
  45. data/lib/ruby-next/language/rewriters/edge/it_param.rb +58 -0
  46. data/lib/ruby-next/language/rewriters/edge.rb +12 -0
  47. data/lib/ruby-next/language/rewriters/proposed/bind_vars_pattern.rb +3 -0
  48. data/lib/ruby-next/language/rewriters/proposed/method_reference.rb +9 -20
  49. data/lib/ruby-next/language/rewriters/text.rb +132 -0
  50. data/lib/ruby-next/language/runtime.rb +9 -86
  51. data/lib/ruby-next/language/setup.rb +5 -2
  52. data/lib/ruby-next/language/unparser.rb +5 -0
  53. data/lib/ruby-next/language.rb +54 -10
  54. data/lib/ruby-next/pry.rb +1 -1
  55. data/lib/ruby-next/rubocop.rb +2 -0
  56. data/lib/ruby-next/utils.rb +3 -22
  57. data/lib/ruby-next/version.rb +1 -1
  58. data/lib/uby-next.rb +2 -2
  59. metadata +65 -12
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: c392536e2de07a838a8c9818b0651a9d2eb6803471f52d1e3632bada3fb398a4
4
- data.tar.gz: 564822b7a5c55ec7fc278bf223206f0b16a75a71a9277b247d862489c7a342cb
3
+ metadata.gz: 898de1113136ed6652275e5a83ee7e2866bf287ec1533dee71ffefdaf4f8fcae
4
+ data.tar.gz: 5ccd6bb1f8256c048d9bd18246b0ab372770500111ed265c775ab37498d6814c
5
5
  SHA512:
6
- metadata.gz: 33d80d8d5bfaaf75cc0cc42b79eb4758ff57c9d72f1ac21eb86770f1f039c79cebc7eabc5b4a20c42e11a9a266b8a435849b2a4ae74547acd6b9a8fb8db03f39
7
- data.tar.gz: 1a9b048897ad763e2dd726bc0be7112337499dbb1ddad9e02f13b81a2bb98d28d6f32f2e9abe1e29222a05895433117e082a5a8e42be4101d4d4eb056bcf5c61
6
+ metadata.gz: 36876dea9517b88f1390bfe72781f77b13f00c5020079476f485f90642b8d8ec66061e2f3693f1cb2c350e9ccdb3a63f1e4c965b4f7010e36cbb226e5a8e7682
7
+ data.tar.gz: 24d8e22d98e9a3a33a29237b27b02e195bea98d6dda5d1fadb3f5a0ba5a9664bf205b783299f6ed7aeda6879396129a590dbe3aaf7a9e65072b9c4ef9caddfef
data/CHANGELOG.md CHANGED
@@ -2,6 +2,36 @@
2
2
 
3
3
  ## master
4
4
 
5
+ - Add `it` block parameter support. ([@palkan][])
6
+
7
+ - Deprecate AST transpiling mode. ([@palkan][])
8
+
9
+ - Add `Data` backport. ([@palkan][], [@saturnflyer][])
10
+
11
+ - Add `Time#deconstruct_keys` support. ([@palkan][])
12
+
13
+ - Add anonymous rest and keyword rest arguments forwarding support (`def foo(*, **); bar(*, **) end`). ([@palkan][])
14
+
15
+ - Add `MatchData#{deconstruct,deconstruct_keys}` and `#named_captures(symbolize_names: true)` support. ([@palkan][])
16
+
17
+ - Add stats to `nextify`. ([@palkan][])
18
+
19
+ - Add text rewriters and `--import-rewriter` option to `nextify`. ([@palkan][])
20
+
21
+ - Use `.rbnextrc` arguments for runtime transpiling and auto-transpiling gems. ([@palkan][])
22
+
23
+ - Stop testing Ruby <2.3. ([@palkan][])
24
+
25
+ - Extract `require-hooks`. ([@palkan][])
26
+
27
+ - Support passing --overwrite to CLI. ([@prog-supdex][])
28
+
29
+ Use `nextify original_file --single-version --overwrite` to overwrite the original file without create new one.
30
+
31
+ ## 0.15.3 (2022-10-16)
32
+
33
+ - Fix handling nested const patterns. ([@palkan][])
34
+
5
35
  ## 0.15.2 (2022-08-02)
6
36
 
7
37
  - Fix loading transpiled in TruffleRuby. ([@palkan][])
@@ -360,3 +390,5 @@ p a #=> 1
360
390
  [backports]: https://github.com/marcandre/backports
361
391
  [@sl4vr]: https://github.com/sl4vr
362
392
  [@skryukov]: https://github.com/skryukov
393
+ [@prog-supdex]: https://github.com/prog-supdex
394
+ [@saturnflyer]: https://github.com/saturnflyer
data/README.md CHANGED
@@ -14,11 +14,14 @@ Who might be interested in Ruby Next?
14
14
 
15
15
  - **Ruby gems maintainers** who want to write code using the latest Ruby version but still support older ones.
16
16
  - **Application developers** who want to give new features a try without waiting for the final release (or, more often, for the first patch).
17
- - **Users of non-MRI implementations** such as [mruby][], [JRuby][], [TruffleRuby][], [Opal][], [RubyMotion][], [Artichoke][], [Prism][].
17
+ - **Users of non-MRI implementations** such as [mruby][], [JRuby][], [TruffleRuby][], [Natalie][], [Opal][], [RubyMotion][], [Artichoke][], [Prism][].
18
+ - **Ruby syntax enthusiasts** who want to experiment with [custom syntax extensions](#custom-syntax-rewriters) 👩‍🔬👨‍🔬.
18
19
 
19
20
  Ruby Next also aims to help the community to assess new, _experimental_, MRI features by making it easier to play with them.
20
21
  That's why Ruby Next implements the `master` features as fast as possible.
21
22
 
23
+ See also a companion library (extracted from Ruby Next) that provides **code loading hooks** for your needs—[require-hooks][require-hooks].
24
+
22
25
  Read more about the motivation behind the Ruby Next in this post: [Ruby Next: Make all Rubies quack alike](https://evilmartians.com/chronicles/ruby-next-make-all-rubies-quack-alike).
23
26
 
24
27
  <table style="border:none;">
@@ -60,14 +63,13 @@ _Please, submit a PR to add your project to the list!_
60
63
  ## Table of contents
61
64
 
62
65
  - [Overview](#overview)
63
- - [Quick Start](#quick-start)
66
+ - [Quick start](#quick-start)
64
67
  - [Polyfills](#using-only-polyfills)
65
68
  - [Transpiling](#transpiling)
66
69
  - [Modes](#transpiler-modes)
67
70
  - [CLI](#cli)
68
71
  - [Using in gems](#integrating-into-a-gem-development)
69
72
  - [Runtime usage](#runtime-usage)
70
- - [Bootsnap integration](#using-with-bootsnap)
71
73
  - [`ruby -ruby-next`](#uby-next)
72
74
  - [Logging & Debugging](#logging-and-debugging)
73
75
  - [RuboCop](#rubocop)
@@ -75,6 +77,7 @@ _Please, submit a PR to add your project to the list!_
75
77
  - [Using with Pry](#pry)
76
78
  - [Using with EOL Rubies](#using-with-eol-rubies)
77
79
  - [Proposed & edge features](#proposed-and-edge-features)
80
+ - [Custom syntax rewriters](#custom-syntax-rewriters)
78
81
  - [Known limitations](#known-limitations)
79
82
 
80
83
  ## Overview
@@ -86,9 +89,9 @@ Core provides **polyfills** for Ruby core classes APIs via Refinements (default
86
89
  Language is responsible for **transpiling** edge Ruby syntax into older versions. It could be done
87
90
  programmatically or via CLI. It also could be done in runtime.
88
91
 
89
- Currently, Ruby Next supports Ruby versions 2.2+, including JRuby 9.2.8+ and TruffleRuby 20.1+ (with some limitations). Support for EOL versions (<2.5) slightly differs though ([see below](#using-with-eol-rubies)).
92
+ Currently, Ruby Next supports Ruby versions 2.3+, including JRuby 9.2.8+ and TruffleRuby 20.1+ (with some limitations). Support for older versions (<2.5) slightly differs though ([see below](#using-with-eol-rubies)). Versions between 2.0 and 2.3 may work but we no longer test against them.
90
93
 
91
- Please, [open an issue](https://github.com/ruby-next/ruby-next/issues/new/choose) or join the discussion in the existing ones if you would like us to support older Ruby versions.
94
+ Please [open an issue](https://github.com/ruby-next/ruby-next/issues/new/choose) or join the discussion in the existing issues if you would like us to support older Ruby versions.
92
95
 
93
96
  ## Quick start
94
97
 
@@ -100,15 +103,16 @@ $ gem install ruby-next
100
103
 
101
104
  # Call ruby with -ruby-next flag
102
105
  $ ruby -ruby-next -e "
103
- def greet(val) =
104
- case val
106
+ greet = proc do
107
+ case it
105
108
  in hello: hello if hello =~ /human/i
106
109
  '🙂'
107
110
  in hello: 'martian'
108
111
  '👽'
109
112
  end
113
+ end
110
114
 
111
- puts greet(hello: 'martian')
115
+ puts greet.call(hello: 'martian')
112
116
  "
113
117
 
114
118
  => 👽
@@ -126,9 +130,9 @@ gem "ruby-next-core"
126
130
  spec.add_dependency "ruby-next-core"
127
131
  ```
128
132
 
129
- **NOTE:** we use the different _distribution_ gem, `ruby-next-core`, to provide zero-dependency, polyfills-only version.
133
+ **NOTE:** we use a different gem for _distribution_, `ruby-next-core`, to provide a zero-dependency, polyfills-only version.
130
134
 
131
- Then, all you need is to load the Ruby Next:
135
+ Then, all you need is to load Ruby Next:
132
136
 
133
137
  ```ruby
134
138
  require "ruby-next"
@@ -140,9 +144,9 @@ And activate the refinement in every file where you want to use it\*:
140
144
  using RubyNext
141
145
  ```
142
146
 
143
- Ruby Next only refines core classes if necessary; thus, this line wouldn't have any effect in the edge Ruby.
147
+ Ruby Next only refines core classes if necessary; thus, this line wouldn't have any effect in edge Ruby.
144
148
 
145
- **NOTE:** Even if the runtime already contains a monkey-patch with the backported functionality, we consider the method as _dirty_ and activate the refinement for it. Thus, you always have predictable behaviour. That's why we recommend using refinements for gems development.
149
+ **NOTE:** Even if the runtime already contains a monkey-patch with the backported functionality, we consider the method as _dirty_ and activate the refinement for it. Thus, you always have predictable behaviour. That's why we recommend using refinements for gem development.
146
150
 
147
151
  Alternatively, you can go with monkey-patches. Just add this line:
148
152
 
@@ -152,7 +156,7 @@ require "ruby-next/core_ext"
152
156
 
153
157
  The following _rule of thumb_ is recommended when choosing between refinements and monkey-patches:
154
158
 
155
- - Use refinements for libraries development (to avoid conflicts with others code)
159
+ - Use refinements for library development (to avoid conflicts with others' code)
156
160
  - Using core extensions could be considered for application development (no need to think about `using RubyNext`); this approach could potentially lead to conflicts with dependencies (if these dependencies are not using refinements 🙂)
157
161
  - Use core extensions if refinements are not supported by your platform
158
162
 
@@ -160,6 +164,12 @@ The following _rule of thumb_ is recommended when choosing between refinements a
160
164
 
161
165
  [**The list of supported APIs.**][features_core]
162
166
 
167
+ ### Data backport
168
+
169
+ Ruby 3.2 has introduced a new core class—[Data](https://bugs.ruby-lang.org/issues/16122). Ruby Next provides a backport functionality which is automatically activated when you `require "ruby-next"` **if and only if the constant is undefined**. If you want to use a custom backport, make sure you loaded it first.
170
+
171
+ If you want to opt-out from loading Data backport, you must set the `RUBY_NEXT_DISABLE_DATA` env variable to `true`.
172
+
163
173
  ## Transpiling
164
174
 
165
175
  Ruby Next allows you to transpile\* edge Ruby syntax to older versions.
@@ -187,21 +197,11 @@ gem install ruby-next
187
197
 
188
198
  ### Transpiler modes
189
199
 
190
- Ruby Next currently provides two different modes of generating transpiled code: _AST_ and _rewrite_.
191
-
192
- In the AST mode, we parse the source code into AST, modifies this AST and **generate a new code from AST** (using [unparser][unparser]). Thus, the transpiled code being identical in terms of functionality has different formatting.
193
-
194
- In the rewrite mode, we apply changes to the source code itself, thus, keeping the original formatting of the unaffected code (in a similar way to RuboCop's autocorrect feature).
200
+ Since v1.0, Ruby Next only support the _rewrite_ mode, i.e., the code transformations are applied directly to the original source code. This allows us to keep formatting as close as possible to the original code.
195
201
 
196
202
  The main benefit of the rewrite mode is that it preserves the original code line numbers and layout, which is especially useful in debugging.
197
203
 
198
- By default, we use the rewrite mode. If you found a bug with rewrite mode which is not reproducible in the AST mode, please, let us know.
199
-
200
- You can change the transpiler mode:
201
-
202
- - From code by setting `RubyNext::Language.mode = :ast` or `RubyNext::Language.mode = :rewrite`.
203
- - Via environmental variable `RUBY_NEXT_TRANSPILE_MODE=ast`.
204
- - Via CLI option ([see below](#cli)).
204
+ The legacy AST mode (regenerating source code from the modified abstract syntax tree) is deprecated (though still supported).
205
205
 
206
206
  ## CLI
207
207
 
@@ -219,12 +219,14 @@ Usage: ruby-next nextify DIRECTORY_OR_FILE [options]
219
219
  -o, --output=OUTPUT Specify output directory or file or stdout
220
220
  --min-version=VERSION Specify the minimum Ruby version to support
221
221
  --single-version Only create one version of a file (for the earliest Ruby version)
222
+ --overwrite Overwrites the original file with one version of --single-version (works only with --single-version or --rewrite)
222
223
  --edge Enable edge (master) Ruby features
223
224
  --proposed Enable proposed/experimental Ruby features
224
225
  --transpile-mode=MODE Transpiler mode (ast or rewrite). Default: ast
225
226
  --[no-]refine Do not inject `using RubyNext`
226
227
  --list-rewriters List available rewriters
227
228
  --rewrite=REWRITERS... Specify particular Ruby features to rewrite
229
+ --import-rewriter=PATHS... Specify the paths to custom rewriters to load
228
230
  -h, --help Print help
229
231
  -V Turn on verbose mode
230
232
  --dry-run Print verbose output without generating files
@@ -302,6 +304,8 @@ nextify: |
302
304
  --edge
303
305
  ```
304
306
 
307
+ **NOTE:** The `nextify` section is also used by auto-transpiling when installing the gem from source and by runtime transpiling.
308
+
305
309
  ## Integrating into a gem development
306
310
 
307
311
  We recommend _pre-transpiling_ source code to work with older versions before releasing it.
@@ -361,19 +365,21 @@ This feature, _auto-transpiling_, is **disabled** by default (will likely be ena
361
365
 
362
366
  It is also possible to transpile Ruby source code in run-time via Ruby Next.
363
367
 
364
- All you need is to `require "ruby-next/language/runtime"` as early as possible to hijack `Kernel#require` and friends.
365
- You can also automatically inject `using RubyNext` to every\* loaded file by also adding `require "ruby-next/core/runtime"`.
368
+ All you need is to `require "ruby-next/language/runtime"` to hijack `Kernel#require` and friends before loading the files you want to transpile. You can also automatically inject `using RubyNext` to every\* loaded file by also adding `require "ruby-next/core/runtime"`.
366
369
 
367
- Since the runtime mode requires Kernel monkey-patching, it should be used carefully. For example, we use it in Ruby Next tests—works perfectly. But think twice before enabling it in production.
370
+ Runtime mode is backed by [require-hooks][require-hooks]—a standalone gem which has been extracted from Ruby Next. Depending on the current runtime, it picks an optimal strategy for hijacking the loading mechanism. Please, refer to its documentation for more details.
368
371
 
369
- Consider using [Bootsnap](#using-with-bootsnap) integration, 'cause its monkey-patching has been bullet-proofed 😉.
372
+ \* Ruby Next doesn't hijack every required file but only the configured directories: `./app/`, `./lib/`, `./spec/`, `./test/` (relative to the `pwd`). It also excludes the `./vendor/bundle` directory by default.
370
373
 
371
- \* Ruby Next doesn't hijack every required file but _watches_ only the configured directories: `./app/`, `./lib/`, `./spec/`, `./test/` (relative to the `pwd`). You can configure the watch dirs:
374
+ You can customize target files via the `include_patterns` and `exclude_patterns` configuration options:
372
375
 
373
376
  ```ruby
374
- RubyNext::Language.watch_dirs << "path/to/other/dir"
377
+ RubyNext::Language.include_patterns << "path/to/other/dir/*.rb"
378
+ RubyNext::Language.exclude_patterns << "path/to/other/dir/subdir/*"
375
379
  ```
376
380
 
381
+ **NOTE:** Directories MUST be configured before requiring `ruby-next/language/runtime`.
382
+
377
383
  ### Eval & similar
378
384
 
379
385
  By default, we do not hijack `Kernel.eval` and similar methods due to some limitations (e.g., there is no easy and efficient way to access the caller's scope, or _binding_, and some evaluations relies on local variables).
@@ -384,20 +390,6 @@ If you want to support transpiling in `eval`-like methods, opt-in explicitly by
384
390
  using RubyNext::Language::Eval
385
391
  ```
386
392
 
387
- ## Using with Bootsnap
388
-
389
- [Bootsnap][] is a great tool to speed-up your application load and it's included into the default Rails Gemfile. It patches Ruby mechanism of loading source files to make it possible to cache the intermediate representation (_iseq_).
390
-
391
- Ruby Next provides a specific integration which allows to add a transpiling step to this process, thus making the transpiler overhead as small as possible, because the cached and **already transpiled** version is used if no changes were made.
392
-
393
- To enable this integration, add the following line after the `require "bootsnap/setup"`:
394
-
395
- ```ruby
396
- require "ruby-next/language/bootsnap"
397
- ```
398
-
399
- **NOTE:** There is no way to invalidate the cache when you upgrade Ruby Next (e.g., due to the bug fixes), so you should do this manually.
400
-
401
393
  ## `uby-next`
402
394
 
403
395
  _This is [not a typo](https://github.com/ruby-next/ruby-next/pull/8), that’s the way `ruby -ruby-next` works: it’s equal to `ruby -r uby-next`, and [`uby-next.rb`](https://github.com/ruby-next/ruby-next/blob/master/lib/uby-next.rb) is a special file that activates the runtime mode._
@@ -490,7 +482,7 @@ pry -ruby-next/pry
490
482
 
491
483
  ## Using with EOL Rubies
492
484
 
493
- We currently provide support for Ruby 2.2, 2.3 and 2.4.
485
+ We currently provide support for Ruby 2.3+.
494
486
 
495
487
  **NOTE:** By "support" here we mean using `ruby-next` CLI and runtime transpiling. Transpiled code may run on Ruby 2.0+.
496
488
 
@@ -557,13 +549,85 @@ require "ruby-next/language/runtime"
557
549
 
558
550
  ### Supported edge features
559
551
 
560
- It's too early, Ruby 3.1 has just been released. See its features in the [supported features list](./SUPPORTED_FEATURES.md).
552
+ - Implicit `it` block parameter ([#19890](https://bugs.ruby-lang.org/issues/18980)).
561
553
 
562
554
  ### Supported proposed features
563
555
 
564
556
  - _Method reference_ operator (`.:`) ([#13581](https://bugs.ruby-lang.org/issues/13581)).
565
557
  - Binding non-local variables in pattern matching (`42 => @v`) ([#18408](https://bugs.ruby-lang.org/issues/18408)).
566
558
 
559
+ ## Custom syntax rewriters
560
+
561
+ Wonder what would happen if Ruby get a null coalescing operator (`??=`) or some other syntactic feature you want to try out? Ruby Next is here to help you!
562
+
563
+ Ruby Next allows you to write your own syntax rewriters. Full-featured rewriters (used by Ruby Next itself) operate on AST and usually require parser modifications. However, we also support text-based rewriters which can be used to experiment with new syntax much quicker without dealing with grammars, parsers and syntax trees.
564
+
565
+ To implement a text-based rewriter, you need to create a new class inherited from `RubyNext::Language::Rewriters::Text` and implementing either `#rewrite` or `#safe_rewrite` method. For example, the method reference operator (`.:`) could be implemented as follows:
566
+
567
+ ```ruby
568
+ class MethodReferenceRewriter < RubyNext::Language::Rewriters::Text
569
+ # Rewriter configuration includes its name, a syntax probe and a minimum supported Ruby version.
570
+ # The latter two are used to determine whether the rewriter should be activated for the current file in runtime or when running `ruby-next nextify`.
571
+ NAME = "method-reference"
572
+ SYNTAX_PROBE = "Language.:transform"
573
+ MIN_SUPPORTED_VERSION = Gem::Version.new(RubyNext::NEXT_VERSION)
574
+
575
+ def safe_rewrite(source)
576
+ source.gsub(/\.:([\w_]+)/) do |match|
577
+ context.track! self
578
+
579
+ ".method(:#{$1})"
580
+ end
581
+ end
582
+ end
583
+
584
+ # Add the rewriter to the list of rewriters
585
+ RubyNext::Language.rewriters << MethodReferenceRewriter
586
+ ```
587
+
588
+ The `context` object is responsible for tracking if the rewriter was used for the current file. You must call the `context.track!` method to mark the file as _dirty_ (i.e., it should be transpiled). The input parameter (`source`) is the Ruby source code of the file being transpiled and the output must be the transpiled source code.
589
+
590
+ The `#safe_rewrite` method operates on the normalized source code (i.e., without comments and string literals). It's useful when you want to avoid transpiling inside strings or comments. If you want to transpile the original contents, you can use the `#rewrite` method instead.
591
+
592
+ Under the hood, `#safe_rewrite` uses [Paco][] to parse the source and separate string literals from the rest of the code. You can also leverage [Paco][] in your text rewriters, if you want more control on the parsing process. For better experience, we provide a DSL to define a custom parser and the `#parse` method to use it. Here is an example of implementing the `.:` operator using a Paco parser:
593
+
594
+ ```ruby
595
+ class MethodReferenceRewriter < RubyNext::Language::Rewriters::Text
596
+ NAME = "method-reference"
597
+ SYNTAX_PROBE = "Language.:transform"
598
+
599
+ parser do
600
+ def default
601
+ many(
602
+ alt(
603
+ method_ref,
604
+ any_char
605
+ )
606
+ )
607
+ end
608
+
609
+ def method_ref
610
+ seq(
611
+ string(".:").result(""),
612
+ method_name
613
+ # IMPORTANT: Use `#track!` method to mark the file as dirty
614
+ ).fmap { track! }.fmap { ".method(:#{_1})" }
615
+ end
616
+
617
+ def method_name = regexp(/[\w_]+/)
618
+ end
619
+
620
+ def safe_rewrite(source)
621
+ parse(source).join
622
+ end
623
+ end
624
+
625
+ # Add the rewriter to the list of rewriters
626
+ RubyNext::Language.rewriters << MethodReferenceRewriter
627
+ ```
628
+
629
+ When using the `ruby-next nextify` command, you can load custom rewriters via the `--import-rewriter` option.
630
+
567
631
  ## Known limitations
568
632
 
569
633
  Ruby Next aims to be _reasonably compatible_ with MRI. That means, some edge cases could be uncovered. Below is the list of known limitations.
@@ -596,6 +660,10 @@ Bug reports and pull requests are welcome on GitHub at [https://github.com/ruby-
596
660
 
597
661
  See also the [development guide](./DEVELOPMENT.md).
598
662
 
663
+ ## Acknowledgments
664
+
665
+ - Thanks to [Jim Gay](https://github.com/saturnflyer) for the original Data polyfill implementation ([polyfill-data](https://github.com/saturnflyer/polyfill-data))
666
+
599
667
  ## License
600
668
 
601
669
  The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
@@ -613,6 +681,8 @@ The gem is available as open source under the terms of the [MIT License](https:/
613
681
  [parser]: https://github.com/whitequark/parser
614
682
  [unparser]: https://github.com/mbj/unparser
615
683
  [next_parser]: https://github.com/ruby-next/parser
616
- [Bootsnap]: https://github.com/Shopify/bootsnap
617
684
  [rubocop]: https://github.com/rubocop-hq/rubocop
618
685
  [backports]: https://github.com/marcandre/backports
686
+ [require-hooks]: https://github.com/ruby-next/require-hooks
687
+ [Natalie]: https://natalie-lang.org
688
+ [Paco]: https://github.com/ruby-next/paco
data/bin/mspec ADDED
@@ -0,0 +1,11 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ unless File.directory?('mspec/lib')
4
+ $stdout.puts "Cloning mspec..."
5
+ system("git clone https://github.com/ruby/mspec.git mspec")
6
+ end
7
+
8
+ $:.unshift File.expand_path(File.join(__dir__, '..', 'mspec', 'lib'))
9
+
10
+ require 'mspec/commands/mspec'
11
+ MSpecMain.main(false)