rubocop-cask 0.2.1 → 0.2.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 4c174d108070beae46129bf27159a945ac505691
4
- data.tar.gz: cf04f6d393b83b38a63e2f9bfc8ea12d4dfedef6
3
+ metadata.gz: d6a5b8a17cf24bee4d483755261047e0b8e36545
4
+ data.tar.gz: 411f20070b3fe9c196afa06df99e3bd495080346
5
5
  SHA512:
6
- metadata.gz: 0583722e3bba308c5ab2a4435ec89e8888cbab4f18535d7a1ba79f6c622682beafc372d8b935bac04dba082adef11c9f7cca3d91e7e459d4db525fe934f75f49
7
- data.tar.gz: 22cdf650337a179ce424a0a82fa8e5e7fd4dfebcdb9cf58b561680b372b093206b1ea3b2e66c94d192a896384bc0708175af6fe7ea8b3411e5a9db0e74d041a5
6
+ metadata.gz: 4af05ad480b53239355e3622893caf33db75f510c161e18b6b9d44c1c4ef3207752b4c4d6b8a2b76c3eeaa50bdeab4b9e884d8984903b3f1cfa85949c4c8f45d
7
+ data.tar.gz: 30c12c402d6194e77bdf40077b0b8647da6103b64f7aabd6abde1c4fdfa20f09cc65d58d880bdf7457af6aaf5a0de76ec742792ac03818967073519644be348e
@@ -9,11 +9,12 @@ module RuboCop
9
9
  class CaskBlock
10
10
  extend Forwardable
11
11
 
12
- def initialize(block_node)
12
+ def initialize(block_node, comments)
13
13
  @block_node = block_node
14
+ @comments = comments
14
15
  end
15
16
 
16
- attr_reader :block_node
17
+ attr_reader :block_node, :comments
17
18
 
18
19
  alias_method :cask_node, :block_node
19
20
 
@@ -28,7 +29,7 @@ module RuboCop
28
29
  def stanzas
29
30
  @stanzas ||= cask_body.descendants
30
31
  .select(&:stanza?)
31
- .map { |n| Stanza.new(n) }
32
+ .map { |node| Stanza.new(node, stanza_comments(node)) }
32
33
  end
33
34
 
34
35
  def toplevel_stanzas
@@ -56,6 +57,15 @@ module RuboCop
56
57
  def stanza_order_index(stanza)
57
58
  Constants::STANZA_ORDER.index(stanza.stanza_name)
58
59
  end
60
+
61
+ def stanza_comments(stanza_node)
62
+ comments_hash[stanza_node.loc]
63
+ end
64
+
65
+ def comments_hash
66
+ @comments_hash ||= Parser::Source::Comment
67
+ .associate_locations(cask_node, comments)
68
+ end
59
69
  end
60
70
  end
61
71
  end
@@ -9,22 +9,31 @@ module RuboCop
9
9
  class Stanza
10
10
  extend Forwardable
11
11
 
12
- def initialize(method_node)
12
+ def initialize(method_node, comments)
13
13
  @method_node = method_node
14
+ @comments = comments
14
15
  end
15
16
 
16
- attr_reader :method_node
17
+ attr_reader :method_node, :comments
17
18
 
18
19
  alias_method :stanza_node, :method_node
19
20
 
20
21
  def_delegator :stanza_node, :method_name, :stanza_name
21
22
  def_delegator :stanza_node, :parent, :parent_node
22
23
 
23
- def expression
24
+ def source_range
24
25
  stanza_node.expression
25
26
  end
26
27
 
27
- def_delegator :expression, :source
28
+ def source_range_with_comments
29
+ comments.reduce(source_range) do |range, comment|
30
+ range.join(comment.loc.expression)
31
+ end
32
+ end
33
+
34
+ def_delegator :source_range, :source
35
+ def_delegator :source_range_with_comments, :source,
36
+ :source_with_comments
28
37
 
29
38
  def stanza_group
30
39
  Constants::STANZA_GROUP_HASH[stanza_name]
@@ -4,7 +4,7 @@ module RuboCop
4
4
  module Cask
5
5
  # Version information for the Cask RuboCop plugin.
6
6
  module Version
7
- STRING = '0.2.1'
7
+ STRING = '0.2.2'
8
8
 
9
9
  def self.gem_version
10
10
  Gem::Version.new(STRING)
@@ -8,7 +8,9 @@ module RuboCop
8
8
  return unless respond_to?(:on_cask)
9
9
  return unless block_node.cask_block?
10
10
 
11
- on_cask(block_node)
11
+ comments = processed_source.comments
12
+ cask_block = RuboCop::Cask::AST::CaskBlock.new(block_node, comments)
13
+ on_cask(cask_block)
12
14
  end
13
15
  end
14
16
  end
@@ -21,8 +21,8 @@ module RuboCop
21
21
 
22
22
  MESSAGE = 'Use `%s` instead of `%s`'
23
23
 
