ruby-next-core 0.10.1 → 0.11.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (41) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +31 -1
  3. data/README.md +12 -14
  4. data/bin/reparse +19 -0
  5. data/bin/transform +22 -8
  6. data/lib/.rbnext/2.3/ruby-next/commands/nextify.rb +5 -2
  7. data/lib/.rbnext/2.3/ruby-next/language/eval.rb +4 -4
  8. data/lib/.rbnext/2.3/ruby-next/language/rewriters/base.rb +25 -10
  9. data/lib/.rbnext/2.3/ruby-next/language/rewriters/endless_range.rb +1 -1
  10. data/lib/.rbnext/2.3/ruby-next/language/rewriters/pattern_matching.rb +133 -103
  11. data/lib/.rbnext/2.3/ruby-next/utils.rb +1 -1
  12. data/lib/.rbnext/2.7/ruby-next/core.rb +203 -0
  13. data/lib/ruby-next.rb +5 -38
  14. data/lib/ruby-next/commands/nextify.rb +4 -1
  15. data/lib/ruby-next/config.rb +45 -0
  16. data/lib/ruby-next/core.rb +5 -4
  17. data/lib/ruby-next/core/array/deconstruct.rb +1 -1
  18. data/lib/ruby-next/core/constants/no_matching_pattern_error.rb +1 -1
  19. data/lib/ruby-next/core/hash/deconstruct_keys.rb +1 -1
  20. data/lib/ruby-next/core/struct/deconstruct_keys.rb +3 -3
  21. data/lib/ruby-next/core_ext.rb +2 -0
  22. data/lib/ruby-next/language.rb +16 -5
  23. data/lib/ruby-next/language/bootsnap.rb +1 -1
  24. data/lib/ruby-next/language/edge.rb +0 -6
  25. data/lib/ruby-next/language/eval.rb +3 -3
  26. data/lib/ruby-next/language/parser.rb +19 -0
  27. data/lib/ruby-next/language/rewriters/args_forward.rb +8 -4
  28. data/lib/ruby-next/language/rewriters/args_forward_leading.rb +75 -0
  29. data/lib/ruby-next/language/rewriters/base.rb +21 -6
  30. data/lib/ruby-next/language/rewriters/in_pattern.rb +56 -0
  31. data/lib/ruby-next/language/rewriters/numbered_params.rb +6 -2
  32. data/lib/ruby-next/language/rewriters/pattern_matching.rb +131 -101
  33. data/lib/ruby-next/language/rewriters/safe_navigation.rb +30 -26
  34. data/lib/ruby-next/language/setup.rb +6 -3
  35. data/lib/ruby-next/language/unparser.rb +10 -0
  36. data/lib/ruby-next/rubocop.rb +79 -12
  37. data/lib/ruby-next/setup_self.rb +2 -2
  38. data/lib/ruby-next/version.rb +1 -1
  39. metadata +15 -6
  40. data/lib/.rbnext/2.3/ruby-next/language/rewriters/right_hand_assignment.rb +0 -107
  41. data/lib/ruby-next/language/rewriters/right_hand_assignment.rb +0 -107
@@ -15,13 +15,14 @@ module RubyNext
15
15
 
16
16
  receiver, *args = *node
17
17
 
18
- new_node = node.updated(
19
- :and,
20
- [
21
- process(safe_navigation(receiver)),
22
- s(:send, decsendize(receiver), *args)
23
- ]
24
- )
18
+ new_node = s(:begin,
19
+ node.updated(
20
+ :and,
21
+ [
22
+ process(safe_navigation(receiver)),
23
+ s(:send, decsendize(receiver), *args)
24
+ ]
25
+ ))
25
26
 
26
27
  replace(node.loc.expression, new_node)
27
28
 
@@ -33,13 +34,14 @@ module RubyNext
33
34
 
34
35
  context.track!(self)
35
36
 
