unparser 0.5.3 → 0.6.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -5,7 +5,7 @@ module Unparser
5
5
 
6
6
  # Emitter base class
7
7
  class Emitter
8
- include Adamantium::Flat, AbstractType, Constants, Generation, NodeHelpers
8
+ include Adamantium, AbstractType, Constants, Generation, NodeHelpers
9
9
  include Anima.new(:buffer, :comments, :node, :local_variable_scope)
10
10
 
11
11
  public :node
@@ -14,13 +14,22 @@ module Unparser
14
14
 
15
15
  handle(*MAP.keys)
16
16
 
17
+ def emit_heredoc_reminders
18
+ children.each do |node|
19
+ emitter(node).emit_heredoc_reminders
20
+ end
21
+ end
22
+
17
23
  private
18
24
 
19
25
  def dispatch
20
26
  write(MAP.fetch(node.type))
21
27
 
22
- unless children.empty?
23
- emit_arguments
28
+ if children.one? && n_if?(children.first)
29
+ ws
30
+ emitter(children.first).emit_ternary
31
+ else
32
+ emit_arguments unless children.empty?
24
33
  end
25
34
  end
26
35
 
@@ -4,10 +4,6 @@ module Unparser
4
4
  class Emitter
5
5
  # Emitter for Hash literals
6
6
  class Hash < self
7
- BAREWORD = /\A[A-Za-z_][A-Za-z_0-9]*[?!]?\z/.freeze
8
-
9
- private_constant(*constants(false))
10
-
11
7
  handle :hash
12
8
 
13
9
  def emit_last_argument_hash
@@ -41,34 +37,8 @@ module Unparser
41
37
  end
42
38
 
43
39
  def emit_hash_body
44
- delimited(children, &method(:emit_hash_member))
45
- end
46
-
47
- def emit_hash_member(node)
48
- if n_kwsplat?(node)
49
- visit(node)
50
- else
51
- emit_pair(node)
52
- end
40
+ delimited(children)
53
41
  end
54
-
55
- def emit_pair(pair)
56
- key, value = *pair.children
57
-
58
- if colon?(key)
59
- write(key.children.first.to_s, ': ')
60
- else
61
- visit(key)
62
- write(' => ')
63
- end
64
-
65
- visit(value)
66
- end
67
-
68
- def colon?(key)
69
- n_sym?(key) && BAREWORD.match?(key.children.first)
70
- end
71
-
72
42
  end # Hash
73
43
  end # Emitter
74
44
  end # Unparser
@@ -8,6 +8,14 @@ module Unparser
8
8
 
9
9
  children :condition, :if_branch, :else_branch
10
10
 
11
+ def emit_ternary
12
+ visit(condition)
13
+ write(' ? ')
14
+ visit(if_branch)
15
+ write(' : ')
16
+ visit(else_branch)
17
+ end
18
+
11
19
  private
12
20
 
13
21
  def dispatch
@@ -27,6 +27,8 @@ module Unparser
27
27
  ws
28
28
  write('then')
29
29
  emit_body(branch)
30
+ else
31
+ nl
30
32
  end
31
33
  end
32
34
  end # InPattern
@@ -36,7 +36,7 @@ module Unparser
36
36
  handle :indexasgn
37
37
 
38
38
  VALUE_RANGE = (1..-2).freeze
39
- NO_VALUE_PARENT = IceNine.deep_freeze(%i[and_asgn op_asgn or_asgn].to_set)
39
+ NO_VALUE_PARENT = %i[and_asgn op_asgn or_asgn].to_set.freeze
40
40
 
41
41
  private_constant(*constants(false))
42
42
 
@@ -0,0 +1,13 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Unparser
4
+ class Emitter
5
+ class Kwargs < self
6
+ handle :kwargs
7
+
8
+ def dispatch
9
+ delimited(children)
10
+ end
11
+ end # Kwargs
12
+ end # Emitter
13
+ end # Unparser
@@ -0,0 +1,22 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Unparser
4
+ class Emitter
5
+ # Emitter for in pattern nodes
6
+ class MatchPattern < self
7
+
8
+ handle :match_pattern
9
+ handle :match_pattern_p
10
+
11
+ children :target, :pattern
12
+
13
+ private
14
+
15
+ def dispatch
16
+ visit(target)
17
+ write(' in ')
18
+ visit(pattern)
19
+ end
20
+ end # InPattern
21
+ end # Emitter
22
+ end # Unparser
@@ -7,10 +7,10 @@ module Unparser
7
7
  class BinaryAssign < self
8
8
  children :target, :expression
9
9
 
10
- MAP = IceNine.deep_freeze(
10
+ MAP = {
11
11
  and_asgn: '&&=',
12
12
  or_asgn: '||='
13
- )
13
+ }.freeze
14
14
 
15
15
  handle(*MAP.keys)
16
16
 
