-
-
Notifications
You must be signed in to change notification settings - Fork 3.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Warn duplicated keys in .rubocop.yml
- Loading branch information
Showing
15 changed files
with
177 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
# frozen_string_literal: true | ||
|
||
module RuboCop | ||
# Find duplicated keys from YAML. | ||
module YAMLDuplicationChecker | ||
def self.check(yaml_string, filename, &on_duplicated) | ||
# Specify filename to display helpful message when it raises an error. | ||
tree = YAML.parse(yaml_string, filename: filename) | ||
return unless tree | ||
|
||
traverse(tree, &on_duplicated) | ||
end | ||
|
||
def self.traverse(tree, &on_duplicated) | ||
case tree | ||
when Psych::Nodes::Mapping | ||
tree.children.each_slice(2).with_object([]) do |(key, value), keys| | ||
exist = keys.find { |key2| key2.value == key.value } | ||
on_duplicated.call(exist, key) if exist | ||
keys << key | ||
traverse(value, &on_duplicated) | ||
end | ||
else | ||
children = tree.children | ||
return unless children | ||
|
||
children.each { |c| traverse(c, &on_duplicated) } | ||
end | ||
end | ||
|
||
private_class_method :traverse | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
# frozen_string_literal: true | ||
|
||
RSpec.describe RuboCop::YAMLDuplicationChecker do | ||
def check(yaml, &block) | ||
described_class.check(yaml, 'dummy.yaml', &block) | ||
end | ||
|
||
shared_examples 'call block' do | ||
it 'calls block' do | ||
called = false | ||
check(yaml) do | ||
called = true | ||
end | ||
expect(called).to be(true) | ||
end | ||
end | ||
|
||
context 'when yaml has duplicated keys in the top level' do | ||
let(:yaml) { <<-YAML.strip_indent } | ||
Layout/Tab: | ||
Enabled: true | ||
Layout/Tab: | ||
Enabled: false | ||
YAML | ||
|
||
include_examples 'call block' | ||
|
||
it 'calls block with keys' do | ||
key1 = nil | ||
key2 = nil | ||
check(yaml) do |key_a, key_b| | ||
key1 = key_a | ||
key2 = key_b | ||
end | ||
expect(key1.start_line).to eq(0) | ||
expect(key2.start_line).to eq(3) | ||
expect(key1.value).to eq('Layout/Tab') | ||
expect(key2.value).to eq('Layout/Tab') | ||
end | ||
end | ||
|
||
context 'when yaml has duplicated keys in the second level' do | ||
let(:yaml) { <<-YAML.strip_indent } | ||
Layout/Tab: | ||
Enabled: true | ||
Enabled: false | ||
YAML | ||
|
||
include_examples 'call block' | ||
|
||
it 'calls block with keys' do | ||
key1 = nil | ||
key2 = nil | ||
check(yaml) do |key_a, key_b| | ||
key1 = key_a | ||
key2 = key_b | ||
end | ||
expect(key1.start_line).to eq(1) | ||
expect(key2.start_line).to eq(2) | ||
expect(key1.value).to eq('Enabled') | ||
expect(key2.value).to eq('Enabled') | ||
end | ||
end | ||
|
||
context 'when yaml does not have any duplication' do | ||
let(:yaml) { <<-YAML.strip_indent } | ||
Style/TrailingCommaInHashLiteral: | ||
Enabled: true | ||
EnforcedStyleForMultiline: no_comma | ||
Style/TrailingBodyOnModule: | ||
Enabled: true | ||
YAML | ||
|
||
it 'does not call block' do | ||
called = false | ||
described_class.check(yaml, 'dummy.yaml') do | ||
called = true | ||
end | ||
expect(called).to be(false) | ||
end | ||
end | ||
end |