-
-
Notifications
You must be signed in to change notification settings - Fork 284
Add new Rails/JSONSymbolizeNames
cop
#1534
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
base: master
Are you sure you want to change the base?
Add new Rails/JSONSymbolizeNames
cop
#1534
Conversation
I've seen this pattern a lot: ```ruby JSON.parse(large_json).deep_symbolize_keys ``` instead of travesing Ruby hash once more we can use the `symbolize_names` option: ```ruby JSON.parse(large_json, symbolize_names: true) ``` Caveats / FP scenarios: 1. `symbolize_names` does not work if `create_addition` option is provided. 2. User might use both `symbolize_names: false` and `deep_symbolize_keys`. 3. There's no autocorrection yet, but it should be easy to add.
5d3d3cd
to
d75d110
Compare
Could you first propose this style to the style guide? |
module Rails | ||
# Use `JSON.parse(json, symbolize_names: true)` instead of `JSON.parse(json).deep_symbolize_keys`. | ||
|
||
# Using `symbolize_names: true` is more efficient as it creates symbols during parsing | ||
# rather than requiring a second pass through the data structure. | ||
|
||
# @example | ||
# # bad | ||
# JSON.parse(json).deep_symbolize_keys | ||
# | ||
# # good | ||
# JSON.parse(json, symbolize_names: true) | ||
# |
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.
module Rails | |
# Use `JSON.parse(json, symbolize_names: true)` instead of `JSON.parse(json).deep_symbolize_keys`. | |
# Using `symbolize_names: true` is more efficient as it creates symbols during parsing | |
# rather than requiring a second pass through the data structure. | |
# @example | |
# # bad | |
# JSON.parse(json).deep_symbolize_keys | |
# | |
# # good | |
# JSON.parse(json, symbolize_names: true) | |
# | |
module Rails | |
# Use `JSON.parse(json, symbolize_names: true)` instead of `JSON.parse(json).deep_symbolize_keys`. | |
# | |
# Using `symbolize_names: true` is more efficient as it creates symbols during parsing | |
# rather than requiring a second pass through the data structure. | |
# | |
# @example | |
# # bad | |
# JSON.parse(json).deep_symbolize_keys | |
# | |
# # good | |
# JSON.parse(json, symbolize_names: true) | |
# |
# JSON.parse(json, symbolize_names: true) | ||
# | ||
class JSONSymbolizeNames < Base | ||
MSG = 'Use `symbolize_names` option.' |
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 message only shows the option name, which might make it difficult for users to understand the expected action in their code. It may be simpler to provide autocorrection and show the corrected code instead.
…ep_symbolize_keys` ref: rubocop/rubocop-rails#1534
I've seen this pattern a lot:
instead of travesing Ruby hash once more we can use the
symbolize_names
option:Caveats / FP scenarios:
symbolize_names
does not work ifcreate_addition
option is provided.symbolize_names: false
anddeep_symbolize_keys
.Before submitting the PR make sure the following are checked:
master
(if not - rebase it).bundle exec rake default
. It executes all tests and runs RuboCop on its own code.{change_type}_{change_description}.md
if the new code introduces user-observable changes. See changelog entry format for details.