@@ -0,0 +1,33 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Unparser
4
+ class Emitter
5
+ # Emitter for key value pairs in hash literals or kwargs
6
+ class Pair < self
7
+ BAREWORD = /\A[A-Za-z_][A-Za-z_0-9]*[?!]?\z/.freeze
8
+
9
+ private_constant(*constants(false))
10
+
11
+ handle :pair
12
+
13
+ children :key, :value
14
+
15
+ private
16
+
17
+ def dispatch
18
+ if colon?(key)
19
+ write(key.children.first.to_s, ': ')
20
+ else
21
+ visit(key)
22
+ write(' => ')
23
+ end
24
+
25
+ visit(value)
26
+ end
27
+
28
+ def colon?(key)
29
+ n_sym?(key) && BAREWORD.match?(key.children.first)
30
+ end
31
+ end
32
+ end
33
+ end
@@ -28,11 +28,11 @@ module Unparser
28
28
  RATIONAL_FORMAT = 'i'.freeze
29
29
 
30
30
  MAP =
31
- IceNine.deep_freeze(
31
+ {
32
32
  ::Float => :float,
33
33
  ::Rational => :rational,
34
34
  ::Integer => :int
35
- )
35
+ }.freeze
36
36
 
37
37
  private
38
38
 
@@ -4,7 +4,7 @@ module Unparser
4
4
  class Emitter
5
5
  # Emitter for simple nodes that generate a single token
6
6
  class Simple < self
7
- MAP = IceNine.deep_freeze(
7
+ MAP = {
8
8
  __ENCODING__: '__ENCODING__',
9
9
  __FILE__: '__FILE__',
10
10
  __LINE__: '__LINE__',
@@ -19,7 +19,7 @@ module Unparser
19
19
  self: 'self',
20
20
  true: 'true',
21
21
  zsuper: 'super'
22
- )
22
+ }.freeze
23
23
 
24
24
  handle(*MAP.keys)
25
25
 
@@ -22,12 +22,22 @@ module Unparser
22
22
 
23
23
  children :subject
24
24
 
25
+ def emit_mlhs
26
+ write('*')
27
+ subject_emitter.emit_mlhs if subject
28
+ end
29
+
25
30
  private
26
31
 
27
32
  def dispatch
28
33
  write('*')
29
- visit(subject) if subject
34
+ subject_emitter.write_to_buffer
35
+ end
36
+
37
+ def subject_emitter
38
+ emitter(subject)
30
39
  end
40
+ memoize :subject_emitter
31
41
  end
32
42
  end
33
43
  end # Unparser
@@ -0,0 +1,98 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Unparser
4
+ # Define equality, equivalence and inspection methods
5
+ #
6
+ # Original code before vendoring and reduction from: https://github.com/dkubb/equalizer.
7
+ class Equalizer < Module
8
+ # Initialize an Equalizer with the given keys
9
+ #
10
+ # Will use the keys with which it is initialized to define #cmp?,
11
+ # #hash, and #inspect
12
+ #
13
+ # @param [Array<Symbol>] keys
14
+ #
15
+ # @return [undefined]
16
+ #
17
+ # @api private
18
+ #
19
+ # rubocop:disable Lint/MissingSuper
20
+ def initialize(*keys)
21
+ @keys = keys
22
+ define_methods
23
+ freeze
24
+ end
25
+ # rubocop:enable Lint/MissingSuper
26
+
27
+ private
28
+
29
+ def included(descendant)
30
+ descendant.include(Methods)
31
+ end
32
+
33
+ def define_methods
34
+ define_cmp_method
35
+ define_hash_method
36
+ define_inspect_method
37
+ end
38
+
39
+ def define_cmp_method
40
+ keys = @keys
41
+ define_method(:cmp?) do |comparator, other|
42
+ keys.all? do |key|
43
+ __send__(key).public_send(comparator, other.__send__(key))
44
+ end
45
+ end
46
+ private :cmp?
47
+ end
48
+
49
+ def define_hash_method
50
+ keys = @keys
51
+ define_method(:hash) do
52
+ keys.map(&public_method(:__send__)).push(self.class).hash
53
+ end
54
+ end
55
+
56
+ def define_inspect_method
57
+ keys = @keys
58
+ define_method(:inspect) do
59
+ klass = self.class
60
+ name = klass.name || klass.inspect
61
+ "#<#{name}#{keys.map { |key| " #{key}=#{__send__(key).inspect}" }.join}>"
62
+ end
63
+ end
64
+
65
+ # The comparison methods
66
+ module Methods
67
+ # Compare the object with other object for equality
68
+ #
69
+ # @example
70
+ # object.eql?(other) # => true or false
71
+ #
72
+ # @param [Object] other
73
+ # the other object to compare with
74
+ #
75
+ # @return [Boolean]
76
+ #
77
+ # @api public
78
+ def eql?(other)
79
+ instance_of?(other.class) && cmp?(__method__, other)
80
+ end
81
+
82
+ # Compare the object with other object for equivalency
83
+ #
84
+ # @example
85
+ # object == other # => true or false
86
+ #
87
+ # @param [Object] other
88
+ # the other object to compare with
89
+ #
90
+ # @return [Boolean]
91
+ #
92
+ # @api public
93
+ def ==(other)
94
+ instance_of?(other.class) && cmp?(__method__, other)
95
+ end
96
+ end # module Methods
97
+ end # class Equalizer
98
+ end # Unparser
@@ -204,7 +204,9 @@ module Unparser
204
204
  end
