ruby-next-core 0.9.1 → 0.9.2

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: 5689b631e2b8dd6e9222a60b25434fcb323d008458342c395ff891015d9f651d
4
- data.tar.gz: 6178612f34fbbe59ea3f3042fc62ea23cc3bede62a944e187791a7304164d819
3
+ metadata.gz: 31bef7eea12c9a2c8b04f3daa62816e96b764bcbaf7cfc0f01d4bf1b040f34b9
4
+ data.tar.gz: 793834fef3c0a00944b7026a975f7ee4b97219ae42b2b3ebfb5327d4a3e5311c
5
5
  SHA512:
6
- metadata.gz: f24eef8dc28b0ad4e8f9d5073d1254f31111354291b9bfb60cb8c688fbf312f153ba8fe17886183798b9413280bac554f9012e2ec2b63d250f0bf3c34a3362eb
7
- data.tar.gz: d9ab4a36680864b81cd545d438e001a51e4c6dfbca56336a971b557af50786b70c7b7f9eee37698fc0373480e56f51c153080b9e06ec218dc8d08e0da2dc79d6
6
+ metadata.gz: 690f42ac5954bbe4b2c6a0e4ee0a05f4842c87a4ade5bccb3f7be90d76b92df89e715a07c785f5286bb4137ac3357fe4341e90b41f010db8716fab5a9bdcfb83
7
+ data.tar.gz: 48bd02bbd66656cd9892cad1c8fe767c205bfba5eb47c59cfacdf3060af3c7cf886fbba6396283f3e19eb1da66cd53e9b6a5125595ec694cd8c37ad6ecb4e0f1
@@ -2,6 +2,13 @@
2
2
 
3
3
  ## master
4
4
 
5
+ ## 0.9.2 (2020-06-24)
6
+
7
+ - Support passing rewriters to CLI. ([@sl4vr][])
8
+
9
+ Use `nextify --list-rewriters` to view all available rewriters.
10
+ Use `nextify` with `--rewrite=REWRITERS...` option to specify which particular rewriters to use.
11
+
5
12
  ## 0.9.1 (2020-06-05)
6
13
 
7
14
  - Keep `ruby-next` version in sync with `ruby-next-core`. ([@palkan][])
@@ -195,3 +202,4 @@ p a #=> 1
195
202
 
196
203
  [@palkan]: https://github.com/palkan
197
204
  [backports]: https://github.com/marcandre/backports
