unparser 0.4.8 → 0.5.3
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +20 -5
- data/bin/unparser +1 -1
- data/lib/unparser.rb +115 -61
- data/lib/unparser/ast.rb +0 -1
- data/lib/unparser/ast/local_variable_scope.rb +6 -76
- data/lib/unparser/buffer.rb +19 -16
- data/lib/unparser/cli.rb +26 -39
- data/lib/unparser/color.rb +0 -3
- data/lib/unparser/comments.rb +0 -26
- data/lib/unparser/constants.rb +4 -53
- data/lib/unparser/diff.rb +0 -17
- data/lib/unparser/dsl.rb +0 -32
- data/lib/unparser/emitter.rb +23 -425
- data/lib/unparser/emitter/alias.rb +2 -8
- data/lib/unparser/emitter/args.rb +45 -0
- data/lib/unparser/emitter/argument.rb +8 -166
- data/lib/unparser/emitter/array.rb +27 -0
- data/lib/unparser/emitter/array_pattern.rb +29 -0
- data/lib/unparser/emitter/assignment.rb +36 -127
- data/lib/unparser/emitter/begin.rb +9 -84
- data/lib/unparser/emitter/binary.rb +7 -20
- data/lib/unparser/emitter/block.rb +57 -41
- data/lib/unparser/emitter/case.rb +6 -48
- data/lib/unparser/emitter/case_guard.rb +27 -0
- data/lib/unparser/emitter/case_match.rb +40 -0
- data/lib/unparser/emitter/cbase.rb +1 -3
- data/lib/unparser/emitter/class.rb +6 -26
- data/lib/unparser/emitter/const_pattern.rb +24 -0
- data/lib/unparser/emitter/def.rb +7 -51
- data/lib/unparser/emitter/defined.rb +2 -12
- data/lib/unparser/emitter/dstr.rb +22 -0
- data/lib/unparser/emitter/dsym.rb +41 -0
- data/lib/unparser/emitter/flipflop.rb +11 -10
- data/lib/unparser/emitter/float.rb +29 -0
- data/lib/unparser/emitter/flow_modifier.rb +8 -55
- data/lib/unparser/emitter/for.rb +5 -19
- data/lib/unparser/emitter/hash.rb +74 -0
- data/lib/unparser/emitter/hash_pattern.rb +67 -0
- data/lib/unparser/emitter/hookexe.rb +5 -11
- data/lib/unparser/emitter/if.rb +9 -73
- data/lib/unparser/emitter/in_match.rb +21 -0
- data/lib/unparser/emitter/in_pattern.rb +34 -0
- data/lib/unparser/emitter/index.rb +21 -88
- data/lib/unparser/emitter/kwbegin.rb +31 -0
- data/lib/unparser/emitter/lambda.rb +0 -8
- data/lib/unparser/emitter/masgn.rb +20 -0
- data/lib/unparser/emitter/match.rb +3 -17
- data/lib/unparser/emitter/match_alt.rb +23 -0
- data/lib/unparser/emitter/match_as.rb +21 -0
- data/lib/unparser/emitter/match_rest.rb +26 -0
- data/lib/unparser/emitter/match_var.rb +19 -0
- data/lib/unparser/emitter/mlhs.rb +40 -0
- data/lib/unparser/emitter/module.rb +3 -9
- data/lib/unparser/emitter/op_assign.rb +12 -27
- data/lib/unparser/emitter/pin.rb +19 -0
- data/lib/unparser/emitter/primitive.rb +93 -0
- data/lib/unparser/emitter/range.rb +35 -0
- data/lib/unparser/emitter/regexp.rb +35 -0
- data/lib/unparser/emitter/repetition.rb +17 -57
- data/lib/unparser/emitter/rescue.rb +1 -97
- data/lib/unparser/emitter/root.rb +17 -1
- data/lib/unparser/emitter/send.rb +10 -219
- data/lib/unparser/emitter/simple.rb +33 -0
- data/lib/unparser/emitter/splat.rb +2 -18
- data/lib/unparser/emitter/super.rb +1 -29
- data/lib/unparser/emitter/undef.rb +1 -9
- data/lib/unparser/emitter/variable.rb +1 -31
- data/lib/unparser/emitter/xstr.rb +72 -0
- data/lib/unparser/emitter/yield.rb +1 -9
- data/lib/unparser/generation.rb +250 -0
- data/lib/unparser/node_details.rb +21 -0
- data/lib/unparser/node_details/send.rb +62 -0
- data/lib/unparser/node_helpers.rb +45 -6
- data/lib/unparser/validation.rb +58 -35
- data/lib/unparser/writer.rb +15 -0
- data/lib/unparser/writer/binary.rb +99 -0
- data/lib/unparser/writer/dynamic_string.rb +233 -0
- data/lib/unparser/writer/resbody.rb +40 -0
- data/lib/unparser/writer/rescue.rb +39 -0
- data/lib/unparser/writer/send.rb +124 -0
- data/lib/unparser/{emitter → writer}/send/attribute_assignment.rb +11 -26
- data/lib/unparser/writer/send/binary.rb +27 -0
- data/lib/unparser/writer/send/conditional.rb +25 -0
- data/lib/unparser/writer/send/regular.rb +33 -0
- data/lib/unparser/{emitter → writer}/send/unary.rb +10 -17
- metadata +65 -89
- data/.github/workflows/ci.yml +0 -90
- data/.gitignore +0 -37
- data/.rspec +0 -4
- data/.rubocop.yml +0 -126
- data/Changelog.md +0 -162
- data/Gemfile +0 -9
- data/Gemfile.lock +0 -111
- data/LICENSE +0 -20
- data/Rakefile +0 -22
- data/config/devtools.yml +0 -2
- data/config/flay.yml +0 -3
- data/config/flog.yml +0 -2
- data/config/mutant.yml +0 -6
- data/config/reek.yml +0 -98
- data/config/yardstick.yml +0 -2
- data/lib/unparser/emitter/empty.rb +0 -23
- data/lib/unparser/emitter/ensure.rb +0 -37
- data/lib/unparser/emitter/literal.rb +0 -10
- data/lib/unparser/emitter/literal/array.rb +0 -29
- data/lib/unparser/emitter/literal/dynamic.rb +0 -53
- data/lib/unparser/emitter/literal/dynamic_body.rb +0 -132
- data/lib/unparser/emitter/literal/execute_string.rb +0 -38
- data/lib/unparser/emitter/literal/hash.rb +0 -156
- data/lib/unparser/emitter/literal/primitive.rb +0 -145
- data/lib/unparser/emitter/literal/range.rb +0 -36
- data/lib/unparser/emitter/literal/regexp.rb +0 -114
- data/lib/unparser/emitter/literal/singleton.rb +0 -26
- data/lib/unparser/emitter/meta.rb +0 -16
- data/lib/unparser/emitter/redo.rb +0 -25
- data/lib/unparser/emitter/resbody.rb +0 -76
- data/lib/unparser/emitter/retry.rb +0 -25
- data/lib/unparser/emitter/send/binary.rb +0 -57
- data/lib/unparser/emitter/send/conditional.rb +0 -40
- data/lib/unparser/emitter/send/regular.rb +0 -40
- data/lib/unparser/preprocessor.rb +0 -159
- data/spec/integration/unparser/corpus_spec.rb +0 -125
- data/spec/integrations.yml +0 -92
- data/spec/spec_helper.rb +0 -42
- data/spec/unit/unparser/buffer/append_spec.rb +0 -24
- data/spec/unit/unparser/buffer/append_without_prefix_spec.rb +0 -23
- data/spec/unit/unparser/buffer/capture_content_spec.rb +0 -17
- data/spec/unit/unparser/buffer/content_spec.rb +0 -38
- data/spec/unit/unparser/buffer/fresh_line_spec.rb +0 -20
- data/spec/unit/unparser/buffer/indent_spec.rb +0 -20
- data/spec/unit/unparser/buffer/nl_spec.rb +0 -16
- data/spec/unit/unparser/buffer/unindent_spec.rb +0 -20
- data/spec/unit/unparser/color_spec.rb +0 -40
- data/spec/unit/unparser/comments/consume_spec.rb +0 -22
- data/spec/unit/unparser/comments/take_all_spec.rb +0 -19
- data/spec/unit/unparser/comments/take_before_spec.rb +0 -46
- data/spec/unit/unparser/comments/take_eol_comments_spec.rb +0 -32
- data/spec/unit/unparser/diff_spec.rb +0 -189
- data/spec/unit/unparser/emitter/class_methods/handle_spec.rb +0 -17
- data/spec/unit/unparser/validation_spec.rb +0 -327
- data/spec/unit/unparser_spec.rb +0 -1920
- data/unparser.gemspec +0 -35
@@ -1,26 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
module Unparser
|
4
|
-
class Emitter
|
5
|
-
class Literal
|
6
|
-
# Emiter for literal singletons
|
7
|
-
class Singleton < self
|
8
|
-
|
9
|
-
handle :self, :true, :false, :nil
|
10
|
-
|
11
|
-
private
|
12
|
-
|
13
|
-
# Perform dispatco
|
14
|
-
#
|
15
|
-
# @return [undefined]
|
16
|
-
#
|
17
|
-
# @api private
|
18
|
-
#
|
19
|
-
def dispatch
|
20
|
-
buffer.append(node.type.to_s)
|
21
|
-
end
|
22
|
-
|
23
|
-
end # Singleton
|
24
|
-
end # Literal
|
25
|
-
end # Emitter
|
26
|
-
end # Unparser
|
@@ -1,16 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
module Unparser
|
4
|
-
class Emitter
|
5
|
-
# Namespace class for meta emitters
|
6
|
-
class Meta < self
|
7
|
-
include Terminated
|
8
|
-
|
9
|
-
handle(:__ENCODING__, :__FILE__, :__LINE__)
|
10
|
-
|
11
|
-
def dispatch
|
12
|
-
write(node.type.to_s)
|
13
|
-
end
|
14
|
-
end # Meta
|
15
|
-
end # Emitter
|
16
|
-
end # Unparser
|
@@ -1,25 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
module Unparser
|
4
|
-
class Emitter
|
5
|
-
# Emitter for redo nodes
|
6
|
-
class Redo < self
|
7
|
-
include Terminated
|
8
|
-
|
9
|
-
handle :redo
|
10
|
-
|
11
|
-
private
|
12
|
-
|
13
|
-
# Perform dispatch
|
14
|
-
#
|
15
|
-
# @return [undefined]
|
16
|
-
#
|
17
|
-
# @api private
|
18
|
-
#
|
19
|
-
def dispatch
|
20
|
-
write(K_REDO)
|
21
|
-
end
|
22
|
-
|
23
|
-
end # Redo
|
24
|
-
end # Emitter
|
25
|
-
end # Unparser
|
@@ -1,76 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
module Unparser
|
4
|
-
class Emitter
|
5
|
-
# Emitter for rescue body nodes
|
6
|
-
class Resbody < self
|
7
|
-
|
8
|
-
children :exception, :assignment, :body
|
9
|
-
|
10
|
-
# Emitter for resbody in standalone form
|
11
|
-
class Standalone < self
|
12
|
-
|
13
|
-
private
|
14
|
-
|
15
|
-
# Perform dispatch
|
16
|
-
#
|
17
|
-
# @return [undefined]
|
18
|
-
#
|
19
|
-
# @api private
|
20
|
-
#
|
21
|
-
def dispatch
|
22
|
-
write(K_RESCUE, WS)
|
23
|
-
visit_plain(body)
|
24
|
-
end
|
25
|
-
end
|
26
|
-
|
27
|
-
# Emitter for resbody in keyworkd-embedded form
|
28
|
-
class Embedded < self
|
29
|
-
|
30
|
-
handle :resbody
|
31
|
-
|
32
|
-
private
|
33
|
-
|
34
|
-
# Perform dispatch
|
35
|
-
#
|
36
|
-
# @return [undefined]
|
37
|
-
#
|
38
|
-
# @api private
|
39
|
-
#
|
40
|
-
def dispatch
|
41
|
-
write(K_RESCUE)
|
42
|
-
emit_exception
|
43
|
-
emit_assignment
|
44
|
-
emit_body
|
45
|
-
end
|
46
|
-
|
47
|
-
# Emit exception
|
48
|
-
#
|
49
|
-
# @return [undefined]
|
50
|
-
#
|
51
|
-
# @api private
|
52
|
-
#
|
53
|
-
def emit_exception
|
54
|
-
return unless exception
|
55
|
-
|
56
|
-
ws
|
57
|
-
delimited(exception.children)
|
58
|
-
end
|
59
|
-
|
60
|
-
# Emit assignment
|
61
|
-
#
|
62
|
-
# @return [undefined]
|
63
|
-
#
|
64
|
-
# @api private
|
65
|
-
#
|
66
|
-
def emit_assignment
|
67
|
-
return unless assignment
|
68
|
-
|
69
|
-
write(WS, T_ASR, WS)
|
70
|
-
visit(assignment)
|
71
|
-
end
|
72
|
-
|
73
|
-
end # Resbody
|
74
|
-
end
|
75
|
-
end # Emitter
|
76
|
-
end # Unparser
|
@@ -1,25 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
module Unparser
|
4
|
-
class Emitter
|
5
|
-
# Emitter for retry nodes
|
6
|
-
class Retry < self
|
7
|
-
include Terminated
|
8
|
-
|
9
|
-
handle :retry
|
10
|
-
|
11
|
-
private
|
12
|
-
|
13
|
-
# Perform dispatch
|
14
|
-
#
|
15
|
-
# @return [undefined]
|
16
|
-
#
|
17
|
-
# @api private
|
18
|
-
#
|
19
|
-
def dispatch
|
20
|
-
write(K_RETRY)
|
21
|
-
end
|
22
|
-
|
23
|
-
end # Break
|
24
|
-
end # Emitter
|
25
|
-
end # Unparser
|
@@ -1,57 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
module Unparser
|
4
|
-
class Emitter
|
5
|
-
class Send
|
6
|
-
# Emitter for binary sends
|
7
|
-
class Binary < self
|
8
|
-
include Unterminated
|
9
|
-
|
10
|
-
private
|
11
|
-
|
12
|
-
# Return undefined
|
13
|
-
#
|
14
|
-
# @return [undefined]
|
15
|
-
#
|
16
|
-
# @api private
|
17
|
-
#
|
18
|
-
def dispatch
|
19
|
-
visit(receiver)
|
20
|
-
emit_operator
|
21
|
-
emit_right
|
22
|
-
end
|
23
|
-
|
24
|
-
# Emit operator
|
25
|
-
#
|
26
|
-
# @return [undefined]
|
27
|
-
#
|
28
|
-
# @api private
|
29
|
-
#
|
30
|
-
def emit_operator
|
31
|
-
write(WS, string_selector, WS)
|
32
|
-
end
|
33
|
-
|
34
|
-
# Return right node
|
35
|
-
#
|
36
|
-
# @return [Parser::AST::Node]
|
37
|
-
#
|
38
|
-
# @api private
|
39
|
-
#
|
40
|
-
def right_node
|
41
|
-
children[2]
|
42
|
-
end
|
43
|
-
|
44
|
-
# Emit right
|
45
|
-
#
|
46
|
-
# @return [undefined]
|
47
|
-
#
|
48
|
-
# @api private
|
49
|
-
#
|
50
|
-
def emit_right
|
51
|
-
visit(right_node)
|
52
|
-
end
|
53
|
-
|
54
|
-
end # Binary
|
55
|
-
end # Send
|
56
|
-
end # Emitter
|
57
|
-
end # Unparser
|
@@ -1,40 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
module Unparser
|
4
|
-
class Emitter
|
5
|
-
class Send
|
6
|
-
# Emitter for "conditional" receiver&.selector(arguments...) case
|
7
|
-
class Conditional < self
|
8
|
-
include Terminated
|
9
|
-
|
10
|
-
handle :csend
|
11
|
-
|
12
|
-
private
|
13
|
-
|
14
|
-
# Perform regular dispatch
|
15
|
-
#
|
16
|
-
# @return [undefined]
|
17
|
-
#
|
18
|
-
# @api private
|
19
|
-
#
|
20
|
-
def dispatch
|
21
|
-
emit_receiver
|
22
|
-
emit_selector
|
23
|
-
emit_arguments
|
24
|
-
end
|
25
|
-
|
26
|
-
# Emit receiver
|
27
|
-
#
|
28
|
-
# @return [undefined]
|
29
|
-
#
|
30
|
-
# @api private
|
31
|
-
#
|
32
|
-
def emit_receiver
|
33
|
-
visit(receiver)
|
34
|
-
write(T_AMP, T_DOT)
|
35
|
-
end
|
36
|
-
|
37
|
-
end # Regular
|
38
|
-
end # Send
|
39
|
-
end # Emitter
|
40
|
-
end # Unparser
|
@@ -1,40 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
module Unparser
|
4
|
-
class Emitter
|
5
|
-
class Send
|
6
|
-
# Emitter for "regular" receiver.selector(arguments...) case
|
7
|
-
class Regular < self
|
8
|
-
include Terminated
|
9
|
-
|
10
|
-
private
|
11
|
-
|
12
|
-
# Perform regular dispatch
|
13
|
-
#
|
14
|
-
# @return [undefined]
|
15
|
-
#
|
16
|
-
# @api private
|
17
|
-
#
|
18
|
-
def dispatch
|
19
|
-
emit_receiver
|
20
|
-
emit_selector
|
21
|
-
emit_arguments
|
22
|
-
end
|
23
|
-
|
24
|
-
# Emit receiver
|
25
|
-
#
|
26
|
-
# @return [undefined]
|
27
|
-
#
|
28
|
-
# @api private
|
29
|
-
#
|
30
|
-
def emit_receiver
|
31
|
-
return unless first_child
|
32
|
-
|
33
|
-
visit(receiver)
|
34
|
-
write(T_DOT)
|
35
|
-
end
|
36
|
-
|
37
|
-
end # Regular
|
38
|
-
end # Send
|
39
|
-
end # Emitter
|
40
|
-
end # Unparser
|
@@ -1,159 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
module Unparser
|
4
|
-
# Preprocessor to normalize AST generated by parser
|
5
|
-
class Preprocessor
|
6
|
-
include Adamantium::Flat, NodeHelpers, AbstractType, Concord.new(:node, :parent_type), Procto.call(:result)
|
7
|
-
|
8
|
-
# Return preprocessor result
|
9
|
-
#
|
10
|
-
# @return [Parser::AST::Node]
|
11
|
-
#
|
12
|
-
# @api private
|
13
|
-
#
|
14
|
-
abstract_method :result
|
15
|
-
|
16
|
-
EMPTY = Parser::AST::Node.new(:empty)
|
17
|
-
|
18
|
-
# Run preprocessor for node
|
19
|
-
#
|
20
|
-
# @param [Parser::AST::Node, nil] node
|
21
|
-
#
|
22
|
-
# @return [Parser::AST::Node, nil]
|
23
|
-
#
|
24
|
-
# @api private
|
25
|
-
#
|
26
|
-
def self.run(node, parent_type = nil)
|
27
|
-
return EMPTY if node.nil?
|
28
|
-
|
29
|
-
REGISTRY.fetch(node.type, [Noop]).reduce(node) do |current, processor|
|
30
|
-
processor.call(current, parent_type)
|
31
|
-
end
|
32
|
-
end
|
33
|
-
|
34
|
-
REGISTRY = Hash.new { |hash, key| hash[key] = [] }
|
35
|
-
|
36
|
-
# Register preprocessor
|
37
|
-
#
|
38
|
-
# @param [Symbol] type
|
39
|
-
#
|
40
|
-
# @return [undefined]
|
41
|
-
#
|
42
|
-
# @api private
|
43
|
-
#
|
44
|
-
def self.register(type)
|
45
|
-
REGISTRY[type] << self
|
46
|
-
end
|
47
|
-
private_class_method :register
|
48
|
-
|
49
|
-
private
|
50
|
-
|
51
|
-
# Visit node
|
52
|
-
#
|
53
|
-
# @param [Parser::AST::Node] child
|
54
|
-
#
|
55
|
-
# @return [undefined]
|
56
|
-
#
|
57
|
-
# @api private
|
58
|
-
#
|
59
|
-
def visit(child)
|
60
|
-
self.class.run(child, node.type)
|
61
|
-
end
|
62
|
-
|
63
|
-
# Return children
|
64
|
-
#
|
65
|
-
# @return [Array<Parser::AST::Node>]
|
66
|
-
#
|
67
|
-
# @api private
|
68
|
-
#
|
69
|
-
def children
|
70
|
-
node.children
|
71
|
-
end
|
72
|
-
|
73
|
-
# Return visited children
|
74
|
-
#
|
75
|
-
# @return [Array<Parser::Ast::Node>]
|
76
|
-
#
|
77
|
-
# @api private
|
78
|
-
#
|
79
|
-
def visited_children
|
80
|
-
children.map do |node|
|
81
|
-
if node.is_a?(Parser::AST::Node)
|
82
|
-
visit(node)
|
83
|
-
else
|
84
|
-
node
|
85
|
-
end
|
86
|
-
end
|
87
|
-
end
|
88
|
-
|
89
|
-
# Noop preprocessor that just passes node through.
|
90
|
-
class Noop < self
|
91
|
-
|
92
|
-
register :int
|
93
|
-
register :str
|
94
|
-
|
95
|
-
# Return preprocessor result
|
96
|
-
#
|
97
|
-
# @return [Parser::AST::Node]
|
98
|
-
#
|
99
|
-
# @api private
|
100
|
-
#
|
101
|
-
def result
|
102
|
-
node.updated(nil, visited_children)
|
103
|
-
end
|
104
|
-
|
105
|
-
end # Noop
|
106
|
-
|
107
|
-
# Preprocessor transforming numeric nodes with infinity as value to round trippable equivalent.
|
108
|
-
class Infinity < self
|
109
|
-
|
110
|
-
register :float
|
111
|
-
register :int
|
112
|
-
|
113
|
-
NEG_INFINITY = -(Float::INFINITY - 1)
|
114
|
-
|
115
|
-
# Return preprocessor result
|
116
|
-
#
|
117
|
-
# @return [Parser::AST::Node]
|
118
|
-
#
|
119
|
-
# @api private
|
120
|
-
#
|
121
|
-
def result
|
122
|
-
value = node.children.first
|
123
|
-
case value
|
124
|
-
when Float::INFINITY
|
125
|
-
s(:const, s(:const, nil, :Float), :INFINITY)
|
126
|
-
when NEG_INFINITY
|
127
|
-
s(:send, s(:const, s(:const, nil, :Float), :INFINITY), :-@)
|
128
|
-
else
|
129
|
-
node
|
130
|
-
end
|
131
|
-
end
|
132
|
-
end
|
133
|
-
|
134
|
-
# Preprocessor for begin nodes. Removes begin nodes with one child.
|
135
|
-
#
|
136
|
-
# This reduces the amount of complex logic needed inside unparser to emit "nice" syntax with minimal
|
137
|
-
# tokens.
|
138
|
-
#
|
139
|
-
class Begin < self
|
140
|
-
|
141
|
-
register :begin
|
142
|
-
|
143
|
-
# Return preprocessor result
|
144
|
-
#
|
145
|
-
# @return [Parser::AST::Node]
|
146
|
-
#
|
147
|
-
# @api private
|
148
|
-
#
|
149
|
-
def result
|
150
|
-
if children.one? && !parent_type.equal?(:regexp)
|
151
|
-
visit(children.first)
|
152
|
-
else
|
153
|
-
Noop.call(node, parent_type)
|
154
|
-
end
|
155
|
-
end
|
156
|
-
|
157
|
-
end # Begin
|
158
|
-
end # Preprocessor
|
159
|
-
end # Unparser
|