36
- new_node = super(node.updated(
37
- :and,
38
- [
39
- process(safe_navigation(node.children[0].children[0])),
40
- process(node.updated(nil, node.children.map(&method(:decsendize))))
41
- ]
42
- ))
37
+ new_node = s(:begin,
38
+ super(node.updated(
39
+ :and,
40
+ [
41
+ process(safe_navigation(node.children[0].children[0])),
42
+ process(node.updated(nil, node.children.map(&method(:decsendize))))
43
+ ]
44
+ )))
43
45
 
44
46
  replace(node.loc.expression, new_node)
45
47
 
@@ -51,13 +53,14 @@ module RubyNext
51
53
 
52
54
  context.track!(self)
53
55
 
54
- new_node = super(node.updated(
55
- :and,
56
- [
57
- process(safe_navigation(node.children[0].children[0])),
58
- process(node.updated(nil, node.children.map(&method(:decsendize))))
59
- ]
60
- ))
56
+ new_node = s(:begin,
57
+ super(node.updated(
58
+ :and,
59
+ [
60
+ process(safe_navigation(node.children[0].children[0])),
61
+ process(node.updated(nil, node.children.map(&method(:decsendize))))
62
+ ]
63
+ )))
61
64
 
62
65
  replace(node.loc.expression, new_node)
63
66
 
@@ -75,11 +78,12 @@ module RubyNext
75
78
  # Transform: x&.y -> (!x.nil? && x.y) || nil
76
79
  # This allows us to handle `false&.to_s == "false"`
77
80
  def safe_navigation(node)
78
- s(:or,
79
- s(:send,
80
- s(:send, node, :nil?),
81
- :!),
82
- s(:nil))
81
+ s(:begin,
82
+ s(:or,
83
+ s(:send,
84
+ s(:send, node, :nil?),
85
+ :!),
86
+ s(:nil)))
83
87
  end
84
88
  end
85
89
  end
@@ -2,6 +2,7 @@
2
2
 
3
3
  # Make sure Core is loaded
4
4
  require "ruby-next"
5
+ require "pathname"
5
6
 
6
7
  module RubyNext
7
8
  module Language
@@ -48,11 +49,13 @@ module RubyNext
48
49
 
49
50
  GemTranspiler.maybe_transpile(File.dirname(dirname), lib_dir, next_dirname) if transpile
50
51
 
51
- current_index = $LOAD_PATH.index(dirname)
52
+ current_index = $LOAD_PATH.find_index do |load_path|
53
+ Pathname.new(load_path).cleanpath.to_s == dirname
54
+ end
52
55
 
53
56
  raise "Gem's lib is not in the $LOAD_PATH: #{dirname}" if current_index.nil?
54
57
 
55
- version = RubyNext.next_version
58
+ version = RubyNext.next_ruby_version
56
59
 
57
60
  loop do
58
61
  break unless version
@@ -64,7 +67,7 @@ module RubyNext
64
67
  current_index += 1
65
68
  end
66
69
 
67
- version = RubyNext.next_version(version)
70
+ version = RubyNext.next_ruby_version(version)
68
71
  end
69
72
  end
70
73
  end
@@ -6,3 +6,13 @@ require "parser/current"
6
6
  $VERBOSE = save_verbose
7
7
 
8
8
  require "unparser"
9
+
10
+ # PR: https://github.com/mbj/unparser/pull/230
11
+ if defined? Unparser::Emitter::InPattern
12
+ Unparser::Emitter::InPattern.prepend(Module.new do
13
+ def dispatch
14
+ super
15
+ nl unless branch
16
+ end
17
+ end)
18
+ end
@@ -4,6 +4,7 @@
4
4
  # edge features and fix some bugs with 2.7+ syntax
5
5
 
6
6
  require "parser/ruby-next/version"
7
+ require "ruby-next/language/parser"
7
8
 
8
9
  module RuboCop
9
10
  # Transform Ruby Next parser version to a float, e.g.: "2.8.0.1" => 2.801
@@ -37,7 +38,6 @@ module RuboCop
37
38
  def parser_class(version)
38
39
  return super unless version == RUBY_NEXT_VERSION
39
40
 