205
+ [@sl4vr]: https://github.com/sl4vr
data/README.md CHANGED
@@ -1,3 +1,4 @@
1
+ [![Cult Of Martians](http://cultofmartians.com/assets/badges/badge.svg)](https://cultofmartians.com/tasks/ruby-next-cli-rewriters.html#task)
1
2
  [![Gem Version](https://badge.fury.io/rb/ruby-next.svg)](https://rubygems.org/gems/ruby-next) [![Build](https://github.com/ruby-next/ruby-next/workflows/Build/badge.svg)](https://github.com/ruby-next/ruby-next/actions)
2
3
  [![JRuby Build](https://github.com/ruby-next/ruby-next/workflows/JRuby%20Build/badge.svg)](https://github.com/ruby-next/ruby-next/actions?query=workflow%3A%22TruffleRuby+Build%22)
3
4
  [![TruffleRuby Build](https://github.com/ruby-next/ruby-next/workflows/TruffleRuby%20Build/badge.svg)](https://github.com/ruby-next/ruby-next/actions?query=workflow%3A%22TruffleRuby+Build%22)
@@ -200,12 +201,15 @@ It has the following interface:
200
201
  ```sh
201
202
  $ ruby-next nextify
202
203
  Usage: ruby-next nextify DIRECTORY_OR_FILE [options]
203
- -o, --output=OUTPUT Specify output directory or file or stdout (use -o stdout for that)
204
+ -o, --output=OUTPUT Specify output directory or file or stdout
204
205
  --min-version=VERSION Specify the minimum Ruby version to support
205
206
  --single-version Only create one version of a file (for the earliest Ruby version)
206
- --enable-method-reference Enable reverted method reference syntax (requires custom parser)
207
+ --edge Enable edge (master) Ruby features
208
+ --proposed Enable proposed/experimental Ruby features
207
209
  --transpile-mode=MODE Transpiler mode (ast or rewrite). Default: ast
208
210
  --[no-]refine Do not inject `using RubyNext`
211
+ --list-rewriters List available rewriters
212
+ --rewrite=REWRITERS... Specify particular Ruby features to rewrite
209
213
  -h, --help Print help
210
214
  -V Turn on verbose mode
211
215
  --dry-run Print verbose output without generating files
@@ -10,7 +10,7 @@ module RubyNext
10
10
  class Nextify < Base
11
11
  using RubyNext
12
12
 
13
- attr_reader :lib_path, :paths, :out_path, :min_version, :single_version
13
+ attr_reader :lib_path, :paths, :out_path, :min_version, :single_version, :specified_rewriters
14
14
 
15
15
  def run
16
16
  log "RubyNext core strategy: #{RubyNext::Core.strategy}"
@@ -18,6 +18,8 @@ module RubyNext
18
18
 
19
19
  remove_rbnext!
20
20
 
21
+ @min_version ||= MIN_SUPPORTED_VERSION
22
+
21
23
  paths.each do |path|
22
24
  contents = File.read(path)
23
25
  transpile path, contents
@@ -26,7 +28,8 @@ module RubyNext
26
28
 
27
29
  def parse!(args)
28
30
  print_help = false
29
- @min_version = MIN_SUPPORTED_VERSION
31
+ print_rewriters = false
32
+ rewriter_names = []
30
33
  @single_version = false
31
34
 
32
35
  optparser = base_parser do |opts|
@@ -63,6 +66,14 @@ module RubyNext
63
66
  Core.strategy = :core_ext unless val
64
67
  end
65
68
 
69
+ opts.on("--list-rewriters", "List available rewriters") do |val|
70
+ print_rewriters = true
71
+ end
72
+
73
+ opts.on("--rewrite=REWRITERS...", "Specify particular Ruby features to rewrite") do |val|
74
+ rewriter_names << val
75
+ end
76
+
66
77
  opts.on("-h", "--help", "Print help") do
67
78
  print_help = true
68
79
  end
@@ -77,12 +88,35 @@ module RubyNext
77
88
  exit 0
78
89
  end
79
90
 
91
+ if print_rewriters
92
+ Language.rewriters.each do |rewriter|
93
+ $stdout.puts "#{rewriter::NAME} (\"#{rewriter::SYNTAX_PROBE}\")"
94
+ end
95
+ exit 0
96
+ end
97
+
80
98
  unless lib_path&.then(&File.method(:exist?))
81
99
  $stdout.puts "Path not found: #{lib_path}"
82
100
  $stdout.puts optparser.help
83
101
  exit 2
84
102
  end
85
103
 
104
+ if rewriter_names.any? && min_version
105
+ $stdout.puts "--rewrite cannot be used with --min-version simultaneously"
106
+ exit 2
107
+ end
108
+
109
+ @specified_rewriters =
110
+ if rewriter_names.any?
111
+ begin
112
+ Language.select_rewriters(*rewriter_names)
113
+ rescue Language::RewriterNotFoundError => error
114
+ $stdout.puts error.message
115
+ $stdout.puts "Try --list-rewriters to see list of available rewriters"
116
+ exit 2
117
+ end
118
+ end
119
+
86
120
  @paths =
87
121
  if File.directory?(lib_path)
88
122
  Dir[File.join(lib_path, "**/*.rb")]
@@ -96,16 +130,11 @@ module RubyNext
96
130
  private
97
131
 
98
132
  def transpile(path, contents, version: min_version)
99
- rewriters = Language.rewriters.select { |rw| rw.unsupported_version?(version) }
133
+ rewriters = specified_rewriters || Language.rewriters.select { |rw| rw.unsupported_version?(version) }
100
134
 
101
135
  context = Language::TransformContext.new
102
136
 
103
- new_contents =
104
- if Gem::Version.new(version) >= Gem::Version.new("2.7.0") && !defined?(Unparser::Emitter::CaseMatch)
105
- Language.rewrite contents, context: context, rewriters: rewriters
106
- else
107
- Language.transform contents, context: context, rewriters: rewriters
108
- end
137
+ new_contents = Language.transform contents, context: context, rewriters: rewriters
109
138
 
110
139
  return unless context.dirty?
111
140
 
@@ -161,7 +190,9 @@ module RubyNext
161
190
  out_path == "stdout"
162
191
  end
163
192
 
164
- alias single_version? single_version
193
+ def single_version?
194
+ single_version || specified_rewriters
195
+ end
165
196
  end
166
197
  end
167
198
  end
@@ -23,6 +23,8 @@ module RubyNext
23
23
  require "ruby-next/language/parser"
24
24
  require "ruby-next/language/unparser"
25
25
 
26
+ RewriterNotFoundError = Class.new(StandardError)
27
+
26
28
  class TransformContext
27
29
  attr_reader :versions, :use_ruby_next
28
30
 
@@ -85,12 +87,6 @@ module RubyNext
85
87
  def runtime!
86
88
  require "ruby-next/language/rewriters/runtime"
87
89
 
88
- if Gem::Version.new(RUBY_VERSION) >= Gem::Version.new("2.7.0") && !defined?(Unparser::Emitter::CaseMatch)
89
- RubyNext.warn "Ruby Next fallbacks to \"rewrite\" transpiling mode since Unparser doesn't support 2.7 AST yet.\n" \
90
- "See https://github.com/mbj/unparser/pull/142"
91
- self.mode = :rewrite
92
- end
93
-
94
90
  @runtime = true
95
91
  end
96
92
 
@@ -104,6 +100,13 @@ module RubyNext
104
100
  else
105
101
  regenerate(*args, **kwargs)
106
102
  end
103
+ rescue Unparser::UnknownEmitterError
104
+ if Gem::Version.new(RUBY_VERSION) >= Gem::Version.new("2.7.0")
105
+ RubyNext.warn "Ruby Next fallbacks to \"rewrite\" transpiling mode since Unparser doesn't support 2.7+ AST yet.\n" \
106
+ "See https://github.com/mbj/unparser/pull/142"
107
+ self.mode = :rewrite
108
+ end
109
+ rewrite(*args, **kwargs)
107
110
  end
108
111
 
109
112
  def regenerate(source, rewriters: self.rewriters, using: RubyNext::Core.refine?, context: TransformContext.new)
@@ -149,6 +152,16 @@ module RubyNext
149
152
  @current_rewriters ||= rewriters.select(&:unsupported_syntax?)
150
153
  end
151
154
 
155
+ # This method guarantees that rewriters will be returned in order they defined in Language module
156
+ def select_rewriters(*names)
157
+ rewriters_delta = names - rewriters.map { |rewriter| rewriter::NAME }
158
+ if rewriters_delta.any?
159
+ raise RewriterNotFoundError, "Rewriters not found: #{rewriters_delta.join(",")}"
160
+ end
161
+
162
+ rewriters.select { |rewriter| names.include?(rewriter::NAME) }
163
+ end
164
+
152
165
  private
153
166
 
154
167
  attr_writer :watch_dirs
@@ -6,6 +6,9 @@ module RubyNext
6
6
  module Language
7
7
  class Builder < ::Parser::Builders::Default
8
8
  modernize
9
+
10
+ # FIXME: add support for 2.8 forward args
11
+ self.emit_forward_arg = false
9
12
  end
10
13
 
11
14
  class << self
@@ -4,6 +4,7 @@ module RubyNext
4
4
  module Language
5
5
  module Rewriters
6
6
  class ArgsForward < Base
7
+ NAME = "args-forward"
7
8
  SYNTAX_PROBE = "obj = Object.new; def obj.foo(...) super(...); end"
8
9
  MIN_SUPPORTED_VERSION = Gem::Version.new("2.7.0")
9
10
 
@@ -4,6 +4,7 @@ module RubyNext
4
4
  module Language
5
5
  module Rewriters
6
6
  class EndlessMethod < Base
7
+ NAME = "endless-method"
7
8
  SYNTAX_PROBE = "obj = Object.new; def obj.foo() = 42"
8
9
  MIN_SUPPORTED_VERSION = Gem::Version.new("2.8.0")
9
10
 
@@ -4,6 +4,7 @@ module RubyNext
4
4
  module Language
5
5
  module Rewriters
6
6
  class EndlessRange < Base
7
+ NAME = "endless-range"
7
8
  SYNTAX_PROBE = "[0, 1][1..]"
8
9
  MIN_SUPPORTED_VERSION = Gem::Version.new("2.6.0")
9
10
 
@@ -4,6 +4,7 @@ module RubyNext
4
4
  module Language
5
5
  module Rewriters
6
6
  class MethodReference < Base
7
+ NAME = "method-reference"
7
8
  SYNTAX_PROBE = "Language.:transform"
8
9
  MIN_SUPPORTED_VERSION = Gem::Version.new("2.7.0")
9
10
 
@@ -6,6 +6,7 @@ module RubyNext
6
6
  class NumberedParams < Base
7
7
  using RubyNext
8
8
 
9
+ NAME = "numbered-params"
9
10
  SYNTAX_PROBE = "proc { _1 }.call(1)"
10
11
  MIN_SUPPORTED_VERSION = Gem::Version.new("2.7.0")
11
12
 
@@ -216,6 +216,7 @@ module RubyNext
216
216
  end
217
217
 
218
218
  class PatternMatching < Base
219
+ NAME = "pattern-matching"
219
220
  SYNTAX_PROBE = "case 0; in 0; true; else; 1; end"
220
221
  MIN_SUPPORTED_VERSION = Gem::Version.new("2.7.0")
221
222
 
@@ -4,6 +4,7 @@ module RubyNext
4
4
  module Language
5
5
  module Rewriters
6
6
  class RightHandAssignment < Base
7
+ NAME = "right-hand-assignment"
7
8
  SYNTAX_PROBE = "1 + 2 => a"
8
9
  MIN_SUPPORTED_VERSION = Gem::Version.new("2.8.0")
9
10
 
@@ -6,3 +6,17 @@ require "parser/current"
6
6
  $VERBOSE = save_verbose
7
7
 
8
8
  require "unparser"
9
+
10
+ unless defined?(Unparser::UnknownEmitterError)
11
+ module Unparser
12
+ class UnknownEmitterError < ArgumentError
13
+ end
14
+
15
+ Emitter.singleton_class.prepend(Module.new do
16
+ def emitter(node, parent)
17
+ raise UnknownEmitterError, "No emitter for node: #{node.type.inspect}" unless Emitter::REGISTRY.key?(node.type)
18
+ super
19
+ end
20
+ end)
21
+ end
22
+ end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module RubyNext
4
- VERSION = "0.9.1"
4
+ VERSION = "0.9.2"
5
5
  end
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.9.1
4
+ version: 0.9.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Vladimir Dementyev
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2020-06-05 00:00:00.000000000 Z
11
+ date: 2020-06-23 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: ruby-next-parser
@@ -16,14 +16,14 @@ dependencies:
16
16
  requirements:
17
17
  - - ">="
18
18
  - !ruby/object:Gem::Version
19
- version: 2.8.0.6
19
+ version: 2.8.0.7
20
20
  type: :development
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
24
  - - ">="
25
25
  - !ruby/object:Gem::Version
26
- version: 2.8.0.6
26
+ version: 2.8.0.7
27
27
  - !ruby/object:Gem::Dependency
28
28
  name: unparser
29
29
  requirement: !ruby/object:Gem::Requirement