24
- def on_cask(cask_node)
25
- @cask_header = cask_header(cask_node.method_node)
24
+ def on_cask(cask_block)
25
+ @cask_header = cask_block.header
26
26
  return unless offense?
27
27
  offense
28
28
  end
@@ -17,8 +17,8 @@ module RuboCop
17
17
  EXTRA_LINE_MSG = 'stanzas within the same group should have no lines ' \
18
18
  'between them'
19
19
 
20
- def on_cask(cask_node)
21
- @cask_block = RuboCop::Cask::AST::CaskBlock.new(cask_node)
20
+ def on_cask(cask_block)
21
+ @cask_block = cask_block
22
22
  @line_ops = {}
23
23
  add_offenses
24
24
  end
@@ -69,7 +69,7 @@ module RuboCop
69
69
  end
70
70
 
71
71
  def index_of_line_after(stanza)
72
- stanza.expression.last_line
72
+ stanza.source_range.last_line
73
73
  end
74
74
 
75
75
  def add_offense_missing_line(stanza)
@@ -12,8 +12,8 @@ module RuboCop
12
12
 
13
13
  MESSAGE = '`%s` stanza out of order'
14
14
 
15
- def on_cask(cask_node)
16
- @cask_block = RuboCop::Cask::AST::CaskBlock.new(cask_node)
15
+ def on_cask(cask_block)
16
+ @cask_block = cask_block
17
17
  add_offenses
18
18
  end
19
19
 
@@ -21,7 +21,8 @@ module RuboCop
21
21
  lambda do |corrector|
22
22
  correct_stanza_index = toplevel_stanzas.index(stanza)
23
23
  correct_stanza = sorted_toplevel_stanzas[correct_stanza_index]
24
- corrector.replace(stanza.expression, correct_stanza.source)
24
+ corrector.replace(stanza.source_range_with_comments,
25
+ correct_stanza.source_with_comments)
25
26
  end
26
27
  end
27
28
 
@@ -34,7 +35,7 @@ module RuboCop
34
35
  def add_offenses
35
36
  offending_stanzas.each do |stanza|
36
37
  message = format(MESSAGE, stanza.stanza_name)
37
- add_offense(stanza, stanza.expression, message)
38
+ add_offense(stanza, stanza.source_range_with_comments, message)
38
39
  end
39
40
  end
40
41
 
@@ -232,6 +232,48 @@ describe RuboCop::Cop::Cask::StanzaGrouping do
232
232
  include_examples 'autocorrects source'
233
233
  end
234
234
 
235
+ context 'when a stanza has a comment' do
236
+ let(:source) do
237
+ <<-CASK.undent
238
+ cask 'foo' do
239
+ version :latest
240
+ sha256 :no_check
241
+ # comment with an empty line between
242
+
243
+ # comment directly above
244
+ postflight do
245
+ puts 'We have liftoff!'
246
+ end
247
+ url 'https://foo.example.com/foo.zip'
248
+ name 'Foo'
249
+ app 'Foo.app'
250
+ end
251
+ CASK
252
+ end
253
+ let(:correct_source) do
254
+ <<-CASK.undent
255
+ cask 'foo' do
256
+ version :latest
257
+ sha256 :no_check
258
+
259
+ # comment with an empty line between
260
+
261
+ # comment directly above
262
+ postflight do
263
+ puts 'We have liftoff!'
264
+ end
265
+
266
+ url 'https://foo.example.com/foo.zip'
267
+ name 'Foo'
268
+
269
+ app 'Foo.app'
270
+ end
271
+ CASK
272
+ end
273
+
274
+ include_examples 'autocorrects source'
275
+ end
276
+
235
277
  # TODO: detect incorrectly grouped stanzas in nested expressions
236
278
  context 'when stanzas are nested in a conditional expression' do
237
279
  let(:source) do
@@ -154,6 +154,39 @@ describe RuboCop::Cop::Cask::StanzaOrder do
154
154
  end
155
155
  end
156
156
 
157
+ context 'when a stanza has a comment' do
158
+ let(:source) do
159
+ <<-CASK.undent
160
+ cask 'foo' do
161
+ version :latest
162
+ # comment with an empty line between
163
+
164
+ # comment directly above
165
+ postflight do
166
+ puts 'We have liftoff!'
167
+ end
168
+ sha256 :no_check
169
+ end
170
+ CASK
171
+ end
172
+ let(:correct_source) do
173
+ <<-CASK.undent
174
+ cask 'foo' do
175
+ version :latest
176
+ sha256 :no_check
177
+ # comment with an empty line between
178
+
179
+ # comment directly above
180
+ postflight do
181
+ puts 'We have liftoff!'
182
+ end
183
+ end
184
+ CASK
185
+ end
186
+
187
+ include_examples 'autocorrects source'
188
+ end
189
+
157
190
  context 'when the caveats stanza is out of order' do
158
191
  let(:source) do
159
192
  format(<<-CASK.undent, caveats.strip)
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rubocop-cask
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.1
4
+ version: 0.2.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Joshua Hagins