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.
[Fixes rubocop#22] AutoConstToSet builds Set variants of constants au…
…tomatically
- Loading branch information
Showing
6 changed files
with
93 additions
and
38 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,26 @@ | ||
# frozen_string_literal: true | ||
|
||
module RuboCop | ||
module AST | ||
# If a module extends this, then `SOME_CONSTANT_SET` will be a set created | ||
# automatically from `SOME_CONSTANT` | ||
# | ||
# class Foo | ||
# extend AutoConstToSet | ||
# | ||
# WORDS = %w[hello world].freeze | ||
# end | ||
# | ||
# Foo::WORDS_SET # => Set['hello', 'world'].freeze | ||
module AutoConstToSet | ||
def const_missing(name) | ||
return super unless name =~ /(?<array_name>.*)_SET/ | ||
|
||
array = const_get(Regexp.last_match(:array_name)) | ||
raise TypeError, 'Already a set!' if array.is_a?(Set) | ||
|
||
const_set(name, array.to_set.freeze) | ||
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
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,24 @@ | ||
# frozen_string_literal: true | ||
|
||
RSpec.describe RuboCop::AST::AutoConstToSet do | ||
let(:mod) do | ||
Module.new do | ||
extend RuboCop::AST::AutoConstToSet | ||
end | ||
end | ||
|
||
before do | ||
stub_const('Mod', mod) | ||
stub_const('Mod::WORDS', %w[hello world].freeze) | ||
end | ||
|
||
it 'automatically creates set variants for array constants' do | ||
expect(mod.constants).not_to include :WORDS_SET | ||
expect(mod::WORDS_SET).to eq Set['hello', 'world'] | ||
end | ||
|
||
it 'raises an erreor if constant is already a set' do | ||
stub_const('Mod::WORDS', %w[hello world].to_set.freeze) | ||
expect { mod::WORDS_SET }.to raise_error(TypeError) | ||
end | ||
end |