ruby-next-core 0.10.5 → 0.11.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (35) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +12 -0
  3. data/README.md +9 -14
  4. data/lib/.rbnext/2.3/ruby-next/commands/nextify.rb +1 -1
  5. data/lib/.rbnext/2.3/ruby-next/language/eval.rb +3 -3
  6. data/lib/.rbnext/2.3/ruby-next/language/rewriters/base.rb +19 -1
  7. data/lib/.rbnext/2.3/ruby-next/language/rewriters/pattern_matching.rb +17 -14
  8. data/lib/.rbnext/2.7/ruby-next/core.rb +203 -0
  9. data/lib/ruby-next.rb +5 -38
  10. data/lib/ruby-next/commands/nextify.rb +1 -1
  11. data/lib/ruby-next/config.rb +45 -0
  12. data/lib/ruby-next/core.rb +5 -4
  13. data/lib/ruby-next/core/array/deconstruct.rb +1 -1
  14. data/lib/ruby-next/core/constants/no_matching_pattern_error.rb +1 -1
  15. data/lib/ruby-next/core/hash/deconstruct_keys.rb +1 -1
  16. data/lib/ruby-next/core/struct/deconstruct_keys.rb +3 -3
  17. data/lib/ruby-next/core_ext.rb +2 -0
  18. data/lib/ruby-next/language.rb +15 -4
  19. data/lib/ruby-next/language/bootsnap.rb +1 -1
  20. data/lib/ruby-next/language/edge.rb +0 -6
  21. data/lib/ruby-next/language/eval.rb +3 -3
  22. data/lib/ruby-next/language/parser.rb +19 -0
  23. data/lib/ruby-next/language/rewriters/args_forward.rb +8 -4
  24. data/lib/ruby-next/language/rewriters/args_forward_leading.rb +75 -0
  25. data/lib/ruby-next/language/rewriters/base.rb +19 -1
  26. data/lib/ruby-next/language/rewriters/in_pattern.rb +56 -0
  27. data/lib/ruby-next/language/rewriters/pattern_matching.rb +17 -14
  28. data/lib/ruby-next/language/setup.rb +6 -3
  29. data/lib/ruby-next/language/unparser.rb +10 -0
  30. data/lib/ruby-next/rubocop.rb +9 -18
  31. data/lib/ruby-next/setup_self.rb +2 -2
  32. data/lib/ruby-next/version.rb +1 -1
  33. metadata +8 -6
  34. data/lib/.rbnext/2.3/ruby-next/language/rewriters/right_hand_assignment.rb +0 -117
  35. data/lib/ruby-next/language/rewriters/right_hand_assignment.rb +0 -117
@@ -13,12 +13,12 @@ module RubyNext
13
13
 
14
14
  # Useful to generate simple operation nodes
15
15
  # (e.g., 'a + b')
16
- def -(val)
17
- ::Parser::AST::Node.new(:send, [self, :-, val.to_ast_node])
16
+ def -(other)
17
+ ::Parser::AST::Node.new(:send, [self, :-, other.to_ast_node])
18
18
  end
19
19
 
20
- def +(val)
21
- ::Parser::AST::Node.new(:send, [self, :+, val.to_ast_node])
20
+ def +(other)
21
+ ::Parser::AST::Node.new(:send, [self, :+, other.to_ast_node])
22
22
  end
23
23
  end
24
24
 
@@ -218,9 +218,10 @@ module RubyNext
218
218
  predicate_clause(:respond_to_deconstruct_keys, node)
219
219
  end
220
220
 
221
- def hash_key(node, key)
222
- key = key.children.first if key.is_a?(::Parser::AST::Node)
223
- predicate_clause(:"hash_key_#{key}", node)
221
+ def hash_keys(node, keys)
222
+ keys = keys.map { |key| key.is_a?(::Parser::AST::Node) ? key.children.first : key }
223
+
224
+ predicate_clause(:"hash_keys_#{keys.join("_p_")}", node)
224
225
  end
225
226
  end
226
227
  end
@@ -266,7 +267,7 @@ module RubyNext
266
267
  )
267
268
  end
268
269
 
269
- def on_in_match(node)
270
+ def on_match_pattern(node)
270
271
  context.track! self
271
272
 
272
273
  @deconstructed_keys = {}
@@ -303,6 +304,8 @@ module RubyNext
303
304
  end
304
305
  end
305
306
 
307
+ alias on_in_match on_match_pattern
308
+
306
309
  private
307
310
 
308
311
  def rewrite_case_in!(node, matchee, new_node)
@@ -883,14 +886,14 @@ module RubyNext
883
886
  end
884
887
 
885
888
  def having_hash_keys(keys, hash = s(:lvar, locals[:hash]))
886
- key = keys.shift
887
- node = predicates.hash_key(hash_has_key(key, hash), key)
889
+ keys.reduce(nil) do |acc, key|
890
+ pnode = hash_has_key(key, hash)
891
+ next pnode unless acc
888
892
 
889
- keys.reduce(node) do |res, key|
890
893
  s(:begin,
891
- s(:and,
892
- res,
893
- predicates.hash_key(hash_has_key(key, hash), key)))
894
+ s(:and, acc, pnode))
895
+ end.then do |node|
896
+ predicates.hash_keys(node, keys)
894
897
  end