205
205
 
206
206
  def emit_rescue_postcontrol(node)
207
- writer_with(Writer::Rescue, node).emit_postcontrol
207
+ writer = writer_with(Writer::Rescue, node)
208
+ writer.emit_postcontrol
209
+ writer.emit_heredoc_reminders
208
210
  end
209
211
 
210
212
  def emit_rescue_regular(node)
@@ -6,7 +6,7 @@ module Unparser
6
6
 
7
7
  def self.included(descendant)
8
8
  descendant.class_eval do
9
- include Adamantium::Flat, Concord.new(:node)
9
+ include Adamantium, Concord.new(:node)
10
10
 
11
11
  extend DSL
12
12
  end
@@ -45,6 +45,7 @@ module Unparser
45
45
  ensure
46
46
  hash
47
47
  hash_pattern
48
+ if
48
49
  in_pattern
49
50
  int
50
51
  kwsplat
@@ -3,7 +3,7 @@
3
3
  module Unparser
4
4
  # Validation of unparser results
5
5
  class Validation
6
- include Adamantium::Flat, Anima.new(
6
+ include Adamantium, Anima.new(
7
7
  :generated_node,
8
8
  :generated_source,
9
9
  :identification,
@@ -64,7 +64,7 @@ module Unparser
64
64
 
65
65
  new(
66
66
  identification: '(string)',
67
- original_source: MPrelude::Either::Right.new(original_source),
67
+ original_source: Either::Right.new(original_source),
68
68
  original_node: original_node,
69
69
  generated_source: generated_source,
70
70
  generated_node: generated_node
@@ -86,7 +86,7 @@ module Unparser
86
86
  new(
87
87
  identification: '(string)',
88
88
  original_source: generated_source,
89
- original_node: MPrelude::Either::Right.new(original_node),
89
+ original_node: Either::Right.new(original_node),
90
90
  generated_source: generated_source,
91
91
  generated_node: generated_node
92
92
  )
@@ -3,7 +3,7 @@
3
3
  module Unparser
4
4
  module Writer
5
5
  class Binary
6
- include Writer, Adamantium::Flat
6
+ include Writer, Adamantium
7
7
 
8
8
  children :left, :right
9
9
 
@@ -3,24 +3,22 @@
3
3
  module Unparser
4
4
  module Writer
5
5
  class DynamicString
6
- include Writer, Adamantium::Flat
6
+ include Writer, Adamantium
7
7
 
8
- PATTERNS_2 = IceNine.deep_freeze(
8
+ PATTERNS_2 =
9
9
  [
10
- %i[str_empty begin],
11
- %i[begin str_nl]
12
- ]
13
- )
10
+ %i[str_empty begin].freeze,
11
+ %i[begin str_nl].freeze
12
+ ].freeze
14
13
 
15
- PATTERNS_3 = IceNine.deep_freeze(
14
+ PATTERNS_3 =
16
15
  [
17
- %i[begin str_nl_eol str_nl_eol],
18
- %i[str_nl_eol begin str_nl_eol],
19
- %i[str_ws begin str_nl_eol]
20
- ]
21
- )
16
+ %i[begin str_nl_eol str_nl_eol].freeze,
17
+ %i[str_nl_eol begin str_nl_eol].freeze,
18
+ %i[str_ws begin str_nl_eol].freeze
19
+ ].freeze
22
20
 
23
- FLAT_INTERPOLATION = %i[ivar cvar gvar].to_set.freeze
21
+ FLAT_INTERPOLATION = %i[ivar cvar gvar nth_ref].to_set.freeze
24
22
 
25
23
  private_constant(*constants(false))
26
24
 
@@ -42,7 +40,7 @@ module Unparser
42
40
  private
43
41
 
44
42
  def heredoc_header
45
- need_squiggly? ? '<<~HEREDOC' : '<<-HEREDOC'
43
+ '<<-HEREDOC'
46
44
  end
47
45
 
48
46
  def heredoc?
@@ -55,11 +53,7 @@ module Unparser
55
53
 
56
54
  def emit_heredoc_body
57
55
  nl
58
- if need_squiggly?
59
- emit_squiggly_heredoc_body
60
- else
61
- emit_normal_heredoc_body
62
- end
56
+ emit_normal_heredoc_body
63
57
  end
64
58
 
65
59
  def emit_heredoc_footer
@@ -119,10 +113,6 @@ module Unparser
119
113
  n_str?(last) && last.children.first[-1].eql?("\n")
120
114
  end
121
115
 
122
- def need_squiggly?
123
- children.any?(s(:str, ''))
124
- end
125
-
126
116
  def emit_squiggly_heredoc_body
127
117
  buffer.indent
128
118
  children.each do |child|