unparser 0.1.3 → 0.1.4
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/Changelog.md +5 -0
- data/Gemfile +1 -1
- data/Gemfile.devtools +5 -5
- data/README.md +1 -1
- data/bin/test-unparser +6 -6
- data/config/flay.yml +1 -1
- data/config/reek.yml +4 -5
- data/config/rubocop.yml +88 -0
- data/lib/unparser.rb +2 -0
- data/lib/unparser/buffer.rb +5 -3
- data/lib/unparser/comments.rb +18 -16
- data/lib/unparser/constants.rb +0 -1
- data/lib/unparser/emitter.rb +11 -39
- data/lib/unparser/emitter/assignment.rb +1 -2
- data/lib/unparser/emitter/binary.rb +2 -2
- data/lib/unparser/emitter/break.rb +1 -1
- data/lib/unparser/emitter/flipflop.rb +2 -2
- data/lib/unparser/emitter/hookexe.rb +2 -2
- data/lib/unparser/emitter/literal/dynamic_body.rb +5 -5
- data/lib/unparser/emitter/literal/primitive.rb +1 -1
- data/lib/unparser/emitter/literal/range.rb +2 -2
- data/lib/unparser/emitter/next.rb +1 -1
- data/lib/unparser/emitter/op_assign.rb +2 -2
- data/lib/unparser/emitter/repetition.rb +4 -4
- data/spec/unit/unparser/comments/skip_eol_comment_spec.rb +2 -2
- data/spec/unit/unparser/emitter/class_methods/handle_spec.rb +1 -1
- data/spec/unit/unparser_spec.rb +135 -134
- data/unparser.gemspec +6 -6
- metadata +9 -10
- data/spec/unit/unparser/emitter/source_map/class_methods/emit_spec.rb +0 -20
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: bd330877f92585ef66cfd283dba22e1d6202446c
|
4
|
+
data.tar.gz: ec9dbaaf5b7e460bc19e2f6564e25e016b4149c8
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 2ac84d618bb92e5b9ec2f8360378c820f55545f993532febac3b5b4715cf85e16ccc88271fad3a2c6a8efa34e5297432799b0cc31481396807799c415ecab7b0
|
7
|
+
data.tar.gz: 45bc61077ae22a0c74538fda91621319ea99ace383338a5e6a6f90f66eb5e02ed88938f07f2d5f95999b1ed2372f68c8ddddc9009b23ea1df493854d6d52de90
|
data/Changelog.md
CHANGED
data/Gemfile
CHANGED
data/Gemfile.devtools
CHANGED
@@ -7,7 +7,7 @@ group :development do
|
|
7
7
|
end
|
8
8
|
|
9
9
|
group :yard do
|
10
|
-
gem 'kramdown', '~> 1.
|
10
|
+
gem 'kramdown', '~> 1.2.0'
|
11
11
|
end
|
12
12
|
|
13
13
|
group :guard do
|
@@ -15,7 +15,7 @@ group :guard do
|
|
15
15
|
gem 'guard-bundler', '~> 1.0.0'
|
16
16
|
gem 'guard-rspec', '~> 3.0.2'
|
17
17
|
gem 'guard-rubocop', '~> 0.2.0'
|
18
|
-
|
18
|
+
gem 'guard-mutant', '~> 0.0.1'
|
19
19
|
|
20
20
|
# file system change event handling
|
21
21
|
gem 'listen', '~> 1.3.0'
|
@@ -30,16 +30,16 @@ group :guard do
|
|
30
30
|
end
|
31
31
|
|
32
32
|
group :metrics do
|
33
|
-
gem 'coveralls', '~> 0.
|
33
|
+
gem 'coveralls', '~> 0.7.0'
|
34
34
|
gem 'flay', '~> 2.4.0'
|
35
35
|
gem 'flog', '~> 4.1.1'
|
36
36
|
gem 'reek', '~> 1.3.2'
|
37
|
-
gem 'rubocop', '~> 0.
|
37
|
+
gem 'rubocop', '~> 0.14.1'
|
38
38
|
gem 'simplecov', '~> 0.7.1'
|
39
39
|
gem 'yardstick', '~> 0.9.7', git: 'https://github.com/dkubb/yardstick.git'
|
40
40
|
|
41
41
|
platforms :ruby_19, :ruby_20 do
|
42
|
-
|
42
|
+
gem 'mutant', git: 'https://github.com/mbj/mutant.git'
|
43
43
|
gem 'yard-spellcheck', '~> 0.1.5'
|
44
44
|
end
|
45
45
|
end
|
data/README.md
CHANGED
@@ -58,7 +58,7 @@ People
|
|
58
58
|
------
|
59
59
|
|
60
60
|
* [Markus Schirp (mbj)](https://github.com/mbj) Author
|
61
|
-
* [Trent Ogren](https://github.com/misfo) Adding comment reproduction
|
61
|
+
* [Trent Ogren (misfo)](https://github.com/misfo) Adding comment reproduction
|
62
62
|
|
63
63
|
Contributing
|
64
64
|
-------------
|
data/bin/test-unparser
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
#!/usr/bin/env ruby
|
2
2
|
|
3
3
|
trap('INT') do |status|
|
4
|
-
exit! 128+status
|
4
|
+
exit! 128 + status
|
5
5
|
end
|
6
6
|
|
7
7
|
require 'unparser'
|
@@ -13,14 +13,14 @@ ARGV.each do |file|
|
|
13
13
|
generated = Unparser.unparse(node)
|
14
14
|
unparsed = Parser::CurrentRuby.parse(generated)
|
15
15
|
unless unparsed == node
|
16
|
-
$stderr.puts
|
16
|
+
$stderr.puts 'Node:'
|
17
17
|
$stderr.puts node.inspect
|
18
|
-
$stderr.puts
|
18
|
+
$stderr.puts 'Unparsed-Node:'
|
19
19
|
$stderr.puts unparsed.inspect
|
20
|
-
$stderr.puts
|
20
|
+
$stderr.puts 'Original:'
|
21
21
|
$stderr.puts source
|
22
|
-
$stderr.puts
|
22
|
+
$stderr.puts 'Generated:'
|
23
23
|
$stderr.puts generated
|
24
|
-
fail
|
24
|
+
fail 'BUG!'
|
25
25
|
end
|
26
26
|
end
|
data/config/flay.yml
CHANGED
data/config/reek.yml
CHANGED
@@ -63,11 +63,7 @@ NestedIterators:
|
|
63
63
|
enabled: true
|
64
64
|
max_allowed_nesting: 1
|
65
65
|
TooManyStatements:
|
66
|
-
max_statements:
|
67
|
-
exclude:
|
68
|
-
- Unparser::Emitter::Block#dispatch # 6 statements
|
69
|
-
- Unparser::Emitter::Def#dispatch # 6 statements
|
70
|
-
- Unparser::Emitter::Class#dispatch # 6 statements
|
66
|
+
max_statements: 6
|
71
67
|
enabled: true
|
72
68
|
DuplicateMethodCall:
|
73
69
|
allow_calls: []
|
@@ -91,6 +87,8 @@ UncommunicativeVariableName:
|
|
91
87
|
- !ruby/regexp /[A-Z]/
|
92
88
|
RepeatedConditional:
|
93
89
|
exclude:
|
90
|
+
- Unparser::Emitter # FIXME
|
91
|
+
- Unparser::Comments # FIXME
|
94
92
|
- Unparser::Emitter::Send # TODO Fixme
|
95
93
|
- Unparser::Emitter::If
|
96
94
|
enabled: true
|
@@ -103,6 +101,7 @@ DataClump:
|
|
103
101
|
ControlParameter:
|
104
102
|
exclude:
|
105
103
|
- Unparser::Emitter#emit_body # false positive
|
104
|
+
- Unparser::Emitter#conditional_parentheses # Intentionally a control structure like helper
|
106
105
|
enabled: true
|
107
106
|
NilCheck:
|
108
107
|
enabled: false
|
data/config/rubocop.yml
ADDED
@@ -0,0 +1,88 @@
|
|
1
|
+
AllCops:
|
2
|
+
Includes:
|
3
|
+
- '**/*.rake'
|
4
|
+
- 'Gemfile'
|
5
|
+
- 'Gemfile.triage'
|
6
|
+
Excludes:
|
7
|
+
- '**/vendor/**'
|
8
|
+
- '**/benchmarks/**'
|
9
|
+
|
10
|
+
# Avoid parameter lists longer than five parameters.
|
11
|
+
ParameterLists:
|
12
|
+
Max: 3
|
13
|
+
CountKeywordArgs: true
|
14
|
+
|
15
|
+
MethodLength:
|
16
|
+
CountComments: false
|
17
|
+
Max: 15
|
18
|
+
|
19
|
+
# Avoid more than `Max` levels of nesting.
|
20
|
+
BlockNesting:
|
21
|
+
Max: 3
|
22
|
+
|
23
|
+
# Align with the style guide.
|
24
|
+
CollectionMethods:
|
25
|
+
PreferredMethods:
|
26
|
+
collect: 'map'
|
27
|
+
inject: 'reduce'
|
28
|
+
find: 'detect'
|
29
|
+
find_all: 'select'
|
30
|
+
|
31
|
+
# Do not force public/protected/private keyword to be indented at the same
|
32
|
+
# level as the def keyword. My personal preference is to outdent these keywords
|
33
|
+
# because I think when scanning code it makes it easier to identify the
|
34
|
+
# sections of code and visually separate them. When the keyword is at the same
|
35
|
+
# level I think it sort of blends in with the def keywords and makes it harder
|
36
|
+
# to scan the code and see where the sections are.
|
37
|
+
AccessControl:
|
38
|
+
Enabled: false
|
39
|
+
|
40
|
+
# Limit line length
|
41
|
+
LineLength:
|
42
|
+
Max: 113 # TODO: lower to 79 once the rubocop branch in shared/Gemfile is removed
|
43
|
+
|
44
|
+
# Disabled because of indenting with private keyword in class bodies.
|
45
|
+
IndentationWidth:
|
46
|
+
Enabled: false
|
47
|
+
|
48
|
+
# I like raise more
|
49
|
+
SignalException:
|
50
|
+
Enabled: false
|
51
|
+
|
52
|
+
# Disable Symbol cop because unparse has to prodice "WIERD" symbols in his specs
|
53
|
+
#
|
54
|
+
# I'd love I could ignore a specific occurance.
|
55
|
+
#
|
56
|
+
SymbolName:
|
57
|
+
Enabled: false
|
58
|
+
|
59
|
+
# False positive in unparser source
|
60
|
+
OneLineConditional:
|
61
|
+
Enabled: false
|
62
|
+
|
63
|
+
Documentation:
|
64
|
+
Enabled: false
|
65
|
+
|
66
|
+
# Disable documentation checking until a class needs to be documented once
|
67
|
+
Documentation:
|
68
|
+
Enabled: false
|
69
|
+
|
70
|
+
# Do not favor modifier if/unless usage when you have a single-line body
|
71
|
+
IfUnlessModifier:
|
72
|
+
Enabled: false
|
73
|
+
|
74
|
+
# Allow case equality operator (in limited use within the specs)
|
75
|
+
CaseEquality:
|
76
|
+
Enabled: false
|
77
|
+
|
78
|
+
# Constants do not always have to use SCREAMING_SNAKE_CASE
|
79
|
+
ConstantName:
|
80
|
+
Enabled: false
|
81
|
+
|
82
|
+
# Not all trivial readers/writers can be defined with attr_* methods
|
83
|
+
TrivialAccessors:
|
84
|
+
Enabled: false
|
85
|
+
|
86
|
+
# Do not prefer do/end over {} for multiline blocks
|
87
|
+
Blocks:
|
88
|
+
Enabled: false
|
data/lib/unparser.rb
CHANGED
data/lib/unparser/buffer.rb
CHANGED
@@ -52,7 +52,7 @@ module Unparser
|
|
52
52
|
# @api private
|
53
53
|
#
|
54
54
|
def indent
|
55
|
-
@indent+=1
|
55
|
+
@indent += 1
|
56
56
|
self
|
57
57
|
end
|
58
58
|
|
@@ -63,7 +63,7 @@ module Unparser
|
|
63
63
|
# @api private
|
64
64
|
#
|
65
65
|
def unindent
|
66
|
-
@indent-=1
|
66
|
+
@indent -= 1
|
67
67
|
self
|
68
68
|
end
|
69
69
|
|
@@ -116,6 +116,8 @@ module Unparser
|
|
116
116
|
|
117
117
|
private
|
118
118
|
|
119
|
+
INDENT_SPACE = ' '.freeze
|
120
|
+
|
119
121
|
# Write prefix
|
120
122
|
#
|
121
123
|
# @return [String]
|
@@ -123,7 +125,7 @@ module Unparser
|
|
123
125
|
# @api private
|
124
126
|
#
|
125
127
|
def prefix
|
126
|
-
@content <<
|
128
|
+
@content << INDENT_SPACE * @indent
|
127
129
|
end
|
128
130
|
|
129
131
|
end # Buffer
|
data/lib/unparser/comments.rb
CHANGED
@@ -1,9 +1,6 @@
|
|
1
1
|
module Unparser
|
2
2
|
|
3
3
|
# Holds the comments that remain to be emitted
|
4
|
-
#
|
5
|
-
# @api private
|
6
|
-
#
|
7
4
|
class Comments
|
8
5
|
|
9
6
|
# Initialize object
|
@@ -14,7 +11,7 @@ module Unparser
|
|
14
11
|
#
|
15
12
|
def initialize(comments)
|
16
13
|
@comments = comments.dup
|
17
|
-
@eol_text_to_skip = nil
|
14
|
+
@last_range_consumed = @eol_text_to_skip = nil
|
18
15
|
end
|
19
16
|
|
20
17
|
# Consume part or all of the node
|
@@ -25,8 +22,9 @@ module Unparser
|
|
25
22
|
# @return [undefined]
|
26
23
|
#
|
27
24
|
def consume(node, source_part = :expression)
|
28
|
-
|
29
|
-
|
25
|
+
location = node.location
|
26
|
+
return unless location
|
27
|
+
@last_range_consumed = location.public_send(source_part)
|
30
28
|
end
|
31
29
|
|
32
30
|
# Skip any EOL comment with the specified text next time they're taken
|
@@ -46,10 +44,10 @@ module Unparser
|
|
46
44
|
def take_eol_comments
|
47
45
|
text_to_skip = @eol_text_to_skip
|
48
46
|
@eol_text_to_skip = nil
|
49
|
-
return []
|
47
|
+
return [] unless @last_range_consumed
|
50
48
|
comments = take_up_to_line(@last_range_consumed.end.line)
|
51
49
|
eol_comments = unshift_documents(comments)
|
52
|
-
eol_comments.reject {|comment| comment.text == text_to_skip }
|
50
|
+
eol_comments.reject { |comment| comment.text == text_to_skip }
|
53
51
|
end
|
54
52
|
|
55
53
|
# Take all remaining comments
|
@@ -68,10 +66,13 @@ module Unparser
|
|
68
66
|
# @return [Array]
|
69
67
|
#
|
70
68
|
def take_before(node, source_part)
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
69
|
+
location = node.location
|
70
|
+
if location
|
71
|
+
range = location.public_send(source_part)
|
72
|
+
take_while { |comment| comment.location.expression.end_pos <= range.begin_pos }
|
73
|
+
else
|
74
|
+
EMPTY_ARRAY
|
75
|
+
end
|
75
76
|
end
|
76
77
|
|
77
78
|
private
|
@@ -83,7 +84,7 @@ module Unparser
|
|
83
84
|
# @return [Array]
|
84
85
|
#
|
85
86
|
def take_while
|
86
|
-
number_to_take = @comments.index {|comment| !yield(comment) } || @comments.size
|
87
|
+
number_to_take = @comments.index { |comment| !yield(comment) } || @comments.size
|
87
88
|
@comments.shift(number_to_take)
|
88
89
|
end
|
89
90
|
|
@@ -105,8 +106,9 @@ module Unparser
|
|
105
106
|
#
|
106
107
|
def unshift_documents(comments)
|
107
108
|
doc_comments, other_comments = comments.partition(&:document?)
|
108
|
-
doc_comments.reverse_each {|comment| @comments.unshift(comment) }
|
109
|
+
doc_comments.reverse_each { |comment| @comments.unshift(comment) }
|
109
110
|
other_comments
|
110
111
|
end
|
111
|
-
|
112
|
-
end
|
112
|
+
|
113
|
+
end # Comments
|
114
|
+
end # Unparser
|
data/lib/unparser/constants.rb
CHANGED
data/lib/unparser/emitter.rb
CHANGED
@@ -8,7 +8,7 @@ module Unparser
|
|
8
8
|
# Registry for node emitters
|
9
9
|
REGISTRY = {}
|
10
10
|
|
11
|
-
NOINDENT = [:rescue, :ensure].to_set
|
11
|
+
NOINDENT = [:rescue, :ensure].to_set.freeze
|
12
12
|
|
13
13
|
DEFAULT_DELIMITER = ', '.freeze
|
14
14
|
|
@@ -148,7 +148,7 @@ module Unparser
|
|
148
148
|
def buffer
|
149
149
|
parent.buffer
|
150
150
|
end
|
151
|
-
memoize :buffer, :
|
151
|
+
memoize :buffer, freezer: :noop
|
152
152
|
|
153
153
|
# Return comments
|
154
154
|
#
|
@@ -159,7 +159,7 @@ module Unparser
|
|
159
159
|
def comments
|
160
160
|
parent.comments
|
161
161
|
end
|
162
|
-
memoize :comments, :
|
162
|
+
memoize :comments, freezer: :noop
|
163
163
|
|
164
164
|
private
|
165
165
|
|
@@ -169,22 +169,12 @@ module Unparser
|
|
169
169
|
#
|
170
170
|
# @api private
|
171
171
|
#
|
172
|
-
def parentheses(open=M_PO, close=M_PC)
|
172
|
+
def parentheses(open = M_PO, close = M_PC)
|
173
173
|
write(open)
|
174
174
|
yield
|
175
175
|
write(close)
|
176
176
|
end
|
177
177
|
|
178
|
-
# Emit nodes source map
|
179
|
-
#
|
180
|
-
# @return [undefined]
|
181
|
-
#
|
182
|
-
# @api private
|
183
|
-
#
|
184
|
-
def emit_source_map
|
185
|
-
SourceMap.emit(node, buffer)
|
186
|
-
end
|
187
|
-
|
188
178
|
# Visit node
|
189
179
|
#
|
190
180
|
# @param [Parser::AST::Node] node
|
@@ -208,7 +198,7 @@ module Unparser
|
|
208
198
|
#
|
209
199
|
def visit_terminated(node)
|
210
200
|
emitter = emitter(node)
|
211
|
-
|
201
|
+
conditional_parentheses(!emitter.terminated?) do
|
212
202
|
emitter.write_to_buffer
|
213
203
|
end
|
214
204
|
emitter.write_to_buffer
|
@@ -236,7 +226,7 @@ module Unparser
|
|
236
226
|
#
|
237
227
|
# @api private
|
238
228
|
#
|
239
|
-
def
|
229
|
+
def conditional_parentheses(flag)
|
240
230
|
if flag
|
241
231
|
parentheses { yield }
|
242
232
|
else
|
@@ -339,15 +329,15 @@ module Unparser
|
|
339
329
|
|
340
330
|
# Write each comment to a separate line
|
341
331
|
#
|
342
|
-
# @param [Array]
|
332
|
+
# @param [Array] comments
|
343
333
|
#
|
344
334
|
# @return [undefined]
|
345
335
|
#
|
346
336
|
# @api private
|
347
337
|
#
|
348
|
-
def emit_comments(
|
349
|
-
max =
|
350
|
-
|
338
|
+
def emit_comments(comments)
|
339
|
+
max = comments.size - 1
|
340
|
+
comments.each_with_index do |comment, index|
|
351
341
|
if comment.type == :document
|
352
342
|
buffer.append_without_prefix(comment.text.chomp)
|
353
343
|
else
|
@@ -429,7 +419,7 @@ module Unparser
|
|
429
419
|
#
|
430
420
|
# @api private
|
431
421
|
#
|
432
|
-
def emit_body(body =
|
422
|
+
def emit_body(body = body)
|
433
423
|
unless body
|
434
424
|
buffer.indent
|
435
425
|
nl
|
@@ -496,23 +486,5 @@ module Unparser
|
|
496
486
|
comments.skip_eol_comment(comment)
|
497
487
|
end
|
498
488
|
|
499
|
-
# Emitter that fully relies on parser source maps
|
500
|
-
class SourceMap < self
|
501
|
-
|
502
|
-
# Perform dispatch
|
503
|
-
#
|
504
|
-
# @param [Node] node
|
505
|
-
# @param [Buffer] buffer
|
506
|
-
#
|
507
|
-
# @return [self]
|
508
|
-
#
|
509
|
-
# @api private
|
510
|
-
#
|
511
|
-
def self.emit(node, buffer)
|
512
|
-
buffer.append(node.location.expression.source)
|
513
|
-
self
|
514
|
-
end
|
515
|
-
|
516
|
-
end # SourceMap
|
517
489
|
end # Emitter
|
518
490
|
end # Unparser
|