refract 1.1.0 → 1.2.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.
- checksums.yaml +4 -4
- data/README.md +15 -0
- data/lib/refract/basic_visitor.rb +3 -3
- data/lib/refract/converter.rb +99 -7
- data/lib/refract/formatter.rb +401 -168
- data/lib/refract/mutation_visitor.rb +17 -1
- data/lib/refract/nodes/alias_global_variable_node.rb +1 -1
- data/lib/refract/nodes/call_and_write_node.rb +3 -2
- data/lib/refract/nodes/call_node.rb +4 -2
- data/lib/refract/nodes/call_operator_write_node.rb +3 -2
- data/lib/refract/nodes/call_or_write_node.rb +3 -2
- data/lib/refract/nodes/false_node.rb +1 -0
- data/lib/refract/nodes/forwarding_arguments_node.rb +1 -0
- data/lib/refract/nodes/forwarding_parameter_node.rb +1 -0
- data/lib/refract/nodes/hash_pattern_node.rb +3 -2
- data/lib/refract/nodes/implicit_rest_node.rb +1 -0
- data/lib/refract/nodes/interpolated_match_last_line_node.rb +3 -2
- data/lib/refract/nodes/interpolated_regular_expression_node.rb +3 -2
- data/lib/refract/nodes/it_local_variable_read_node.rb +1 -0
- data/lib/refract/nodes/it_parameters_node.rb +1 -0
- data/lib/refract/nodes/match_last_line_node.rb +3 -2
- data/lib/refract/nodes/missing_node.rb +1 -0
- data/lib/refract/nodes/nil_node.rb +1 -0
- data/lib/refract/nodes/no_keywords_parameter_node.rb +1 -0
- data/lib/refract/nodes/numbered_parameters_node.rb +1 -0
- data/lib/refract/nodes/program_node.rb +4 -2
- data/lib/refract/nodes/redo_node.rb +1 -0
- data/lib/refract/nodes/regular_expression_node.rb +3 -2
- data/lib/refract/nodes/retry_node.rb +1 -0
- data/lib/refract/nodes/self_node.rb +1 -0
- data/lib/refract/nodes/shareable_constant_node.rb +3 -2
- data/lib/refract/nodes/source_encoding_node.rb +1 -0
- data/lib/refract/nodes/source_file_node.rb +1 -0
- data/lib/refract/nodes/source_line_node.rb +1 -0
- data/lib/refract/nodes/true_node.rb +1 -0
- data/lib/refract/version.rb +1 -1
- data/lib/refract/visitor.rb +3 -1
- data/lib/refract.rb +27 -0
- metadata +2 -2
data/lib/refract/formatter.rb
CHANGED
|
@@ -4,20 +4,77 @@ module Refract
|
|
|
4
4
|
class Formatter < BasicVisitor
|
|
5
5
|
Result = Data.define(:source, :source_map)
|
|
6
6
|
|
|
7
|
+
IDENTIFIER = /(?:[A-Za-z_]|[^\x00-\x7F])(?:\w|[^\x00-\x7F])*/
|
|
8
|
+
BARE_IDENTIFIER = /\A#{IDENTIFIER}\z/
|
|
9
|
+
SETTER = /\A#{IDENTIFIER}=\z/
|
|
10
|
+
LABEL = /\A#{IDENTIFIER}[?!]?\z/
|
|
11
|
+
BARE_SYMBOL = %r{
|
|
12
|
+
\A(?:
|
|
13
|
+
#{IDENTIFIER}[?!=]?
|
|
14
|
+
| @@?#{IDENTIFIER}
|
|
15
|
+
| \$(?:#{IDENTIFIER}|-\w|\d+|[~*$?!@/\\;,.=:<>"&`'+])
|
|
16
|
+
| \[\]=? | [+-]@? | [~!] | \*\*? | [/%&|^`] | <=> | ===? | =~ | !~ | != | <[<=]? | >[>=]?
|
|
17
|
+
)\z
|
|
18
|
+
}x
|
|
19
|
+
|
|
20
|
+
STRING_ESCAPES = {
|
|
21
|
+
"\n" => "\\n",
|
|
22
|
+
"\t" => "\\t",
|
|
23
|
+
"\r" => "\\r",
|
|
24
|
+
"\f" => "\\f",
|
|
25
|
+
"\v" => "\\v",
|
|
26
|
+
"\a" => "\\a",
|
|
27
|
+
"\b" => "\\b",
|
|
28
|
+
"\e" => "\\e",
|
|
29
|
+
}.freeze
|
|
30
|
+
|
|
31
|
+
# In a regexp, `#` only needs escaping where it would start interpolation.
|
|
32
|
+
# The end of the source counts, since the closing `/` can form `$/`.
|
|
33
|
+
REGEXP_INTERPOLATION = %r{#(?=\{|@@?(?:[A-Za-z_]|[^\x00-\x7F])|\$(?:[A-Za-z_~*$?!@/\\;,.=:<>"&`'+\d]|-\w|[^\x00-\x7F]|\z))}
|
|
34
|
+
|
|
35
|
+
ESCAPE_PATTERNS = {
|
|
36
|
+
'"' => /[\\"]|#(?=[{@$])|[^[:print:]]/,
|
|
37
|
+
"`" => /[\\`]|#(?=[{@$])|[^[:print:]]/,
|
|
38
|
+
}.freeze
|
|
39
|
+
|
|
40
|
+
NON_ASCII = /[^\x00-\x7F]/
|
|
41
|
+
|
|
42
|
+
REGEXP_ENCODINGS = {
|
|
43
|
+
ascii_8bit: "n",
|
|
44
|
+
euc_jp: "e",
|
|
45
|
+
windows_31j: "s",
|
|
46
|
+
utf_8: "u",
|
|
47
|
+
}.freeze
|
|
48
|
+
|
|
7
49
|
def initialize(starting_line: 1)
|
|
8
50
|
super()
|
|
9
51
|
@buffer = []
|
|
10
52
|
@source_map = []
|
|
53
|
+
@inferred_lines = Set.new
|
|
11
54
|
@current_line = starting_line
|
|
12
55
|
@indent = 0
|
|
56
|
+
@shareable_constant_value = :none
|
|
13
57
|
end
|
|
14
58
|
|
|
15
|
-
def around_visit(node)
|
|
16
|
-
|
|
17
|
-
|
|
59
|
+
def around_visit(node, &)
|
|
60
|
+
value = shareable_constant_value_within(node) if statement?(node)
|
|
61
|
+
|
|
62
|
+
if value && (value != :none || @shareable_constant_value != :none)
|
|
63
|
+
shareable_constant_value(value) { map_source_and_visit(node, &) }
|
|
64
|
+
else
|
|
65
|
+
map_source_and_visit(node, &)
|
|
18
66
|
end
|
|
67
|
+
end
|
|
19
68
|
|
|
20
|
-
|
|
69
|
+
def visit_each(nodes)
|
|
70
|
+
nodes = nodes.compact
|
|
71
|
+
last = nodes.length - 1
|
|
72
|
+
|
|
73
|
+
nodes.each_with_index do |node, index|
|
|
74
|
+
length = @buffer.length
|
|
75
|
+
visit node
|
|
76
|
+
yield if block_given? && index < last && @buffer.length > length
|
|
77
|
+
end
|
|
21
78
|
end
|
|
22
79
|
|
|
23
80
|
def format_node(node)
|
|
@@ -75,24 +132,23 @@ module Refract
|
|
|
75
132
|
visit ArrayPatternNode do |node|
|
|
76
133
|
visit node.constant
|
|
77
134
|
brackets do
|
|
78
|
-
visit_each(node.requireds) { push ", " }
|
|
79
|
-
visit(node.rest)
|
|
135
|
+
visit_each([*node.requireds, node.rest, *node.posts]) { push ", " }
|
|
80
136
|
end
|
|
81
137
|
end
|
|
82
138
|
|
|
83
139
|
visit AssocNode do |node|
|
|
84
140
|
case node.key
|
|
85
141
|
when SymbolNode
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
142
|
+
bare = !node.key.quoted && bare?(node.key.unescaped, LABEL)
|
|
143
|
+
push(bare ? node.key.unescaped : quote(node.key.unescaped))
|
|
144
|
+
push ":"
|
|
145
|
+
|
|
146
|
+
if ImplicitNode === node.value && bare && shorthand?(node.key, node.value.value)
|
|
147
|
+
visit node.value
|
|
90
148
|
else
|
|
91
|
-
|
|
149
|
+
space
|
|
150
|
+
visit((ImplicitNode === node.value) ? node.value.value : node.value)
|
|
92
151
|
end
|
|
93
|
-
push ":"
|
|
94
|
-
space unless ImplicitNode === node.value
|
|
95
|
-
visit node.value
|
|
96
152
|
else
|
|
97
153
|
visit node.key
|
|
98
154
|
push " => "
|
|
@@ -190,25 +246,33 @@ module Refract
|
|
|
190
246
|
end
|
|
191
247
|
|
|
192
248
|
visit CallAndWriteNode do |node|
|
|
193
|
-
|
|
194
|
-
visit node.receiver
|
|
195
|
-
push "."
|
|
196
|
-
end
|
|
197
|
-
|
|
249
|
+
call_receiver(node)
|
|
198
250
|
push node.read_name
|
|
199
|
-
|
|
200
|
-
push "&&="
|
|
201
|
-
space
|
|
251
|
+
push " &&= "
|
|
202
252
|
visit node.value
|
|
203
253
|
end
|
|
204
254
|
|
|
205
255
|
visit CallNode do |node|
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
256
|
+
arguments = node.arguments&.arguments
|
|
257
|
+
|
|
258
|
+
if node.attribute_write && node.block.nil? && arguments
|
|
259
|
+
if node.name == :[]= && !node.safe_navigation
|
|
260
|
+
*index, value = arguments
|
|
261
|
+
parenthesize(low_precedence?(node.receiver)) { visit node.receiver }
|
|
262
|
+
brackets { visit_each(index) { push ", " } }
|
|
263
|
+
push " = "
|
|
264
|
+
visit value
|
|
265
|
+
return
|
|
266
|
+
elsif node.name.match?(SETTER) && arguments.length == 1
|
|
267
|
+
call_receiver(node)
|
|
268
|
+
push node.name.name.delete_suffix("=")
|
|
269
|
+
push " = "
|
|
270
|
+
visit arguments.first
|
|
271
|
+
return
|
|
272
|
+
end
|
|
210
273
|
end
|
|
211
274
|
|
|
275
|
+
call_receiver(node)
|
|
212
276
|
push node.name
|
|
213
277
|
|
|
214
278
|
case node.block
|
|
@@ -231,30 +295,23 @@ module Refract
|
|
|
231
295
|
else
|
|
232
296
|
if node.arguments
|
|
233
297
|
parens { visit node.arguments }
|
|
298
|
+
elsif !node.receiver && !node.variable_call && node.name.match?(BARE_IDENTIFIER)
|
|
299
|
+
push "()"
|
|
234
300
|
end
|
|
235
301
|
end
|
|
236
302
|
end
|
|
237
303
|
|
|
238
304
|
visit CallOperatorWriteNode do |node|
|
|
239
|
-
|
|
240
|
-
visit node.receiver
|
|
241
|
-
push "."
|
|
242
|
-
end
|
|
243
|
-
|
|
305
|
+
call_receiver(node)
|
|
244
306
|
push node.read_name
|
|
245
307
|
space
|
|
246
308
|
push node.binary_operator
|
|
247
|
-
push "="
|
|
248
|
-
space
|
|
309
|
+
push "= "
|
|
249
310
|
visit node.value
|
|
250
311
|
end
|
|
251
312
|
|
|
252
313
|
visit CallOrWriteNode do |node|
|
|
253
|
-
|
|
254
|
-
visit node.receiver
|
|
255
|
-
push "."
|
|
256
|
-
end
|
|
257
|
-
|
|
314
|
+
call_receiver(node)
|
|
258
315
|
push node.read_name
|
|
259
316
|
push " ||= "
|
|
260
317
|
visit node.value
|
|
@@ -574,10 +631,17 @@ module Refract
|
|
|
574
631
|
end
|
|
575
632
|
|
|
576
633
|
visit HashPatternNode do |node|
|
|
577
|
-
|
|
578
|
-
|
|
579
|
-
|
|
580
|
-
|
|
634
|
+
if node.constant
|
|
635
|
+
visit node.constant
|
|
636
|
+
brackets do
|
|
637
|
+
visit_each([*node.elements, node.rest]) { push ", " }
|
|
638
|
+
end
|
|
639
|
+
else
|
|
640
|
+
braces do
|
|
641
|
+
space
|
|
642
|
+
visit_each([*node.elements, node.rest]) { push ", " }
|
|
643
|
+
space
|
|
644
|
+
end
|
|
581
645
|
end
|
|
582
646
|
end
|
|
583
647
|
|
|
@@ -586,13 +650,8 @@ module Refract
|
|
|
586
650
|
visit node.statements
|
|
587
651
|
push " if "
|
|
588
652
|
visit node.predicate
|
|
589
|
-
|
|
590
|
-
|
|
591
|
-
if IfNode === @stack[-2]
|
|
592
|
-
push "elsif "
|
|
593
|
-
else
|
|
594
|
-
push "if "
|
|
595
|
-
end
|
|
653
|
+
elsif (IfNode === @stack[-2]) && @stack[-2].subsequent.equal?(node)
|
|
654
|
+
push "elsif "
|
|
596
655
|
visit node.predicate
|
|
597
656
|
if node.statements
|
|
598
657
|
indent do
|
|
@@ -603,7 +662,19 @@ module Refract
|
|
|
603
662
|
new_line
|
|
604
663
|
visit node.subsequent
|
|
605
664
|
end
|
|
606
|
-
|
|
665
|
+
else
|
|
666
|
+
parenthesize(modifier_ambiguous?) do
|
|
667
|
+
push "if "
|
|
668
|
+
visit node.predicate
|
|
669
|
+
if node.statements
|
|
670
|
+
indent do
|
|
671
|
+
visit node.statements
|
|
672
|
+
end
|
|
673
|
+
end
|
|
674
|
+
if node.subsequent
|
|
675
|
+
new_line
|
|
676
|
+
visit node.subsequent
|
|
677
|
+
end
|
|
607
678
|
new_line
|
|
608
679
|
push "end"
|
|
609
680
|
end
|
|
@@ -706,70 +777,34 @@ module Refract
|
|
|
706
777
|
|
|
707
778
|
visit InterpolatedMatchLastLineNode do |node|
|
|
708
779
|
push "/"
|
|
709
|
-
node.parts
|
|
710
|
-
case part
|
|
711
|
-
when StringNode
|
|
712
|
-
push part.unescaped.gsub("/", "\\/")
|
|
713
|
-
else
|
|
714
|
-
visit part
|
|
715
|
-
end
|
|
716
|
-
end
|
|
780
|
+
visit_parts(node.parts) { |string| push escape_regexp(string) }
|
|
717
781
|
push "/"
|
|
782
|
+
regexp_flags(node)
|
|
718
783
|
end
|
|
719
784
|
|
|
720
785
|
visit InterpolatedRegularExpressionNode do |node|
|
|
721
786
|
push "/"
|
|
722
|
-
node.parts
|
|
723
|
-
case part
|
|
724
|
-
when StringNode
|
|
725
|
-
push part.unescaped.gsub("/", "\\/")
|
|
726
|
-
else
|
|
727
|
-
visit part
|
|
728
|
-
end
|
|
729
|
-
end
|
|
787
|
+
visit_parts(node.parts) { |string| push escape_regexp(string) }
|
|
730
788
|
push "/"
|
|
731
|
-
|
|
732
|
-
push "m" if node.multi_line
|
|
733
|
-
push "x" if node.extended
|
|
734
|
-
push "o" if node.once
|
|
789
|
+
regexp_flags(node)
|
|
735
790
|
end
|
|
736
791
|
|
|
737
792
|
visit InterpolatedStringNode do |node|
|
|
738
793
|
doubles do
|
|
739
|
-
node.parts
|
|
740
|
-
case part
|
|
741
|
-
when StringNode
|
|
742
|
-
push part.unescaped.gsub('"', '\"')
|
|
743
|
-
else
|
|
744
|
-
visit part
|
|
745
|
-
end
|
|
746
|
-
end
|
|
794
|
+
visit_parts(node.parts) { |string| push escape_string(string, '"') }
|
|
747
795
|
end
|
|
748
796
|
end
|
|
749
797
|
|
|
750
798
|
visit InterpolatedSymbolNode do |node|
|
|
751
|
-
push
|
|
752
|
-
|
|
753
|
-
|
|
754
|
-
when StringNode
|
|
755
|
-
push part.unescaped.gsub('"', '\"')
|
|
756
|
-
else
|
|
757
|
-
visit part
|
|
758
|
-
end
|
|
799
|
+
push ":"
|
|
800
|
+
doubles do
|
|
801
|
+
visit_parts(node.parts) { |string| push escape_string(string, '"') }
|
|
759
802
|
end
|
|
760
|
-
push '"'
|
|
761
803
|
end
|
|
762
804
|
|
|
763
805
|
visit InterpolatedXStringNode do |node|
|
|
764
806
|
push "`"
|
|
765
|
-
node.parts
|
|
766
|
-
case part
|
|
767
|
-
when StringNode
|
|
768
|
-
push part.unescaped.gsub("`", "\\`")
|
|
769
|
-
else
|
|
770
|
-
visit part
|
|
771
|
-
end
|
|
772
|
-
end
|
|
807
|
+
visit_parts(node.parts) { |string| push escape_string(string, "`") }
|
|
773
808
|
push "`"
|
|
774
809
|
end
|
|
775
810
|
|
|
@@ -853,12 +888,9 @@ module Refract
|
|
|
853
888
|
|
|
854
889
|
visit MatchLastLineNode do |node|
|
|
855
890
|
push "/"
|
|
856
|
-
push node.unescaped
|
|
891
|
+
push escape_regexp(node.unescaped)
|
|
857
892
|
push "/"
|
|
858
|
-
|
|
859
|
-
push "m" if node.multi_line
|
|
860
|
-
push "x" if node.extended
|
|
861
|
-
push "o" if node.once
|
|
893
|
+
regexp_flags(node)
|
|
862
894
|
end
|
|
863
895
|
|
|
864
896
|
visit MatchPredicateNode do |node|
|
|
@@ -874,8 +906,9 @@ module Refract
|
|
|
874
906
|
end
|
|
875
907
|
|
|
876
908
|
visit MatchWriteNode do |node|
|
|
877
|
-
visit node.call
|
|
878
|
-
|
|
909
|
+
visit node.call.receiver
|
|
910
|
+
push " =~ "
|
|
911
|
+
visit node.call.arguments
|
|
879
912
|
end
|
|
880
913
|
|
|
881
914
|
visit MissingNode do |node|
|
|
@@ -896,7 +929,9 @@ module Refract
|
|
|
896
929
|
end
|
|
897
930
|
|
|
898
931
|
visit MultiTargetNode do |node|
|
|
899
|
-
|
|
932
|
+
parenthesize(!(ForNode === @stack[-2])) do
|
|
933
|
+
visit_each([*node.lefts, node.rest, *node.rights]) { push ", " }
|
|
934
|
+
end
|
|
900
935
|
end
|
|
901
936
|
|
|
902
937
|
visit MultiWriteNode do |node|
|
|
@@ -998,6 +1033,18 @@ module Refract
|
|
|
998
1033
|
end
|
|
999
1034
|
|
|
1000
1035
|
visit ProgramNode do |node|
|
|
1036
|
+
@escape_non_ascii = node.encoding && node.encoding != Encoding::UTF_8
|
|
1037
|
+
|
|
1038
|
+
if node.encoding && node.encoding != Encoding::UTF_8
|
|
1039
|
+
push "# encoding: #{node.encoding.name}"
|
|
1040
|
+
new_line
|
|
1041
|
+
end
|
|
1042
|
+
|
|
1043
|
+
unless node.frozen_string_literal.nil?
|
|
1044
|
+
push "# frozen_string_literal: #{node.frozen_string_literal}"
|
|
1045
|
+
new_line
|
|
1046
|
+
end
|
|
1047
|
+
|
|
1001
1048
|
visit node.statements
|
|
1002
1049
|
end
|
|
1003
1050
|
|
|
@@ -1008,12 +1055,7 @@ module Refract
|
|
|
1008
1055
|
end
|
|
1009
1056
|
|
|
1010
1057
|
visit RationalNode do |node|
|
|
1011
|
-
|
|
1012
|
-
push node.numerator.to_s
|
|
1013
|
-
push "r"
|
|
1014
|
-
else
|
|
1015
|
-
push "#{(node.numerator.to_f / node.denominator)}r"
|
|
1016
|
-
end
|
|
1058
|
+
push rational(node.numerator, node.denominator)
|
|
1017
1059
|
end
|
|
1018
1060
|
|
|
1019
1061
|
visit RedoNode do |node|
|
|
@@ -1022,12 +1064,9 @@ module Refract
|
|
|
1022
1064
|
|
|
1023
1065
|
visit RegularExpressionNode do |node|
|
|
1024
1066
|
push "/"
|
|
1025
|
-
push node.unescaped
|
|
1067
|
+
push escape_regexp(node.unescaped)
|
|
1026
1068
|
push "/"
|
|
1027
|
-
|
|
1028
|
-
push "m" if node.multi_line
|
|
1029
|
-
push "x" if node.extended
|
|
1030
|
-
push "o" if node.once
|
|
1069
|
+
regexp_flags(node)
|
|
1031
1070
|
end
|
|
1032
1071
|
|
|
1033
1072
|
visit RequiredKeywordParameterNode do |node|
|
|
@@ -1091,7 +1130,11 @@ module Refract
|
|
|
1091
1130
|
end
|
|
1092
1131
|
|
|
1093
1132
|
visit ShareableConstantNode do |node|
|
|
1094
|
-
|
|
1133
|
+
if @stack.length == 1
|
|
1134
|
+
shareable_constant_value(node.value) { visit node.write }
|
|
1135
|
+
else
|
|
1136
|
+
visit node.write
|
|
1137
|
+
end
|
|
1095
1138
|
end
|
|
1096
1139
|
|
|
1097
1140
|
visit SingletonClassNode do |node|
|
|
@@ -1117,7 +1160,7 @@ module Refract
|
|
|
1117
1160
|
end
|
|
1118
1161
|
|
|
1119
1162
|
visit SourceLineNode do |node|
|
|
1120
|
-
push "__LINE__"
|
|
1163
|
+
push node.start_line&.to_s || "__LINE__"
|
|
1121
1164
|
end
|
|
1122
1165
|
|
|
1123
1166
|
visit SplatNode do |node|
|
|
@@ -1126,47 +1169,30 @@ module Refract
|
|
|
1126
1169
|
end
|
|
1127
1170
|
|
|
1128
1171
|
visit StatementsNode do |node|
|
|
1129
|
-
visit_each(node.body
|
|
1172
|
+
visit_each(flatten_statements(node.body)) { new_line }
|
|
1130
1173
|
end
|
|
1131
1174
|
|
|
1132
1175
|
visit StringNode do |node|
|
|
1133
|
-
|
|
1134
|
-
push node.unescaped.gsub('"', '\"')
|
|
1135
|
-
end
|
|
1176
|
+
push quote(node.unescaped)
|
|
1136
1177
|
end
|
|
1137
1178
|
|
|
1138
1179
|
visit SuperNode do |node|
|
|
1139
1180
|
push "super"
|
|
1140
|
-
case node.block
|
|
1141
|
-
when BlockNode
|
|
1142
|
-
if node.arguments
|
|
1143
|
-
parens { visit node.arguments }
|
|
1144
|
-
end
|
|
1145
1181
|
|
|
1182
|
+
parens do
|
|
1183
|
+
visit_each([node.arguments, (node.block if BlockArgumentNode === node.block)]) { push ", " }
|
|
1184
|
+
end
|
|
1185
|
+
|
|
1186
|
+
if BlockNode === node.block
|
|
1146
1187
|
space
|
|
1147
1188
|
visit node.block
|
|
1148
|
-
when BlockArgumentNode
|
|
1149
|
-
parens do
|
|
1150
|
-
if node.arguments
|
|
1151
|
-
visit node.arguments
|
|
1152
|
-
push ", "
|
|
1153
|
-
end
|
|
1154
|
-
|
|
1155
|
-
visit node.block
|
|
1156
|
-
end
|
|
1157
|
-
else
|
|
1158
|
-
if node.arguments
|
|
1159
|
-
parens { visit node.arguments }
|
|
1160
|
-
end
|
|
1161
1189
|
end
|
|
1162
1190
|
end
|
|
1163
1191
|
|
|
1164
1192
|
visit SymbolNode do |node|
|
|
1165
1193
|
push ":"
|
|
1166
|
-
if node.quoted
|
|
1167
|
-
|
|
1168
|
-
push node.unescaped.gsub('"', '\"')
|
|
1169
|
-
end
|
|
1194
|
+
if node.quoted || !bare?(node.unescaped, BARE_SYMBOL)
|
|
1195
|
+
push quote(node.unescaped)
|
|
1170
1196
|
else
|
|
1171
1197
|
push node.unescaped
|
|
1172
1198
|
end
|
|
@@ -1188,22 +1214,24 @@ module Refract
|
|
|
1188
1214
|
push " unless "
|
|
1189
1215
|
visit node.predicate
|
|
1190
1216
|
else
|
|
1191
|
-
|
|
1192
|
-
|
|
1217
|
+
parenthesize(modifier_ambiguous?) do
|
|
1218
|
+
push "unless "
|
|
1219
|
+
visit node.predicate
|
|
1220
|
+
|
|
1221
|
+
if node.statements
|
|
1222
|
+
indent do
|
|
1223
|
+
visit node.statements
|
|
1224
|
+
end
|
|
1225
|
+
end
|
|
1193
1226
|
|
|
1194
|
-
|
|
1195
|
-
|
|
1196
|
-
visit node.
|
|
1227
|
+
if node.else_clause
|
|
1228
|
+
new_line
|
|
1229
|
+
visit node.else_clause
|
|
1197
1230
|
end
|
|
1198
|
-
end
|
|
1199
1231
|
|
|
1200
|
-
if node.else_clause
|
|
1201
1232
|
new_line
|
|
1202
|
-
|
|
1233
|
+
push "end"
|
|
1203
1234
|
end
|
|
1204
|
-
|
|
1205
|
-
new_line
|
|
1206
|
-
push "end"
|
|
1207
1235
|
end
|
|
1208
1236
|
end
|
|
1209
1237
|
|
|
@@ -1213,15 +1241,17 @@ module Refract
|
|
|
1213
1241
|
push " until "
|
|
1214
1242
|
visit node.predicate
|
|
1215
1243
|
else
|
|
1216
|
-
|
|
1217
|
-
|
|
1218
|
-
|
|
1219
|
-
|
|
1220
|
-
|
|
1244
|
+
parenthesize(modifier_ambiguous?) do
|
|
1245
|
+
push "until "
|
|
1246
|
+
visit node.predicate
|
|
1247
|
+
if node.statements
|
|
1248
|
+
indent do
|
|
1249
|
+
visit node.statements
|
|
1250
|
+
end
|
|
1221
1251
|
end
|
|
1252
|
+
new_line
|
|
1253
|
+
push "end"
|
|
1222
1254
|
end
|
|
1223
|
-
new_line
|
|
1224
|
-
push "end"
|
|
1225
1255
|
end
|
|
1226
1256
|
end
|
|
1227
1257
|
|
|
@@ -1240,21 +1270,23 @@ module Refract
|
|
|
1240
1270
|
push " while "
|
|
1241
1271
|
visit node.predicate
|
|
1242
1272
|
else
|
|
1243
|
-
|
|
1244
|
-
|
|
1245
|
-
|
|
1246
|
-
|
|
1247
|
-
|
|
1273
|
+
parenthesize(modifier_ambiguous?) do
|
|
1274
|
+
push "while "
|
|
1275
|
+
visit node.predicate
|
|
1276
|
+
if node.statements
|
|
1277
|
+
indent do
|
|
1278
|
+
visit node.statements
|
|
1279
|
+
end
|
|
1248
1280
|
end
|
|
1281
|
+
new_line
|
|
1282
|
+
push "end"
|
|
1249
1283
|
end
|
|
1250
|
-
new_line
|
|
1251
|
-
push "end"
|
|
1252
1284
|
end
|
|
1253
1285
|
end
|
|
1254
1286
|
|
|
1255
1287
|
visit XStringNode do |node|
|
|
1256
1288
|
push "`"
|
|
1257
|
-
push node.unescaped
|
|
1289
|
+
push escape_string(node.unescaped, "`")
|
|
1258
1290
|
push "`"
|
|
1259
1291
|
end
|
|
1260
1292
|
|
|
@@ -1275,6 +1307,7 @@ module Refract
|
|
|
1275
1307
|
value.name
|
|
1276
1308
|
end
|
|
1277
1309
|
|
|
1310
|
+
@current_line += string.count("\n") if string
|
|
1278
1311
|
@buffer << string
|
|
1279
1312
|
end
|
|
1280
1313
|
|
|
@@ -1333,5 +1366,205 @@ module Refract
|
|
|
1333
1366
|
yield
|
|
1334
1367
|
push '"'
|
|
1335
1368
|
end
|
|
1369
|
+
|
|
1370
|
+
private def rational(numerator, denominator)
|
|
1371
|
+
rest, twos, fives = denominator, 0, 0
|
|
1372
|
+
(rest /= 2) && (twos += 1) while rest.even?
|
|
1373
|
+
(rest /= 5) && (fives += 1) while (rest % 5).zero?
|
|
1374
|
+
return "(#{numerator}r/#{denominator})" unless rest == 1
|
|
1375
|
+
|
|
1376
|
+
places = [twos, fives].max
|
|
1377
|
+
digits = (numerator.abs * (10 ** places) / denominator).to_s.rjust(places + 1, "0")
|
|
1378
|
+
sign = numerator.negative? ? "-" : ""
|
|
1379
|
+
|
|
1380
|
+
if places.zero?
|
|
1381
|
+
"#{sign}#{digits}r"
|
|
1382
|
+
else
|
|
1383
|
+
"#{sign}#{digits[0...-places]}.#{digits[-places..]}r"
|
|
1384
|
+
end
|
|
1385
|
+
end
|
|
1386
|
+
|
|
1387
|
+
private def shorthand?(key, value)
|
|
1388
|
+
case value
|
|
1389
|
+
when LocalVariableReadNode, LocalVariableTargetNode, ConstantReadNode
|
|
1390
|
+
value.name.name == key.unescaped
|
|
1391
|
+
when CallNode
|
|
1392
|
+
value.name.name == key.unescaped && !value.receiver && !value.arguments && !value.block
|
|
1393
|
+
else
|
|
1394
|
+
false
|
|
1395
|
+
end
|
|
1396
|
+
end
|
|
1397
|
+
|
|
1398
|
+
# The outermost located node that starts on a line claims it. A node
|
|
1399
|
+
# without a location only fills an unclaimed line, with the line of its
|
|
1400
|
+
# nearest located ancestor, until a located node starts there.
|
|
1401
|
+
private def map_source_and_visit(node)
|
|
1402
|
+
if (start_line = node.start_line)
|
|
1403
|
+
if @source_map[@current_line].nil? || @inferred_lines.delete?(@current_line)
|
|
1404
|
+
@source_map[@current_line] = start_line
|
|
1405
|
+
end
|
|
1406
|
+
elsif @source_map[@current_line].nil? && (start_line = @stack.reverse_each.lazy.filter_map(&:start_line).first)
|
|
1407
|
+
@source_map[@current_line] = start_line
|
|
1408
|
+
@inferred_lines << @current_line
|
|
1409
|
+
end
|
|
1410
|
+
|
|
1411
|
+
yield(node)
|
|
1412
|
+
end
|
|
1413
|
+
|
|
1414
|
+
# A statement on lines of its own, so a magic comment can go before and after it.
|
|
1415
|
+
private def statement?(node)
|
|
1416
|
+
StatementsNode === @stack[-2] && !inline_statements?(@stack[-3])
|
|
1417
|
+
end
|
|
1418
|
+
|
|
1419
|
+
private def inline_statements?(parent)
|
|
1420
|
+
case parent
|
|
1421
|
+
when IfNode, UnlessNode, WhileNode, UntilNode
|
|
1422
|
+
parent.inline
|
|
1423
|
+
when ParenthesesNode, EmbeddedStatementsNode
|
|
1424
|
+
true
|
|
1425
|
+
else
|
|
1426
|
+
false
|
|
1427
|
+
end
|
|
1428
|
+
end
|
|
1429
|
+
|
|
1430
|
+
private def shareable_constant_value_within(node)
|
|
1431
|
+
case node
|
|
1432
|
+
when ShareableConstantNode
|
|
1433
|
+
return node.value
|
|
1434
|
+
when ConstantWriteNode, ConstantOrWriteNode, ConstantAndWriteNode, ConstantOperatorWriteNode,
|
|
1435
|
+
ConstantPathWriteNode, ConstantPathOrWriteNode, ConstantPathAndWriteNode, ConstantPathOperatorWriteNode
|
|
1436
|
+
|
|
1437
|
+
return :none
|
|
1438
|
+
end
|
|
1439
|
+
|
|
1440
|
+
node.class.attributes.each do |name|
|
|
1441
|
+
Array(node.public_send(name)).each do |child|
|
|
1442
|
+
next unless Node === child
|
|
1443
|
+
next if StatementsNode === child && !inline_statements?(node)
|
|
1444
|
+
|
|
1445
|
+
value = shareable_constant_value_within(child)
|
|
1446
|
+
return value if value
|
|
1447
|
+
end
|
|
1448
|
+
end
|
|
1449
|
+
|
|
1450
|
+
nil
|
|
1451
|
+
end
|
|
1452
|
+
|
|
1453
|
+
private def shareable_constant_value(value)
|
|
1454
|
+
previous = @shareable_constant_value
|
|
1455
|
+
@shareable_constant_value = value
|
|
1456
|
+
|
|
1457
|
+
push "# shareable_constant_value: #{value}"
|
|
1458
|
+
new_line
|
|
1459
|
+
yield
|
|
1460
|
+
new_line
|
|
1461
|
+
push "# shareable_constant_value: #{previous}"
|
|
1462
|
+
ensure
|
|
1463
|
+
@shareable_constant_value = previous
|
|
1464
|
+
end
|
|
1465
|
+
|
|
1466
|
+
private def bare?(name, pattern)
|
|
1467
|
+
name.valid_encoding? && (name.ascii_only? || !@escape_non_ascii) && name.match?(pattern)
|
|
1468
|
+
end
|
|
1469
|
+
|
|
1470
|
+
private def low_precedence?(node)
|
|
1471
|
+
case node
|
|
1472
|
+
when AndNode, OrNode, RescueModifierNode, RangeNode, FlipFlopNode, MatchPredicateNode, MatchRequiredNode, ShareableConstantNode
|
|
1473
|
+
true
|
|
1474
|
+
when IfNode, UnlessNode, WhileNode, UntilNode
|
|
1475
|
+
node.inline
|
|
1476
|
+
when CallNode
|
|
1477
|
+
node.attribute_write
|
|
1478
|
+
else
|
|
1479
|
+
node.type.end_with?("write_node")
|
|
1480
|
+
end
|
|
1481
|
+
end
|
|
1482
|
+
|
|
1483
|
+
private def call_receiver(node)
|
|
1484
|
+
return unless node.receiver
|
|
1485
|
+
|
|
1486
|
+
parenthesize(low_precedence?(node.receiver)) { visit node.receiver }
|
|
1487
|
+
push "&" if node.safe_navigation
|
|
1488
|
+
push "."
|
|
1489
|
+
end
|
|
1490
|
+
|
|
1491
|
+
private def parenthesize(condition, &)
|
|
1492
|
+
condition ? parens(&) : yield
|
|
1493
|
+
end
|
|
1494
|
+
|
|
1495
|
+
# `return`, `break`, `next` and `rescue` are complete on their own, so a
|
|
1496
|
+
# keyword expression after them would be read as a modifier.
|
|
1497
|
+
private def modifier_ambiguous?
|
|
1498
|
+
case @stack[-2]
|
|
1499
|
+
when RescueNode
|
|
1500
|
+
true
|
|
1501
|
+
when RescueModifierNode
|
|
1502
|
+
@stack[-2].rescue_expression.equal?(@stack[-1])
|
|
1503
|
+
when ArgumentsNode
|
|
1504
|
+
ReturnNode === @stack[-3] || BreakNode === @stack[-3] || NextNode === @stack[-3]
|
|
1505
|
+
else
|
|
1506
|
+
false
|
|
1507
|
+
end
|
|
1508
|
+
end
|
|
1509
|
+
|
|
1510
|
+
private def quote(string)
|
|
1511
|
+
if string.include?('"') && string.valid_encoding? && (string.ascii_only? || !@escape_non_ascii) && !string.match?(/['\\]|[^[:print:]]/)
|
|
1512
|
+
"'#{string}'"
|
|
1513
|
+
else
|
|
1514
|
+
%("#{escape_string(string, '"')}")
|
|
1515
|
+
end
|
|
1516
|
+
end
|
|
1517
|
+
|
|
1518
|
+
private def escape_string(string, delimiter)
|
|
1519
|
+
string = string.b unless string.encoding == Encoding::UTF_8 && string.valid_encoding?
|
|
1520
|
+
|
|
1521
|
+
pattern = ESCAPE_PATTERNS.fetch(delimiter)
|
|
1522
|
+
pattern = Regexp.union(pattern, NON_ASCII) if @escape_non_ascii
|
|
1523
|
+
|
|
1524
|
+
string.gsub(pattern) do |char|
|
|
1525
|
+
if (escape = STRING_ESCAPES[char])
|
|
1526
|
+
escape
|
|
1527
|
+
elsif char.bytesize == 1 && char.match?(/[[:print:]]/)
|
|
1528
|
+
"\\#{char}"
|
|
1529
|
+
elsif char.bytesize == 1
|
|
1530
|
+
format("\\x%02X", char.ord)
|
|
1531
|
+
else
|
|
1532
|
+
format("\\u{%X}", char.ord)
|
|
1533
|
+
end
|
|
1534
|
+
end
|
|
1535
|
+
end
|
|
1536
|
+
|
|
1537
|
+
private def escape_regexp(string)
|
|
1538
|
+
string = string.b unless string.valid_encoding?
|
|
1539
|
+
string.gsub(%r{\\.|/|#{REGEXP_INTERPOLATION}}m) { |match| (match.length == 2) ? match : "\\#{match}" }
|
|
1540
|
+
end
|
|
1541
|
+
|
|
1542
|
+
private def regexp_flags(node)
|
|
1543
|
+
push "i" if node.ignore_case
|
|
1544
|
+
push "m" if node.multi_line
|
|
1545
|
+
push "x" if node.extended
|
|
1546
|
+
push "o" if node.once
|
|
1547
|
+
push REGEXP_ENCODINGS.fetch(node.encoding) if node.encoding
|
|
1548
|
+
end
|
|
1549
|
+
|
|
1550
|
+
private def visit_parts(parts)
|
|
1551
|
+
flatten_parts(parts)
|
|
1552
|
+
.chunk_while { |a, b| StringNode === a && StringNode === b }
|
|
1553
|
+
.each do |chunk|
|
|
1554
|
+
if StringNode === chunk.first
|
|
1555
|
+
yield chunk.map(&:unescaped).join
|
|
1556
|
+
else
|
|
1557
|
+
visit chunk.first
|
|
1558
|
+
end
|
|
1559
|
+
end
|
|
1560
|
+
end
|
|
1561
|
+
|
|
1562
|
+
private def flatten_statements(body)
|
|
1563
|
+
body.flat_map { |statement| (StatementsNode === statement) ? flatten_statements(statement.body) : statement }
|
|
1564
|
+
end
|
|
1565
|
+
|
|
1566
|
+
private def flatten_parts(parts)
|
|
1567
|
+
parts.flat_map { |part| (InterpolatedStringNode === part) ? flatten_parts(part.parts) : part }
|
|
1568
|
+
end
|
|
1336
1569
|
end
|
|
1337
1570
|
end
|