40
- require "parser/rubynext"
41
41
  Parser::RubyNext
42
42
  end
43
43
  end
@@ -52,7 +52,7 @@ module RuboCop
52
52
  module AST
53
53
  module Traversal
54
54
  # Fixed in https://github.com/rubocop-hq/rubocop/pull/7786
55
- %i[case_match in_pattern find_pattern].each do |type|
55
+ %i[case_match in_pattern find_pattern match_pattern match_pattern_p].each do |type|
56
56
  next if method_defined?(:"on_#{type}")
57
57
  module_eval(<<-RUBY, __FILE__, __LINE__ + 1)
58
58
  def on_#{type}(node)
@@ -62,6 +62,10 @@ end
62
62
  RUBY
63
63
  end
64
64
  end
65
+
66
+ unless Builder.method_defined?(:match_pattern_p)
67
+ Builder.include RubyNext::Language::BuilderExt
68
+ end
65
69
  end
66
70
  end
67
71
  # rubocop:enable Layout/HeredocIndentation
@@ -102,20 +106,83 @@ module RuboCop
102
106
  send(:"on_#{body_node.type}", body_node)
103
107
  end
104
108
  end
109
+ end
110
+
111
+ module Layout
112
+ require "rubocop/cop/layout/assignment_indentation"
105
113
 
106
- unless method_defined?(:on_rasgn)
107
- def on_rasgn(node)
108
- val_node, asgn_node = *node
109
- send(:"on_#{asgn_node.type}", asgn_node)
110
- send(:"on_#{val_node.type}", val_node)
114
+ POTENTIAL_RIGHT_TYPES = %i[ivasgn lvasgn cvasgn gvasgn casgn masgn].freeze
115
+
116
+ AssignmentIndentation.prepend(Module.new do
117
+ def check_assignment(node, *)
118
+ return if rightward?(node)
119
+ super
111
120
  end
112
121
 
113
- def on_mrasgn(node)
114
- lhs, rhs = *node
115
- send(:"on_#{lhs.type}", lhs)
116
- send(:"on_#{rhs.type}", rhs)
122
+ private
123
+
124
+ def rightward?(node)
125
+ return unless POTENTIAL_RIGHT_TYPES.include?(node.type)
126
+
127
+ return unless node.loc.operator
128
+
129
+ assignee_loc =
130
+ if node.type == :masgn
131
+ node.children[0].loc.expression
132
+ else
133
+ node.loc.name
134
+ end
135
+
136
+ return false unless assignee_loc
137
+
138
+ assignee_loc.begin_pos > node.loc.operator.end_pos
117
139
  end
118
- end
140
+ end)
141
+
142
+ require "rubocop/cop/layout/empty_line_between_defs"
143
+ EmptyLineBetweenDefs.prepend(Module.new do
144
+ def def_end(node)
145
+ return super unless node.loc.end.nil?
146
+
147
+ node.loc.expression.line
148
+ end
149
+ end)
150
+ end
151
+
152
+ module Style
153
+ require "rubocop/cop/style/single_line_methods"
154
+ SingleLineMethods.prepend(Module.new do
155
+ def on_def(node)
156
+ return if node.loc.end.nil?
157
+ super
158
+ end
159
+
160
+ def on_defs(node)
161
+ return if node.loc.end.nil?
162
+ super
163
+ end
164
+ end)
165
+
166
+ require "rubocop/cop/style/def_with_parentheses"
167
+ DefWithParentheses.prepend(Module.new do
168
+ def on_def(node)
169
+ return if node.loc.end.nil?
170
+ super
171
+ end
172
+
173
+ def on_defs(node)
174
+ return if node.loc.end.nil?
175
+ super
176
+ end
177
+ end)
178
+
179
+ require "rubocop/cop/style/trailing_method_end_statement"
180
+ TrailingMethodEndStatement.prepend(Module.new do
181
+ def on_def(node)
182
+ return if node.loc.end.nil?
183
+ super
184
+ end
185
+ end)
119
186
  end
120
187
  end
121
188
  end
