Skip to content

Commit cf2b248

Browse files
authored
Merge pull request #2 from Zopolis4/bureau
Add Chromebrew/OrderedCompatibility cop
2 parents 638a0c6 + 4950f3a commit cf2b248

File tree

4 files changed

+86
-0
lines changed

4 files changed

+86
-0
lines changed

config/default.yml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,8 @@ Chromebrew/CompatibilityAll:
55
Description: "This cop checks for compatibility values that can be replaced by 'all'."
66
Enabled: true
77
VersionAdded: '0.0.1'
8+
9+
Chromebrew/OrderedCompatibility:
10+
Description: 'This cop checks for compatibility values that are not in alphabetical order.'
11+
Enabled: true
12+
VersionAdded: '0.0.3'
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
module RuboCop
2+
module Cop
3+
module Chromebrew
4+
# Compatibility values should be in alphabetic order.
5+
#
6+
# @example
7+
# # bad
8+
# compatibility 'x86_64 aarch64 armv7l'
9+
#
10+
# # good
11+
# compatibility 'aarch64 armv7l x86_64'
12+
#
13+
class OrderedCompatibility < Base
14+
extend AutoCorrector
15+
MSG = 'Compatibility values should be in alphabetical order.'
16+
17+
def_node_matcher :compatibility?, <<~PATTERN
18+
(send nil? :compatibility
19+
(str $_))
20+
PATTERN
21+
22+
def on_send(node)
23+
# Check that we're operating on the compatibility property.
24+
value = compatibility?(node)
25+
return unless value
26+
27+
sorted_value = value.split.sort.join(' ')
28+
29+
return if sorted_value == value
30+
31+
# If requested, replace the offending compatibility value with the sorted version.
32+
add_offense(node.children.last) do |corrector|
33+
corrector.replace(node.children.last, "'#{sorted_value}'")
34+
end
35+
end
36+
end
37+
end
38+
end
39+
end

lib/rubocop/cop/chromebrew_cops.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
require_relative 'chromebrew/compatibility_all'
2+
require_relative 'chromebrew/ordered_compatibility'
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
RSpec.describe RuboCop::Cop::Chromebrew::OrderedCompatibility, :config do
2+
it 'registers an offense when compatibiltiy is not in alphabetical order with three elements' do
3+
expect_offense(<<~'RUBY')
4+
compatibility 'x86_64 aarch64 armv7l'
5+
^^^^^^^^^^^^^^^^^^^^^^^ Compatibility values should be in alphabetical order.
6+
RUBY
7+
8+
expect_correction(<<~'RUBY')
9+
compatibility 'aarch64 armv7l x86_64'
10+
RUBY
11+
end
12+
13+
it 'registers an offense when compatibiltiy is not in alphabetical order with two elements' do
14+
expect_offense(<<~'RUBY')
15+
compatibility 'x86_64 i686'
16+
^^^^^^^^^^^^^ Compatibility values should be in alphabetical order.
17+
RUBY
18+
19+
expect_correction(<<~'RUBY')
20+
compatibility 'i686 x86_64'
21+
RUBY
22+
end
23+
24+
it 'does not register an offense when compatibility is in alphabetical order' do
25+
expect_no_offenses(<<~'RUBY')
26+
compatibility 'aarch64 armv7l x86_64'
27+
RUBY
28+
end
29+
30+
it 'does not register an offense when compatibility is a single architecture' do
31+
expect_no_offenses(<<~'RUBY')
32+
compatibility 'x86_64'
33+
RUBY
34+
end
35+
36+
it 'does not register an offense when compatibility is all' do
37+
expect_no_offenses(<<~'RUBY')
38+
compatibility 'all'
39+
RUBY
40+
end
41+
end

0 commit comments

Comments
 (0)