forked from rubocop/rubocop-ast
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
77 additions
and
30 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,25 @@ | ||
# 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 | ||
# | ||
# LIST = %w[hello world].freeze | ||
# end | ||
# | ||
# Foo::LIST_SET # => Set['hello', 'world'] | ||
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,17 @@ | ||
# frozen_string_literal: true | ||
|
||
RSpec.describe RuboCop::AST::AutoConstToSet do | ||
let(:mod) do | ||
Module.new do | ||
extend RuboCop::AST::AutoConstToSet | ||
|
||
WORDS = %w[hello world].freeze | ||
end | ||
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'] | ||
expect { mod::WORDS_SET_SET }.to raise_error(TypeError) | ||
end | ||
end |