@@ -3,7 +3,7 @@
3
3
  # This file setup LOAD_PATH to load Ruby Next own transpiled paths
4
4
  # (we cannot use language/setup here, 'cause it requires Core to be loaded)
5
5
 
6
- version = RubyNext.next_version
6
+ version = RubyNext.next_ruby_version
7
7
  next_dirname = File.join(__dir__, "..", ".rbnext")
8
8
  lib_path = File.realpath(File.join(__dir__, ".."))
9
9
  current_index = $LOAD_PATH.index(lib_path)
@@ -18,5 +18,5 @@ loop do
18
18
  current_index += 1
19
19
  end
20
20
 
21
- version = RubyNext.next_version(version)
21
+ version = RubyNext.next_ruby_version(version)
22
22
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module RubyNext
4
- VERSION = "0.10.1"
4
+ VERSION = "0.11.0"
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.10.1
4
+ version: 0.11.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: 2020-09-09 00:00:00.000000000 Z
11
+ date: 2020-12-24 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: 3.0.0.0
19
+ version: 3.0.0.3
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: 3.0.0.0
26
+ version: 3.0.0.3
27
27
  - !ruby/object:Gem::Dependency
28
28
  name: unparser
29
29
  requirement: !ruby/object:Gem::Requirement
@@ -31,6 +31,9 @@ dependencies:
31
31
  - - ">="
32
32
  - !ruby/object:Gem::Version
33
33
  version: 0.4.8
34
+ - - "<"
35
+ - !ruby/object:Gem::Version
36
+ version: 0.6.0
34
37
  type: :development
35
38
  prerelease: false
36
39
  version_requirements: !ruby/object:Gem::Requirement
@@ -38,6 +41,9 @@ dependencies:
38
41
  - - ">="
39
42
  - !ruby/object:Gem::Version
40
43
  version: 0.4.8
44
+ - - "<"
45
+ - !ruby/object:Gem::Version
46
+ version: 0.6.0
41
47
  description: "\n Ruby Next Core is a zero deps version of Ruby Next meant to be
42
48
  used\n as as dependency in your gems.\n\n It contains all the polyfills and
43
49
  utility files but doesn't require transpiler\n dependencies to be install.\n
@@ -53,6 +59,7 @@ files:
53
59
  - LICENSE.txt
54
60
  - README.md
55
61
  - bin/parse
62
+ - bin/reparse
56
63
  - bin/ruby-next
57
64
  - bin/transform
58
65
  - lib/.rbnext/2.3/ruby-next/commands/core_ext.rb
@@ -61,13 +68,14 @@ files:
61
68
  - lib/.rbnext/2.3/ruby-next/language/rewriters/base.rb
62
69
  - lib/.rbnext/2.3/ruby-next/language/rewriters/endless_range.rb
63
70
  - lib/.rbnext/2.3/ruby-next/language/rewriters/pattern_matching.rb
64
- - lib/.rbnext/2.3/ruby-next/language/rewriters/right_hand_assignment.rb
65
71
  - lib/.rbnext/2.3/ruby-next/utils.rb
72
+ - lib/.rbnext/2.7/ruby-next/core.rb
66
73
  - lib/ruby-next.rb
67
74
  - lib/ruby-next/cli.rb
68
75
  - lib/ruby-next/commands/base.rb
69
76
  - lib/ruby-next/commands/core_ext.rb
70
77
  - lib/ruby-next/commands/nextify.rb
78
+ - lib/ruby-next/config.rb
71
79
  - lib/ruby-next/core.rb
72
80
  - lib/ruby-next/core/array/deconstruct.rb
73
81
  - lib/ruby-next/core/array/difference_union_intersection.rb
@@ -98,14 +106,15 @@ files:
98
106
  - lib/ruby-next/language/parser.rb
99
107
  - lib/ruby-next/language/proposed.rb
100
108
  - lib/ruby-next/language/rewriters/args_forward.rb
109
+ - lib/ruby-next/language/rewriters/args_forward_leading.rb
101
110
  - lib/ruby-next/language/rewriters/base.rb