895
898
  end
896
899
 
@@ -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,27 +106,14 @@ module RuboCop
102
106
  send(:"on_#{body_node.type}", body_node)
103
107
  end
104
108
  end
105
-
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)
111
- end
112
-
113
- def on_mrasgn(node)
114
- lhs, rhs = *node
115
- send(:"on_#{lhs.type}", lhs)
116
- send(:"on_#{rhs.type}", rhs)
117
- end
118
- end
119
109
  end
120
110
 
121
111
  module Layout
122
112
  require "rubocop/cop/layout/assignment_indentation"
123
- AssignmentIndentation.prepend(Module.new do
124
- POTENTIAL_RIGHT_TYPES = %i[ivasgn lvasgn cvasgn gvasgn casgn masgn].freeze
125
113
 
114
+ POTENTIAL_RIGHT_TYPES = %i[ivasgn lvasgn cvasgn gvasgn casgn masgn].freeze
115
+
116
+ AssignmentIndentation.prepend(Module.new do
126
117
  def check_assignment(node, *)
127
118
  return if rightward?(node)
128
119
  super
@@ -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.5"
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.5
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-10-13 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.1
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.1
26
+ version: 3.0.0.3
27
27
  - !ruby/object:Gem::Dependency
28
28
  name: unparser
29
29
  requirement: !ruby/object:Gem::Requirement
@@ -68,13 +68,14 @@ files:
68
68
  - lib/.rbnext/2.3/ruby-next/language/rewriters/base.rb
69
69
  - lib/.rbnext/2.3/ruby-next/language/rewriters/endless_range.rb
70
70
  - lib/.rbnext/2.3/ruby-next/language/rewriters/pattern_matching.rb
71
- - lib/.rbnext/2.3/ruby-next/language/rewriters/right_hand_assignment.rb
72
71
  - lib/.rbnext/2.3/ruby-next/utils.rb
72
+ - lib/.rbnext/2.7/ruby-next/core.rb
73
73
  - lib/ruby-next.rb
74
74
  - lib/ruby-next/cli.rb
75
75
  - lib/ruby-next/commands/base.rb
76
76
  - lib/ruby-next/commands/core_ext.rb
77
77
  - lib/ruby-next/commands/nextify.rb
78
+ - lib/ruby-next/config.rb
78
79
  - lib/ruby-next/core.rb
79
80
  - lib/ruby-next/core/array/deconstruct.rb
80
81
  - lib/ruby-next/core/array/difference_union_intersection.rb
@@ -105,14 +106,15 @@ files:
105
106
  - lib/ruby-next/language/parser.rb
106
107
  - lib/ruby-next/language/proposed.rb
107
108
  - lib/ruby-next/language/rewriters/args_forward.rb
109
+ - lib/ruby-next/language/rewriters/args_forward_leading.rb
108
110
  - lib/ruby-next/language/rewriters/base.rb
109
111
  - lib/ruby-next/language/rewriters/endless_method.rb
110
112
  - lib/ruby-next/language/rewriters/endless_range.rb
111
113
  - lib/ruby-next/language/rewriters/find_pattern.rb
114
+ - lib/ruby-next/language/rewriters/in_pattern.rb
112
115
  - lib/ruby-next/language/rewriters/method_reference.rb
113
116
  - lib/ruby-next/language/rewriters/numbered_params.rb
114
117
  - lib/ruby-next/language/rewriters/pattern_matching.rb
115
- - lib/ruby-next/language/rewriters/right_hand_assignment.rb
116
118
  - lib/ruby-next/language/rewriters/runtime.rb
117
119
  - lib/ruby-next/language/rewriters/runtime/dir.rb
118
120
  - lib/ruby-next/language/rewriters/safe_navigation.rb
@@ -1,117 +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
- node = super(node)
85
-
86
- lhs, rhs = *node
87
-
88
- node.updated(
89
- nil,
90
- [
91
- lhs,
92
- s(:begin, rhs)
93
- ]
94
- )
95
- end
96
-
97
- private
98
-
99
- def rightward?(node)
100
- # Location could be empty for node built by rewriters
101
- return false unless ((!node.loc.nil? || nil) && node.loc.operator)
102
-
103
- assignee_loc =
104
- if node.type == :masgn
105
- node.children[0].loc.expression
106
- else
107
- node.loc.name
108
- end
109
-
110
- return false unless assignee_loc
111
-
112
- assignee_loc.begin_pos > node.loc.operator.end_pos
113
- end
114
- end
115
- end
116
- end
117
- end
@@ -1,117 +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
- node = super(node)
85
-
86
- lhs, rhs = *node
87
-
88
- node.updated(
89
- nil,
90
- [
91
- lhs,
92
- s(:begin, rhs)
93
- ]
94
- )
95
- end
96
-
97
- private
98
-
99
- def rightward?(node)
100
- # Location could be empty for node built by rewriters
101
- return false unless node.loc&.operator
102
-
103
- assignee_loc =
104
- if node.type == :masgn
105
- node.children[0].loc.expression
106
- else
107
- node.loc.name
108
- end
109
-
110
- return false unless assignee_loc
111
-
112
- assignee_loc.begin_pos > node.loc.operator.end_pos
113
- end
114
- end
115
- end
116
- end
117
- end