forked from rubocop/rubocop-ast
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
186 additions
and
75 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
# frozen_string_literal: true | ||
|
||
module RuboCop | ||
module AST | ||
# FastArray represents a frozen `Array` with fast lookup | ||
# using `include?`. | ||
# Like `Set`, the case equality `===` is an alias for `include?` | ||
# | ||
# FOO = FastArray[:hello, :world] | ||
# FOO.include?(:hello) # => true, quickly | ||
# | ||
# case bar | ||
# when FOO # Note: no splat | ||
# # decided quickly | ||
# # ... | ||
class FastArray < ::Array | ||
attr_reader :to_set | ||
|
||
def initialize(ary) | ||
raise ArgumentError, 'Must be initialized with an array' unless ary.is_a?(Array) | ||
|
||
super | ||
freeze | ||
end | ||
|
||
def self.[](*values) | ||
new(values) | ||
end | ||
|
||
def freeze | ||
@to_set ||= Set.new(self).freeze | ||
super | ||
end | ||
|
||
# Return self, not a newly allocated FastArray | ||
def to_a | ||
self | ||
end | ||
|
||
def include?(value) | ||
@to_set.include?(value) | ||
end | ||
|
||
alias === include? | ||
end | ||
end | ||
end | ||
|
||
def FastArray(list) # rubocop:disable Naming/MethodName | ||
RuboCop::AST::FastArray.new(list) | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
# frozen_string_literal: true | ||
|
||
RSpec.describe RuboCop::AST::FastArray do | ||
shared_examples 'a fast_array' do | ||
it { is_expected.to be_frozen } | ||
it { expect(fast_array.include?(:included)).to be true } | ||
it { expect(fast_array.include?(:not_included)).to be false } | ||
it { is_expected.to eq fast_array.dup } | ||
|
||
describe '#to_a' do | ||
subject { fast_array.to_a } | ||
|
||
it { is_expected.to equal fast_array.to_a } | ||
it { is_expected.to be_frozen } | ||
it { is_expected.to include :included } | ||
end | ||
|
||
describe '#to_set' do | ||
subject { fast_array.to_set } | ||
|
||
it { is_expected.to equal fast_array.to_set } | ||
it { is_expected.to be_frozen } | ||
it { is_expected.to be >= Set[:included] } | ||
end | ||
end | ||
|
||
let(:values) { %i[included also_included] } | ||
|
||
describe '.new' do | ||
subject(:fast_array) { described_class.new(values) } | ||
|
||
it_behaves_like 'a fast_array' | ||
|
||
it 'enforces a single array argument' do | ||
expect { described_class.new }.to raise_error ArgumentError | ||
expect { described_class.new(5) }.to raise_error ArgumentError | ||
end | ||
|
||
it 'has freeze return self' do | ||
expect(fast_array.freeze).to equal fast_array | ||
end | ||
|
||
it 'has the right case equality' do | ||
expect(fast_array).to be === :included # rubocop:disable Style/CaseEquality | ||
end | ||
end | ||
|
||
describe '.[]' do | ||
subject(:fast_array) { described_class[*values] } | ||
|
||
it_behaves_like 'a fast_array' | ||
end | ||
|
||
describe '()' do | ||
subject(:fast_array) { FastArray values } | ||
|
||
it_behaves_like 'a fast_array' | ||
end | ||
end |