102
111
  - lib/ruby-next/language/rewriters/endless_method.rb
103
112
  - lib/ruby-next/language/rewriters/endless_range.rb
104
113
  - lib/ruby-next/language/rewriters/find_pattern.rb
114
+ - lib/ruby-next/language/rewriters/in_pattern.rb
105
115
  - lib/ruby-next/language/rewriters/method_reference.rb
106
116
  - lib/ruby-next/language/rewriters/numbered_params.rb
107
117
  - lib/ruby-next/language/rewriters/pattern_matching.rb
108
- - lib/ruby-next/language/rewriters/right_hand_assignment.rb
109
118
  - lib/ruby-next/language/rewriters/runtime.rb
110
119
  - lib/ruby-next/language/rewriters/runtime/dir.rb
111
120
  - lib/ruby-next/language/rewriters/safe_navigation.rb
@@ -1,107 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module RubyNext
4
- module Language
5
- module Rewriters
6
- class RightHandAssignment < Base
7
- NAME = "right-hand-assignment"
8
- SYNTAX_PROBE = "1 + 2 => a"
9
- MIN_SUPPORTED_VERSION = Gem::Version.new("3.0.0")
10
-
11
- def on_rasgn(node)
12
- context.track! self
13
-
14
- node = super(node)
15
-
16
- val_node, asgn_node = *node
17
-
18
- remove(val_node.loc.expression.end.join(asgn_node.loc.expression))
19
- insert_before(val_node.loc.expression, "#{asgn_node.loc.expression.source} = ")
20
-
21
- asgn_node.updated(
22
- nil,
23
- asgn_node.children + [val_node]
24
- )
25
- end
26
-
27
- def on_vasgn(node)
28
- return super(node) unless rightward?(node)
29
-
30
- context.track! self
31
-
32
- name, val_node = *node
33
-
34
- remove(val_node.loc.expression.end.join(node.loc.name))
35
- insert_before(val_node.loc.expression, "#{name} = ")
36
-
37
- super(node)
38
- end
39
-
40
- def on_casgn(node)
41
- return super(node) unless rightward?(node)
42
-
43
- context.track! self
44
-
45
- scope_node, name, val_node = *node
46
-
47
- if scope_node
48
- scope = scope_node.type == :cbase ? scope_node.loc.expression.source : "#{scope_node.loc.expression.source}::"
49
- name = "#{scope}#{name}"
50
- end
51
-
52
- remove(val_node.loc.expression.end.join(node.loc.name))
53
- insert_before(val_node.loc.expression, "#{name} = ")
54
-
55
- super(node)
56
- end
57
-
58
- def on_mrasgn(node)
59
- context.track! self
60
-
61
- node = super(node)
62
-
63
- lhs, rhs = *node
64
-
65
- replace(lhs.loc.expression.end.join(rhs.loc.expression), ")")
66
- insert_before(lhs.loc.expression, "#{rhs.loc.expression.source} = (")
67
-
68
- node.updated(
69
- :masgn,
70
- [rhs, lhs]
71
- )
72
- end
73
-
74
- def on_masgn(node)
75
- return super(node) unless rightward?(node)
76
-
77
- context.track! self
78
-
79
- rhs, lhs = *node
80
-
81
- replace(lhs.loc.expression.end.join(rhs.loc.expression), ")")
82
- insert_before(lhs.loc.expression, "#{rhs.loc.expression.source} = (")
83
-
84
- super(node)
85
- end
86
-
87
- private
88
-
89
- def rightward?(node)
90
- # Location could be empty for node built by rewriters
91
- return false unless ((!node.loc.nil?) || nil) && node.loc.operator
92
-
93
- assignee_loc =
94
- if node.type == :masgn
95
- node.children[0].loc.expression
96
- else
97
- node.loc.name
98
- end
99
-
100
- return false unless assignee_loc
101
-
102
- assignee_loc.begin_pos > node.loc.operator.end_pos
103
- end
104
- end
105
- end
106
- end
107
- end