New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Initial support for ActiveRecord enums (and other things, too) #1618
Merged
Merged
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
2a82609
Very simplest return values
presidentbeef f1378ae
Slightly better pluralize
presidentbeef 217eecd
Add Sexp#method_length
presidentbeef 02b9bd1
Better faster simple method check
presidentbeef 304534d
Support ActiveRecord enums
presidentbeef be0cfb4
Move all_literals? to Brakeman::Util
presidentbeef 964c37f
Check for Hash literals too for simple methods
presidentbeef 3554a2c
Support Hash#include? like Array#include?
presidentbeef 5342cbe
Enum name is an instance method, not class method
presidentbeef 73b8710
Test the array form of `enum`
presidentbeef e44ae9a
Use simple method checks from MethInfo
presidentbeef 1920a23
Additional test for enums
presidentbeef File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -73,6 +73,8 @@ def process_call exp | |
@current_class.set_attr_accessible exp | ||
when :attr_protected | ||
@current_class.set_attr_protected exp | ||
when :enum | ||
add_enum_method exp | ||
else | ||
if @current_class | ||
@current_class.add_option method, exp | ||
|
@@ -87,4 +89,33 @@ def process_call exp | |
call | ||
end | ||
end | ||
|
||
def add_enum_method call | ||
arg = call.first_arg | ||
return unless hash? arg | ||
|
||
enum_name = arg[1].value # first key | ||
enums = arg[2] # first value | ||
enums_name = pluralize(enum_name.to_s).to_sym | ||
|
||
call_line = call.line | ||
|
||
if hash? enums | ||
enum_values = enums | ||
elsif array? enums | ||
# Build hash for enum values like Rails does | ||
enum_values = s(:hash).line(call_line) | ||
|
||
enums.each_sexp.with_index do |v, index| | ||
enum_values << v | ||
enum_values << s(:lit, index).line(call_line) | ||
end | ||
end | ||
|
||
enum_method = s(:defn, enum_name, s(:args), safe_literal(call_line)) | ||
enums_method = s(:defs, s(:self), enums_name, s(:args), enum_values) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I may be wrong but I think that enum(item_type: %i[digital]) #=> define digital
enum(item_type: %i[digital], _prefix: true) #=> define item_type_digital |
||
|
||
@current_class.add_method :public, enum_name, enum_method, @current_file | ||
@current_class.add_method :public, enums_name, enums_method, @current_file | ||
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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This line raises
WrongSexpError: Sexp#value called on multi-item Sexp
ifarg[1]
is string interpolation like the followings. Unfortunately our project contains this rare case 😢