-
-
Notifications
You must be signed in to change notification settings - Fork 52
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
NodePattern: Optimize unions of literals into Sets
- Loading branch information
Showing
7 changed files
with
84 additions
and
1 deletion.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
# frozen_string_literal: true | ||
|
||
module RuboCop | ||
module AST | ||
class NodePattern | ||
# Utility to assign a set of values to a constant | ||
module Sets | ||
REGISTRY = Hash.new do |h, set| | ||
name = Sets.name(set).freeze | ||
Sets.const_set(name, set) | ||
h[set] = "::RuboCop::AST::NodePattern::Sets::#{name}" | ||
end | ||
|
||
MAX = 4 | ||
def self.name(set) | ||
elements = set | ||
elements = set.first(MAX - 1) << :etc if set.size > MAX | ||
name = elements.to_a.join('_').upcase.gsub(/[^A-Z0-9_]/, '') | ||
uniq("SET_#{name}") | ||
end | ||
|
||
def self.uniq(name) | ||
return name unless Sets.const_defined?(name) | ||
|
||
(2..Float::INFINITY).each do |i| | ||
uniq = "#{name}_#{i}" | ||
return uniq unless Sets.const_defined?(uniq) | ||
end | ||
end | ||
|
||
def self.[](set) | ||
REGISTRY[set] | ||
end | ||
end | ||
end | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
# frozen_string_literal: true | ||
|
||
RSpec.describe RuboCop::AST::NodePattern::Sets do | ||
subject(:name) { described_class[set] } | ||
|
||
let(:set) { Set[1, 2, 3, 4, 5, 6] } | ||
|
||
it { is_expected.to eq '::RuboCop::AST::NodePattern::Sets::SET_1_2_3_ETC' } | ||
|
||
it { is_expected.to eq described_class[Set[6, 5, 4, 3, 2, 1]] } | ||
|
||
it { is_expected.not_to eq described_class[Set[1, 2, 3, 4, 5, 6, 7]] } | ||
|
||
it 'creates a constant with the right value' do | ||
expect(eval(name)).to eq set # rubocop:disable Security/Eval | ||
end | ||
end |